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 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | * Version number query string |
| 145 | * |
| 146 | * @access public |
| 147 | * @return string |
| 148 | */ |
| 149 | function _version() |
| 150 | { |
| 151 | return sqlite_libversion(); |
| 152 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Execute the query |
| 158 | * |
| 159 | * @access private called by the base class |
| 160 | * @param string an SQL query |
| 161 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | function _execute($sql) |
| 164 | { |
| 165 | $sql = $this->_prep_query($sql); |
| 166 | return @sqlite_query($this->conn_id, $sql); |
| 167 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | // -------------------------------------------------------------------- |
| 170 | |
| 171 | /** |
| 172 | * Prep the query |
| 173 | * |
| 174 | * If needed, each database adapter can prep the query string |
| 175 | * |
| 176 | * @access private called by execute() |
| 177 | * @param string an SQL query |
| 178 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | function _prep_query($sql) |
| 181 | { |
| 182 | return $sql; |
| 183 | } |
| 184 | |
| 185 | // -------------------------------------------------------------------- |
| 186 | |
| 187 | /** |
| 188 | * Begin Transaction |
| 189 | * |
| 190 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | * @return bool |
| 192 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | function trans_begin($test_mode = FALSE) |
| 194 | { |
| 195 | if ( ! $this->trans_enabled) |
| 196 | { |
| 197 | return TRUE; |
| 198 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 201 | if ($this->_trans_depth > 0) |
| 202 | { |
| 203 | return TRUE; |
| 204 | } |
| 205 | |
| 206 | // Reset the transaction failure flag. |
| 207 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 208 | // even if the queries produce a successful result. |
| 209 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 210 | |
| 211 | $this->simple_query('BEGIN TRANSACTION'); |
| 212 | return TRUE; |
| 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Commit Transaction |
| 219 | * |
| 220 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | * @return bool |
| 222 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | function trans_commit() |
| 224 | { |
| 225 | if ( ! $this->trans_enabled) |
| 226 | { |
| 227 | return TRUE; |
| 228 | } |
| 229 | |
| 230 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 231 | if ($this->_trans_depth > 0) |
| 232 | { |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
| 236 | $this->simple_query('COMMIT'); |
| 237 | return TRUE; |
| 238 | } |
| 239 | |
| 240 | // -------------------------------------------------------------------- |
| 241 | |
| 242 | /** |
| 243 | * Rollback Transaction |
| 244 | * |
| 245 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 246 | * @return bool |
| 247 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | function trans_rollback() |
| 249 | { |
| 250 | if ( ! $this->trans_enabled) |
| 251 | { |
| 252 | return TRUE; |
| 253 | } |
| 254 | |
| 255 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 256 | if ($this->_trans_depth > 0) |
| 257 | { |
| 258 | return TRUE; |
| 259 | } |
| 260 | |
| 261 | $this->simple_query('ROLLBACK'); |
| 262 | return TRUE; |
| 263 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | // -------------------------------------------------------------------- |
| 266 | |
| 267 | /** |
| 268 | * Escape String |
| 269 | * |
| 270 | * @access public |
| 271 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 272 | * @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] | 273 | * @return string |
| 274 | */ |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 275 | function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 277 | if (is_array($str)) |
| 278 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 279 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 281 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return $str; |
| 285 | } |
| 286 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 287 | $str = sqlite_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 289 | // escape LIKE condition wildcards |
| 290 | if ($like === TRUE) |
| 291 | { |
| 292 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
| 293 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 294 | $str); |
| 295 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 297 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | // -------------------------------------------------------------------- |
| 301 | |
| 302 | /** |
| 303 | * Affected Rows |
| 304 | * |
| 305 | * @access public |
| 306 | * @return integer |
| 307 | */ |
| 308 | function affected_rows() |
| 309 | { |
| 310 | return sqlite_changes($this->conn_id); |
| 311 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | // -------------------------------------------------------------------- |
| 314 | |
| 315 | /** |
| 316 | * Insert ID |
| 317 | * |
| 318 | * @access public |
| 319 | * @return integer |
| 320 | */ |
| 321 | function insert_id() |
| 322 | { |
| 323 | return @sqlite_last_insert_rowid($this->conn_id); |
| 324 | } |
| 325 | |
| 326 | // -------------------------------------------------------------------- |
| 327 | |
| 328 | /** |
| 329 | * "Count All" query |
| 330 | * |
| 331 | * Generates a platform-specific query string that counts all records in |
| 332 | * the specified database |
| 333 | * |
| 334 | * @access public |
| 335 | * @param string |
| 336 | * @return string |
| 337 | */ |
| 338 | function count_all($table = '') |
| 339 | { |
| 340 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 341 | { |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | $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] | 346 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 348 | { |
| 349 | return 0; |
| 350 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | |
| 352 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 353 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 354 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // -------------------------------------------------------------------- |
| 358 | |
| 359 | /** |
| 360 | * List table query |
| 361 | * |
| 362 | * Generates a platform-specific query string so that the table names can be fetched |
| 363 | * |
| 364 | * @access private |
| 365 | * @param boolean |
| 366 | * @return string |
| 367 | */ |
| 368 | function _list_tables($prefix_limit = FALSE) |
| 369 | { |
| 370 | $sql = "SELECT name from sqlite_master WHERE type='table'"; |
| 371 | |
| 372 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 373 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 374 | $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] | 375 | } |
| 376 | return $sql; |
| 377 | } |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
| 382 | * Show column query |
| 383 | * |
| 384 | * Generates a platform-specific query string so that the column names can be fetched |
| 385 | * |
| 386 | * @access public |
| 387 | * @param string the table name |
| 388 | * @return string |
| 389 | */ |
| 390 | function _list_columns($table = '') |
| 391 | { |
| 392 | // Not supported |
| 393 | return FALSE; |
| 394 | } |
| 395 | |
| 396 | // -------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
| 399 | * Field data query |
| 400 | * |
| 401 | * Generates a platform-specific query so that the column data can be retrieved |
| 402 | * |
| 403 | * @access public |
| 404 | * @param string the table name |
| 405 | * @return object |
| 406 | */ |
| 407 | function _field_data($table) |
| 408 | { |
| 409 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * The error message string |
| 416 | * |
| 417 | * @access private |
| 418 | * @return string |
| 419 | */ |
| 420 | function _error_message() |
| 421 | { |
| 422 | return sqlite_error_string(sqlite_last_error($this->conn_id)); |
| 423 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | // -------------------------------------------------------------------- |
| 426 | |
| 427 | /** |
| 428 | * The error message number |
| 429 | * |
| 430 | * @access private |
| 431 | * @return integer |
| 432 | */ |
| 433 | function _error_number() |
| 434 | { |
| 435 | return sqlite_last_error($this->conn_id); |
| 436 | } |
| 437 | |
| 438 | // -------------------------------------------------------------------- |
| 439 | |
| 440 | /** |
| 441 | * Escape the SQL Identifiers |
| 442 | * |
| 443 | * This function escapes column and table names |
| 444 | * |
| 445 | * @access private |
| 446 | * @param string |
| 447 | * @return string |
| 448 | */ |
| 449 | function _escape_identifiers($item) |
| 450 | { |
| 451 | if ($this->_escape_char == '') |
| 452 | { |
| 453 | return $item; |
| 454 | } |
| 455 | |
| 456 | foreach ($this->_reserved_identifiers as $id) |
| 457 | { |
| 458 | if (strpos($item, '.'.$id) !== FALSE) |
| 459 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 461 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | // remove duplicates if the user already included the escape |
| 463 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 464 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | if (strpos($item, '.') !== FALSE) |
| 468 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | $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] | 470 | } |
| 471 | else |
| 472 | { |
| 473 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 474 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 475 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | // remove duplicates if the user already included the escape |
| 477 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 478 | } |
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 | // -------------------------------------------------------------------- |
| 481 | |
| 482 | /** |
| 483 | * From Tables |
| 484 | * |
| 485 | * This function implicitly groups FROM tables so there is no confusion |
| 486 | * about operator precedence in harmony with SQL standards |
| 487 | * |
| 488 | * @access public |
| 489 | * @param type |
| 490 | * @return type |
| 491 | */ |
| 492 | function _from_tables($tables) |
| 493 | { |
| 494 | if ( ! is_array($tables)) |
| 495 | { |
| 496 | $tables = array($tables); |
| 497 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 498 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | return '('.implode(', ', $tables).')'; |
| 500 | } |
| 501 | |
| 502 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | /** |
| 505 | * Insert statement |
| 506 | * |
| 507 | * Generates a platform-specific insert string from the supplied data |
| 508 | * |
| 509 | * @access public |
| 510 | * @param string the table name |
| 511 | * @param array the insert keys |
| 512 | * @param array the insert values |
| 513 | * @return string |
| 514 | */ |
| 515 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 516 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 518 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 519 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | // -------------------------------------------------------------------- |
| 521 | |
| 522 | /** |
| 523 | * Update statement |
| 524 | * |
| 525 | * Generates a platform-specific update string from the supplied data |
| 526 | * |
| 527 | * @access public |
| 528 | * @param string the table name |
| 529 | * @param array the update data |
| 530 | * @param array the where clause |
| 531 | * @param array the orderby clause |
| 532 | * @param array the limit clause |
| 533 | * @return string |
| 534 | */ |
| 535 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 536 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 537 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | { |
| 539 | $valstr[] = $key." = ".$val; |
| 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 | $limit = ( ! $limit) ? '' : ' LIMIT '.$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 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 545 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 546 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 547 | |
| 548 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 549 | |
| 550 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 551 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | return $sql; |
| 553 | } |
| 554 | |
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 | // -------------------------------------------------------------------- |
| 557 | |
| 558 | /** |
| 559 | * Truncate statement |
| 560 | * |
| 561 | * Generates a platform-specific truncate string from the supplied data |
| 562 | * If the database does not support the truncate() command |
| 563 | * This function maps to "DELETE FROM table" |
| 564 | * |
| 565 | * @access public |
| 566 | * @param string the table name |
| 567 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 568 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 569 | function _truncate($table) |
| 570 | { |
| 571 | return $this->_delete($table); |
| 572 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 573 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | // -------------------------------------------------------------------- |
| 575 | |
| 576 | /** |
| 577 | * Delete statement |
| 578 | * |
| 579 | * Generates a platform-specific delete string from the supplied data |
| 580 | * |
| 581 | * @access public |
| 582 | * @param string the table name |
| 583 | * @param array the where clause |
| 584 | * @param string the limit clause |
| 585 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 586 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 588 | { |
| 589 | $conditions = ''; |
| 590 | |
| 591 | if (count($where) > 0 OR count($like) > 0) |
| 592 | { |
| 593 | $conditions = "\nWHERE "; |
| 594 | $conditions .= implode("\n", $this->ar_where); |
| 595 | |
| 596 | if (count($where) > 0 && count($like) > 0) |
| 597 | { |
| 598 | $conditions .= " AND "; |
| 599 | } |
| 600 | $conditions .= implode("\n", $like); |
| 601 | } |
| 602 | |
| 603 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 604 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | return "DELETE FROM ".$table.$conditions.$limit; |
| 606 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 607 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | // -------------------------------------------------------------------- |
| 609 | |
| 610 | /** |
| 611 | * Limit string |
| 612 | * |
| 613 | * Generates a platform-specific LIMIT clause |
| 614 | * |
| 615 | * @access public |
| 616 | * @param string the sql query string |
| 617 | * @param integer the number of rows to limit the query to |
| 618 | * @param integer the offset value |
| 619 | * @return string |
| 620 | */ |
| 621 | function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 622 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 623 | if ($offset == 0) |
| 624 | { |
| 625 | $offset = ''; |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | $offset .= ", "; |
| 630 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 631 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | return $sql."LIMIT ".$offset.$limit; |
| 633 | } |
| 634 | |
| 635 | // -------------------------------------------------------------------- |
| 636 | |
| 637 | /** |
| 638 | * Close DB Connection |
| 639 | * |
| 640 | * @access public |
| 641 | * @param resource |
| 642 | * @return void |
| 643 | */ |
| 644 | function _close($conn_id) |
| 645 | { |
| 646 | @sqlite_close($conn_id); |
| 647 | } |
| 648 | |
| 649 | |
| 650 | } |
| 651 | |
| 652 | |
| 653 | /* End of file sqlite_driver.php */ |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame^] | 654 | /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */ |