| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | *
|
|---|
| 4 | * @package dbal
|
|---|
| 5 | * @version $Id: sqlite.php 8814 2008-09-04 12:01:47Z acydburn $
|
|---|
| 6 | * @copyright (c) 2005 phpBB Group
|
|---|
| 7 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | /**
|
|---|
| 12 | * @ignore
|
|---|
| 13 | */
|
|---|
| 14 | if (!defined('IN_PHPBB'))
|
|---|
| 15 | {
|
|---|
| 16 | exit;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Sqlite Database Abstraction Layer
|
|---|
| 23 | * Minimum Requirement: 2.8.2+
|
|---|
| 24 | * @package dbal
|
|---|
| 25 | */
|
|---|
| 26 | class dbal_sqlite extends dbal
|
|---|
| 27 | {
|
|---|
| 28 | /**
|
|---|
| 29 | * Connect to server
|
|---|
| 30 | */
|
|---|
| 31 | function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
|
|---|
| 32 | {
|
|---|
| 33 | $this->persistency = $persistency;
|
|---|
| 34 | $this->user = $sqluser;
|
|---|
| 35 | $this->server = $sqlserver . (($port) ? ':' . $port : '');
|
|---|
| 36 | $this->dbname = $database;
|
|---|
| 37 |
|
|---|
| 38 | $error = '';
|
|---|
| 39 | $this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error);
|
|---|
| 40 |
|
|---|
| 41 | if ($this->db_connect_id)
|
|---|
| 42 | {
|
|---|
| 43 | @sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
|
|---|
| 44 | // @sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | return ($this->db_connect_id) ? true : array('message' => $error);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Version information about used database
|
|---|
| 52 | * @param bool $raw if true, only return the fetched sql_server_version
|
|---|
| 53 | * @return string sql server version
|
|---|
| 54 | */
|
|---|
| 55 | function sql_server_info($raw = false)
|
|---|
| 56 | {
|
|---|
| 57 | global $cache;
|
|---|
| 58 |
|
|---|
| 59 | if (empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false)
|
|---|
| 60 | {
|
|---|
| 61 | $result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id);
|
|---|
| 62 | $row = @sqlite_fetch_array($result, SQLITE_ASSOC);
|
|---|
| 63 |
|
|---|
| 64 | $this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0;
|
|---|
| 65 | $cache->put('sqlite_version', $this->sql_server_version);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * SQL Transaction
|
|---|
| 73 | * @access private
|
|---|
| 74 | */
|
|---|
| 75 | function _sql_transaction($status = 'begin')
|
|---|
| 76 | {
|
|---|
| 77 | switch ($status)
|
|---|
| 78 | {
|
|---|
| 79 | case 'begin':
|
|---|
| 80 | return @sqlite_query('BEGIN', $this->db_connect_id);
|
|---|
| 81 | break;
|
|---|
| 82 |
|
|---|
| 83 | case 'commit':
|
|---|
| 84 | return @sqlite_query('COMMIT', $this->db_connect_id);
|
|---|
| 85 | break;
|
|---|
| 86 |
|
|---|
| 87 | case 'rollback':
|
|---|
| 88 | return @sqlite_query('ROLLBACK', $this->db_connect_id);
|
|---|
| 89 | break;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | return true;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Base query method
|
|---|
| 97 | *
|
|---|
| 98 | * @param string $query Contains the SQL query which shall be executed
|
|---|
| 99 | * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
|
|---|
| 100 | * @return mixed When casted to bool the returned value returns true on success and false on failure
|
|---|
| 101 | *
|
|---|
| 102 | * @access public
|
|---|
| 103 | */
|
|---|
| 104 | function sql_query($query = '', $cache_ttl = 0)
|
|---|
| 105 | {
|
|---|
| 106 | if ($query != '')
|
|---|
| 107 | {
|
|---|
| 108 | global $cache;
|
|---|
| 109 |
|
|---|
| 110 | // EXPLAIN only in extra debug mode
|
|---|
| 111 | if (defined('DEBUG_EXTRA'))
|
|---|
| 112 | {
|
|---|
| 113 | $this->sql_report('start', $query);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
|
|---|
| 117 | $this->sql_add_num_queries($this->query_result);
|
|---|
| 118 |
|
|---|
| 119 | if ($this->query_result === false)
|
|---|
| 120 | {
|
|---|
| 121 | if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false)
|
|---|
| 122 | {
|
|---|
| 123 | $this->sql_error($query);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | if (defined('DEBUG_EXTRA'))
|
|---|
| 127 | {
|
|---|
| 128 | $this->sql_report('stop', $query);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | if ($cache_ttl && method_exists($cache, 'sql_save'))
|
|---|
| 132 | {
|
|---|
| 133 | $this->open_queries[(int) $this->query_result] = $this->query_result;
|
|---|
| 134 | $cache->sql_save($query, $this->query_result, $cache_ttl);
|
|---|
| 135 | }
|
|---|
| 136 | else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
|---|
| 137 | {
|
|---|
| 138 | $this->open_queries[(int) $this->query_result] = $this->query_result;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | else if (defined('DEBUG_EXTRA'))
|
|---|
| 142 | {
|
|---|
| 143 | $this->sql_report('fromcache', $query);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | else
|
|---|
| 147 | {
|
|---|
| 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | return $this->query_result;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Build LIMIT query
|
|---|
| 156 | */
|
|---|
| 157 | function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
|---|
| 158 | {
|
|---|
| 159 | $this->query_result = false;
|
|---|
| 160 |
|
|---|
| 161 | // if $total is set to 0 we do not want to limit the number of rows
|
|---|
| 162 | if ($total == 0)
|
|---|
| 163 | {
|
|---|
| 164 | $total = -1;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
|
|---|
| 168 |
|
|---|
| 169 | return $this->sql_query($query, $cache_ttl);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * Return number of affected rows
|
|---|
| 174 | */
|
|---|
| 175 | function sql_affectedrows()
|
|---|
| 176 | {
|
|---|
| 177 | return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Fetch current row
|
|---|
| 182 | */
|
|---|
| 183 | function sql_fetchrow($query_id = false)
|
|---|
| 184 | {
|
|---|
| 185 | global $cache;
|
|---|
| 186 |
|
|---|
| 187 | if ($query_id === false)
|
|---|
| 188 | {
|
|---|
| 189 | $query_id = $this->query_result;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | if (isset($cache->sql_rowset[$query_id]))
|
|---|
| 193 | {
|
|---|
| 194 | return $cache->sql_fetchrow($query_id);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /**
|
|---|
| 201 | * Seek to given row number
|
|---|
| 202 | * rownum is zero-based
|
|---|
| 203 | */
|
|---|
| 204 | function sql_rowseek($rownum, &$query_id)
|
|---|
| 205 | {
|
|---|
| 206 | global $cache;
|
|---|
| 207 |
|
|---|
| 208 | if ($query_id === false)
|
|---|
| 209 | {
|
|---|
| 210 | $query_id = $this->query_result;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | if (isset($cache->sql_rowset[$query_id]))
|
|---|
| 214 | {
|
|---|
| 215 | return $cache->sql_rowseek($rownum, $query_id);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | * Get last inserted id after insert statement
|
|---|
| 223 | */
|
|---|
| 224 | function sql_nextid()
|
|---|
| 225 | {
|
|---|
| 226 | return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /**
|
|---|
| 230 | * Free sql result
|
|---|
| 231 | */
|
|---|
| 232 | function sql_freeresult($query_id = false)
|
|---|
| 233 | {
|
|---|
| 234 | global $cache;
|
|---|
| 235 |
|
|---|
| 236 | if ($query_id === false)
|
|---|
| 237 | {
|
|---|
| 238 | $query_id = $this->query_result;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | if (isset($cache->sql_rowset[$query_id]))
|
|---|
| 242 | {
|
|---|
| 243 | return $cache->sql_freeresult($query_id);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | return true;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /**
|
|---|
| 250 | * Escape string used in sql query
|
|---|
| 251 | */
|
|---|
| 252 | function sql_escape($msg)
|
|---|
| 253 | {
|
|---|
| 254 | return @sqlite_escape_string($msg);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /**
|
|---|
| 258 | * Correctly adjust LIKE expression for special characters
|
|---|
| 259 | * For SQLite an underscore is a not-known character... this may change with SQLite3
|
|---|
| 260 | */
|
|---|
| 261 | function sql_like_expression($expression)
|
|---|
| 262 | {
|
|---|
| 263 | // Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
|
|---|
| 264 | // We only catch * and ? here, not the character map possible on file globbing.
|
|---|
| 265 | $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
|
|---|
| 266 |
|
|---|
| 267 | $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
|
|---|
| 268 | $expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
|
|---|
| 269 |
|
|---|
| 270 | return 'GLOB \'' . $this->sql_escape($expression) . '\'';
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /**
|
|---|
| 274 | * return sql error array
|
|---|
| 275 | * @access private
|
|---|
| 276 | */
|
|---|
| 277 | function _sql_error()
|
|---|
| 278 | {
|
|---|
| 279 | return array(
|
|---|
| 280 | 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
|
|---|
| 281 | 'code' => @sqlite_last_error($this->db_connect_id)
|
|---|
| 282 | );
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /**
|
|---|
| 286 | * Build db-specific query data
|
|---|
| 287 | * @access private
|
|---|
| 288 | */
|
|---|
| 289 | function _sql_custom_build($stage, $data)
|
|---|
| 290 | {
|
|---|
| 291 | return $data;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /**
|
|---|
| 295 | * Close sql connection
|
|---|
| 296 | * @access private
|
|---|
| 297 | */
|
|---|
| 298 | function _sql_close()
|
|---|
| 299 | {
|
|---|
| 300 | return @sqlite_close($this->db_connect_id);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /**
|
|---|
| 304 | * Build db-specific report
|
|---|
| 305 | * @access private
|
|---|
| 306 | */
|
|---|
| 307 | function _sql_report($mode, $query = '')
|
|---|
| 308 | {
|
|---|
| 309 | switch ($mode)
|
|---|
| 310 | {
|
|---|
| 311 | case 'start':
|
|---|
| 312 | break;
|
|---|
| 313 |
|
|---|
| 314 | case 'fromcache':
|
|---|
| 315 | $endtime = explode(' ', microtime());
|
|---|
| 316 | $endtime = $endtime[0] + $endtime[1];
|
|---|
| 317 |
|
|---|
| 318 | $result = @sqlite_query($query, $this->db_connect_id);
|
|---|
| 319 | while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
|
|---|
| 320 | {
|
|---|
| 321 | // Take the time spent on parsing rows into account
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | $splittime = explode(' ', microtime());
|
|---|
| 325 | $splittime = $splittime[0] + $splittime[1];
|
|---|
| 326 |
|
|---|
| 327 | $this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
|---|
| 328 |
|
|---|
| 329 | break;
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | ?>
|
|---|