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