SQLiteConnection.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: SQLiteConnection.php,v 1.10 2004/05/15 00:53:22 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/Connection.php'; 00023 require_once 'creole/common/ConnectionCommon.php'; 00024 00034 class SQLiteConnection extends ConnectionCommon implements Connection { 00035 00042 private $sqliteAssocCase; 00043 00047 function connect($dsninfo, $flags = 0) 00048 { 00049 if (!extension_loaded('sqlite')) { 00050 throw new SQLException('sqlite extension not loaded'); 00051 } 00052 00053 $file = $dsninfo['database']; 00054 00055 $this->dsn = $dsninfo; 00056 $this->flags = $flags; 00057 00058 $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT); 00059 00060 $nochange = (($flags & Creole::NO_ASSOC_LOWER) === Creole::NO_ASSOC_LOWER); 00061 if ($nochange) { 00062 $this->sqliteAssocCase = 0; 00063 } else { 00064 $this->sqliteAssocCase = 2; 00065 } 00066 00067 if ($file === null) { 00068 throw new SQLException("No SQLite database specified."); 00069 } 00070 00071 $mode = (isset($dsninfo['mode']) && is_numeric($dsninfo['mode'])) ? $dsninfo['mode'] : 0644; 00072 00073 if ($file != ':memory:') { 00074 if (!file_exists($file)) { 00075 touch($file); 00076 chmod($file, $mode); 00077 if (!file_exists($file)) { 00078 throw new SQLException("Unable to create SQLite database."); 00079 } 00080 } 00081 if (!is_file($file)) { 00082 throw new SQLException("Unable to open SQLite database: not a valid file."); 00083 } 00084 if (!is_readable($file)) { 00085 throw new SQLException("Unable to read SQLite database."); 00086 } 00087 } 00088 00089 $connect_function = $persistent ? 'sqlite_popen' : 'sqlite_open'; 00090 if (!($conn = @$connect_function($file, $mode, $errmsg) )) { 00091 throw new SQLException("Unable to connect to SQLite database", $errmsg); 00092 } 00093 00094 $this->dblink = $conn; 00095 } 00096 00100 public function getDatabaseInfo() 00101 { 00102 require_once 'creole/drivers/sqlite/metadata/SQLiteDatabaseInfo.php'; 00103 return new SQLiteDatabaseInfo($this); 00104 } 00105 00109 public function getIdGenerator() 00110 { 00111 require_once 'creole/drivers/sqlite/SQLiteIdGenerator.php'; 00112 return new SQLiteIdGenerator($this); 00113 } 00114 00118 public function prepareStatement($sql) 00119 { 00120 require_once 'creole/drivers/sqlite/SQLitePreparedStatement.php'; 00121 return new SQLitePreparedStatement($this, $sql); 00122 } 00123 00127 public function prepareCall($sql) { 00128 throw new SQLException('SQLite does not support stored procedures using CallableStatement.'); 00129 } 00130 00134 public function createStatement() 00135 { 00136 require_once 'creole/drivers/sqlite/SQLiteStatement.php'; 00137 return new SQLiteStatement($this); 00138 } 00139 00143 function close() 00144 { 00145 $ret = @sqlite_close($this->dblink); 00146 $this->dblink = null; 00147 return $ret; 00148 } 00149 00153 public function executeQuery($sql, $fetchmode = null) 00154 { 00155 ini_set('sqlite.assoc_case', $this->sqliteAssocCase); 00156 $result = @sqlite_query($this->dblink, $sql); 00157 if (!$result) { 00158 throw new SQLException('Could not execute query', $php_errormsg, $sql); //sqlite_error_string(sqlite_last_error($this->dblink)) 00159 } 00160 require_once 'creole/drivers/sqlite/SQLiteResultSet.php'; 00161 return new SQLiteResultSet($this, $result, $fetchmode); 00162 } 00163 00167 function executeUpdate($sql) 00168 { 00169 if (!$this->autocommit) { 00170 if ($this->transactionOpcount === 0) { 00171 $result = @sqlite_query($this->dblink, 'BEGIN'); 00172 if (!$result) { 00173 throw new SQLException('Could not begin transaction', $php_errormsg); //sqlite_error_string(sqlite_last_error($this->dblink)) 00174 } 00175 } 00176 $this->transactionOpcount++; 00177 } 00178 00179 $result = @sqlite_query($this->dblink, $sql); 00180 if (!$result) { 00181 throw new SQLException('Could not execute update', $php_errormsg, $sql); //sqlite_error_string(sqlite_last_error($this->dblink)) 00182 } 00183 return (int) @sqlite_changes($this->dblink); 00184 } 00185 00189 function commit() 00190 { 00191 if ($this->transactionOpcount > 0) { 00192 $result = @sqlite_query($this->dblink, 'COMMIT'); 00193 $this->transactionOpcount = 0; 00194 if (!$result) { 00195 throw new SQLException('Can not commit transaction', $php_errormsg); // sqlite_error_string(sqlite_last_error($this->dblink)) 00196 } 00197 } 00198 } 00199 00205 function rollback() 00206 { 00207 if ($this->transactionOpcount > 0) { 00208 $result = @sqlite_query($this->dblink, 'ROLLBACK'); 00209 $this->transactionOpcount = 0; 00210 if (!$result) { 00211 throw new SQLException('Could not rollback transaction', $php_errormsg); // sqlite_error_string(sqlite_last_error($this->dblink)) 00212 } 00213 } 00214 } 00215 00222 function getUpdateCount() 00223 { 00224 return (int) @sqlite_changes($this->dblink); 00225 } 00226 00227 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS