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);
44 // @sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id);
47 return ($this->db_connect_id) ? true : array('message' => $error);
51 * Version information about used database
52 * @param bool $raw if true, only return the fetched sql_server_version
53 * @param bool $use_cache if true, it is safe to retrieve the stored value from the cache
54 * @return string sql server version
56 function sql_server_info($raw = false, $use_cache = true)
60 if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false)
62 $result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id);
63 $row = @sqlite_fetch_array($result, SQLITE_ASSOC);
65 $this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0;
67 if (!empty($cache) && $use_cache)
69 $cache->put('sqlite_version', $this->sql_server_version);
73 return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version;
80 function _sql_transaction($status = 'begin')
85 return @sqlite_query('BEGIN', $this->db_connect_id);
89 return @sqlite_query('COMMIT', $this->db_connect_id);
93 return @sqlite_query('ROLLBACK', $this->db_connect_id);
103 * @param string $query Contains the SQL query which shall be executed
104 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
105 * @return mixed When casted to bool the returned value returns true on success and false on failure
109 function sql_query($query = '', $cache_ttl = 0)
115 // EXPLAIN only in extra debug mode
116 if (defined('DEBUG_EXTRA'))
118 $this->sql_report('start', $query);
121 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
122 $this->sql_add_num_queries($this->query_result);
124 if ($this->query_result === false)
126 if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false)
128 $this->sql_error($query);
131 if (defined('DEBUG_EXTRA'))
133 $this->sql_report('stop', $query);
136 if ($cache_ttl && method_exists($cache, 'sql_save'))
138 $this->open_queries[(int) $this->query_result] = $this->query_result;
139 $cache->sql_save($query, $this->query_result, $cache_ttl);
141 else if (strpos($query, 'SELECT') === 0 && $this->query_result)
143 $this->open_queries[(int) $this->query_result] = $this->query_result;
146 else if (defined('DEBUG_EXTRA'))
148 $this->sql_report('fromcache', $query);
156 return $this->query_result;
162 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
164 $this->query_result = false;
166 // if $total is set to 0 we do not want to limit the number of rows
172 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
174 return $this->sql_query($query, $cache_ttl);
178 * Return number of affected rows
180 function sql_affectedrows()
182 return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
188 function sql_fetchrow($query_id = false)
192 if ($query_id === false)
194 $query_id = $this->query_result;
197 if (isset($cache->sql_rowset[$query_id]))
199 return $cache->sql_fetchrow($query_id);
202 return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false;
206 * Seek to given row number
207 * rownum is zero-based
209 function sql_rowseek($rownum, &$query_id)
213 if ($query_id === false)
215 $query_id = $this->query_result;
218 if (isset($cache->sql_rowset[$query_id]))
220 return $cache->sql_rowseek($rownum, $query_id);
223 return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false;
227 * Get last inserted id after insert statement
229 function sql_nextid()
231 return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
237 function sql_freeresult($query_id = false)
241 if ($query_id === false)
243 $query_id = $this->query_result;
246 if (isset($cache->sql_rowset[$query_id]))
248 return $cache->sql_freeresult($query_id);
255 * Escape string used in sql query
257 function sql_escape($msg)
259 return @sqlite_escape_string($msg);
263 * Correctly adjust LIKE expression for special characters
264 * For SQLite an underscore is a not-known character... this may change with SQLite3
266 function sql_like_expression($expression)
268 // Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
269 // We only catch * and ? here, not the character map possible on file globbing.
270 $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
272 $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
273 $expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
275 return 'GLOB \'' . $this->sql_escape($expression) . '\'';
279 * return sql error array
282 function _sql_error()
285 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
286 'code' => @sqlite_last_error($this->db_connect_id)
291 * Build db-specific query data
294 function _sql_custom_build($stage, $data)
300 * Close sql connection
303 function _sql_close()
305 return @sqlite_close($this->db_connect_id);
309 * Build db-specific report
312 function _sql_report($mode, $query = '')
320 $endtime = explode(' ', microtime());
321 $endtime = $endtime[0] + $endtime[1];
323 $result = @sqlite_query($query, $this->db_connect_id);
324 while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
326 // Take the time spent on parsing rows into account
329 $splittime = explode(' ', microtime());
330 $splittime = $splittime[0] + $splittime[1];
332 $this->sql_report('record_fromcache', $query, $endtime, $splittime);