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