[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/creole/drivers/odbc/ -> ODBCPreparedStatement.php (source)

   1  <?php
   2  /*
   3   *  $Id: ODBCPreparedStatement.php,v 1.4 2005/11/13 01:29:01 gamr Exp $
   4   *
   5   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   6   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   7   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   8   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   9   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16   *
  17   * This software consists of voluntary contributions made by many individuals
  18   * and is licensed under the LGPL. For more information please see
  19   * <http://creole.phpdb.org>.
  20   */
  21  
  22  require_once 'creole/PreparedStatement.php';
  23  require_once 'creole/common/PreparedStatementCommon.php';
  24  require_once 'creole/util/Lob.php';
  25  
  26  /**
  27   * ODBC specific PreparedStatement functions.
  28   *
  29   * @author    Dave Lawson <dlawson@masterytech.com>
  30   * @version   $Revision: 1.4 $
  31   * @package   creole.drivers.odbc
  32   */
  33  class ODBCPreparedStatement extends PreparedStatementCommon implements PreparedStatement
  34  {
  35      /**
  36       * This does nothing since ODBC natively supports prepared statements.
  37       * @see PreparedStatementCommon::replaceParams()
  38       */
  39      protected function replaceParams()
  40      {
  41          if ($this->conn->getAdapter()->emulatePrepareStmt())
  42              return parent::replaceParams();
  43          else
  44              return $this->sql;
  45      }
  46  
  47      /**
  48       * Internal function to call native ODBC prepare/execute functions.
  49       */
  50      protected function _execute($sql, $params, $fetchmode, $isupdate)
  51      {
  52          if ($this->resultSet)
  53          {
  54              $this->resultSet->close();
  55              $this->resultSet = null;
  56          }
  57  
  58          $this->updateCount = null;
  59  
  60          if ($this->conn->getAdapter()->emulatePrepareStmt())
  61          {
  62              $stmt = @odbc_exec($this->conn->getResource(), $sql);
  63              $ret = ($stmt !== false);
  64          }
  65          else
  66          {
  67              // Trim surrounding quotes added from default set methods.
  68              // Exception: for LOB-based parameters, odbc_execute() will
  69              // accept a filename surrounded by single-quotes.
  70              foreach ($this->boundInVars as $idx => $var)
  71              {
  72                  if ($var instanceof Lob)
  73                  {
  74                      $file = ($isupdate ? $var->getInputFile() : $var->getOutputFile());
  75                      $this->boundInVars[$idx] = "'$file'";
  76                  }
  77                  else if (is_string($var))
  78                  {
  79                      $this->boundInVars[$idx] = trim($var, "\"\'");
  80                  }
  81              }
  82  
  83              $stmt = @odbc_prepare($this->conn->getResource(), $sql);
  84  
  85              if ($stmt === FALSE)
  86                  throw new SQLException('Could not prepare query', $this->conn->nativeError(), $sql);
  87  
  88              $ret = @odbc_execute($stmt, $this->boundInVars);
  89          }
  90  
  91          if ($ret === FALSE)
  92          {
  93              @odbc_free_result($stmt);
  94              throw new SQLException('Could not execute query', $this->conn->nativeError(), $sql);
  95          }
  96  
  97          return $this->conn->createResultSet(new ODBCResultResource($stmt), $fetchmode);
  98      }
  99  
 100      /**
 101       * @see PreparedStatement::executeQuery()
 102       */
 103      public function executeQuery()
 104      {
 105          switch (func_num_args()) {
 106          case 2:
 107          list($params, $fetchmode) = func_get_args();
 108              if (!is_array($params)) {
 109                  unset($params);
 110              }
 111              break;
 112          case 1:
 113              $params = null;
 114              list($fetchmode) = func_get_args();
 115              break;
 116          case 0:
 117              $params = null;
 118              $fetchmode = null;
 119              break;
 120          }    
 121          
 122          // Set any params passed directly
 123          if (isset($params)) {
 124              for($i=0,$cnt=count($params); $i < $cnt; $i++) {
 125                  $this->set($i+1, $params[$i]);
 126              }
 127          }
 128          
 129          $sql = $this->replaceParams();
 130          
 131          if ($this->conn->getAdapter()->hasLimitOffset())
 132          {
 133              if ($this->limit > 0 || $this->offset > 0)
 134                  $this->conn->applyLimit($sql, $this->offset, $this->limit);
 135          }
 136  
 137          $this->resultSet = $this->_execute($sql, $params, $fetchmode, false);
 138  
 139          if (!$this->conn->getAdapter()->hasLimitOffset())
 140          {
 141              $this->resultSet->_setOffset($this->offset);
 142              $this->resultSet->_setLimit($this->limit);
 143          }
 144  
 145          return $this->resultSet;
 146      }
 147  
 148      /**
 149       * @see PreparedStatement::executeUpdate()
 150       */
 151      public function executeUpdate($params = null)
 152      {
 153          // Set any params passed directly
 154          if ($params) {
 155              for($i=0,$cnt=count($params); $i < $cnt; $i++) {
 156                  $this->set($i+1, $params[$i]);
 157              }
 158          }
 159  
 160          $sql = $this->replaceParams();
 161          $this->_execute($sql, $params, 0, true);
 162          $this->updateCount = $this->conn->getUpdateCount();
 163  
 164          return $this->updateCount;
 165      }
 166  
 167      /**
 168       * @see PreparedStatementCommon::escape()
 169       */
 170      protected function escape($str)
 171      {
 172          if ($this->conn->getAdapter()->emulatePrepareStmt())
 173              return $this->conn->getAdapter()->escape($str);
 174              
 175          // Nothing to do here. odbc_execute() takes care of escaping strings.
 176          return $str;
 177      }
 178  
 179      /**
 180       * @see PreparedStatement::setNull()
 181       */
 182      function setNull($paramIndex)
 183      {
 184          $this->sql_cache_valid = false;
 185          $this->boundInVars[$paramIndex] = null;
 186      }
 187  
 188      /**
 189       * @see PreparedStatement::setBlob()
 190       */
 191      function setBlob($paramIndex, $blob)
 192      {
 193          if ($this->conn->getAdapter()->emulatePrepareStmt())
 194              return parent::setBlob($paramIndex, $blob);
 195              
 196          $this->sql_cache_valid = false;
 197          if ($blob === null)
 198          {
 199              $this->setNull($paramIndex);
 200              return;
 201          }
 202  
 203          if ($blob instanceof Blob)
 204          {
 205              if ($blob->isFromFile() && !$blob->isModified())
 206              {
 207                  $this->boundInVars[$paramIndex] = $blob;
 208                  return;
 209              }
 210  
 211              $blob = $blob->__toString();
 212          }
 213  
 214          $this->boundInVars[$paramIndex] = "'" . $this->escape($blob) . "'";
 215      }
 216  
 217      /**
 218       * @see PreparedStatement::setClob()
 219       */
 220      function setClob($paramIndex, $clob)
 221      {
 222          if ($this->conn->getAdapter()->emulatePrepareStmt())
 223              return parent::setClob($paramIndex, $clob);
 224  
 225          $this->sql_cache_valid = false;
 226          if ($clob === null)
 227          {
 228              $this->setNull($paramIndex);
 229              return;
 230          }
 231  
 232          if ($clob instanceof Clob)
 233          {
 234              if ($clob->isFromFile() && !$clob->isModified())
 235              {
 236                  $this->boundInVars[$paramIndex] = $clob;
 237                  return;
 238              }
 239  
 240              $clob = $clob->__toString();
 241          }
 242  
 243          $this->boundInVars[$paramIndex] = "'" . $this->escape($clob) . "'";
 244      }
 245  
 246  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7