00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 if (PHP_VERSION == '5.0.4' || PHP_VERSION == '5.0.5') {
00061 $nochange = TRUE;
00062 } else {
00063 $nochange = !(($flags & Creole::COMPAT_ASSOC_LOWER) === Creole::COMPAT_ASSOC_LOWER);
00064 }
00065
00066 if ($nochange) {
00067 $this->sqliteAssocCase = 0;
00068 } else {
00069 $this->sqliteAssocCase = 2;
00070 }
00071
00072 if ($file === null) {
00073 throw new SQLException("No SQLite database specified.");
00074 }
00075
00076 $mode = (isset($dsninfo['mode']) && is_numeric($dsninfo['mode'])) ? $dsninfo['mode'] : 0644;
00077
00078 if ($file != ':memory:') {
00079 if (!file_exists($file)) {
00080 touch($file);
00081 chmod($file, $mode);
00082 if (!file_exists($file)) {
00083 throw new SQLException("Unable to create SQLite database.");
00084 }
00085 }
00086 if (!is_file($file)) {
00087 throw new SQLException("Unable to open SQLite database: not a valid file.");
00088 }
00089 if (!is_readable($file)) {
00090 throw new SQLException("Unable to read SQLite database.");
00091 }
00092 }
00093
00094 $connect_function = $persistent ? 'sqlite_popen' : 'sqlite_open';
00095 if (!($conn = @$connect_function($file, $mode, $errmsg) )) {
00096 throw new SQLException("Unable to connect to SQLite database", $errmsg);
00097 }
00098
00099 $this->dblink = $conn;
00100 }
00101
00105 public function getDatabaseInfo()
00106 {
00107 require_once 'creole/drivers/sqlite/metadata/SQLiteDatabaseInfo.php';
00108 return new SQLiteDatabaseInfo($this);
00109 }
00110
00114 public function getIdGenerator()
00115 {
00116 require_once 'creole/drivers/sqlite/SQLiteIdGenerator.php';
00117 return new SQLiteIdGenerator($this);
00118 }
00119
00123 public function prepareStatement($sql)
00124 {
00125 require_once 'creole/drivers/sqlite/SQLitePreparedStatement.php';
00126 return new SQLitePreparedStatement($this, $sql);
00127 }
00128
00132 public function prepareCall($sql) {
00133 throw new SQLException('SQLite does not support stored procedures using CallableStatement.');
00134 }
00135
00139 public function createStatement()
00140 {
00141 require_once 'creole/drivers/sqlite/SQLiteStatement.php';
00142 return new SQLiteStatement($this);
00143 }
00144
00148 function close()
00149 {
00150 $ret = @sqlite_close($this->dblink);
00151 $this->dblink = null;
00152 return $ret;
00153 }
00154
00158 public function applyLimit(&$sql, $offset, $limit)
00159 {
00160 if ( $limit > 0 ) {
00161 $sql .= " LIMIT " . $limit . ($offset > 0 ? " OFFSET " . $offset : "");
00162 } elseif ( $offset > 0 ) {
00163 $sql .= " LIMIT -1 OFFSET " . $offset;
00164 }
00165 }
00166
00170 public function executeQuery($sql, $fetchmode = null)
00171 {
00172 ini_set('sqlite.assoc_case', $this->sqliteAssocCase);
00173 $this->lastQuery = $sql;
00174 $result = @sqlite_query($this->dblink, $this->lastQuery);
00175 if (!$result) {
00176 throw new SQLException('Could not execute query', $php_errormsg, $this->lastQuery);
00177 }
00178 require_once 'creole/drivers/sqlite/SQLiteResultSet.php';
00179 return new SQLiteResultSet($this, $result, $fetchmode);
00180 }
00181
00185 function executeUpdate($sql)
00186 {
00187 $this->lastQuery = $sql;
00188 $result = @sqlite_query($this->dblink, $this->lastQuery);
00189 if (!$result) {
00190 throw new SQLException('Could not execute update', $php_errormsg, $this->lastQuery);
00191 }
00192 return (int) @sqlite_changes($this->dblink);
00193 }
00194
00200 protected function beginTrans()
00201 {
00202 $result = @sqlite_query($this->dblink, 'BEGIN');
00203 if (!$result) {
00204 throw new SQLException('Could not begin transaction', $php_errormsg);
00205 }
00206 }
00207
00213 protected function commitTrans()
00214 {
00215 $result = @sqlite_query($this->dblink, 'COMMIT');
00216 if (!$result) {
00217 throw new SQLException('Can not commit transaction', $php_errormsg);
00218 }
00219 }
00220
00226 protected function rollbackTrans()
00227 {
00228 $result = @sqlite_query($this->dblink, 'ROLLBACK');
00229 if (!$result) {
00230 throw new SQLException('Could not rollback transaction', $php_errormsg);
00231 }
00232 }
00233
00240 function getUpdateCount()
00241 {
00242 return (int) @sqlite_changes($this->dblink);
00243 }
00244
00245 }