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