Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 1 | <?php |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * SQLite3 Database Adapter Class |
| 31 | * |
| 32 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | ffe7a0a | 2012-04-26 13:48:18 +0100 | [diff] [blame] | 33 | * creates dynamically based on whether the query builder |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 34 | * class is being used or not. |
| 35 | * |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 36 | * @package CodeIgniter |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 37 | * @subpackage Drivers |
| 38 | * @category Database |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 39 | * @author Andrey Andreev |
| 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | * @since Version 3.0 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 42 | */ |
| 43 | class CI_DB_sqlite3_driver extends CI_DB { |
| 44 | |
| 45 | public $dbdriver = 'sqlite3'; |
| 46 | |
| 47 | // The character used for escaping |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 48 | protected $_escape_char = '"'; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 49 | |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 50 | protected $_random_keyword = ' RANDOM()'; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Non-persistent database connection |
| 54 | * |
| 55 | * @return object type SQLite3 |
| 56 | */ |
| 57 | public function db_connect() |
| 58 | { |
| 59 | try |
| 60 | { |
| 61 | return ( ! $this->password) |
| 62 | ? new SQLite3($this->database) |
| 63 | : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password); |
| 64 | } |
| 65 | catch (Exception $e) |
| 66 | { |
| 67 | return FALSE; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------- |
| 72 | |
| 73 | /** |
| 74 | * Persistent database connection |
| 75 | * |
| 76 | * @return object type SQLite3 |
| 77 | */ |
| 78 | public function db_pconnect() |
| 79 | { |
| 80 | log_message('debug', 'SQLite3 doesn\'t support persistent connections'); |
Andrey Andreev | 9d533ae | 2012-06-04 15:56:56 +0300 | [diff] [blame] | 81 | return $this->db_connect(); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
Andrey Andreev | 80e34f9 | 2012-03-03 03:25:23 +0200 | [diff] [blame] | 87 | * Database version number |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 88 | * |
| 89 | * @return string |
| 90 | */ |
Andrey Andreev | 80e34f9 | 2012-03-03 03:25:23 +0200 | [diff] [blame] | 91 | public function version() |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 92 | { |
Andrey Andreev | 80e34f9 | 2012-03-03 03:25:23 +0200 | [diff] [blame] | 93 | if (isset($this->data_cache['version'])) |
| 94 | { |
| 95 | return $this->data_cache['version']; |
| 96 | } |
| 97 | |
Andrey Andreev | fc11dcc | 2012-06-04 16:39:19 +0300 | [diff] [blame] | 98 | $version = SQLite3::version(); |
Andrey Andreev | 80e34f9 | 2012-03-03 03:25:23 +0200 | [diff] [blame] | 99 | return $this->data_cache['version'] = $version['versionString']; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // -------------------------------------------------------------------- |
| 103 | |
| 104 | /** |
| 105 | * Execute the query |
| 106 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 107 | * @todo Implement use of SQLite3::querySingle(), if needed |
| 108 | * @param string $sql |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 109 | * @return mixed SQLite3Result object or bool |
| 110 | */ |
| 111 | protected function _execute($sql) |
| 112 | { |
Andrey Andreev | a92c7cd | 2012-03-02 13:51:22 +0200 | [diff] [blame] | 113 | return $this->is_write_type($sql) |
| 114 | ? $this->conn_id->exec($sql) |
| 115 | : $this->conn_id->query($sql); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // -------------------------------------------------------------------- |
| 119 | |
| 120 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 121 | * Begin Transaction |
| 122 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 123 | * @param bool $test_mode = FALSE |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 124 | * @return bool |
| 125 | */ |
| 126 | public function trans_begin($test_mode = FALSE) |
| 127 | { |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 128 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 72d7a6e | 2012-01-19 16:02:32 +0200 | [diff] [blame] | 129 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 130 | { |
| 131 | return TRUE; |
| 132 | } |
| 133 | |
| 134 | // Reset the transaction failure flag. |
| 135 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 136 | // even if the queries produce a successful result. |
| 137 | $this->_trans_failure = ($test_mode === TRUE); |
| 138 | |
| 139 | return $this->conn_id->exec('BEGIN TRANSACTION'); |
| 140 | } |
| 141 | |
| 142 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
| 145 | * Commit Transaction |
| 146 | * |
| 147 | * @return bool |
| 148 | */ |
| 149 | public function trans_commit() |
| 150 | { |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 151 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 72d7a6e | 2012-01-19 16:02:32 +0200 | [diff] [blame] | 152 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 153 | { |
| 154 | return TRUE; |
| 155 | } |
| 156 | |
| 157 | return $this->conn_id->exec('END TRANSACTION'); |
| 158 | } |
| 159 | |
| 160 | // -------------------------------------------------------------------- |
| 161 | |
| 162 | /** |
| 163 | * Rollback Transaction |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | public function trans_rollback() |
| 168 | { |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 169 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 72d7a6e | 2012-01-19 16:02:32 +0200 | [diff] [blame] | 170 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 171 | { |
| 172 | return TRUE; |
| 173 | } |
| 174 | |
| 175 | return $this->conn_id->exec('ROLLBACK'); |
| 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
| 181 | * Escape String |
| 182 | * |
| 183 | * @param string |
| 184 | * @param bool whether or not the string will be used in a LIKE condition |
| 185 | * @return string |
| 186 | */ |
| 187 | public function escape_str($str, $like = FALSE) |
| 188 | { |
| 189 | if (is_array($str)) |
| 190 | { |
| 191 | foreach ($str as $key => $val) |
| 192 | { |
| 193 | $str[$key] = $this->escape_str($val, $like); |
| 194 | } |
| 195 | |
| 196 | return $str; |
| 197 | } |
| 198 | |
| 199 | $str = $this->conn_id->escapeString(remove_invisible_characters($str)); |
| 200 | |
| 201 | // escape LIKE condition wildcards |
| 202 | if ($like === TRUE) |
| 203 | { |
Andrey Andreev | dc3de15 | 2012-03-12 15:41:49 +0200 | [diff] [blame] | 204 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 205 | 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] | 206 | $str); |
| 207 | } |
| 208 | |
| 209 | return $str; |
| 210 | } |
| 211 | |
| 212 | // -------------------------------------------------------------------- |
| 213 | |
| 214 | /** |
| 215 | * Affected Rows |
| 216 | * |
| 217 | * @return int |
| 218 | */ |
| 219 | public function affected_rows() |
| 220 | { |
| 221 | return $this->conn_id->changes(); |
| 222 | } |
| 223 | |
| 224 | // -------------------------------------------------------------------- |
| 225 | |
| 226 | /** |
| 227 | * Insert ID |
| 228 | * |
| 229 | * @return int |
| 230 | */ |
| 231 | public function insert_id() |
| 232 | { |
| 233 | return $this->conn_id->lastInsertRowID(); |
| 234 | } |
| 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 239 | * Show table query |
| 240 | * |
| 241 | * Generates a platform-specific query string so that the table names can be fetched |
| 242 | * |
| 243 | * @param bool |
| 244 | * @return string |
| 245 | */ |
| 246 | protected function _list_tables($prefix_limit = FALSE) |
| 247 | { |
| 248 | return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\'' |
| 249 | .(($prefix_limit !== FALSE && $this->dbprefix != '') |
| 250 | ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr) |
| 251 | : ''); |
| 252 | } |
| 253 | |
| 254 | // -------------------------------------------------------------------- |
| 255 | |
| 256 | /** |
| 257 | * Show column query |
| 258 | * |
| 259 | * Generates a platform-specific query string so that the column names can be fetched |
| 260 | * |
| 261 | * @param string the table name |
| 262 | * @return string |
| 263 | */ |
| 264 | protected function _list_columns($table = '') |
| 265 | { |
| 266 | // Not supported |
| 267 | return FALSE; |
| 268 | } |
| 269 | |
| 270 | // -------------------------------------------------------------------- |
| 271 | |
| 272 | /** |
| 273 | * Field data query |
| 274 | * |
| 275 | * Generates a platform-specific query so that the column data can be retrieved |
| 276 | * |
| 277 | * @param string the table name |
| 278 | * @return string |
| 279 | */ |
| 280 | protected function _field_data($table) |
| 281 | { |
| 282 | return 'SELECT * FROM '.$table.' LIMIT 0,1'; |
| 283 | } |
| 284 | |
| 285 | // -------------------------------------------------------------------- |
| 286 | |
| 287 | /** |
Andrey Andreev | ebbfefa | 2012-10-05 17:46:47 +0300 | [diff] [blame] | 288 | * Error |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 289 | * |
Andrey Andreev | ebbfefa | 2012-10-05 17:46:47 +0300 | [diff] [blame] | 290 | * Returns an array containing code and message of the last |
| 291 | * database error that has occured. |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 292 | * |
Andrey Andreev | ebbfefa | 2012-10-05 17:46:47 +0300 | [diff] [blame] | 293 | * @return array |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 294 | */ |
Andrey Andreev | ebbfefa | 2012-10-05 17:46:47 +0300 | [diff] [blame] | 295 | public function error() |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 296 | { |
Andrey Andreev | ebbfefa | 2012-10-05 17:46:47 +0300 | [diff] [blame] | 297 | return array('code' => $this->conn_id->lastErrorCode(), 'message' => $this->conn_id->lastErrorMsg()); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // -------------------------------------------------------------------- |
| 301 | |
| 302 | /** |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 303 | * Replace statement |
| 304 | * |
| 305 | * Generates a platform-specific replace string from the supplied data |
| 306 | * |
| 307 | * @param string the table name |
| 308 | * @param array the insert keys |
| 309 | * @param array the insert values |
| 310 | * @return string |
| 311 | */ |
| 312 | protected function _replace($table, $keys, $values) |
| 313 | { |
| 314 | return 'INSERT OR '.parent::_replace($table, $keys, $values); |
| 315 | } |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 320 | * Truncate statement |
| 321 | * |
| 322 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 323 | * |
| 324 | * If the database does not support the truncate() command, then, |
| 325 | * then this method maps to 'DELETE FROM table' |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 326 | * |
| 327 | * @param string the table name |
| 328 | * @return string |
| 329 | */ |
| 330 | protected function _truncate($table) |
| 331 | { |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 332 | return 'DELETE FROM '.$table; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // -------------------------------------------------------------------- |
| 336 | |
| 337 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 338 | * Close DB Connection |
| 339 | * |
| 340 | * @return void |
| 341 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 342 | protected function _close() |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 343 | { |
| 344 | $this->conn_id->close(); |
| 345 | } |
| 346 | |
| 347 | } |
| 348 | |
| 349 | /* End of file sqlite3_driver.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 350 | /* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */ |