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 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 415 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 417 | * Returns an array containing code and message of the last |
| 418 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 420 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 422 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 424 | $error = array('code' => sqlite_last_error($this->conn_id)); |
| 425 | $error['message'] = sqlite_error_string($error['code']); |
| 426 | return $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // -------------------------------------------------------------------- |
| 430 | |
| 431 | /** |
| 432 | * Escape the SQL Identifiers |
| 433 | * |
| 434 | * This function escapes column and table names |
| 435 | * |
| 436 | * @access private |
| 437 | * @param string |
| 438 | * @return string |
| 439 | */ |
| 440 | function _escape_identifiers($item) |
| 441 | { |
| 442 | if ($this->_escape_char == '') |
| 443 | { |
| 444 | return $item; |
| 445 | } |
| 446 | |
| 447 | foreach ($this->_reserved_identifiers as $id) |
| 448 | { |
| 449 | if (strpos($item, '.'.$id) !== FALSE) |
| 450 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 452 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | // remove duplicates if the user already included the escape |
| 454 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 455 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 457 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | if (strpos($item, '.') !== FALSE) |
| 459 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | $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] | 461 | } |
| 462 | else |
| 463 | { |
| 464 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 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 | // remove duplicates if the user already included the escape |
| 468 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 469 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 470 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | // -------------------------------------------------------------------- |
| 472 | |
| 473 | /** |
| 474 | * From Tables |
| 475 | * |
| 476 | * This function implicitly groups FROM tables so there is no confusion |
| 477 | * about operator precedence in harmony with SQL standards |
| 478 | * |
| 479 | * @access public |
| 480 | * @param type |
| 481 | * @return type |
| 482 | */ |
| 483 | function _from_tables($tables) |
| 484 | { |
| 485 | if ( ! is_array($tables)) |
| 486 | { |
| 487 | $tables = array($tables); |
| 488 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | return '('.implode(', ', $tables).')'; |
| 491 | } |
| 492 | |
| 493 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 494 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | /** |
| 496 | * Insert statement |
| 497 | * |
| 498 | * Generates a platform-specific insert string from the supplied data |
| 499 | * |
| 500 | * @access public |
| 501 | * @param string the table name |
| 502 | * @param array the insert keys |
| 503 | * @param array the insert values |
| 504 | * @return string |
| 505 | */ |
| 506 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 507 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 509 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 510 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | // -------------------------------------------------------------------- |
| 512 | |
| 513 | /** |
| 514 | * Update statement |
| 515 | * |
| 516 | * Generates a platform-specific update string from the supplied data |
| 517 | * |
| 518 | * @access public |
| 519 | * @param string the table name |
| 520 | * @param array the update data |
| 521 | * @param array the where clause |
| 522 | * @param array the orderby clause |
| 523 | * @param array the limit clause |
| 524 | * @return string |
| 525 | */ |
| 526 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 527 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 528 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | { |
| 530 | $valstr[] = $key." = ".$val; |
| 531 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 532 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 534 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 536 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 538 | |
| 539 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 540 | |
| 541 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 542 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | return $sql; |
| 544 | } |
| 545 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 546 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | // -------------------------------------------------------------------- |
| 548 | |
| 549 | /** |
| 550 | * Truncate statement |
| 551 | * |
| 552 | * Generates a platform-specific truncate string from the supplied data |
| 553 | * If the database does not support the truncate() command |
| 554 | * This function maps to "DELETE FROM table" |
| 555 | * |
| 556 | * @access public |
| 557 | * @param string the table name |
| 558 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 559 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | function _truncate($table) |
| 561 | { |
| 562 | return $this->_delete($table); |
| 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 | // -------------------------------------------------------------------- |
| 566 | |
| 567 | /** |
| 568 | * Delete statement |
| 569 | * |
| 570 | * Generates a platform-specific delete string from the supplied data |
| 571 | * |
| 572 | * @access public |
| 573 | * @param string the table name |
| 574 | * @param array the where clause |
| 575 | * @param string the limit clause |
| 576 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 577 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 579 | { |
| 580 | $conditions = ''; |
| 581 | |
| 582 | if (count($where) > 0 OR count($like) > 0) |
| 583 | { |
| 584 | $conditions = "\nWHERE "; |
| 585 | $conditions .= implode("\n", $this->ar_where); |
| 586 | |
| 587 | if (count($where) > 0 && count($like) > 0) |
| 588 | { |
| 589 | $conditions .= " AND "; |
| 590 | } |
| 591 | $conditions .= implode("\n", $like); |
| 592 | } |
| 593 | |
| 594 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 595 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | return "DELETE FROM ".$table.$conditions.$limit; |
| 597 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 598 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | // -------------------------------------------------------------------- |
| 600 | |
| 601 | /** |
| 602 | * Limit string |
| 603 | * |
| 604 | * Generates a platform-specific LIMIT clause |
| 605 | * |
| 606 | * @access public |
| 607 | * @param string the sql query string |
| 608 | * @param integer the number of rows to limit the query to |
| 609 | * @param integer the offset value |
| 610 | * @return string |
| 611 | */ |
| 612 | function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 613 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 614 | if ($offset == 0) |
| 615 | { |
| 616 | $offset = ''; |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | $offset .= ", "; |
| 621 | } |
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 | return $sql."LIMIT ".$offset.$limit; |
| 624 | } |
| 625 | |
| 626 | // -------------------------------------------------------------------- |
| 627 | |
| 628 | /** |
| 629 | * Close DB Connection |
| 630 | * |
| 631 | * @access public |
| 632 | * @param resource |
| 633 | * @return void |
| 634 | */ |
| 635 | function _close($conn_id) |
| 636 | { |
| 637 | @sqlite_close($conn_id); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | } |
| 642 | |
| 643 | |
| 644 | /* End of file sqlite_driver.php */ |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 645 | /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */ |