5
* @version $Id: sqlite.php,v 1.38 2007/10/05 14:36:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
19
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
22
* Sqlite Database Abstraction Layer
23
* Minimum Requirement: 2.8.2+
26
class dbal_sqlite extends dbal
31
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
33
$this->persistency = $persistency;
34
$this->user = $sqluser;
35
$this->server = $sqlserver . (($port) ? ':' . $port : '');
36
$this->dbname = $database;
39
$this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error);
41
if ($this->db_connect_id)
43
@sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
47
return ($this->db_connect_id) ? true : array('message' => $error);
51
* Version information about used database
53
function sql_server_info()
55
return 'SQLite ' . @sqlite_libversion();
62
function _sql_transaction($status = 'begin')
67
return @sqlite_query('BEGIN', $this->db_connect_id);
71
return @sqlite_query('COMMIT', $this->db_connect_id);
75
return @sqlite_query('ROLLBACK', $this->db_connect_id);
85
* @param string $query Contains the SQL query which shall be executed
86
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
87
* @return mixed When casted to bool the returned value returns true on success and false on failure
91
function sql_query($query = '', $cache_ttl = 0)
97
// EXPLAIN only in extra debug mode
98
if (defined('DEBUG_EXTRA'))
100
$this->sql_report('start', $query);
103
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
104
$this->sql_add_num_queries($this->query_result);
106
if ($this->query_result === false)
108
if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false)
110
$this->sql_error($query);
113
if (defined('DEBUG_EXTRA'))
115
$this->sql_report('stop', $query);
118
if ($cache_ttl && method_exists($cache, 'sql_save'))
120
$this->open_queries[(int) $this->query_result] = $this->query_result;
121
$cache->sql_save($query, $this->query_result, $cache_ttl);
123
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
125
$this->open_queries[(int) $this->query_result] = $this->query_result;
128
else if (defined('DEBUG_EXTRA'))
130
$this->sql_report('fromcache', $query);
138
return ($this->query_result) ? $this->query_result : false;
144
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
146
$this->query_result = false;
148
// if $total is set to 0 we do not want to limit the number of rows
154
$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
156
return $this->sql_query($query, $cache_ttl);
160
* Return number of affected rows
162
function sql_affectedrows()
164
return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
170
function sql_fetchrow($query_id = false)
174
if ($query_id === false)
176
$query_id = $this->query_result;
179
if (isset($cache->sql_rowset[$query_id]))
181
return $cache->sql_fetchrow($query_id);
184
return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false;
188
* Seek to given row number
189
* rownum is zero-based
191
function sql_rowseek($rownum, &$query_id)
195
if ($query_id === false)
197
$query_id = $this->query_result;
200
if (isset($cache->sql_rowset[$query_id]))
202
return $cache->sql_rowseek($rownum, $query_id);
205
return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false;
209
* Get last inserted id after insert statement
211
function sql_nextid()
213
return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
219
function sql_freeresult($query_id = false)
223
if ($query_id === false)
225
$query_id = $this->query_result;
228
if (isset($cache->sql_rowset[$query_id]))
230
return $cache->sql_freeresult($query_id);
237
* Escape string used in sql query
239
function sql_escape($msg)
241
return @sqlite_escape_string($msg);
245
* Correctly adjust LIKE expression for special characters
246
* For SQLite an underscore is a not-known character... this may change with SQLite3
248
function sql_like_expression($expression)
250
// Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
251
// We only catch * and ? here, not the character map possible on file globbing.
252
$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
254
$expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
255
$expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
257
return 'GLOB \'' . $this->sql_escape($expression) . '\'';
261
* return sql error array
264
function _sql_error()
267
'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
268
'code' => @sqlite_last_error($this->db_connect_id)
273
* Build db-specific query data
276
function _sql_custom_build($stage, $data)
282
* Close sql connection
285
function _sql_close()
287
return @sqlite_close($this->db_connect_id);
291
* Build db-specific report
294
function _sql_report($mode, $query = '')
302
$endtime = explode(' ', microtime());
303
$endtime = $endtime[0] + $endtime[1];
305
$result = @sqlite_query($query, $this->db_connect_id);
306
while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
308
// Take the time spent on parsing rows into account
311
$splittime = explode(' ', microtime());
312
$splittime = $splittime[0] + $splittime[1];
314
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
b'\\ No newline at end of file'