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