Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Database Driver Class |
| 31 | * |
| 32 | * This is the platform-independent base DB implementation class. |
| 33 | * This class will not be called directly. Rather, the adapter |
| 34 | * class for the specific database will extend and instantiate it. |
| 35 | * |
| 36 | * @package CodeIgniter |
| 37 | * @subpackage Drivers |
| 38 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 39 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | */ |
Timothy Warren | 7eeda53 | 2012-03-19 16:01:55 -0400 | [diff] [blame] | 42 | abstract class CI_DB_driver { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 43 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Data Source Name / Connect string |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
Andrey Andreev | 738f534 | 2012-03-06 20:43:40 +0200 | [diff] [blame] | 49 | public $dsn; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Username |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 56 | public $username; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Password |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 63 | public $password; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Hostname |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 70 | public $hostname; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Database name |
| 74 | * |
| 75 | * @var string |
| 76 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 77 | public $database; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Database driver |
| 81 | * |
| 82 | * @var string |
| 83 | */ |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 84 | public $dbdriver = 'mysqli'; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Sub-driver |
| 88 | * |
| 89 | * @used-by CI_DB_pdo_driver |
| 90 | * @var string |
| 91 | */ |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 92 | public $subdriver; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Table prefix |
| 96 | * |
| 97 | * @var string |
| 98 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 99 | public $dbprefix = ''; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Character set |
| 103 | * |
| 104 | * @var string |
| 105 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 106 | public $char_set = 'utf8'; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Collation |
| 110 | * |
| 111 | * @var string |
| 112 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 113 | public $dbcollat = 'utf8_general_ci'; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Auto-init flag |
| 117 | * |
| 118 | * Whether to automatically initialize the DB connection. |
| 119 | * |
| 120 | * @var bool |
| 121 | */ |
| 122 | public $autoinit = TRUE; |
| 123 | |
| 124 | /** |
| 125 | * Encryption flag/data |
| 126 | * |
| 127 | * @var mixed |
| 128 | */ |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 129 | public $encrypt = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Swap Prefix |
| 133 | * |
| 134 | * @var string |
| 135 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 136 | public $swap_pre = ''; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Database port |
| 140 | * |
| 141 | * @var int |
| 142 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 143 | public $port = ''; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Persistent connection flag |
| 147 | * |
| 148 | * @var bool |
| 149 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 150 | public $pconnect = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Connection ID |
| 154 | * |
| 155 | * @var object|resource |
| 156 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 157 | public $conn_id = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Result ID |
| 161 | * |
| 162 | * @var object|resource |
| 163 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 164 | public $result_id = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Debug flag |
| 168 | * |
| 169 | * Whether to display error messages. |
| 170 | * |
| 171 | * @var bool |
| 172 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 173 | public $db_debug = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Benchmark time |
| 177 | * |
| 178 | * @var int |
| 179 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 180 | public $benchmark = 0; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * Executed queries count |
| 184 | * |
| 185 | * @var int |
| 186 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 187 | public $query_count = 0; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Bind marker |
| 191 | * |
| 192 | * Character used to identify values in a prepared statement. |
| 193 | * |
| 194 | * @var string |
| 195 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 196 | public $bind_marker = '?'; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * Save queries flag |
| 200 | * |
| 201 | * Whether to keep an in-memory history of queries for debugging purposes. |
| 202 | * |
| 203 | * @var bool |
| 204 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 205 | public $save_queries = TRUE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Queries list |
| 209 | * |
| 210 | * @see CI_DB_driver::$save_queries |
| 211 | * @var string[] |
| 212 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 213 | public $queries = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Query times |
| 217 | * |
| 218 | * A list of times that queries took to execute. |
| 219 | * |
| 220 | * @var array |
| 221 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 222 | public $query_times = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * Data cache |
| 226 | * |
| 227 | * An internal generic value cache. |
| 228 | * |
| 229 | * @var array |
| 230 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 231 | public $data_cache = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 233 | /** |
| 234 | * Transaction enabled flag |
| 235 | * |
| 236 | * @var bool |
| 237 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 238 | public $trans_enabled = TRUE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * Strict transaction mode flag |
| 242 | * |
| 243 | * @var bool |
| 244 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 245 | public $trans_strict = TRUE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * Transaction depth level |
| 249 | * |
| 250 | * @var int |
| 251 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 252 | protected $_trans_depth = 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 254 | /** |
| 255 | * Transaction status flag |
| 256 | * |
| 257 | * Used with transactions to determine if a rollback should occur. |
| 258 | * |
| 259 | * @var bool |
| 260 | */ |
| 261 | protected $_trans_status = TRUE; |
| 262 | |
| 263 | /** |
| 264 | * Cache On flag |
| 265 | * |
| 266 | * @var bool |
| 267 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 268 | public $cache_on = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * Cache directory path |
| 272 | * |
| 273 | * @var bool |
| 274 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 275 | public $cachedir = ''; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Cache auto-delete flag |
| 279 | * |
| 280 | * @var bool |
| 281 | */ |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 282 | public $cache_autodel = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 283 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 284 | /** |
| 285 | * DB Cache object |
| 286 | * |
| 287 | * @see CI_DB_cache |
| 288 | * @var object |
| 289 | */ |
| 290 | public $CACHE; |
| 291 | |
| 292 | /** |
| 293 | * Protect identifiers flag |
| 294 | * |
| 295 | * @var bool |
| 296 | */ |
Jamie Rumbelow | bcee50f | 2012-03-08 13:00:15 +0000 | [diff] [blame] | 297 | protected $_protect_identifiers = TRUE; |
Andrey Andreev | d1add43 | 2012-03-06 20:26:01 +0200 | [diff] [blame] | 298 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 299 | /** |
| 300 | * List of reserved identifiers |
| 301 | * |
| 302 | * Identifiers that must NOT be escaped. |
| 303 | * |
| 304 | * @var string[] |
| 305 | */ |
| 306 | protected $_reserved_identifiers = array('*'); |
| 307 | |
| 308 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 309 | * Identifier escape character |
| 310 | * |
| 311 | * @var string |
| 312 | */ |
| 313 | protected $_escape_char = '"'; |
| 314 | |
| 315 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 316 | * ESCAPE statement string |
| 317 | * |
| 318 | * @var string |
| 319 | */ |
Andrey Andreev | 2ea33c3 | 2012-10-04 12:37:51 +0300 | [diff] [blame] | 320 | protected $_like_escape_str = " ESCAPE '%s' "; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * ESCAPE character |
| 324 | * |
| 325 | * @var string |
| 326 | */ |
Andrey Andreev | 2ea33c3 | 2012-10-04 12:37:51 +0300 | [diff] [blame] | 327 | protected $_like_escape_chr = '!'; |
| 328 | |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 329 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 330 | * ORDER BY random keyword |
| 331 | * |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 332 | * @var array |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 333 | */ |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 334 | protected $_random_keyword = array('RAND()', 'RAND(%d)'); |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 335 | |
| 336 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 337 | * COUNT string |
| 338 | * |
| 339 | * @used-by CI_DB_driver::count_all() |
| 340 | * @used-by CI_DB_query_builder::count_all_results() |
| 341 | * |
| 342 | * @var string |
Andrey Andreev | 77a5d94 | 2012-07-05 15:42:14 +0300 | [diff] [blame] | 343 | */ |
| 344 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 345 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 346 | // -------------------------------------------------------------------- |
| 347 | |
Andrey Andreev | 77a5d94 | 2012-07-05 15:42:14 +0300 | [diff] [blame] | 348 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 349 | * Class constructor |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 350 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 351 | * @param array $params |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 352 | * @return void |
| 353 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 354 | public function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | { |
| 356 | if (is_array($params)) |
| 357 | { |
| 358 | foreach ($params as $key => $val) |
| 359 | { |
| 360 | $this->$key = $val; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | log_message('debug', 'Database Driver Class Initialized'); |
| 365 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | |
Gints Murans | 89f77ee | 2012-05-23 00:23:50 +0300 | [diff] [blame] | 367 | // -------------------------------------------------------------------- |
Root | 36237d8 | 2012-05-21 18:30:00 -0400 | [diff] [blame] | 368 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | /** |
| 370 | * Initialize Database Settings |
| 371 | * |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 372 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | */ |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 374 | public function initialize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 376 | /* If an established connection is available, then there's |
| 377 | * no need to connect and select the database. |
| 378 | * |
| 379 | * Depending on the database driver, conn_id can be either |
| 380 | * boolean TRUE, a resource or an object. |
| 381 | */ |
| 382 | if ($this->conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | { |
| 384 | return TRUE; |
| 385 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 386 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | // Connect to the database and set the connection ID |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 390 | $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 392 | // No connection resource? Check if there is a failover else throw an error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | if ( ! $this->conn_id) |
| 394 | { |
Felix Balfoort | 5d581b6 | 2011-11-29 15:53:01 +0100 | [diff] [blame] | 395 | // Check if there is a failover set |
| 396 | if ( ! empty($this->failover) && is_array($this->failover)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | { |
Felix Balfoort | 5d581b6 | 2011-11-29 15:53:01 +0100 | [diff] [blame] | 398 | // Go over all the failovers |
| 399 | foreach ($this->failover as $failover) |
| 400 | { |
| 401 | // Replace the current settings with those of the failover |
| 402 | foreach ($failover as $key => $val) |
| 403 | { |
| 404 | $this->$key = $val; |
| 405 | } |
| 406 | |
| 407 | // Try to connect |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 408 | $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect(); |
Felix Balfoort | 5d581b6 | 2011-11-29 15:53:01 +0100 | [diff] [blame] | 409 | |
| 410 | // If a connection is made break the foreach loop |
| 411 | if ($this->conn_id) |
| 412 | { |
| 413 | break; |
| 414 | } |
| 415 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | } |
Felix Balfoort | 5d581b6 | 2011-11-29 15:53:01 +0100 | [diff] [blame] | 417 | |
| 418 | // We still don't have a connection? |
| 419 | if ( ! $this->conn_id) |
| 420 | { |
| 421 | log_message('error', 'Unable to connect to the database'); |
| 422 | |
| 423 | if ($this->db_debug) |
| 424 | { |
| 425 | $this->display_error('db_unable_to_connect'); |
| 426 | } |
| 427 | return FALSE; |
| 428 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 431 | // Now we set the character set and that's all |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 432 | return $this->db_set_charset($this->char_set); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 434 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | // -------------------------------------------------------------------- |
| 436 | |
| 437 | /** |
Andrey Andreev | 2cae193 | 2012-03-23 16:08:41 +0200 | [diff] [blame] | 438 | * Reconnect |
| 439 | * |
| 440 | * Keep / reestablish the db connection if no queries have been |
| 441 | * sent for a length of time exceeding the server's idle timeout. |
| 442 | * |
| 443 | * This is just a dummy method to allow drivers without such |
| 444 | * functionality to not declare it, while others will override it. |
| 445 | * |
| 446 | * @return void |
| 447 | */ |
| 448 | public function reconnect() |
| 449 | { |
| 450 | } |
| 451 | |
| 452 | // -------------------------------------------------------------------- |
| 453 | |
| 454 | /** |
Andrey Andreev | bee6644 | 2012-03-28 15:28:19 +0300 | [diff] [blame] | 455 | * Select database |
| 456 | * |
| 457 | * This is just a dummy method to allow drivers without such |
| 458 | * functionality to not declare it, while others will override it. |
| 459 | * |
| 460 | * @return bool |
| 461 | */ |
| 462 | public function db_select() |
| 463 | { |
| 464 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // -------------------------------------------------------------------- |
| 468 | |
| 469 | /** |
| 470 | * Set client character set |
| 471 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | * @param string |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 473 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | */ |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 475 | public function db_set_charset($charset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | { |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 477 | if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | { |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 479 | log_message('error', 'Unable to set database connection charset: '.$charset); |
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 | if ($this->db_debug) |
| 482 | { |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 483 | $this->display_error('db_unable_to_set_charset', $charset); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | return FALSE; |
| 487 | } |
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 TRUE; |
| 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 | * The name of the platform in use (mysql, mssql, etc...) |
| 496 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | * @return string |
| 498 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 499 | public function platform() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | { |
| 501 | return $this->dbdriver; |
| 502 | } |
| 503 | |
| 504 | // -------------------------------------------------------------------- |
| 505 | |
| 506 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 507 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | * |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 509 | * Returns a string containing the version of the database being used. |
| 510 | * Most drivers will override this method. |
| 511 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | * @return string |
| 513 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 514 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 516 | if (isset($this->data_cache['version'])) |
| 517 | { |
| 518 | return $this->data_cache['version']; |
| 519 | } |
| 520 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | if (FALSE === ($sql = $this->_version())) |
| 522 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 523 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | } |
Derek Allard | 3683f77 | 2009-12-16 17:32:33 +0000 | [diff] [blame] | 525 | |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 526 | $query = $this->query($sql); |
| 527 | $query = $query->row(); |
| 528 | return $this->data_cache['version'] = $query->ver; |
| 529 | } |
Derek Allard | 3683f77 | 2009-12-16 17:32:33 +0000 | [diff] [blame] | 530 | |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 531 | // -------------------------------------------------------------------- |
| 532 | |
| 533 | /** |
| 534 | * Version number query string |
| 535 | * |
| 536 | * @return string |
| 537 | */ |
| 538 | protected function _version() |
| 539 | { |
| 540 | return 'SELECT VERSION() AS ver'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | } |
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 | // -------------------------------------------------------------------- |
| 544 | |
| 545 | /** |
| 546 | * Execute the query |
| 547 | * |
| 548 | * Accepts an SQL string as input and returns a result object upon |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 549 | * successful execution of a "read" type query. Returns boolean TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | * upon successful execution of a "write" type query. Returns boolean |
| 551 | * FALSE upon failure, and if the $db_debug variable is set to TRUE |
| 552 | * will raise an error. |
| 553 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 554 | * @param string $sql |
| 555 | * @param array $binds = FALSE An array of binding data |
| 556 | * @param bool $return_object = NULL |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 557 | * @return mixed |
| 558 | */ |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 559 | public function query($sql, $binds = FALSE, $return_object = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 561 | if ($sql === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | { |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 563 | log_message('error', 'Invalid query: '.$sql); |
| 564 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 565 | return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | } |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 567 | elseif ( ! is_bool($return_object)) |
| 568 | { |
| 569 | $return_object = ! $this->is_write_type($sql); |
| 570 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | |
| 572 | // Verify table prefix and replace if necessary |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 573 | if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre) |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 574 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 575 | $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | } |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 577 | |
Ryan Dial | ef7474c | 2012-03-01 16:11:36 -0500 | [diff] [blame] | 578 | // Compile binds if needed |
| 579 | if ($binds !== FALSE) |
| 580 | { |
| 581 | $sql = $this->compile_binds($sql, $binds); |
| 582 | } |
| 583 | |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 584 | // Is query caching enabled? If the query is a "read type" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 585 | // we will load the caching class and return the previously |
| 586 | // cached query if it exists |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 587 | if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 589 | $this->load_rdriver(); |
| 590 | if (FALSE !== ($cache = $this->CACHE->read($sql))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 592 | return $cache; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 595 | |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 596 | // Save the query for debugging |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 597 | if ($this->save_queries === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | { |
| 599 | $this->queries[] = $sql; |
| 600 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 601 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | // Start the Query Timer |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 603 | $time_start = microtime(TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 604 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | // Run the Query |
| 606 | if (FALSE === ($this->result_id = $this->simple_query($sql))) |
| 607 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 608 | if ($this->save_queries === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 609 | { |
| 610 | $this->query_times[] = 0; |
| 611 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 612 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | // This will trigger a rollback if transactions are being used |
| 614 | $this->_trans_status = FALSE; |
| 615 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 616 | // Grab the error now, as we might run some additional queries before displaying the error |
| 617 | $error = $this->error(); |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 618 | |
| 619 | // Log errors |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 620 | log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql); |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 621 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | if ($this->db_debug) |
| 623 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | // We call this function in order to roll-back queries |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 625 | // if transactions are enabled. If we don't call this here |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 626 | // the error message will trigger an exit, causing the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | // transactions to remain in limbo. |
Dionysis Arvanitis | 635ced1 | 2013-02-19 23:11:34 +0200 | [diff] [blame] | 628 | $this->_trans_depth > 0 && $this->trans_complete(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 629 | |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 630 | // Display errors |
Andrey Andreev | 2798458 | 2012-03-03 04:43:10 +0200 | [diff] [blame] | 631 | return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 633 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | return FALSE; |
| 635 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 636 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | // Stop and aggregate the query time results |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 638 | $time_end = microtime(TRUE); |
| 639 | $this->benchmark += $time_end - $time_start; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 640 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 641 | if ($this->save_queries === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | { |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 643 | $this->query_times[] = $time_end - $time_start; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 645 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 646 | // Increment the query counter |
| 647 | $this->query_count++; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 648 | |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 649 | // Will we have a result object instantiated? If not - we'll simply return TRUE |
| 650 | if ($return_object !== TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | { |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 652 | // If caching is enabled we'll auto-cleanup any existing files related to this particular URI |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 653 | if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | { |
| 655 | $this->CACHE->delete(); |
| 656 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 657 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 658 | return TRUE; |
| 659 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 660 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 661 | // Return TRUE if we don't need to create a result object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | if ($return_object !== TRUE) |
| 663 | { |
| 664 | return TRUE; |
| 665 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 666 | |
| 667 | // Load and instantiate the result driver |
Andrey Andreev | 57bdeb6 | 2012-03-05 15:59:16 +0200 | [diff] [blame] | 668 | $driver = $this->load_rdriver(); |
| 669 | $RES = new $driver($this); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 670 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 671 | // Is query caching enabled? If so, we'll serialize the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | // result object and save it to a cache file. |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 673 | if ($this->cache_on === TRUE && $this->_cache_init()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 674 | { |
| 675 | // We'll create a new instance of the result object |
| 676 | // only without the platform specific driver since |
| 677 | // we can't use it with cached data (the query result |
| 678 | // resource ID won't be any good once we've cached the |
| 679 | // result object, so we'll have to compile the data |
| 680 | // and save it) |
Andrey Andreev | 83b2b1c | 2012-11-13 10:58:38 +0200 | [diff] [blame] | 681 | $CR = new CI_DB_result($this); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | $CR->result_object = $RES->result_object(); |
| 683 | $CR->result_array = $RES->result_array(); |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 684 | $CR->num_rows = $RES->num_rows(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 685 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | // Reset these since cached objects can not utilize resource IDs. |
| 687 | $CR->conn_id = NULL; |
| 688 | $CR->result_id = NULL; |
| 689 | |
| 690 | $this->CACHE->write($sql, $CR); |
| 691 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 692 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 693 | return $RES; |
| 694 | } |
| 695 | |
| 696 | // -------------------------------------------------------------------- |
| 697 | |
| 698 | /** |
| 699 | * Load the result drivers |
| 700 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 701 | * @return string the name of the result class |
| 702 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 703 | public function load_rdriver() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 704 | { |
| 705 | $driver = 'CI_DB_'.$this->dbdriver.'_result'; |
| 706 | |
| 707 | if ( ! class_exists($driver)) |
| 708 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 709 | include_once(BASEPATH.'database/DB_result.php'); |
| 710 | include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 711 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 712 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 713 | return $driver; |
| 714 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 715 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 716 | // -------------------------------------------------------------------- |
| 717 | |
| 718 | /** |
| 719 | * Simple Query |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 720 | * This is a simplified version of the query() function. Internally |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 721 | * we only use it when running transaction commands since they do |
| 722 | * not require all the features of the main query() function. |
| 723 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 724 | * @param string the sql query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 725 | * @return mixed |
| 726 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 727 | public function simple_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 728 | { |
| 729 | if ( ! $this->conn_id) |
| 730 | { |
| 731 | $this->initialize(); |
| 732 | } |
| 733 | |
| 734 | return $this->_execute($sql); |
| 735 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 736 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 737 | // -------------------------------------------------------------------- |
| 738 | |
| 739 | /** |
| 740 | * Disable Transactions |
| 741 | * This permits transactions to be disabled at run-time. |
| 742 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 743 | * @return void |
| 744 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 745 | public function trans_off() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | { |
| 747 | $this->trans_enabled = FALSE; |
| 748 | } |
| 749 | |
| 750 | // -------------------------------------------------------------------- |
| 751 | |
| 752 | /** |
| 753 | * Enable/disable Transaction Strict Mode |
| 754 | * When strict mode is enabled, if you are running multiple groups of |
| 755 | * transactions, if one group fails all groups will be rolled back. |
| 756 | * If strict mode is disabled, each group is treated autonomously, meaning |
| 757 | * a failure of one group will not affect any others |
| 758 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 759 | * @param bool $mode = TRUE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 760 | * @return void |
| 761 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 762 | public function trans_strict($mode = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 763 | { |
| 764 | $this->trans_strict = is_bool($mode) ? $mode : TRUE; |
| 765 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 766 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 767 | // -------------------------------------------------------------------- |
| 768 | |
| 769 | /** |
| 770 | * Start Transaction |
| 771 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 772 | * @param bool $test_mode = FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 773 | * @return void |
| 774 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 775 | public function trans_start($test_mode = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 776 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 777 | if ( ! $this->trans_enabled) |
| 778 | { |
| 779 | return FALSE; |
| 780 | } |
| 781 | |
| 782 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 783 | if ($this->_trans_depth > 0) |
| 784 | { |
| 785 | $this->_trans_depth += 1; |
| 786 | return; |
| 787 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 788 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 789 | $this->trans_begin($test_mode); |
Jacob Terry | 07fcedf | 2011-11-22 11:21:36 -0500 | [diff] [blame] | 790 | $this->_trans_depth += 1; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // -------------------------------------------------------------------- |
| 794 | |
| 795 | /** |
| 796 | * Complete Transaction |
| 797 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 798 | * @return bool |
| 799 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 800 | public function trans_complete() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 801 | { |
| 802 | if ( ! $this->trans_enabled) |
| 803 | { |
| 804 | return FALSE; |
| 805 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 806 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 807 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 808 | if ($this->_trans_depth > 1) |
| 809 | { |
| 810 | $this->_trans_depth -= 1; |
| 811 | return TRUE; |
| 812 | } |
Jacob Terry | 07fcedf | 2011-11-22 11:21:36 -0500 | [diff] [blame] | 813 | else |
| 814 | { |
| 815 | $this->_trans_depth = 0; |
| 816 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 817 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 818 | // The query() function will set this flag to FALSE in the event that a query failed |
| 819 | if ($this->_trans_status === FALSE) |
| 820 | { |
| 821 | $this->trans_rollback(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 822 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 823 | // If we are NOT running in strict mode, we will reset |
| 824 | // the _trans_status flag so that subsequent groups of transactions |
| 825 | // will be permitted. |
| 826 | if ($this->trans_strict === FALSE) |
| 827 | { |
| 828 | $this->_trans_status = TRUE; |
| 829 | } |
| 830 | |
| 831 | log_message('debug', 'DB Transaction Failure'); |
| 832 | return FALSE; |
| 833 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 834 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 835 | $this->trans_commit(); |
| 836 | return TRUE; |
| 837 | } |
| 838 | |
| 839 | // -------------------------------------------------------------------- |
| 840 | |
| 841 | /** |
| 842 | * Lets you retrieve the transaction flag to determine if it has failed |
| 843 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 844 | * @return bool |
| 845 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 846 | public function trans_status() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 847 | { |
| 848 | return $this->_trans_status; |
| 849 | } |
| 850 | |
| 851 | // -------------------------------------------------------------------- |
| 852 | |
| 853 | /** |
| 854 | * Compile Bindings |
| 855 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 856 | * @param string the sql statement |
| 857 | * @param array an array of bind data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 858 | * @return string |
| 859 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 860 | public function compile_binds($sql, $binds) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 861 | { |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 862 | if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 863 | { |
| 864 | return $sql; |
| 865 | } |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 866 | elseif ( ! is_array($binds)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 867 | { |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 868 | $binds = array($binds); |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 869 | $bind_count = 1; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 870 | } |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 871 | else |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 872 | { |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 873 | // Make sure we're using numeric keys |
| 874 | $binds = array_values($binds); |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 875 | $bind_count = count($binds); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 878 | // We'll need the marker length later |
| 879 | $ml = strlen($this->bind_marker); |
| 880 | |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 881 | // Make sure not to replace a chunk inside a string that happens to match the bind marker |
| 882 | if ($c = preg_match_all("/'[^']*'/i", $sql, $matches)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 883 | { |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 884 | $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i', |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 885 | str_replace($matches[0], |
| 886 | str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]), |
| 887 | $sql, $c), |
| 888 | $matches, PREG_OFFSET_CAPTURE); |
| 889 | |
| 890 | // Bind values' count must match the count of markers in the query |
| 891 | if ($bind_count !== $c) |
| 892 | { |
| 893 | return $sql; |
| 894 | } |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 895 | } |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 896 | elseif (($c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i', $sql, $matches, PREG_OFFSET_CAPTURE)) !== $bind_count) |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 897 | { |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 898 | return $sql; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 901 | do |
| 902 | { |
| 903 | $c--; |
| 904 | $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml); |
| 905 | } |
| 906 | while ($c !== 0); |
| 907 | |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 908 | return $sql; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 909 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 910 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 911 | // -------------------------------------------------------------------- |
| 912 | |
| 913 | /** |
| 914 | * Determines if a query is a "write" type. |
| 915 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 916 | * @param string An SQL query string |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame] | 917 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 918 | */ |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame] | 919 | public function is_write_type($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | { |
Andrey Andreev | 9e31f8f | 2012-10-05 17:31:46 +0300 | [diff] [blame] | 921 | return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 922 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 923 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 924 | // -------------------------------------------------------------------- |
| 925 | |
| 926 | /** |
| 927 | * Calculate the aggregate query elapsed time |
| 928 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 929 | * @param int The number of decimal places |
| 930 | * @return int |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 931 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 932 | public function elapsed_time($decimals = 6) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 933 | { |
| 934 | return number_format($this->benchmark, $decimals); |
| 935 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 936 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 937 | // -------------------------------------------------------------------- |
| 938 | |
| 939 | /** |
| 940 | * Returns the total number of queries |
| 941 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 942 | * @return int |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 943 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 944 | public function total_queries() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 945 | { |
| 946 | return $this->query_count; |
| 947 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 948 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 949 | // -------------------------------------------------------------------- |
| 950 | |
| 951 | /** |
| 952 | * Returns the last query that was executed |
| 953 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 954 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 955 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 956 | public function last_query() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 957 | { |
| 958 | return end($this->queries); |
| 959 | } |
| 960 | |
| 961 | // -------------------------------------------------------------------- |
| 962 | |
| 963 | /** |
| 964 | * "Smart" Escape String |
| 965 | * |
| 966 | * Escapes data based on type |
| 967 | * Sets boolean and null types |
| 968 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 969 | * @param string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 970 | * @return mixed |
| 971 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 972 | public function escape($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 973 | { |
Andrey Andreev | 3a5efc2 | 2012-11-20 21:18:08 +0200 | [diff] [blame] | 974 | if (is_string($str) OR (is_object($str) && method_exists($str, '__toString'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 975 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 976 | return "'".$this->escape_str($str)."'"; |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 977 | } |
| 978 | elseif (is_bool($str)) |
| 979 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 980 | return ($str === FALSE) ? 0 : 1; |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 981 | } |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 982 | elseif ($str === NULL) |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 983 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 984 | return 'NULL'; |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 985 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 986 | |
| 987 | return $str; |
| 988 | } |
| 989 | |
| 990 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 991 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 992 | /** |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 993 | * Escape String |
| 994 | * |
| 995 | * @param string $str |
| 996 | * @param bool $like Whether or not the string will be used in a LIKE condition |
| 997 | * @return string |
| 998 | */ |
| 999 | public function escape_str($str, $like = FALSE) |
| 1000 | { |
| 1001 | if (is_array($str)) |
| 1002 | { |
| 1003 | foreach ($str as $key => $val) |
| 1004 | { |
| 1005 | $str[$key] = $this->escape_str($val, $like); |
| 1006 | } |
| 1007 | |
| 1008 | return $str; |
| 1009 | } |
| 1010 | |
| 1011 | $str = $this->_escape_str($str); |
| 1012 | |
| 1013 | // escape LIKE condition wildcards |
| 1014 | if ($like === TRUE) |
| 1015 | { |
| 1016 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 1017 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 1018 | $str); |
| 1019 | } |
| 1020 | |
| 1021 | return $str; |
| 1022 | } |
| 1023 | |
| 1024 | // -------------------------------------------------------------------- |
| 1025 | |
| 1026 | /** |
Derek Jones | bdc7fb9 | 2009-02-20 21:55:10 +0000 | [diff] [blame] | 1027 | * Escape LIKE String |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1028 | * |
| 1029 | * Calls the individual driver for platform |
| 1030 | * specific escaping for LIKE conditions |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1031 | * |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 1032 | * @param string|string[] |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1033 | * @return mixed |
| 1034 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1035 | public function escape_like_str($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1036 | { |
| 1037 | return $this->escape_str($str, TRUE); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1038 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1039 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1040 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1041 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1042 | /** |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 1043 | * Platform-dependant string escape |
| 1044 | * |
| 1045 | * @param string |
| 1046 | * @return string |
| 1047 | */ |
| 1048 | protected function _escape_str($str) |
| 1049 | { |
| 1050 | return str_replace("'", "''", remove_invisible_characters($str)); |
| 1051 | } |
| 1052 | |
| 1053 | // -------------------------------------------------------------------- |
| 1054 | |
| 1055 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1056 | * Primary |
| 1057 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1058 | * Retrieves the primary key. It assumes that the row in the first |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1059 | * position is the primary key |
| 1060 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1061 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1062 | * @return string |
| 1063 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1064 | public function primary($table = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1065 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1066 | $fields = $this->list_fields($table); |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1067 | return is_array($fields) ? current($fields) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | // -------------------------------------------------------------------- |
| 1071 | |
| 1072 | /** |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1073 | * "Count All" query |
| 1074 | * |
| 1075 | * Generates a platform-specific query string that counts all records in |
| 1076 | * the specified database |
| 1077 | * |
| 1078 | * @param string |
| 1079 | * @return int |
| 1080 | */ |
| 1081 | public function count_all($table = '') |
| 1082 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1083 | if ($table === '') |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1084 | { |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | $query = $this->query($this->_count_string.$this->escape_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1089 | if ($query->num_rows() === 0) |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1090 | { |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | $query = $query->row(); |
| 1095 | $this->_reset_select(); |
| 1096 | return (int) $query->numrows; |
| 1097 | } |
| 1098 | |
| 1099 | // -------------------------------------------------------------------- |
| 1100 | |
| 1101 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1102 | * Returns an array of table names |
| 1103 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1104 | * @param string $constrain_by_prefix = FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1105 | * @return array |
| 1106 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1107 | public function list_tables($constrain_by_prefix = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1108 | { |
| 1109 | // Is there a cached result? |
| 1110 | if (isset($this->data_cache['table_names'])) |
| 1111 | { |
| 1112 | return $this->data_cache['table_names']; |
| 1113 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1115 | if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) |
| 1116 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1117 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1120 | $this->data_cache['table_names'] = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1121 | $query = $this->query($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1122 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1123 | foreach ($query->result_array() as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1124 | { |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1125 | // Do we know from which column to get the table name? |
| 1126 | if ( ! isset($key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1127 | { |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1128 | if (isset($row['table_name'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1129 | { |
| 1130 | $key = 'table_name'; |
| 1131 | } |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1132 | elseif (isset($row['TABLE_NAME'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1133 | { |
| 1134 | $key = 'TABLE_NAME'; |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | /* We have no other choice but to just get the first element's key. |
| 1139 | * Due to array_shift() accepting it's argument by reference, if |
| 1140 | * E_STRICT is on, this would trigger a warning. So we'll have to |
| 1141 | * assign it first. |
| 1142 | */ |
| 1143 | $key = array_keys($row); |
| 1144 | $key = array_shift($key); |
| 1145 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1148 | $this->data_cache['table_names'][] = $row[$key]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1151 | return $this->data_cache['table_names']; |
| 1152 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1154 | // -------------------------------------------------------------------- |
| 1155 | |
| 1156 | /** |
| 1157 | * Determine if a particular table exists |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1158 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1159 | * @param string $table_name |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1160 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1161 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1162 | public function table_exists($table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1163 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1164 | return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1165 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1166 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1167 | // -------------------------------------------------------------------- |
| 1168 | |
| 1169 | /** |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1170 | * Fetch Field Names |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1171 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1172 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1173 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1174 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1175 | public function list_fields($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1176 | { |
| 1177 | // Is there a cached result? |
| 1178 | if (isset($this->data_cache['field_names'][$table])) |
| 1179 | { |
| 1180 | return $this->data_cache['field_names'][$table]; |
| 1181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1182 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1183 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1184 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1185 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1186 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1187 | |
Greg Aker | 1edde30 | 2010-01-26 00:17:01 +0000 | [diff] [blame] | 1188 | if (FALSE === ($sql = $this->_list_columns($table))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1189 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1190 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1191 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1192 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1193 | $query = $this->query($sql); |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1194 | $this->data_cache['field_names'][$table] = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1195 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1196 | foreach ($query->result_array() as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1197 | { |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1198 | // Do we know from where to get the column's name? |
| 1199 | if ( ! isset($key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1200 | { |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1201 | if (isset($row['column_name'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1202 | { |
| 1203 | $key = 'column_name'; |
| 1204 | } |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1205 | elseif (isset($row['COLUMN_NAME'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1206 | { |
| 1207 | $key = 'COLUMN_NAME'; |
| 1208 | } |
| 1209 | else |
| 1210 | { |
RJ garcia | 395e2df | 2013-03-21 10:48:24 -0500 | [diff] [blame^] | 1211 | // We have no other choice but to just get the first element's key. |
| 1212 | $key = key($row); |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1213 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1214 | } |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1215 | |
| 1216 | $this->data_cache['field_names'][$table][] = $row[$key]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1217 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1218 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1219 | return $this->data_cache['field_names'][$table]; |
| 1220 | } |
| 1221 | |
| 1222 | // -------------------------------------------------------------------- |
| 1223 | |
| 1224 | /** |
| 1225 | * Determine if a particular field exists |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1226 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1227 | * @param string |
| 1228 | * @param string |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1229 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1230 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1231 | public function field_exists($field_name, $table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1232 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1233 | return in_array($field_name, $this->list_fields($table_name)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1234 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1236 | // -------------------------------------------------------------------- |
| 1237 | |
| 1238 | /** |
| 1239 | * Returns an object with field data |
| 1240 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1241 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1242 | * @return object |
| 1243 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1244 | public function field_data($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1245 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1246 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1247 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1248 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1249 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1250 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1251 | $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1252 | return $query->field_data(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1253 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1254 | |
| 1255 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1256 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1257 | /** |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1258 | * Escape the SQL Identifiers |
| 1259 | * |
| 1260 | * This function escapes column and table names |
| 1261 | * |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 1262 | * @param mixed |
| 1263 | * @return mixed |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1264 | */ |
| 1265 | public function escape_identifiers($item) |
| 1266 | { |
Andrey Andreev | a7ddd2d | 2012-11-06 11:53:45 +0200 | [diff] [blame] | 1267 | if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers)) |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1268 | { |
| 1269 | return $item; |
| 1270 | } |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 1271 | elseif (is_array($item)) |
| 1272 | { |
| 1273 | foreach ($item as $key => $value) |
| 1274 | { |
| 1275 | $item[$key] = $this->escape_identifiers($value); |
| 1276 | } |
| 1277 | |
| 1278 | return $item; |
| 1279 | } |
Andrey Andreev | 49aa45b | 2012-07-06 16:22:21 +0300 | [diff] [blame] | 1280 | // Avoid breaking functions and literal values inside queries |
Andrey Andreev | 9859cb0 | 2012-07-13 13:07:58 +0300 | [diff] [blame] | 1281 | elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE) |
Andrey Andreev | 7db0f59 | 2012-06-28 17:54:27 +0300 | [diff] [blame] | 1282 | { |
| 1283 | return $item; |
| 1284 | } |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1285 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 1286 | static $preg_ec = array(); |
| 1287 | |
| 1288 | if (empty($preg_ec)) |
| 1289 | { |
| 1290 | if (is_array($this->_escape_char)) |
| 1291 | { |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1292 | $preg_ec = array( |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 1293 | preg_quote($this->_escape_char[0], '/'), preg_quote($this->_escape_char[1], '/'), |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1294 | $this->_escape_char[0], $this->_escape_char[1] |
| 1295 | ); |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 1296 | } |
| 1297 | else |
| 1298 | { |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 1299 | $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/'); |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1300 | $preg_ec[2] = $preg_ec[3] = $this->_escape_char; |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1304 | foreach ($this->_reserved_identifiers as $id) |
| 1305 | { |
| 1306 | if (strpos($item, '.'.$id) !== FALSE) |
| 1307 | { |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1308 | return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\./i', $preg_ec[2].'$1'.$preg_ec[3].'.', $item); |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1312 | return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?(\.)?/i', $preg_ec[2].'$1'.$preg_ec[3].'$2', $item); |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | // -------------------------------------------------------------------- |
| 1316 | |
| 1317 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1318 | * Generate an insert string |
| 1319 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1320 | * @param string the table upon which the query will be performed |
| 1321 | * @param array an associative array data of key/values |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1322 | * @return string |
| 1323 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1324 | public function insert_string($table, $data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1325 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1326 | $fields = $values = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1327 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1328 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1329 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1330 | $fields[] = $this->escape_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1331 | $values[] = $this->escape($val); |
| 1332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1333 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1334 | return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1337 | // -------------------------------------------------------------------- |
| 1338 | |
| 1339 | /** |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1340 | * Insert statement |
| 1341 | * |
| 1342 | * Generates a platform-specific insert string from the supplied data |
| 1343 | * |
| 1344 | * @param string the table name |
| 1345 | * @param array the insert keys |
| 1346 | * @param array the insert values |
| 1347 | * @return string |
| 1348 | */ |
| 1349 | protected function _insert($table, $keys, $values) |
| 1350 | { |
| 1351 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
| 1352 | } |
| 1353 | |
| 1354 | // -------------------------------------------------------------------- |
| 1355 | |
| 1356 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1357 | * Generate an update string |
| 1358 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1359 | * @param string the table upon which the query will be performed |
| 1360 | * @param array an associative array data of key/values |
| 1361 | * @param mixed the "where" statement |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1362 | * @return string |
| 1363 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1364 | public function update_string($table, $data, $where) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1365 | { |
Andrey Andreev | 87f4dc2 | 2012-11-01 01:11:22 +0200 | [diff] [blame] | 1366 | if (empty($where)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1367 | { |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1368 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1369 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1370 | |
Andrey Andreev | 87f4dc2 | 2012-11-01 01:11:22 +0200 | [diff] [blame] | 1371 | $this->where($where); |
| 1372 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1373 | $fields = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1374 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1375 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1376 | $fields[$this->protect_identifiers($key)] = $this->escape($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Andrey Andreev | d337a26 | 2012-11-20 19:24:10 +0200 | [diff] [blame] | 1379 | return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1380 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1381 | |
| 1382 | // -------------------------------------------------------------------- |
| 1383 | |
| 1384 | /** |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1385 | * Update statement |
| 1386 | * |
| 1387 | * Generates a platform-specific update string from the supplied data |
| 1388 | * |
| 1389 | * @param string the table name |
Andrey Andreev | 822317b | 2012-07-19 16:00:32 +0300 | [diff] [blame] | 1390 | * @param array the update data |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1391 | * @return string |
| 1392 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 1393 | protected function _update($table, $values) |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1394 | { |
| 1395 | foreach ($values as $key => $val) |
| 1396 | { |
| 1397 | $valstr[] = $key.' = '.$val; |
| 1398 | } |
| 1399 | |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1400 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr) |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1401 | .$this->_compile_wh('qb_where') |
| 1402 | .$this->_compile_order_by() |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 1403 | .($this->qb_limit ? ' LIMIT '.$this->qb_limit : ''); |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | // -------------------------------------------------------------------- |
| 1407 | |
| 1408 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1409 | * Tests whether the string has an SQL operator |
| 1410 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1411 | * @param string |
| 1412 | * @return bool |
| 1413 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1414 | protected function _has_operator($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1415 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 1416 | return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | // -------------------------------------------------------------------- |
| 1420 | |
| 1421 | /** |
Andrey Andreev | 929fd2d | 2012-06-17 17:29:57 +0300 | [diff] [blame] | 1422 | * Returns the SQL string operator |
| 1423 | * |
| 1424 | * @param string |
| 1425 | * @return string |
| 1426 | */ |
| 1427 | protected function _get_operator($str) |
| 1428 | { |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1429 | static $_operators; |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1430 | |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1431 | if (empty($_operators)) |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 1432 | { |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1433 | $_les = ($this->_like_escape_str !== '') |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 1434 | ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/') |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1435 | : ''; |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1436 | $_operators = array( |
| 1437 | '\s*(?:<|>|!)?=\s*', // =, <=, >=, != |
| 1438 | '\s*<>?\s*', // <, <> |
| 1439 | '\s*>\s*', // > |
| 1440 | '\s+IS NULL', // IS NULL |
| 1441 | '\s+IS NOT NULL', // IS NOT NULL |
| 1442 | '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value |
| 1443 | '\s+IN\s*\([^\)]+\)', // IN(list) |
| 1444 | '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list) |
| 1445 | '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s'] |
| 1446 | '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s'] |
| 1447 | ); |
| 1448 | |
| 1449 | } |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 1450 | |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1451 | return preg_match('/'.implode('|', $_operators).'/i', $str, $match) |
| 1452 | ? $match[0] : FALSE; |
Andrey Andreev | 929fd2d | 2012-06-17 17:29:57 +0300 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | // -------------------------------------------------------------------- |
| 1456 | |
| 1457 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1458 | * Enables a native PHP function to be run, using a platform agnostic wrapper. |
| 1459 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1460 | * @param string $function Function name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1461 | * @return mixed |
| 1462 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1463 | public function call_function($function) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1464 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1465 | $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1466 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1467 | if (FALSE === strpos($driver, $function)) |
| 1468 | { |
| 1469 | $function = $driver.$function; |
| 1470 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1471 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1472 | if ( ! function_exists($function)) |
| 1473 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1474 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1475 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1476 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1477 | return (func_num_args() > 1) |
| 1478 | ? call_user_func_array($function, array_splice(func_get_args(), 1)) |
| 1479 | : call_user_func($function); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | // -------------------------------------------------------------------- |
| 1483 | |
| 1484 | /** |
| 1485 | * Set Cache Directory Path |
| 1486 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1487 | * @param string the path to the cache directory |
| 1488 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1489 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1490 | public function cache_set_path($path = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1491 | { |
| 1492 | $this->cachedir = $path; |
| 1493 | } |
| 1494 | |
| 1495 | // -------------------------------------------------------------------- |
| 1496 | |
| 1497 | /** |
| 1498 | * Enable Query Caching |
| 1499 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1500 | * @return bool cache_on value |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1501 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1502 | public function cache_on() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1503 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1504 | return $this->cache_on = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | // -------------------------------------------------------------------- |
| 1508 | |
| 1509 | /** |
| 1510 | * Disable Query Caching |
| 1511 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1512 | * @return bool cache_on value |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1513 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1514 | public function cache_off() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1515 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1516 | return $this->cache_on = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1517 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1518 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1519 | // -------------------------------------------------------------------- |
| 1520 | |
| 1521 | /** |
| 1522 | * Delete the cache files associated with a particular URI |
| 1523 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1524 | * @param string $segment_one = '' |
| 1525 | * @param string $segment_two = '' |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1526 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1527 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1528 | public function cache_delete($segment_one = '', $segment_two = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1529 | { |
Andrey Andreev | 043f260 | 2012-03-06 20:16:42 +0200 | [diff] [blame] | 1530 | return ($this->_cache_init()) |
| 1531 | ? $this->CACHE->delete($segment_one, $segment_two) |
| 1532 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | // -------------------------------------------------------------------- |
| 1536 | |
| 1537 | /** |
| 1538 | * Delete All cache files |
| 1539 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1540 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1541 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1542 | public function cache_delete_all() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1543 | { |
Andrey Andreev | 043f260 | 2012-03-06 20:16:42 +0200 | [diff] [blame] | 1544 | return ($this->_cache_init()) |
| 1545 | ? $this->CACHE->delete_all() |
| 1546 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | // -------------------------------------------------------------------- |
| 1550 | |
| 1551 | /** |
| 1552 | * Initialize the Cache Class |
| 1553 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1554 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1555 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1556 | protected function _cache_init() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1557 | { |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 1558 | if (class_exists('CI_DB_Cache', FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1559 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1560 | if (is_object($this->CACHE)) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1561 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1562 | return TRUE; |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1563 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1564 | } |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1565 | elseif ( ! @include_once(BASEPATH.'database/DB_cache.php')) |
| 1566 | { |
| 1567 | return $this->cache_off(); |
| 1568 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1569 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1570 | $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects |
| 1571 | return TRUE; |
| 1572 | } |
| 1573 | |
| 1574 | // -------------------------------------------------------------------- |
| 1575 | |
| 1576 | /** |
| 1577 | * Close DB Connection |
| 1578 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1579 | * @return void |
| 1580 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1581 | public function close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1582 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1583 | if ($this->conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1584 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 1585 | $this->_close(); |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1586 | $this->conn_id = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1587 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1588 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1589 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1590 | // -------------------------------------------------------------------- |
| 1591 | |
| 1592 | /** |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 1593 | * Close DB Connection |
| 1594 | * |
| 1595 | * This method would be overriden by most of the drivers. |
| 1596 | * |
| 1597 | * @return void |
| 1598 | */ |
| 1599 | protected function _close() |
| 1600 | { |
| 1601 | $this->conn_id = FALSE; |
| 1602 | } |
| 1603 | |
| 1604 | // -------------------------------------------------------------------- |
| 1605 | |
| 1606 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1607 | * Display an error message |
| 1608 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1609 | * @param string the error message |
| 1610 | * @param string any "swap" values |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1611 | * @param bool whether to localize the message |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1612 | * @return string sends the application/error_db.php template |
| 1613 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1614 | public function display_error($error = '', $swap = '', $native = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1615 | { |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1616 | $LANG =& load_class('Lang', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1617 | $LANG->load('db'); |
| 1618 | |
| 1619 | $heading = $LANG->line('db_error_heading'); |
| 1620 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1621 | if ($native === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1622 | { |
Andrey Andreev | 85a99cc | 2011-09-24 17:17:37 +0300 | [diff] [blame] | 1623 | $message = (array) $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1624 | } |
| 1625 | else |
| 1626 | { |
Andrey Andreev | 1194ad7 | 2012-10-05 17:05:46 +0300 | [diff] [blame] | 1627 | $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1628 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1629 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1630 | // Find the most likely culprit of the error by going through |
| 1631 | // the backtrace until the source file is no longer in the |
| 1632 | // database folder. |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1633 | $trace = debug_backtrace(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1634 | foreach ($trace as $call) |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1635 | { |
Andrey Andreev | 342bb7e | 2012-11-20 23:17:28 +0200 | [diff] [blame] | 1636 | if (isset($call['file'], $call['class'])) |
Andrey Andreev | 1194ad7 | 2012-10-05 17:05:46 +0300 | [diff] [blame] | 1637 | { |
Andrey Andreev | 342bb7e | 2012-11-20 23:17:28 +0200 | [diff] [blame] | 1638 | // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes |
| 1639 | if (DIRECTORY_SEPARATOR !== '/') |
| 1640 | { |
| 1641 | $call['file'] = str_replace('\\', '/', $call['file']); |
| 1642 | } |
Andrey Andreev | 1194ad7 | 2012-10-05 17:05:46 +0300 | [diff] [blame] | 1643 | |
Andrey Andreev | 342bb7e | 2012-11-20 23:17:28 +0200 | [diff] [blame] | 1644 | if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE) |
| 1645 | { |
| 1646 | // Found it - use a relative path for safety |
| 1647 | $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']); |
| 1648 | $message[] = 'Line Number: '.$call['line']; |
| 1649 | break; |
| 1650 | } |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1651 | } |
| 1652 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1653 | |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1654 | $error =& load_class('Exceptions', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1655 | echo $error->show_error($heading, $message, 'error_db'); |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 1656 | exit(EXIT_DATABASE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | // -------------------------------------------------------------------- |
| 1660 | |
| 1661 | /** |
| 1662 | * Protect Identifiers |
| 1663 | * |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1664 | * This function is used extensively by the Query Builder class, and by |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1665 | * a couple functions in this class. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1666 | * It takes a column or table name (optionally with an alias) and inserts |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1667 | * the table prefix onto it. Some logic is necessary in order to deal with |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1668 | * column names that include the path. Consider a query like this: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1669 | * |
| 1670 | * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table |
| 1671 | * |
| 1672 | * Or a query with aliasing: |
| 1673 | * |
| 1674 | * SELECT m.member_id, m.member_name FROM members AS m |
| 1675 | * |
| 1676 | * Since the column name can include up to four segments (host, DB, table, column) |
| 1677 | * or also have an alias prefix, we need to do a bit of work to figure this out and |
| 1678 | * insert the table prefix (if it exists) in the proper position, and escape only |
| 1679 | * the correct identifiers. |
| 1680 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1681 | * @param string |
| 1682 | * @param bool |
| 1683 | * @param mixed |
| 1684 | * @param bool |
| 1685 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1686 | */ |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1687 | public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1688 | { |
| 1689 | if ( ! is_bool($protect_identifiers)) |
| 1690 | { |
Jamie Rumbelow | 0cd8c79 | 2012-03-08 12:52:24 +0000 | [diff] [blame] | 1691 | $protect_identifiers = $this->_protect_identifiers; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1692 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1693 | |
| 1694 | if (is_array($item)) |
| 1695 | { |
| 1696 | $escaped_array = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1697 | foreach ($item as $k => $v) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1698 | { |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1699 | $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | return $escaped_array; |
| 1703 | } |
| 1704 | |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1705 | // This is basically a bug fix for queries that use MAX, MIN, etc. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1706 | // If a parenthesis is found we know that we do not need to |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1707 | // escape the data or add a prefix. There's probably a more graceful |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1708 | // way to deal with this, but I'm not thinking of it -- Rick |
| 1709 | if (strpos($item, '(') !== FALSE) |
| 1710 | { |
Andrey Andreev | 4db1632 | 2012-06-10 15:12:02 +0300 | [diff] [blame] | 1711 | return $item; |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1714 | // Convert tabs or multiple spaces into single spaces |
| 1715 | $item = preg_replace('/\s+/', ' ', $item); |
| 1716 | |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1717 | // If the item has an alias declaration we remove it and set it aside. |
Andrey Andreev | 929fd2d | 2012-06-17 17:29:57 +0300 | [diff] [blame] | 1718 | // Note: strripos() is used in order to support spaces in table names |
| 1719 | if ($offset = strripos($item, ' AS ')) |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1720 | { |
Andrey Andreev | 929fd2d | 2012-06-17 17:29:57 +0300 | [diff] [blame] | 1721 | $alias = ($protect_identifiers) |
| 1722 | ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4)) |
| 1723 | : substr($item, $offset); |
| 1724 | $item = substr($item, 0, $offset); |
| 1725 | } |
| 1726 | elseif ($offset = strrpos($item, ' ')) |
| 1727 | { |
| 1728 | $alias = ($protect_identifiers) |
| 1729 | ? ' '.$this->escape_identifiers(substr($item, $offset + 1)) |
| 1730 | : substr($item, $offset); |
| 1731 | $item = substr($item, 0, $offset); |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1732 | } |
| 1733 | else |
| 1734 | { |
| 1735 | $alias = ''; |
| 1736 | } |
| 1737 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1738 | // Break the string apart if it contains periods, then insert the table prefix |
| 1739 | // in the correct location, assuming the period doesn't indicate that we're dealing |
| 1740 | // with an alias. While we're at it, we will escape the components |
| 1741 | if (strpos($item, '.') !== FALSE) |
| 1742 | { |
| 1743 | $parts = explode('.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1744 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1745 | // Does the first segment of the exploded item match |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1746 | // one of the aliases previously identified? If so, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1747 | // we have nothing more to do other than escape the item |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1748 | if (in_array($parts[0], $this->qb_aliased_tables)) |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1749 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1750 | if ($protect_identifiers === TRUE) |
| 1751 | { |
| 1752 | foreach ($parts as $key => $val) |
| 1753 | { |
| 1754 | if ( ! in_array($val, $this->_reserved_identifiers)) |
| 1755 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1756 | $parts[$key] = $this->escape_identifiers($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1757 | } |
| 1758 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1759 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1760 | $item = implode('.', $parts); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1761 | } |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1762 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1763 | return $item.$alias; |
| 1764 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1765 | |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1766 | // Is there a table prefix defined in the config file? If not, no need to do anything |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1767 | if ($this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1768 | { |
| 1769 | // We now add the table prefix based on some logic. |
| 1770 | // Do we have 4 segments (hostname.database.table.column)? |
| 1771 | // If so, we add the table prefix to the column name in the 3rd segment. |
| 1772 | if (isset($parts[3])) |
| 1773 | { |
| 1774 | $i = 2; |
| 1775 | } |
| 1776 | // Do we have 3 segments (database.table.column)? |
| 1777 | // If so, we add the table prefix to the column name in 2nd position |
| 1778 | elseif (isset($parts[2])) |
| 1779 | { |
| 1780 | $i = 1; |
| 1781 | } |
| 1782 | // Do we have 2 segments (table.column)? |
| 1783 | // If so, we add the table prefix to the column name in 1st segment |
| 1784 | else |
| 1785 | { |
| 1786 | $i = 0; |
| 1787 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1788 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1789 | // This flag is set when the supplied $item does not contain a field name. |
| 1790 | // This can happen when this function is being called from a JOIN. |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1791 | if ($field_exists === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1792 | { |
| 1793 | $i++; |
| 1794 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1795 | |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1796 | // Verify table prefix and replace if necessary |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1797 | if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0) |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1798 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1799 | $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]); |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1800 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1801 | // We only add the table prefix if it does not already exist |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1802 | elseif (strpos($parts[$i], $this->dbprefix) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1803 | { |
| 1804 | $parts[$i] = $this->dbprefix.$parts[$i]; |
| 1805 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1806 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1807 | // Put the parts back together |
| 1808 | $item = implode('.', $parts); |
| 1809 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1810 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1811 | if ($protect_identifiers === TRUE) |
| 1812 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1813 | $item = $this->escape_identifiers($item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1814 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1815 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1816 | return $item.$alias; |
| 1817 | } |
| 1818 | |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1819 | // Is there a table prefix? If not, no need to insert it |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1820 | if ($this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1821 | { |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1822 | // Verify table prefix and replace if necessary |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1823 | if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0) |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1824 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1825 | $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item); |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1826 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1827 | // Do we prefix an item with no segments? |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1828 | elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1829 | { |
| 1830 | $item = $this->dbprefix.$item; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1831 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1834 | if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1835 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1836 | $item = $this->escape_identifiers($item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1837 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1838 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1839 | return $item.$alias; |
| 1840 | } |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1841 | |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1842 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1843 | |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1844 | /** |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 1845 | * Dummy method that allows Query Builder class to be disabled |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1846 | * and keep count_all() working. |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1847 | * |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1848 | * @return void |
| 1849 | */ |
| 1850 | protected function _reset_select() |
| 1851 | { |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1852 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1853 | |
| 1854 | } |
| 1855 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1856 | /* End of file DB_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 1857 | /* Location: ./system/database/DB_driver.php */ |