Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 1.3.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * SQLite Database Adapter Class |
| 42 | * |
| 43 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 44 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | * class is being used or not. |
| 46 | * |
| 47 | * @package CodeIgniter |
| 48 | * @subpackage Drivers |
| 49 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 50 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | * @link http://codeigniter.com/user_guide/database/ |
| 52 | */ |
| 53 | class CI_DB_sqlite_driver extends CI_DB { |
| 54 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 55 | /** |
| 56 | * Database driver |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 60 | public $dbdriver = 'sqlite'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 62 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 64 | /** |
| 65 | * ORDER BY random keyword |
| 66 | * |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 67 | * @var array |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 68 | */ |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 69 | protected $_random_keyword = array('RANDOM()', 'RANDOM()'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 71 | // -------------------------------------------------------------------- |
| 72 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | /** |
| 74 | * Non-persistent database connection |
| 75 | * |
Andrey Andreev | 2e17102 | 2014-02-25 15:21:41 +0200 | [diff] [blame] | 76 | * @param bool $persistent |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | */ |
Andrey Andreev | 2e17102 | 2014-02-25 15:21:41 +0200 | [diff] [blame] | 79 | public function db_connect($persistent = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | { |
Andrey Andreev | 2e17102 | 2014-02-25 15:21:41 +0200 | [diff] [blame] | 81 | $error = NULL; |
| 82 | $conn_id = ($persistent === TRUE) |
| 83 | ? sqlite_popen($this->database, 0666, $error) |
| 84 | : sqlite_open($this->database, 0666, $error); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Andrey Andreev | 2e17102 | 2014-02-25 15:21:41 +0200 | [diff] [blame] | 86 | isset($error) && log_message('error', $error); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | return $conn_id; |
| 89 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | // -------------------------------------------------------------------- |
| 92 | |
| 93 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 94 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | * @return string |
| 97 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 98 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 100 | return isset($this->data_cache['version']) |
| 101 | ? $this->data_cache['version'] |
| 102 | : $this->data_cache['version'] = sqlite_libversion(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * Execute the query |
| 109 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 110 | * @param string $sql an SQL query |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 113 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | { |
Andrey Andreev | a0c2785 | 2012-03-02 13:49:28 +0200 | [diff] [blame] | 115 | return $this->is_write_type($sql) |
Andrey Andreev | 2bbbd1a | 2014-05-09 10:24:14 +0300 | [diff] [blame] | 116 | ? sqlite_exec($this->conn_id, $sql) |
| 117 | : sqlite_query($this->conn_id, $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | // -------------------------------------------------------------------- |
| 121 | |
| 122 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | * Begin Transaction |
| 124 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 125 | * @param bool $test_mode |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | * @return bool |
| 127 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 128 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 131 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
| 133 | return TRUE; |
| 134 | } |
| 135 | |
| 136 | // Reset the transaction failure flag. |
| 137 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 138 | // even if the queries produce a successful result. |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 139 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | |
| 141 | $this->simple_query('BEGIN TRANSACTION'); |
| 142 | return TRUE; |
| 143 | } |
| 144 | |
| 145 | // -------------------------------------------------------------------- |
| 146 | |
| 147 | /** |
| 148 | * Commit Transaction |
| 149 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | * @return bool |
| 151 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 152 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 155 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | { |
| 157 | return TRUE; |
| 158 | } |
| 159 | |
| 160 | $this->simple_query('COMMIT'); |
| 161 | return TRUE; |
| 162 | } |
| 163 | |
| 164 | // -------------------------------------------------------------------- |
| 165 | |
| 166 | /** |
| 167 | * Rollback Transaction |
| 168 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | * @return bool |
| 170 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 171 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 174 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | { |
| 176 | return TRUE; |
| 177 | } |
| 178 | |
| 179 | $this->simple_query('ROLLBACK'); |
| 180 | return TRUE; |
| 181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | // -------------------------------------------------------------------- |
| 184 | |
| 185 | /** |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 186 | * Platform-dependant string escape |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | * |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 188 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | * @return string |
| 190 | */ |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 191 | protected function _escape_str($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | { |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 193 | return sqlite_escape_string($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | // -------------------------------------------------------------------- |
| 197 | |
| 198 | /** |
| 199 | * Affected Rows |
| 200 | * |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 201 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 203 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | { |
| 205 | return sqlite_changes($this->conn_id); |
| 206 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | // -------------------------------------------------------------------- |
| 209 | |
| 210 | /** |
| 211 | * Insert ID |
| 212 | * |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 213 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 215 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | { |
Andrey Andreev | 2bbbd1a | 2014-05-09 10:24:14 +0300 | [diff] [blame] | 217 | return sqlite_last_insert_rowid($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | * List table query |
| 224 | * |
| 225 | * Generates a platform-specific query string so that the table names can be fetched |
| 226 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 227 | * @param bool $prefix_limit |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | * @return string |
| 229 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 230 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | { |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 232 | $sql = "SELECT name FROM sqlite_master WHERE type='table'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 234 | if ($prefix_limit !== FALSE && $this->dbprefix != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | { |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 236 | return $sql." AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | } |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 238 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | return $sql; |
| 240 | } |
| 241 | |
| 242 | // -------------------------------------------------------------------- |
| 243 | |
| 244 | /** |
| 245 | * Show column query |
| 246 | * |
| 247 | * Generates a platform-specific query string so that the column names can be fetched |
| 248 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 249 | * @param string $table |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 250 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 252 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | { |
| 254 | // Not supported |
| 255 | return FALSE; |
| 256 | } |
| 257 | |
| 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
Andrey Andreev | 822e74e | 2012-11-16 02:33:30 +0200 | [diff] [blame] | 261 | * Returns an object with field data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 263 | * @param string $table |
Andrey Andreev | 822e74e | 2012-11-16 02:33:30 +0200 | [diff] [blame] | 264 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | */ |
Andrey Andreev | 5350f05 | 2015-01-12 12:33:37 +0200 | [diff] [blame^] | 266 | public function field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | { |
Andrey Andreev | 822e74e | 2012-11-16 02:33:30 +0200 | [diff] [blame] | 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; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // -------------------------------------------------------------------- |
| 294 | |
| 295 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 296 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 298 | * Returns an array containing code and message of the last |
| 299 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 301 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 303 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 305 | $error = array('code' => sqlite_last_error($this->conn_id)); |
| 306 | $error['message'] = sqlite_error_string($error['code']); |
| 307 | return $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // -------------------------------------------------------------------- |
| 311 | |
| 312 | /** |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 313 | * Replace statement |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | * |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 315 | * Generates a platform-specific replace string from the supplied data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 317 | * @param string $table Table name |
| 318 | * @param array $keys INSERT keys |
| 319 | * @param array $values INSERT values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | * @return string |
| 321 | */ |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 322 | protected function _replace($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | { |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 324 | return 'INSERT OR '.parent::_replace($table, $keys, $values); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | // -------------------------------------------------------------------- |
| 328 | |
| 329 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | * Truncate statement |
| 331 | * |
| 332 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 333 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 334 | * If the database does not support the TRUNCATE statement, |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 335 | * then this function maps to 'DELETE FROM table' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 337 | * @param string $table |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 340 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 342 | return 'DELETE FROM '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | // -------------------------------------------------------------------- |
| 346 | |
| 347 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | * Close DB Connection |
| 349 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | * @return void |
| 351 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 352 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
Andrey Andreev | 2bbbd1a | 2014-05-09 10:24:14 +0300 | [diff] [blame] | 354 | sqlite_close($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | /* End of file sqlite_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 360 | /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */ |