Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * SQLite Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
| 32 | * creates dynamically based on whether the active record |
| 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_sqlite_driver extends CI_DB { |
| 42 | |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 43 | public $dbdriver = 'sqlite'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | // The character used to escape with - not needed for SQLite |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 46 | protected $_escape_char = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = " ESCAPE '%s' "; |
| 50 | protected $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
Timothy Warren | 691a8e7 | 2012-03-19 19:06:00 -0400 | [diff] [blame] | 55 | * used for the count_all() and count_all_results() functions. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | protected $_random_keyword = ' Random()'; // database specific random keyword |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Non-persistent database connection |
| 62 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 65 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | { |
| 67 | if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error)) |
| 68 | { |
| 69 | log_message('error', $error); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | if ($this->db_debug) |
| 72 | { |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 73 | $this->display_error($error, '', TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | return FALSE; |
| 77 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | return $conn_id; |
| 80 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | // -------------------------------------------------------------------- |
| 83 | |
| 84 | /** |
| 85 | * Persistent database connection |
| 86 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 88 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 89 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | { |
| 91 | if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error)) |
| 92 | { |
| 93 | log_message('error', $error); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | if ($this->db_debug) |
| 96 | { |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 97 | $this->display_error($error, '', TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | return FALSE; |
| 101 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | return $conn_id; |
| 104 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | // -------------------------------------------------------------------- |
| 107 | |
| 108 | /** |
| 109 | * Select the database |
| 110 | * |
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 | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 113 | public function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | { |
| 115 | return TRUE; |
| 116 | } |
| 117 | |
| 118 | // -------------------------------------------------------------------- |
| 119 | |
| 120 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 121 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | * @return string |
| 124 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 125 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 127 | return isset($this->data_cache['version']) |
| 128 | ? $this->data_cache['version'] |
| 129 | : $this->data_cache['version'] = sqlite_libversion(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | // -------------------------------------------------------------------- |
| 133 | |
| 134 | /** |
| 135 | * Execute the query |
| 136 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | * @param string an SQL query |
| 138 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 140 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | return @sqlite_query($this->conn_id, $sql); |
| 143 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | // -------------------------------------------------------------------- |
| 146 | |
| 147 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | * Begin Transaction |
| 149 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | * @return bool |
| 151 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 152 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | { |
| 154 | if ( ! $this->trans_enabled) |
| 155 | { |
| 156 | return TRUE; |
| 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 160 | if ($this->_trans_depth > 0) |
| 161 | { |
| 162 | return TRUE; |
| 163 | } |
| 164 | |
| 165 | // Reset the transaction failure flag. |
| 166 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 167 | // even if the queries produce a successful result. |
| 168 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 169 | |
| 170 | $this->simple_query('BEGIN TRANSACTION'); |
| 171 | return TRUE; |
| 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
| 177 | * Commit Transaction |
| 178 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | * @return bool |
| 180 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 181 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | { |
| 183 | if ( ! $this->trans_enabled) |
| 184 | { |
| 185 | return TRUE; |
| 186 | } |
| 187 | |
| 188 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 189 | if ($this->_trans_depth > 0) |
| 190 | { |
| 191 | return TRUE; |
| 192 | } |
| 193 | |
| 194 | $this->simple_query('COMMIT'); |
| 195 | return TRUE; |
| 196 | } |
| 197 | |
| 198 | // -------------------------------------------------------------------- |
| 199 | |
| 200 | /** |
| 201 | * Rollback Transaction |
| 202 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | * @return bool |
| 204 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 205 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | { |
| 207 | if ( ! $this->trans_enabled) |
| 208 | { |
| 209 | return TRUE; |
| 210 | } |
| 211 | |
| 212 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 213 | if ($this->_trans_depth > 0) |
| 214 | { |
| 215 | return TRUE; |
| 216 | } |
| 217 | |
| 218 | $this->simple_query('ROLLBACK'); |
| 219 | return TRUE; |
| 220 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | /** |
| 225 | * Escape String |
| 226 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 228 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | * @return string |
| 230 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 231 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 233 | if (is_array($str)) |
| 234 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 235 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 237 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | return $str; |
| 241 | } |
| 242 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 243 | $str = sqlite_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 245 | // escape LIKE condition wildcards |
| 246 | if ($like === TRUE) |
| 247 | { |
Andrey Andreev | 94708bd | 2012-03-26 12:09:28 +0300 | [diff] [blame] | 248 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
Andrey Andreev | 830f5af | 2012-03-26 12:11:38 +0300 | [diff] [blame^] | 249 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
Andrey Andreev | 94708bd | 2012-03-26 12:09:28 +0300 | [diff] [blame] | 250 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 251 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 252 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 253 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * Affected Rows |
| 260 | * |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 261 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 263 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
| 265 | return sqlite_changes($this->conn_id); |
| 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | // -------------------------------------------------------------------- |
| 269 | |
| 270 | /** |
| 271 | * Insert ID |
| 272 | * |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 273 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 275 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
| 277 | return @sqlite_last_insert_rowid($this->conn_id); |
| 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
| 283 | * "Count All" query |
| 284 | * |
| 285 | * Generates a platform-specific query string that counts all records in |
| 286 | * the specified database |
| 287 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | * @param string |
| 289 | * @return string |
| 290 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 291 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | { |
| 293 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 294 | { |
| 295 | return 0; |
| 296 | } |
| 297 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 298 | $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 300 | { |
| 301 | return 0; |
| 302 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | |
| 304 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 305 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 306 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------- |
| 310 | |
| 311 | /** |
| 312 | * List table query |
| 313 | * |
| 314 | * Generates a platform-specific query string so that the table names can be fetched |
| 315 | * |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 316 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | * @return string |
| 318 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 319 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | { |
| 321 | $sql = "SELECT name from sqlite_master WHERE type='table'"; |
| 322 | |
| 323 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 324 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 325 | $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] | 326 | } |
| 327 | return $sql; |
| 328 | } |
| 329 | |
| 330 | // -------------------------------------------------------------------- |
| 331 | |
| 332 | /** |
| 333 | * Show column query |
| 334 | * |
| 335 | * Generates a platform-specific query string so that the column names can be fetched |
| 336 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | * @param string the table name |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 338 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 340 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
| 342 | // Not supported |
| 343 | return FALSE; |
| 344 | } |
| 345 | |
| 346 | // -------------------------------------------------------------------- |
| 347 | |
| 348 | /** |
| 349 | * Field data query |
| 350 | * |
| 351 | * Generates a platform-specific query so that the column data can be retrieved |
| 352 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | * @param string the table name |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 354 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 356 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | { |
| 358 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 359 | } |
| 360 | |
| 361 | // -------------------------------------------------------------------- |
| 362 | |
| 363 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 364 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 366 | * Returns an array containing code and message of the last |
| 367 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 369 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 371 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 373 | $error = array('code' => sqlite_last_error($this->conn_id)); |
| 374 | $error['message'] = sqlite_error_string($error['code']); |
| 375 | return $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // -------------------------------------------------------------------- |
| 379 | |
| 380 | /** |
| 381 | * Escape the SQL Identifiers |
| 382 | * |
Timothy Warren | 691a8e7 | 2012-03-19 19:06:00 -0400 | [diff] [blame] | 383 | * This function escapes column and table names |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | * @param string |
| 386 | * @return string |
| 387 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 388 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | { |
| 390 | if ($this->_escape_char == '') |
| 391 | { |
| 392 | return $item; |
| 393 | } |
| 394 | |
| 395 | foreach ($this->_reserved_identifiers as $id) |
| 396 | { |
| 397 | if (strpos($item, '.'.$id) !== FALSE) |
| 398 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 400 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | // remove duplicates if the user already included the escape |
| 402 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | if (strpos($item, '.') !== FALSE) |
| 407 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | } |
| 410 | else |
| 411 | { |
| 412 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 413 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | // remove duplicates if the user already included the escape |
| 416 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 417 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | // -------------------------------------------------------------------- |
| 420 | |
| 421 | /** |
| 422 | * From Tables |
| 423 | * |
Timothy Warren | 691a8e7 | 2012-03-19 19:06:00 -0400 | [diff] [blame] | 424 | * This function implicitly groups FROM tables so there is no confusion |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | * about operator precedence in harmony with SQL standards |
| 426 | * |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 427 | * @param array |
| 428 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 430 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
| 432 | if ( ! is_array($tables)) |
| 433 | { |
| 434 | $tables = array($tables); |
| 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | return '('.implode(', ', $tables).')'; |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 441 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | /** |
| 443 | * Insert statement |
| 444 | * |
| 445 | * Generates a platform-specific insert string from the supplied data |
| 446 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | * @param string the table name |
| 448 | * @param array the insert keys |
| 449 | * @param array the insert values |
| 450 | * @return string |
| 451 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 452 | protected function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 453 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 455 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | // -------------------------------------------------------------------- |
| 458 | |
| 459 | /** |
| 460 | * Update statement |
| 461 | * |
| 462 | * Generates a platform-specific update string from the supplied data |
| 463 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | * @param string the table name |
| 465 | * @param array the update data |
| 466 | * @param array the where clause |
| 467 | * @param array the orderby clause |
| 468 | * @param array the limit clause |
| 469 | * @return string |
| 470 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 471 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 473 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
| 475 | $valstr[] = $key." = ".$val; |
| 476 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 481 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 483 | |
| 484 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 485 | |
| 486 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 487 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | return $sql; |
| 489 | } |
| 490 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | // -------------------------------------------------------------------- |
| 493 | |
| 494 | /** |
| 495 | * Truncate statement |
| 496 | * |
| 497 | * Generates a platform-specific truncate string from the supplied data |
| 498 | * If the database does not support the truncate() command |
Timothy Warren | 691a8e7 | 2012-03-19 19:06:00 -0400 | [diff] [blame] | 499 | * This function maps to "DELETE FROM table" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | * @param string the table name |
| 502 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 504 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | { |
| 506 | return $this->_delete($table); |
| 507 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | // -------------------------------------------------------------------- |
| 510 | |
| 511 | /** |
| 512 | * Delete statement |
| 513 | * |
| 514 | * Generates a platform-specific delete string from the supplied data |
| 515 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 516 | * @param string the table name |
| 517 | * @param array the where clause |
| 518 | * @param string the limit clause |
| 519 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 520 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 521 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | { |
| 523 | $conditions = ''; |
| 524 | |
| 525 | if (count($where) > 0 OR count($like) > 0) |
| 526 | { |
| 527 | $conditions = "\nWHERE "; |
| 528 | $conditions .= implode("\n", $this->ar_where); |
| 529 | |
| 530 | if (count($where) > 0 && count($like) > 0) |
| 531 | { |
| 532 | $conditions .= " AND "; |
| 533 | } |
| 534 | $conditions .= implode("\n", $like); |
| 535 | } |
| 536 | |
| 537 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 538 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | return "DELETE FROM ".$table.$conditions.$limit; |
| 540 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | // -------------------------------------------------------------------- |
| 543 | |
| 544 | /** |
| 545 | * Limit string |
| 546 | * |
| 547 | * Generates a platform-specific LIMIT clause |
| 548 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | * @param string the sql query string |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 550 | * @param int the number of rows to limit the query to |
| 551 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | * @return string |
| 553 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 554 | protected function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 555 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | if ($offset == 0) |
| 557 | { |
| 558 | $offset = ''; |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | $offset .= ", "; |
| 563 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 564 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | return $sql."LIMIT ".$offset.$limit; |
| 566 | } |
| 567 | |
| 568 | // -------------------------------------------------------------------- |
| 569 | |
| 570 | /** |
| 571 | * Close DB Connection |
| 572 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 573 | * @param resource |
| 574 | * @return void |
| 575 | */ |
Andrey Andreev | 979b528 | 2012-03-20 16:45:39 +0200 | [diff] [blame] | 576 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | { |
| 578 | @sqlite_close($conn_id); |
| 579 | } |
Timothy Warren | 691a8e7 | 2012-03-19 19:06:00 -0400 | [diff] [blame] | 580 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 583 | /* End of file sqlite_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 584 | /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */ |