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 | * |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 |
Andrey Andreev | f20fb98 | 2012-01-24 15:26:01 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 | * |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 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 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * SQLite3 Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | ffe7a0a | 2012-04-26 13:48:18 +0100 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 33 | * class is being used or not. |
| 34 | * |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 35 | * @package CodeIgniter |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 36 | * @subpackage Drivers |
| 37 | * @category Database |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 38 | * @author Andrey Andreev |
| 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | * @since Version 3.0 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 41 | */ |
| 42 | class CI_DB_sqlite3_driver extends CI_DB { |
| 43 | |
| 44 | public $dbdriver = 'sqlite3'; |
| 45 | |
| 46 | // The character used for escaping |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 47 | protected $_escape_char = '"'; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 48 | |
| 49 | // clause and character used for LIKE escape sequences |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 50 | protected $_like_escape_str = ' ESCAPE \'%s\' '; |
| 51 | protected $_like_escape_chr = '!'; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * The syntax to count rows is slightly different across different |
| 55 | * database engines, so this string appears in each driver and is |
| 56 | * used for the count_all() and count_all_results() functions. |
| 57 | */ |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 58 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 59 | protected $_random_keyword = ' RANDOM()'; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Non-persistent database connection |
| 63 | * |
| 64 | * @return object type SQLite3 |
| 65 | */ |
| 66 | public function db_connect() |
| 67 | { |
| 68 | try |
| 69 | { |
| 70 | return ( ! $this->password) |
| 71 | ? new SQLite3($this->database) |
| 72 | : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password); |
| 73 | } |
| 74 | catch (Exception $e) |
| 75 | { |
| 76 | return FALSE; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
| 83 | * Persistent database connection |
| 84 | * |
| 85 | * @return object type SQLite3 |
| 86 | */ |
| 87 | public function db_pconnect() |
| 88 | { |
| 89 | log_message('debug', 'SQLite3 doesn\'t support persistent connections'); |
| 90 | return $this->db_pconnect(); |
| 91 | } |
| 92 | |
| 93 | // -------------------------------------------------------------------- |
| 94 | |
| 95 | /** |
Andrey Andreev | 80e34f9 | 2012-03-03 03:25:23 +0200 | [diff] [blame] | 96 | * Database version number |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 97 | * |
| 98 | * @return string |
| 99 | */ |
Andrey Andreev | 80e34f9 | 2012-03-03 03:25:23 +0200 | [diff] [blame] | 100 | public function version() |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 101 | { |
Andrey Andreev | 80e34f9 | 2012-03-03 03:25:23 +0200 | [diff] [blame] | 102 | if (isset($this->data_cache['version'])) |
| 103 | { |
| 104 | return $this->data_cache['version']; |
| 105 | } |
| 106 | |
| 107 | $version = $this->conn_id->version(); |
| 108 | return $this->data_cache['version'] = $version['versionString']; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // -------------------------------------------------------------------- |
| 112 | |
| 113 | /** |
| 114 | * Execute the query |
| 115 | * |
| 116 | * @param string an SQL query |
| 117 | * @return mixed SQLite3Result object or bool |
| 118 | */ |
| 119 | protected function _execute($sql) |
| 120 | { |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 121 | // TODO: Implement use of SQLite3::querySingle(), if needed |
Andrey Andreev | a92c7cd | 2012-03-02 13:51:22 +0200 | [diff] [blame] | 122 | |
| 123 | return $this->is_write_type($sql) |
| 124 | ? $this->conn_id->exec($sql) |
| 125 | : $this->conn_id->query($sql); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // -------------------------------------------------------------------- |
| 129 | |
| 130 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 131 | * Begin Transaction |
| 132 | * |
| 133 | * @return bool |
| 134 | */ |
| 135 | public function trans_begin($test_mode = FALSE) |
| 136 | { |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 137 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 72d7a6e | 2012-01-19 16:02:32 +0200 | [diff] [blame] | 138 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 139 | { |
| 140 | return TRUE; |
| 141 | } |
| 142 | |
| 143 | // Reset the transaction failure flag. |
| 144 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 145 | // even if the queries produce a successful result. |
| 146 | $this->_trans_failure = ($test_mode === TRUE); |
| 147 | |
| 148 | return $this->conn_id->exec('BEGIN TRANSACTION'); |
| 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Commit Transaction |
| 155 | * |
| 156 | * @return bool |
| 157 | */ |
| 158 | public function trans_commit() |
| 159 | { |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 160 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 72d7a6e | 2012-01-19 16:02:32 +0200 | [diff] [blame] | 161 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 162 | { |
| 163 | return TRUE; |
| 164 | } |
| 165 | |
| 166 | return $this->conn_id->exec('END TRANSACTION'); |
| 167 | } |
| 168 | |
| 169 | // -------------------------------------------------------------------- |
| 170 | |
| 171 | /** |
| 172 | * Rollback Transaction |
| 173 | * |
| 174 | * @return bool |
| 175 | */ |
| 176 | public function trans_rollback() |
| 177 | { |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 178 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 72d7a6e | 2012-01-19 16:02:32 +0200 | [diff] [blame] | 179 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 180 | { |
| 181 | return TRUE; |
| 182 | } |
| 183 | |
| 184 | return $this->conn_id->exec('ROLLBACK'); |
| 185 | } |
| 186 | |
| 187 | // -------------------------------------------------------------------- |
| 188 | |
| 189 | /** |
| 190 | * Escape String |
| 191 | * |
| 192 | * @param string |
| 193 | * @param bool whether or not the string will be used in a LIKE condition |
| 194 | * @return string |
| 195 | */ |
| 196 | public function escape_str($str, $like = FALSE) |
| 197 | { |
| 198 | if (is_array($str)) |
| 199 | { |
| 200 | foreach ($str as $key => $val) |
| 201 | { |
| 202 | $str[$key] = $this->escape_str($val, $like); |
| 203 | } |
| 204 | |
| 205 | return $str; |
| 206 | } |
| 207 | |
| 208 | $str = $this->conn_id->escapeString(remove_invisible_characters($str)); |
| 209 | |
| 210 | // escape LIKE condition wildcards |
| 211 | if ($like === TRUE) |
| 212 | { |
Andrey Andreev | dc3de15 | 2012-03-12 15:41:49 +0200 | [diff] [blame] | 213 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 214 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 215 | $str); |
| 216 | } |
| 217 | |
| 218 | return $str; |
| 219 | } |
| 220 | |
| 221 | // -------------------------------------------------------------------- |
| 222 | |
| 223 | /** |
| 224 | * Affected Rows |
| 225 | * |
| 226 | * @return int |
| 227 | */ |
| 228 | public function affected_rows() |
| 229 | { |
| 230 | return $this->conn_id->changes(); |
| 231 | } |
| 232 | |
| 233 | // -------------------------------------------------------------------- |
| 234 | |
| 235 | /** |
| 236 | * Insert ID |
| 237 | * |
| 238 | * @return int |
| 239 | */ |
| 240 | public function insert_id() |
| 241 | { |
| 242 | return $this->conn_id->lastInsertRowID(); |
| 243 | } |
| 244 | |
| 245 | // -------------------------------------------------------------------- |
| 246 | |
| 247 | /** |
| 248 | * "Count All" query |
| 249 | * |
| 250 | * Generates a platform-specific query string that counts all records in |
| 251 | * the specified database |
| 252 | * |
| 253 | * @param string |
| 254 | * @return int |
| 255 | */ |
| 256 | public function count_all($table = '') |
| 257 | { |
| 258 | if ($table == '') |
| 259 | { |
| 260 | return 0; |
| 261 | } |
| 262 | |
Andrey Andreev | 0aa9f2e | 2012-03-09 14:55:20 +0200 | [diff] [blame] | 263 | $result = $this->conn_id->querySingle($this->_count_string.$this->protect_identifiers('numrows') |
| 264 | .' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 265 | |
| 266 | return empty($result) ? 0 : (int) $result; |
| 267 | } |
| 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * Show table query |
| 273 | * |
| 274 | * Generates a platform-specific query string so that the table names can be fetched |
| 275 | * |
| 276 | * @param bool |
| 277 | * @return string |
| 278 | */ |
| 279 | protected function _list_tables($prefix_limit = FALSE) |
| 280 | { |
| 281 | return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\'' |
| 282 | .(($prefix_limit !== FALSE && $this->dbprefix != '') |
| 283 | ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr) |
| 284 | : ''); |
| 285 | } |
| 286 | |
| 287 | // -------------------------------------------------------------------- |
| 288 | |
| 289 | /** |
| 290 | * Show column query |
| 291 | * |
| 292 | * Generates a platform-specific query string so that the column names can be fetched |
| 293 | * |
| 294 | * @param string the table name |
| 295 | * @return string |
| 296 | */ |
| 297 | protected function _list_columns($table = '') |
| 298 | { |
| 299 | // Not supported |
| 300 | return FALSE; |
| 301 | } |
| 302 | |
| 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Field data query |
| 307 | * |
| 308 | * Generates a platform-specific query so that the column data can be retrieved |
| 309 | * |
| 310 | * @param string the table name |
| 311 | * @return string |
| 312 | */ |
| 313 | protected function _field_data($table) |
| 314 | { |
| 315 | return 'SELECT * FROM '.$table.' LIMIT 0,1'; |
| 316 | } |
| 317 | |
| 318 | // -------------------------------------------------------------------- |
| 319 | |
| 320 | /** |
| 321 | * The error message string |
| 322 | * |
| 323 | * @return string |
| 324 | */ |
| 325 | protected function _error_message() |
| 326 | { |
| 327 | return $this->conn_id->lastErrorMsg(); |
| 328 | } |
| 329 | |
| 330 | // -------------------------------------------------------------------- |
| 331 | |
| 332 | /** |
| 333 | * The error message number |
| 334 | * |
| 335 | * @return int |
| 336 | */ |
| 337 | protected function _error_number() |
| 338 | { |
| 339 | return $this->conn_id->lastErrorCode(); |
| 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 345 | * From Tables |
| 346 | * |
| 347 | * This function implicitly groups FROM tables so there is no confusion |
| 348 | * about operator precedence in harmony with SQL standards |
| 349 | * |
| 350 | * @param string |
| 351 | * @return string |
| 352 | */ |
| 353 | protected function _from_tables($tables) |
| 354 | { |
| 355 | if ( ! is_array($tables)) |
| 356 | { |
| 357 | $tables = array($tables); |
| 358 | } |
| 359 | |
| 360 | return '('.implode(', ', $tables).')'; |
| 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 366 | * Replace statement |
| 367 | * |
| 368 | * Generates a platform-specific replace string from the supplied data |
| 369 | * |
| 370 | * @param string the table name |
| 371 | * @param array the insert keys |
| 372 | * @param array the insert values |
| 373 | * @return string |
| 374 | */ |
| 375 | protected function _replace($table, $keys, $values) |
| 376 | { |
| 377 | return 'INSERT OR '.parent::_replace($table, $keys, $values); |
| 378 | } |
| 379 | |
| 380 | // -------------------------------------------------------------------- |
| 381 | |
| 382 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 383 | * Truncate statement |
| 384 | * |
| 385 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 386 | * |
| 387 | * If the database does not support the truncate() command, then, |
| 388 | * then this method maps to 'DELETE FROM table' |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 389 | * |
| 390 | * @param string the table name |
| 391 | * @return string |
| 392 | */ |
| 393 | protected function _truncate($table) |
| 394 | { |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 395 | return 'DELETE FROM '.$table; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // -------------------------------------------------------------------- |
| 399 | |
| 400 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 401 | * Limit string |
| 402 | * |
| 403 | * Generates a platform-specific LIMIT clause |
| 404 | * |
| 405 | * @param string the sql query string |
| 406 | * @param int the number of rows to limit the query to |
| 407 | * @param int the offset value |
| 408 | * @return string |
| 409 | */ |
| 410 | protected function _limit($sql, $limit, $offset) |
| 411 | { |
Andrey Andreev | 20bd7bc | 2012-03-12 09:26:40 +0200 | [diff] [blame] | 412 | return $sql.' LIMIT '.($offset ? $offset.',' : '').$limit; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // -------------------------------------------------------------------- |
| 416 | |
| 417 | /** |
| 418 | * Close DB Connection |
| 419 | * |
| 420 | * @return void |
| 421 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame^] | 422 | protected function _close() |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 423 | { |
| 424 | $this->conn_id->close(); |
| 425 | } |
| 426 | |
| 427 | } |
| 428 | |
| 429 | /* End of file sqlite3_driver.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 430 | /* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */ |