[ 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/sqlite/ -> SQLiteConnection.php (source)

   1  <?php
   2  /*
   3   *  $Id: SQLiteConnection.php,v 1.15 2006/01/17 19:44:41 hlellelid 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/Connection.php';
  23  require_once 'creole/common/ConnectionCommon.php';
  24  
  25  /**
  26   * SQLite implementation of Connection.
  27   * 
  28   * @author    Hans Lellelid <hans@xmpl.org>
  29   * @author    Stig Bakken <ssb@fast.no> 
  30   * @author    Lukas Smith
  31   * @version   $Revision: 1.15 $
  32   * @package   creole.drivers.sqlite
  33   */ 
  34  class SQLiteConnection extends ConnectionCommon implements Connection {   
  35      
  36      /**
  37       * The case to use for SQLite results.
  38       * (0=nochange, 1=upper, 2=lower) 
  39       * This is set in each call to executeQuery() in order to ensure that different
  40       * Connections do not overwrite each other's settings
  41       */
  42      private $sqliteAssocCase;
  43      
  44      /**
  45       * @see Connection::connect()
  46       */
  47      function connect($dsninfo, $flags = 0)
  48      {        
  49          if (!extension_loaded('sqlite')) {
  50              throw new SQLException('sqlite extension not loaded');
  51          }
  52  
  53          $file = $dsninfo['database'];
  54          
  55          $this->dsn = $dsninfo;
  56          $this->flags = $flags;
  57          
  58          $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT);
  59          
  60          if (PHP_VERSION == '5.0.4' || PHP_VERSION == '5.0.5') {
  61              $nochange = TRUE;
  62          } else {
  63              $nochange = !(($flags & Creole::COMPAT_ASSOC_LOWER) === Creole::COMPAT_ASSOC_LOWER);
  64          }
  65          
  66          if ($nochange) {     
  67              $this->sqliteAssocCase = 0;
  68          } else {
  69              $this->sqliteAssocCase = 2;
  70          }
  71          
  72          if ($file === null) {
  73              throw new SQLException("No SQLite database specified.");
  74          }
  75          
  76          $mode = (isset($dsninfo['mode']) && is_numeric($dsninfo['mode'])) ? $dsninfo['mode'] : 0644;
  77          
  78          if ($file != ':memory:') {
  79              if (!file_exists($file)) {
  80                  touch($file);
  81                  chmod($file, $mode);
  82                  if (!file_exists($file)) {
  83                      throw new SQLException("Unable to create SQLite database.");
  84                  }
  85              }
  86              if (!is_file($file)) {
  87                  throw new SQLException("Unable to open SQLite database: not a valid file.");
  88              }
  89              if (!is_readable($file)) {
  90                  throw new SQLException("Unable to read SQLite database.");
  91              }
  92          }
  93  
  94          $connect_function = $persistent ? 'sqlite_popen' : 'sqlite_open';
  95          if (!($conn = @$connect_function($file, $mode, $errmsg) )) {
  96              throw new SQLException("Unable to connect to SQLite database", $errmsg);
  97          }
  98          
  99          $this->dblink = $conn;
 100      }   
 101  
 102      /**
 103       * @see Connection::getDatabaseInfo()
 104       */
 105      public function getDatabaseInfo()
 106      {
 107          require_once 'creole/drivers/sqlite/metadata/SQLiteDatabaseInfo.php';
 108          return new SQLiteDatabaseInfo($this);
 109      }
 110      
 111       /**
 112       * @see Connection::getIdGenerator()
 113       */
 114      public function getIdGenerator()
 115      {
 116          require_once 'creole/drivers/sqlite/SQLiteIdGenerator.php';
 117          return new SQLiteIdGenerator($this);
 118      }
 119      
 120      /**
 121       * @see Connection::prepareStatement()
 122       */
 123      public function prepareStatement($sql) 
 124      {
 125          require_once 'creole/drivers/sqlite/SQLitePreparedStatement.php';
 126          return new SQLitePreparedStatement($this, $sql);
 127      }
 128      
 129      /**
 130       * @see Connection::prepareCall()
 131       */
 132      public function prepareCall($sql) {
 133          throw new SQLException('SQLite does not support stored procedures using CallableStatement.');        
 134      }
 135      
 136      /**
 137       * @see Connection::createStatement()
 138       */
 139      public function createStatement()
 140      {
 141          require_once 'creole/drivers/sqlite/SQLiteStatement.php';
 142          return new SQLiteStatement($this);
 143      }
 144          
 145      /**
 146       * @see Connection::close()
 147       */
 148      function close()
 149      {
 150          $ret = @sqlite_close($this->dblink);
 151          $this->dblink = null;
 152          return $ret;
 153      }
 154      
 155      /**
 156       * @see Connection::applyLimit()
 157       */
 158      public function applyLimit(&$sql, $offset, $limit)
 159      {
 160          if ( $limit > 0 ) {
 161              $sql .= " LIMIT " . $limit . ($offset > 0 ? " OFFSET " . $offset : "");
 162          } elseif ( $offset > 0 ) {
 163              $sql .= " LIMIT -1 OFFSET " . $offset;
 164          }
 165      } 
 166  
 167      /**
 168       * @see Connection::executeQuery()
 169       */
 170      public function executeQuery($sql, $fetchmode = null)
 171      {    
 172          ini_set('sqlite.assoc_case', $this->sqliteAssocCase);
 173          $this->lastQuery = $sql;
 174          $result = @sqlite_query($this->dblink, $this->lastQuery);
 175          if (!$result) {
 176              throw new SQLException('Could not execute query', $php_errormsg, $this->lastQuery); //sqlite_error_string(sqlite_last_error($this->dblink))
 177          }
 178          require_once 'creole/drivers/sqlite/SQLiteResultSet.php';
 179          return new SQLiteResultSet($this, $result, $fetchmode);    
 180      }    
 181      
 182      /**
 183       * @see Connection::executeUpdate()
 184       */
 185      function executeUpdate($sql)
 186      {
 187          $this->lastQuery = $sql;
 188          $result = @sqlite_query($this->dblink, $this->lastQuery);
 189          if (!$result) {            
 190              throw new SQLException('Could not execute update', $php_errormsg, $this->lastQuery); //sqlite_error_string(sqlite_last_error($this->dblink))
 191          }
 192          return (int) @sqlite_changes($this->dblink);
 193      }
 194      
 195      /**
 196       * Start a database transaction.
 197       * @throws SQLException
 198       * @return void
 199       */
 200      protected function beginTrans()
 201      {
 202          $result = @sqlite_query($this->dblink, 'BEGIN');
 203          if (!$result) {
 204              throw new SQLException('Could not begin transaction', $php_errormsg); //sqlite_error_string(sqlite_last_error($this->dblink))
 205          }
 206      }
 207      
 208      /**
 209       * Commit the current transaction.
 210       * @throws SQLException
 211       * @return void
 212       */
 213      protected function commitTrans()
 214      {
 215          $result = @sqlite_query($this->dblink, 'COMMIT');
 216          if (!$result) {
 217              throw new SQLException('Can not commit transaction', $php_errormsg); // sqlite_error_string(sqlite_last_error($this->dblink))
 218          }
 219      }
 220  
 221      /**
 222       * Roll back (undo) the current transaction.
 223       * @throws SQLException
 224       * @return void
 225       */
 226      protected function rollbackTrans()
 227      {
 228          $result = @sqlite_query($this->dblink, 'ROLLBACK');
 229          if (!$result) {
 230              throw new SQLException('Could not rollback transaction', $php_errormsg); // sqlite_error_string(sqlite_last_error($this->dblink))
 231          }
 232      }
 233  
 234      /**
 235       * Gets the number of rows affected by the data manipulation
 236       * query.
 237       *
 238       * @return int Number of rows affected by the last query.
 239       */
 240      function getUpdateCount()
 241      {
 242          return (int) @sqlite_changes($this->dblink);
 243      }
 244      
 245  }


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