2
include_once dirname(__FILE__) . '/sqlite.class.php';
3
if( !defined('PDO_ATTR_TIMEOUT') ) define('PDO_ATTR_TIMEOUT', PDO::ATTR_TIMEOUT);
4
if( !defined('PDO_FETCH_ASSOC') ) define('PDO_FETCH_ASSOC', PDO::FETCH_ASSOC);
5
if( !defined('PDO_FETCH_NUM') ) define('PDO_FETCH_NUM', PDO::FETCH_NUM);
6
if( !defined('PDO_FETCH_BOTH') ) define('PDO_FETCH_BOTH', PDO::FETCH_BOTH);
7
if( !defined('PDO_ATTR_PERSISTENT') ) define('PDO_ATTR_PERSISTENT', PDO::ATTR_PERSISTENT);
8
if( !defined('PDO_ATTR_CASE') ) define('PDO_ATTR_CASE', PDO::ATTR_CASE);
9
if( !defined('PDO_CASE_NATURAL') ) define('PDO_CASE_NATURAL', PDO::CASE_NATURAL);
10
if( !defined('PDO_ATTR_AUTOCOMMIT') ) define('PDO_ATTR_AUTOCOMMIT', PDO::ATTR_AUTOCOMMIT);
11
if( !defined('PDO_ATTR_ERRMODE') ) define('PDO_ATTR_ERRMODE', PDO::ATTR_ERRMODE);
12
if( !defined('PDO_ERRMODE_EXCEPTION') ) define('PDO_ERRMODE_EXCEPTION', PDO::ERRMODE_EXCEPTION);
13
if( !defined('PDO_ERRMODE_SILENT') ) define('PDO_ERRMODE_SILENT', PDO::ERRMODE_SILENT);
15
if( !defined('SQLITE_BOTH') ) define('SQLITE_BOTH', PDO_FETCH_BOTH);
16
if( !defined('SQLITE_ASSOC') ) define('SQLITE_ASSOC', PDO_FETCH_ASSOC);
17
if( !defined('SQLITE_NUM') ) define('SQLITE_NUM', PDO_FETCH_NUM);
19
class sqlite3 extends sqlite {
22
function sqlite3($dbPath) {
24
if($dbPath == ':memory:') {
25
$this->readOnly = false;
27
$this->readOnly = !is_writable($dbPath);
29
parent::sqlite($dbPath);
30
if(class_exists('PDO') && $this->connect($dbPath)) {
38
function connect($dbPath) {
42
$arrayAttrib = array();
43
if($dbPath == ':memory:') $arrayAttrib[PDO_ATTR_PERSISTENT] = true;
44
$this->connId = new PDO('sqlite:'.$dbPath, $user, $password, $arrayAttrib);
46
$this->connId->setAttribute(PDO_ATTR_CASE, PDO_CASE_NATURAL);
47
$this->connId->query("PRAGMA count_changes=1;");
48
} catch (PDOException $e) {
50
$this->errorMessage = $e->getMessage();
54
$this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
56
$this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_SILENT);
61
function getError($errorCode = null) {
62
if(is_resource($this->resId)) {
63
$errorInfo = $this->resId->errorInfo();
65
$errorInfo = $this->connId->errorInfo();
67
if(is_array($errorInfo) && isset($errorInfo[2])) {
69
$this->errorMessage = $errorInfo[2];
71
$this->errorMessage = 'not an error';
73
return $this->errorMessage;
76
function getErrorMessage() {
77
return $this->errorMessage;
80
function escape($string) {
81
if(function_exists('sqlite_escape_string')) {
82
$res = sqlite_escape_string($string);
84
$res = str_replace("'", "''", $string);
89
function query($sqlString, $buffered = true, $assign = true) {
91
if($assign && is_object($this->resId)) $this->resId->closeCursor();
92
if(substr(trim($sqlString), -1) != ';') $sqlString .= ';';
93
if(DEBUG) $resId = $this->connId->query($sqlString);
94
else $resId = @$this->connId->query($sqlString);
95
if($assign) $this->resId = $resId;
96
} catch(PDOException $e) {
98
$this->errorMessage = $e->getMessage();
101
$tempErrorInfo = $this->connId->errorInfo();
102
if(is_array($tempErrorInfo) && isset($tempErrorInfo[0]) && ($tempErrorInfo[0] != '00000')) {
104
$this->errorMessage = $tempErrorInfo[2];
110
function array_query($sqlString, $result_type=SQLITE_BOTH, $decode_binary=true) {
112
$result_type = $this->convertResultType($result_type);
113
$q = $this->query($sqlString);
114
$rows = $q->fetchAll($result_type);
115
} catch(PDOException $e) {
117
$this->errorMessage = $e->getMessage();
123
function num_rows($resId = null) {
125
if($resId == null) $resId = $this->resId;
126
if(DEBUG) $out = $resId->rowCount();
127
else $out = @$resId->rowCount();
128
} catch(PDOException $e) {
130
$this->errorMessage = $e->getMessage();
136
function fetch_single($resId=null, $result_type=SQLITE_BOTH) {
138
$result_type = $this->convertResultType($result_type);
139
if($resId == null) $resId = $this->resId;
140
if(DEBUG) $out = $resId->fetchColumn();
141
else $out = @$resId->fetchColumn();
142
} catch(PDOException $e) {
144
$this->errorMessage = $e->getMessage();
150
function fetch_array($resId=null, $result_type=SQLITE_BOTH, $decode_binary=true) {
152
$result_type = $this->convertResultType($result_type);
153
if($resId == null) $resId = $this->resId;
154
if(DEBUG) $out = $resId->fetch($result_type);
155
else $out = @$resId->fetch($result_type);
156
} catch(PDOException $e) {
158
$this->errorMessage = $e->getMessage();
164
function last_insert_id() {
166
if(DEBUG) $out = $this->connId->lastInsertId();
167
else $out = @$this->connId->lastInsertId();
168
} catch(PDOException $e) {
170
$this->errorMessage = $e->getMessage();
176
function changes($resId = null) {
178
if($resId == null) $resId = $this->resId;
179
if(is_object($resId)) $out = $this->num_rows($resId);
181
} catch(PDOException $e) {
183
$this->errorMessage = $e->getMessage();
189
function num_fields($resId = null) {
191
if($resId == null) $resId = $this->resId;
192
if(DEBUG) $out = $resId->columnCount();
193
else $out = @$resId->columnCount();
194
} catch(PDOException $e) {
196
$this->errorMessage = $e->getMessage();
202
function field_name($resId = null, $index) {
204
if($resId == null) $resId = $this->resId;
205
if(DEBUG) $tempColInfo = $resId->getColumnMeta($index);
206
else $tempColInfo = @$resId->getColumnMeta($index);
207
} catch(PDOException $e) {
209
$this->errorMessage = $e->getMessage();
212
return $tempColInfo["name"];
215
function create_function($function_name, $callback, $num_arg=null) {
217
if(method_exists($this->connId, 'sqliteCreateFunction')) {
218
if(DEBUG) return $this->connId->sqliteCreateFunction($function_name, $callback, $num_arg);
219
else return @$this->connId->sqliteCreateFunction($function_name, $callback, $num_arg);
223
} catch(PDOException $e) {
225
$this->errorMessage = $e->getMessage();
230
function create_aggregate($function_name, $step_func, $finalize_func, $num_args=null) {
231
if(method_exists($this->connId, 'sqliteCreateAggregate')) {
233
if(DEBUG) return $this->connId->sqliteCreateAggregate($function_name, $step_func, $finalize_func, $num_args);
234
else return @$this->connId->sqliteCreateAggregate($function_name, $step_func, $finalize_func, $num_args);
235
} catch(PDOException $e) {
237
$this->errorMessage = $e->getMessage();
245
function sqlite_version() {
247
$query = "SELECT sqlite_version();";
248
$res = $this->query($query, true, false);
249
$Result = $this->fetch_single($res, SQLITE_NUM);
250
if($Result) return $Result;
252
} catch(PDOException $e) {
254
$this->errorMessage = $e->getMessage();
263
function sqlitem_busy_timeout($milliseconds=0) {
265
$this->connId->setAttribute(PDO_ATTR_TIMEOUT, $milliseconds);
267
} catch(PDOException $e) {
269
$this->errorMessage = $e->getMessage();
274
function convertResultType($type) {
275
$fetch_style = PDO_FETCH_BOTH;
276
if($type == SQLITE_ASSOC) $fetch_style = PDO_FETCH_ASSOC;
277
elseif($type == SQLITE_NUM) $fetch_style = PDO_FETCH_NUM;
278
elseif($type == SQLITE_BOTH) $fetch_style = PDO_FETCH_BOTH;
282
function beginTransaction() {
284
$this->connId->beginTransaction();
285
} catch(PDOException $e) {
287
$this->errorMessage = $e->getMessage();
292
function commitTransaction() {
294
$this->connId->commit();
295
} catch(PDOException $e) {
297
$this->errorMessage = $e->getMessage();
302
function rollBackTransaction() {
304
$this->connId->rollBack();
305
} catch(PDOException $e) {
307
$this->errorMessage = $e->getMessage();