Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
| 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
| 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * SQLite3 Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
| 32 | * creates dynamically based on whether the active record |
| 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
| 38 | * @author Andrey Andreev |
| 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_sqlite3_driver extends CI_DB { |
| 42 | |
| 43 | public $dbdriver = 'sqlite3'; |
| 44 | |
| 45 | // The character used for escaping |
| 46 | public $_escape_char = '"'; |
| 47 | |
| 48 | // clause and character used for LIKE escape sequences |
| 49 | public $_like_escape_str = ' ESCAPE \'%s\' '; |
| 50 | public $_like_escape_chr = '!'; |
| 51 | |
| 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
| 55 | * used for the count_all() and count_all_results() functions. |
| 56 | */ |
| 57 | public $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | public $_random_keyword = ' RANDOM()'; |
| 59 | |
| 60 | /** |
| 61 | * Non-persistent database connection |
| 62 | * |
| 63 | * @return object type SQLite3 |
| 64 | */ |
| 65 | public function db_connect() |
| 66 | { |
| 67 | try |
| 68 | { |
| 69 | return ( ! $this->password) |
| 70 | ? new SQLite3($this->database) |
| 71 | : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password); |
| 72 | } |
| 73 | catch (Exception $e) |
| 74 | { |
| 75 | return FALSE; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Persistent database connection |
| 83 | * |
| 84 | * @return object type SQLite3 |
| 85 | */ |
| 86 | public function db_pconnect() |
| 87 | { |
| 88 | log_message('debug', 'SQLite3 doesn\'t support persistent connections'); |
| 89 | return $this->db_pconnect(); |
| 90 | } |
| 91 | |
| 92 | // -------------------------------------------------------------------- |
| 93 | |
| 94 | /** |
| 95 | * Reconnect |
| 96 | * |
| 97 | * Keep / reestablish the db connection if no queries have been |
| 98 | * sent for a length of time exceeding the server's idle timeout |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | public function reconnect() |
| 103 | { |
| 104 | // Not supported |
| 105 | } |
| 106 | |
| 107 | // -------------------------------------------------------------------- |
| 108 | |
| 109 | /** |
| 110 | * Select the database |
| 111 | * |
| 112 | * @return bool |
| 113 | */ |
| 114 | public function db_select() |
| 115 | { |
| 116 | // Not needed, in SQLite every pseudo-connection is a database |
| 117 | return TRUE; |
| 118 | } |
| 119 | |
| 120 | // -------------------------------------------------------------------- |
| 121 | |
| 122 | /** |
| 123 | * Set client character set |
| 124 | * |
| 125 | * @param string |
| 126 | * @param string |
| 127 | * @return bool |
| 128 | */ |
| 129 | public function db_set_charset($charset, $collation) |
| 130 | { |
| 131 | // Not (natively) supported |
| 132 | return TRUE; |
| 133 | } |
| 134 | |
| 135 | // -------------------------------------------------------------------- |
| 136 | |
| 137 | /** |
| 138 | * Version number query string |
| 139 | * |
| 140 | * @return string |
| 141 | */ |
| 142 | protected function _version() |
| 143 | { |
| 144 | return implode(' (', $this->conn_id->version()).')'; |
| 145 | } |
| 146 | |
| 147 | // -------------------------------------------------------------------- |
| 148 | |
| 149 | /** |
| 150 | * Execute the query |
| 151 | * |
| 152 | * @param string an SQL query |
| 153 | * @return mixed SQLite3Result object or bool |
| 154 | */ |
| 155 | protected function _execute($sql) |
| 156 | { |
| 157 | if ( ! preg_match('/^(SELECT|EXPLAIN).+$/i', ltrim($sql))) |
| 158 | { |
| 159 | return $this->conn_id->exec($sql); |
| 160 | } |
| 161 | |
| 162 | // TODO: Implement use of SQLite3::querySingle(), if needed |
| 163 | // TODO: Use $this->_prep_query(), if needed |
| 164 | return $this->conn_id->query($sql); |
| 165 | } |
| 166 | |
| 167 | // -------------------------------------------------------------------- |
| 168 | |
| 169 | /** |
| 170 | * Prep the query |
| 171 | * |
| 172 | * If needed, each database adapter can prep the query string |
| 173 | * |
| 174 | * @param string an SQL query |
| 175 | * @return string |
| 176 | */ |
| 177 | private function _prep_query($sql) |
| 178 | { |
| 179 | return $this->conn_id->prepare($sql); |
| 180 | } |
| 181 | |
| 182 | // -------------------------------------------------------------------- |
| 183 | |
| 184 | /** |
| 185 | * Begin Transaction |
| 186 | * |
| 187 | * @return bool |
| 188 | */ |
| 189 | public function trans_begin($test_mode = FALSE) |
| 190 | { |
| 191 | if ( ! $this->trans_enabled) |
| 192 | { |
| 193 | return TRUE; |
| 194 | } |
| 195 | |
| 196 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 197 | if ($this->_trans_depth > 0) |
| 198 | { |
| 199 | return TRUE; |
| 200 | } |
| 201 | |
| 202 | // Reset the transaction failure flag. |
| 203 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 204 | // even if the queries produce a successful result. |
| 205 | $this->_trans_failure = ($test_mode === TRUE); |
| 206 | |
| 207 | return $this->conn_id->exec('BEGIN TRANSACTION'); |
| 208 | } |
| 209 | |
| 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
| 213 | * Commit Transaction |
| 214 | * |
| 215 | * @return bool |
| 216 | */ |
| 217 | public function trans_commit() |
| 218 | { |
| 219 | if ( ! $this->trans_enabled) |
| 220 | { |
| 221 | return TRUE; |
| 222 | } |
| 223 | |
| 224 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 225 | if ($this->_trans_depth > 0) |
| 226 | { |
| 227 | return TRUE; |
| 228 | } |
| 229 | |
| 230 | return $this->conn_id->exec('END TRANSACTION'); |
| 231 | } |
| 232 | |
| 233 | // -------------------------------------------------------------------- |
| 234 | |
| 235 | /** |
| 236 | * Rollback Transaction |
| 237 | * |
| 238 | * @return bool |
| 239 | */ |
| 240 | public function trans_rollback() |
| 241 | { |
| 242 | if ( ! $this->trans_enabled) |
| 243 | { |
| 244 | return TRUE; |
| 245 | } |
| 246 | |
| 247 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 248 | if ($this->_trans_depth > 0) |
| 249 | { |
| 250 | return TRUE; |
| 251 | } |
| 252 | |
| 253 | return $this->conn_id->exec('ROLLBACK'); |
| 254 | } |
| 255 | |
| 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * Escape String |
| 260 | * |
| 261 | * @param string |
| 262 | * @param bool whether or not the string will be used in a LIKE condition |
| 263 | * @return string |
| 264 | */ |
| 265 | public function escape_str($str, $like = FALSE) |
| 266 | { |
| 267 | if (is_array($str)) |
| 268 | { |
| 269 | foreach ($str as $key => $val) |
| 270 | { |
| 271 | $str[$key] = $this->escape_str($val, $like); |
| 272 | } |
| 273 | |
| 274 | return $str; |
| 275 | } |
| 276 | |
| 277 | $str = $this->conn_id->escapeString(remove_invisible_characters($str)); |
| 278 | |
| 279 | // escape LIKE condition wildcards |
| 280 | if ($like === TRUE) |
| 281 | { |
| 282 | return str_replace(array('%', '_', $this->_like_escape_chr), |
| 283 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 284 | $str); |
| 285 | } |
| 286 | |
| 287 | return $str; |
| 288 | } |
| 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Affected Rows |
| 294 | * |
| 295 | * @return int |
| 296 | */ |
| 297 | public function affected_rows() |
| 298 | { |
| 299 | return $this->conn_id->changes(); |
| 300 | } |
| 301 | |
| 302 | // -------------------------------------------------------------------- |
| 303 | |
| 304 | /** |
| 305 | * Insert ID |
| 306 | * |
| 307 | * @return int |
| 308 | */ |
| 309 | public function insert_id() |
| 310 | { |
| 311 | return $this->conn_id->lastInsertRowID(); |
| 312 | } |
| 313 | |
| 314 | // -------------------------------------------------------------------- |
| 315 | |
| 316 | /** |
| 317 | * "Count All" query |
| 318 | * |
| 319 | * Generates a platform-specific query string that counts all records in |
| 320 | * the specified database |
| 321 | * |
| 322 | * @param string |
| 323 | * @return int |
| 324 | */ |
| 325 | public function count_all($table = '') |
| 326 | { |
| 327 | if ($table == '') |
| 328 | { |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | $result = $this->conn_id->querySingle($this->_count_string.$this->_protect_identifiers('numrows') |
| 333 | .' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 334 | |
| 335 | return empty($result) ? 0 : (int) $result; |
| 336 | } |
| 337 | |
| 338 | // -------------------------------------------------------------------- |
| 339 | |
| 340 | /** |
| 341 | * Show table query |
| 342 | * |
| 343 | * Generates a platform-specific query string so that the table names can be fetched |
| 344 | * |
| 345 | * @param bool |
| 346 | * @return string |
| 347 | */ |
| 348 | protected function _list_tables($prefix_limit = FALSE) |
| 349 | { |
| 350 | return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\'' |
| 351 | .(($prefix_limit !== FALSE && $this->dbprefix != '') |
| 352 | ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr) |
| 353 | : ''); |
| 354 | } |
| 355 | |
| 356 | // -------------------------------------------------------------------- |
| 357 | |
| 358 | /** |
| 359 | * Show column query |
| 360 | * |
| 361 | * Generates a platform-specific query string so that the column names can be fetched |
| 362 | * |
| 363 | * @param string the table name |
| 364 | * @return string |
| 365 | */ |
| 366 | protected function _list_columns($table = '') |
| 367 | { |
| 368 | // Not supported |
| 369 | return FALSE; |
| 370 | } |
| 371 | |
| 372 | // -------------------------------------------------------------------- |
| 373 | |
| 374 | /** |
| 375 | * Field data query |
| 376 | * |
| 377 | * Generates a platform-specific query so that the column data can be retrieved |
| 378 | * |
| 379 | * @param string the table name |
| 380 | * @return string |
| 381 | */ |
| 382 | protected function _field_data($table) |
| 383 | { |
| 384 | return 'SELECT * FROM '.$table.' LIMIT 0,1'; |
| 385 | } |
| 386 | |
| 387 | // -------------------------------------------------------------------- |
| 388 | |
| 389 | /** |
| 390 | * The error message string |
| 391 | * |
| 392 | * @return string |
| 393 | */ |
| 394 | protected function _error_message() |
| 395 | { |
| 396 | return $this->conn_id->lastErrorMsg(); |
| 397 | } |
| 398 | |
| 399 | // -------------------------------------------------------------------- |
| 400 | |
| 401 | /** |
| 402 | * The error message number |
| 403 | * |
| 404 | * @return int |
| 405 | */ |
| 406 | protected function _error_number() |
| 407 | { |
| 408 | return $this->conn_id->lastErrorCode(); |
| 409 | } |
| 410 | |
| 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * Escape the SQL Identifiers |
| 415 | * |
| 416 | * This function escapes column and table names |
| 417 | * |
| 418 | * @param string |
| 419 | * @return string |
| 420 | */ |
| 421 | protected function _escape_identifiers($item) |
| 422 | { |
| 423 | if ($this->_escape_char == '') |
| 424 | { |
| 425 | return $item; |
| 426 | } |
| 427 | |
| 428 | foreach ($this->_reserved_identifiers as $id) |
| 429 | { |
| 430 | if (strpos($item, '.'.$id) !== FALSE) |
| 431 | { |
| 432 | $item = str_replace('.', $this->_escape_char.'.', $item); |
| 433 | |
| 434 | // remove duplicates if the user already included the escape |
| 435 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (strpos($item, '.') !== FALSE) |
| 440 | { |
| 441 | $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); |
| 442 | } |
| 443 | |
| 444 | // remove duplicates if the user already included the escape |
| 445 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); |
| 446 | } |
| 447 | |
| 448 | // -------------------------------------------------------------------- |
| 449 | |
| 450 | /** |
| 451 | * From Tables |
| 452 | * |
| 453 | * This function implicitly groups FROM tables so there is no confusion |
| 454 | * about operator precedence in harmony with SQL standards |
| 455 | * |
| 456 | * @param string |
| 457 | * @return string |
| 458 | */ |
| 459 | protected function _from_tables($tables) |
| 460 | { |
| 461 | if ( ! is_array($tables)) |
| 462 | { |
| 463 | $tables = array($tables); |
| 464 | } |
| 465 | |
| 466 | return '('.implode(', ', $tables).')'; |
| 467 | } |
| 468 | |
| 469 | // -------------------------------------------------------------------- |
| 470 | |
| 471 | /** |
| 472 | * Insert statement |
| 473 | * |
| 474 | * Generates a platform-specific insert string from the supplied data |
| 475 | * |
| 476 | * @param string the table name |
| 477 | * @param array the insert keys |
| 478 | * @param array the insert values |
| 479 | * @return string |
| 480 | */ |
| 481 | protected function _insert($table, $keys, $values) |
| 482 | { |
| 483 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
| 484 | } |
| 485 | |
| 486 | // -------------------------------------------------------------------- |
| 487 | |
| 488 | /** |
| 489 | * Update statement |
| 490 | * |
| 491 | * Generates a platform-specific update string from the supplied data |
| 492 | * |
| 493 | * @param string the table name |
| 494 | * @param array the update data |
| 495 | * @param array the where clause |
| 496 | * @param array the orderby clause |
| 497 | * @param array the limit clause |
| 498 | * @return string |
| 499 | */ |
| 500 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 501 | { |
| 502 | foreach ($values as $key => $val) |
| 503 | { |
| 504 | $valstr[] = $key.' = '.$val; |
| 505 | } |
| 506 | |
| 507 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr) |
| 508 | .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') |
| 509 | .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') |
| 510 | .( ! $limit ? '' : ' LIMIT '.$limit); |
| 511 | } |
| 512 | |
| 513 | // -------------------------------------------------------------------- |
| 514 | |
| 515 | /** |
| 516 | * Truncate statement |
| 517 | * |
| 518 | * Generates a platform-specific truncate string from the supplied data |
| 519 | * If the database does not support the truncate() command, then |
| 520 | * this method maps to "DELETE FROM table" |
| 521 | * |
| 522 | * @param string the table name |
| 523 | * @return string |
| 524 | */ |
| 525 | protected function _truncate($table) |
| 526 | { |
| 527 | return $this->_delete($table); |
| 528 | } |
| 529 | |
| 530 | // -------------------------------------------------------------------- |
| 531 | |
| 532 | /** |
| 533 | * Delete statement |
| 534 | * |
| 535 | * Generates a platform-specific delete string from the supplied data |
| 536 | * |
| 537 | * @param string the table name |
| 538 | * @param array the where clause |
| 539 | * @param string the limit clause |
| 540 | * @return string |
| 541 | */ |
| 542 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 543 | { |
| 544 | $conditions = ''; |
| 545 | if (count($where) > 0 OR count($like) > 0) |
| 546 | { |
| 547 | $conditions .= "\nWHERE ".implode("\n", $this->ar_where); |
| 548 | |
| 549 | if (count($where) > 0 && count($like) > 0) |
| 550 | { |
| 551 | $conditions .= ' AND '; |
| 552 | } |
| 553 | $conditions .= implode("\n", $like); |
| 554 | } |
| 555 | |
| 556 | return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); |
| 557 | } |
| 558 | |
| 559 | // -------------------------------------------------------------------- |
| 560 | |
| 561 | /** |
| 562 | * Limit string |
| 563 | * |
| 564 | * Generates a platform-specific LIMIT clause |
| 565 | * |
| 566 | * @param string the sql query string |
| 567 | * @param int the number of rows to limit the query to |
| 568 | * @param int the offset value |
| 569 | * @return string |
| 570 | */ |
| 571 | protected function _limit($sql, $limit, $offset) |
| 572 | { |
| 573 | return $sql.($offset ? $offset.',' : '').$limit; |
| 574 | } |
| 575 | |
| 576 | // -------------------------------------------------------------------- |
| 577 | |
| 578 | /** |
| 579 | * Close DB Connection |
| 580 | * |
| 581 | * @return void |
| 582 | */ |
| 583 | protected function _close() |
| 584 | { |
| 585 | $this->conn_id->close(); |
| 586 | } |
| 587 | |
| 588 | } |
| 589 | |
| 590 | /* End of file sqlite3_driver.php */ |
| 591 | /* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */ |