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