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