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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, 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 | 2e17102 | 2014-02-25 15:21:41 +0200 | [diff] [blame^] | 438 | * Persistent database connection |
| 439 | * |
| 440 | * @return resource |
| 441 | */ |
| 442 | public function db_pconnect() |
| 443 | { |
| 444 | return $this->db_connect(TRUE); |
| 445 | } |
| 446 | |
| 447 | // -------------------------------------------------------------------- |
| 448 | |
| 449 | /** |
Andrey Andreev | 2cae193 | 2012-03-23 16:08:41 +0200 | [diff] [blame] | 450 | * Reconnect |
| 451 | * |
| 452 | * Keep / reestablish the db connection if no queries have been |
| 453 | * sent for a length of time exceeding the server's idle timeout. |
| 454 | * |
| 455 | * This is just a dummy method to allow drivers without such |
| 456 | * functionality to not declare it, while others will override it. |
| 457 | * |
| 458 | * @return void |
| 459 | */ |
| 460 | public function reconnect() |
| 461 | { |
| 462 | } |
| 463 | |
| 464 | // -------------------------------------------------------------------- |
| 465 | |
| 466 | /** |
Andrey Andreev | bee6644 | 2012-03-28 15:28:19 +0300 | [diff] [blame] | 467 | * Select database |
| 468 | * |
| 469 | * This is just a dummy method to allow drivers without such |
| 470 | * functionality to not declare it, while others will override it. |
| 471 | * |
| 472 | * @return bool |
| 473 | */ |
| 474 | public function db_select() |
| 475 | { |
| 476 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // -------------------------------------------------------------------- |
| 480 | |
| 481 | /** |
| 482 | * Set client character set |
| 483 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | * @param string |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 485 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | */ |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 487 | public function db_set_charset($charset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | { |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 489 | if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | { |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 491 | log_message('error', 'Unable to set database connection charset: '.$charset); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 492 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | if ($this->db_debug) |
| 494 | { |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 495 | $this->display_error('db_unable_to_set_charset', $charset); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | return FALSE; |
| 499 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | return TRUE; |
| 502 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | // -------------------------------------------------------------------- |
| 505 | |
| 506 | /** |
| 507 | * The name of the platform in use (mysql, mssql, etc...) |
| 508 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 509 | * @return string |
| 510 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 511 | public function platform() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | { |
| 513 | return $this->dbdriver; |
| 514 | } |
| 515 | |
| 516 | // -------------------------------------------------------------------- |
| 517 | |
| 518 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 519 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | * |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 521 | * Returns a string containing the version of the database being used. |
| 522 | * Most drivers will override this method. |
| 523 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 524 | * @return string |
| 525 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 526 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 528 | if (isset($this->data_cache['version'])) |
| 529 | { |
| 530 | return $this->data_cache['version']; |
| 531 | } |
| 532 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | if (FALSE === ($sql = $this->_version())) |
| 534 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 535 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | } |
Derek Allard | 3683f77 | 2009-12-16 17:32:33 +0000 | [diff] [blame] | 537 | |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 538 | $query = $this->query($sql); |
| 539 | $query = $query->row(); |
| 540 | return $this->data_cache['version'] = $query->ver; |
| 541 | } |
Derek Allard | 3683f77 | 2009-12-16 17:32:33 +0000 | [diff] [blame] | 542 | |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 543 | // -------------------------------------------------------------------- |
| 544 | |
| 545 | /** |
| 546 | * Version number query string |
| 547 | * |
| 548 | * @return string |
| 549 | */ |
| 550 | protected function _version() |
| 551 | { |
| 552 | return 'SELECT VERSION() AS ver'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 554 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | // -------------------------------------------------------------------- |
| 556 | |
| 557 | /** |
| 558 | * Execute the query |
| 559 | * |
| 560 | * Accepts an SQL string as input and returns a result object upon |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 561 | * successful execution of a "read" type query. Returns boolean TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | * upon successful execution of a "write" type query. Returns boolean |
| 563 | * FALSE upon failure, and if the $db_debug variable is set to TRUE |
| 564 | * will raise an error. |
| 565 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 566 | * @param string $sql |
| 567 | * @param array $binds = FALSE An array of binding data |
| 568 | * @param bool $return_object = NULL |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 569 | * @return mixed |
| 570 | */ |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 571 | public function query($sql, $binds = FALSE, $return_object = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 573 | if ($sql === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | { |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 575 | log_message('error', 'Invalid query: '.$sql); |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 576 | return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | } |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 578 | elseif ( ! is_bool($return_object)) |
| 579 | { |
| 580 | $return_object = ! $this->is_write_type($sql); |
| 581 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | |
| 583 | // Verify table prefix and replace if necessary |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 584 | if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre) |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 585 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 586 | $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] | 587 | } |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 588 | |
Ryan Dial | ef7474c | 2012-03-01 16:11:36 -0500 | [diff] [blame] | 589 | // Compile binds if needed |
| 590 | if ($binds !== FALSE) |
| 591 | { |
| 592 | $sql = $this->compile_binds($sql, $binds); |
| 593 | } |
| 594 | |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 595 | // Is query caching enabled? If the query is a "read type" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | // we will load the caching class and return the previously |
| 597 | // cached query if it exists |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 598 | if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 600 | $this->load_rdriver(); |
| 601 | if (FALSE !== ($cache = $this->CACHE->read($sql))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 603 | return $cache; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 606 | |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 607 | // Save the query for debugging |
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->queries[] = $sql; |
| 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 | // Start the Query Timer |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 614 | $time_start = microtime(TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 615 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | // Run the Query |
| 617 | if (FALSE === ($this->result_id = $this->simple_query($sql))) |
| 618 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 619 | if ($this->save_queries === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | { |
| 621 | $this->query_times[] = 0; |
| 622 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 623 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | // This will trigger a rollback if transactions are being used |
| 625 | $this->_trans_status = FALSE; |
| 626 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 627 | // Grab the error now, as we might run some additional queries before displaying the error |
| 628 | $error = $this->error(); |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 629 | |
| 630 | // Log errors |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 631 | log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql); |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 632 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 633 | if ($this->db_debug) |
| 634 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 635 | // We call this function in order to roll-back queries |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 636 | // if transactions are enabled. If we don't call this here |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 637 | // the error message will trigger an exit, causing the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | // transactions to remain in limbo. |
Andrey Andreev | 6c85442 | 2013-10-21 13:58:15 +0300 | [diff] [blame] | 639 | if ($this->_trans_depth !== 0) |
| 640 | { |
| 641 | do |
| 642 | { |
| 643 | $this->trans_complete(); |
| 644 | } |
| 645 | while ($this->_trans_depth !== 0); |
| 646 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 648 | // Display errors |
Andrey Andreev | 2798458 | 2012-03-03 04:43:10 +0200 | [diff] [blame] | 649 | return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 650 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 651 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | return FALSE; |
| 653 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 654 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | // Stop and aggregate the query time results |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 656 | $time_end = microtime(TRUE); |
| 657 | $this->benchmark += $time_end - $time_start; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 658 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 659 | if ($this->save_queries === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 660 | { |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 661 | $this->query_times[] = $time_end - $time_start; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 663 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 664 | // Increment the query counter |
| 665 | $this->query_count++; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 666 | |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 667 | // Will we have a result object instantiated? If not - we'll simply return TRUE |
| 668 | if ($return_object !== TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 669 | { |
Andrey Andreev | b66664b | 2012-06-27 14:22:10 +0300 | [diff] [blame] | 670 | // 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] | 671 | if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | { |
| 673 | $this->CACHE->delete(); |
| 674 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 675 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 676 | return TRUE; |
| 677 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 678 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 679 | // Return TRUE if we don't need to create a result object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | if ($return_object !== TRUE) |
| 681 | { |
| 682 | return TRUE; |
| 683 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 684 | |
| 685 | // Load and instantiate the result driver |
Andrey Andreev | 57bdeb6 | 2012-03-05 15:59:16 +0200 | [diff] [blame] | 686 | $driver = $this->load_rdriver(); |
| 687 | $RES = new $driver($this); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 688 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 689 | // Is query caching enabled? If so, we'll serialize the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 690 | // result object and save it to a cache file. |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 691 | if ($this->cache_on === TRUE && $this->_cache_init()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 692 | { |
| 693 | // We'll create a new instance of the result object |
| 694 | // only without the platform specific driver since |
| 695 | // we can't use it with cached data (the query result |
| 696 | // resource ID won't be any good once we've cached the |
| 697 | // result object, so we'll have to compile the data |
| 698 | // and save it) |
Andrey Andreev | 83b2b1c | 2012-11-13 10:58:38 +0200 | [diff] [blame] | 699 | $CR = new CI_DB_result($this); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 700 | $CR->result_object = $RES->result_object(); |
| 701 | $CR->result_array = $RES->result_array(); |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 702 | $CR->num_rows = $RES->num_rows(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 703 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 704 | // Reset these since cached objects can not utilize resource IDs. |
| 705 | $CR->conn_id = NULL; |
| 706 | $CR->result_id = NULL; |
| 707 | |
| 708 | $this->CACHE->write($sql, $CR); |
| 709 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 710 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 711 | return $RES; |
| 712 | } |
| 713 | |
| 714 | // -------------------------------------------------------------------- |
| 715 | |
| 716 | /** |
| 717 | * Load the result drivers |
| 718 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 719 | * @return string the name of the result class |
| 720 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 721 | public function load_rdriver() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 722 | { |
| 723 | $driver = 'CI_DB_'.$this->dbdriver.'_result'; |
| 724 | |
vlakoff | fadb822 | 2013-05-12 10:57:09 +0200 | [diff] [blame] | 725 | if ( ! class_exists($driver, FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 726 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 727 | include_once(BASEPATH.'database/DB_result.php'); |
| 728 | include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 729 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 730 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | return $driver; |
| 732 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 733 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 734 | // -------------------------------------------------------------------- |
| 735 | |
| 736 | /** |
| 737 | * Simple Query |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 738 | * This is a simplified version of the query() function. Internally |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 739 | * we only use it when running transaction commands since they do |
| 740 | * not require all the features of the main query() function. |
| 741 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 742 | * @param string the sql query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 743 | * @return mixed |
| 744 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 745 | public function simple_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | { |
| 747 | if ( ! $this->conn_id) |
| 748 | { |
| 749 | $this->initialize(); |
| 750 | } |
| 751 | |
| 752 | return $this->_execute($sql); |
| 753 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 754 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 755 | // -------------------------------------------------------------------- |
| 756 | |
| 757 | /** |
| 758 | * Disable Transactions |
| 759 | * This permits transactions to be disabled at run-time. |
| 760 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 761 | * @return void |
| 762 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 763 | public function trans_off() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 764 | { |
| 765 | $this->trans_enabled = FALSE; |
| 766 | } |
| 767 | |
| 768 | // -------------------------------------------------------------------- |
| 769 | |
| 770 | /** |
| 771 | * Enable/disable Transaction Strict Mode |
| 772 | * When strict mode is enabled, if you are running multiple groups of |
| 773 | * transactions, if one group fails all groups will be rolled back. |
| 774 | * If strict mode is disabled, each group is treated autonomously, meaning |
| 775 | * a failure of one group will not affect any others |
| 776 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 777 | * @param bool $mode = TRUE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 778 | * @return void |
| 779 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 780 | public function trans_strict($mode = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 781 | { |
| 782 | $this->trans_strict = is_bool($mode) ? $mode : TRUE; |
| 783 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 784 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 785 | // -------------------------------------------------------------------- |
| 786 | |
| 787 | /** |
| 788 | * Start Transaction |
| 789 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 790 | * @param bool $test_mode = FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 791 | * @return void |
| 792 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 793 | public function trans_start($test_mode = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 794 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 795 | if ( ! $this->trans_enabled) |
| 796 | { |
| 797 | return FALSE; |
| 798 | } |
| 799 | |
| 800 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 801 | if ($this->_trans_depth > 0) |
| 802 | { |
| 803 | $this->_trans_depth += 1; |
| 804 | return; |
| 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 | $this->trans_begin($test_mode); |
Jacob Terry | 07fcedf | 2011-11-22 11:21:36 -0500 | [diff] [blame] | 808 | $this->_trans_depth += 1; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // -------------------------------------------------------------------- |
| 812 | |
| 813 | /** |
| 814 | * Complete Transaction |
| 815 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 816 | * @return bool |
| 817 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 818 | public function trans_complete() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 819 | { |
| 820 | if ( ! $this->trans_enabled) |
| 821 | { |
| 822 | return FALSE; |
| 823 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 824 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 825 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 826 | if ($this->_trans_depth > 1) |
| 827 | { |
| 828 | $this->_trans_depth -= 1; |
| 829 | return TRUE; |
| 830 | } |
Jacob Terry | 07fcedf | 2011-11-22 11:21:36 -0500 | [diff] [blame] | 831 | else |
| 832 | { |
| 833 | $this->_trans_depth = 0; |
| 834 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 835 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 836 | // The query() function will set this flag to FALSE in the event that a query failed |
Katsumi Honda | fa99dc6 | 2013-04-03 22:47:34 +0900 | [diff] [blame] | 837 | if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 838 | { |
| 839 | $this->trans_rollback(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 840 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 841 | // If we are NOT running in strict mode, we will reset |
| 842 | // the _trans_status flag so that subsequent groups of transactions |
| 843 | // will be permitted. |
| 844 | if ($this->trans_strict === FALSE) |
| 845 | { |
| 846 | $this->_trans_status = TRUE; |
| 847 | } |
| 848 | |
| 849 | log_message('debug', 'DB Transaction Failure'); |
| 850 | return FALSE; |
| 851 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 852 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 853 | $this->trans_commit(); |
| 854 | return TRUE; |
| 855 | } |
| 856 | |
| 857 | // -------------------------------------------------------------------- |
| 858 | |
| 859 | /** |
| 860 | * Lets you retrieve the transaction flag to determine if it has failed |
| 861 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 862 | * @return bool |
| 863 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 864 | public function trans_status() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 865 | { |
| 866 | return $this->_trans_status; |
| 867 | } |
| 868 | |
| 869 | // -------------------------------------------------------------------- |
| 870 | |
| 871 | /** |
| 872 | * Compile Bindings |
| 873 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 874 | * @param string the sql statement |
| 875 | * @param array an array of bind data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 876 | * @return string |
| 877 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 878 | public function compile_binds($sql, $binds) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 879 | { |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 880 | 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] | 881 | { |
| 882 | return $sql; |
| 883 | } |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 884 | elseif ( ! is_array($binds)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 885 | { |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 886 | $binds = array($binds); |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 887 | $bind_count = 1; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 888 | } |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 889 | else |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 890 | { |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 891 | // Make sure we're using numeric keys |
| 892 | $binds = array_values($binds); |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 893 | $bind_count = count($binds); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 896 | // We'll need the marker length later |
| 897 | $ml = strlen($this->bind_marker); |
| 898 | |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 899 | // Make sure not to replace a chunk inside a string that happens to match the bind marker |
| 900 | if ($c = preg_match_all("/'[^']*'/i", $sql, $matches)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 901 | { |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 902 | $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i', |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 903 | str_replace($matches[0], |
| 904 | str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]), |
| 905 | $sql, $c), |
| 906 | $matches, PREG_OFFSET_CAPTURE); |
| 907 | |
| 908 | // Bind values' count must match the count of markers in the query |
| 909 | if ($bind_count !== $c) |
| 910 | { |
| 911 | return $sql; |
| 912 | } |
Andrey Andreev | 10cbdf0 | 2012-06-13 13:32:30 +0300 | [diff] [blame] | 913 | } |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 914 | 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] | 915 | { |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 916 | return $sql; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Andrey Andreev | af915ce | 2012-06-13 19:03:06 +0300 | [diff] [blame] | 919 | do |
| 920 | { |
| 921 | $c--; |
| 922 | $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml); |
| 923 | } |
| 924 | while ($c !== 0); |
| 925 | |
Andrey Andreev | 4e9538f | 2012-06-12 14:16:53 +0300 | [diff] [blame] | 926 | return $sql; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 927 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 928 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 929 | // -------------------------------------------------------------------- |
| 930 | |
| 931 | /** |
| 932 | * Determines if a query is a "write" type. |
| 933 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 934 | * @param string An SQL query string |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame] | 935 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 936 | */ |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame] | 937 | public function is_write_type($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 938 | { |
Andrey Andreev | d98c4a3 | 2014-01-07 17:33:32 +0200 | [diff] [blame] | 939 | 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] | 940 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 941 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 942 | // -------------------------------------------------------------------- |
| 943 | |
| 944 | /** |
| 945 | * Calculate the aggregate query elapsed time |
| 946 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 947 | * @param int The number of decimal places |
| 948 | * @return int |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 949 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 950 | public function elapsed_time($decimals = 6) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 951 | { |
| 952 | return number_format($this->benchmark, $decimals); |
| 953 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 954 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 955 | // -------------------------------------------------------------------- |
| 956 | |
| 957 | /** |
| 958 | * Returns the total number of queries |
| 959 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 960 | * @return int |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 961 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 962 | public function total_queries() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 963 | { |
| 964 | return $this->query_count; |
| 965 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 966 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | // -------------------------------------------------------------------- |
| 968 | |
| 969 | /** |
| 970 | * Returns the last query that was executed |
| 971 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 972 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 973 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 974 | public function last_query() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 975 | { |
| 976 | return end($this->queries); |
| 977 | } |
| 978 | |
| 979 | // -------------------------------------------------------------------- |
| 980 | |
| 981 | /** |
| 982 | * "Smart" Escape String |
| 983 | * |
| 984 | * Escapes data based on type |
| 985 | * Sets boolean and null types |
| 986 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 987 | * @param string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 988 | * @return mixed |
| 989 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 990 | public function escape($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 991 | { |
Andrey Andreev | 3a5efc2 | 2012-11-20 21:18:08 +0200 | [diff] [blame] | 992 | if (is_string($str) OR (is_object($str) && method_exists($str, '__toString'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 993 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 994 | return "'".$this->escape_str($str)."'"; |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 995 | } |
| 996 | elseif (is_bool($str)) |
| 997 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 998 | return ($str === FALSE) ? 0 : 1; |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 999 | } |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 1000 | elseif ($str === NULL) |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 1001 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1002 | return 'NULL'; |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 1003 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1004 | |
| 1005 | return $str; |
| 1006 | } |
| 1007 | |
| 1008 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1009 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1010 | /** |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 1011 | * Escape String |
| 1012 | * |
| 1013 | * @param string $str |
| 1014 | * @param bool $like Whether or not the string will be used in a LIKE condition |
| 1015 | * @return string |
| 1016 | */ |
| 1017 | public function escape_str($str, $like = FALSE) |
| 1018 | { |
| 1019 | if (is_array($str)) |
| 1020 | { |
| 1021 | foreach ($str as $key => $val) |
| 1022 | { |
| 1023 | $str[$key] = $this->escape_str($val, $like); |
| 1024 | } |
| 1025 | |
| 1026 | return $str; |
| 1027 | } |
| 1028 | |
| 1029 | $str = $this->_escape_str($str); |
| 1030 | |
| 1031 | // escape LIKE condition wildcards |
| 1032 | if ($like === TRUE) |
| 1033 | { |
| 1034 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 1035 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 1036 | $str); |
| 1037 | } |
| 1038 | |
| 1039 | return $str; |
| 1040 | } |
| 1041 | |
| 1042 | // -------------------------------------------------------------------- |
| 1043 | |
| 1044 | /** |
Derek Jones | bdc7fb9 | 2009-02-20 21:55:10 +0000 | [diff] [blame] | 1045 | * Escape LIKE String |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1046 | * |
| 1047 | * Calls the individual driver for platform |
| 1048 | * specific escaping for LIKE conditions |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1049 | * |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 1050 | * @param string|string[] |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1051 | * @return mixed |
| 1052 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1053 | public function escape_like_str($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1054 | { |
| 1055 | return $this->escape_str($str, TRUE); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1056 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1057 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 1058 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1059 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1060 | /** |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 1061 | * Platform-dependant string escape |
| 1062 | * |
| 1063 | * @param string |
| 1064 | * @return string |
| 1065 | */ |
| 1066 | protected function _escape_str($str) |
| 1067 | { |
| 1068 | return str_replace("'", "''", remove_invisible_characters($str)); |
| 1069 | } |
| 1070 | |
| 1071 | // -------------------------------------------------------------------- |
| 1072 | |
| 1073 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1074 | * Primary |
| 1075 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1076 | * Retrieves the primary key. It assumes that the row in the first |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1077 | * position is the primary key |
| 1078 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1079 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1080 | * @return string |
| 1081 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1082 | public function primary($table = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1083 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1084 | $fields = $this->list_fields($table); |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1085 | return is_array($fields) ? current($fields) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | // -------------------------------------------------------------------- |
| 1089 | |
| 1090 | /** |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1091 | * "Count All" query |
| 1092 | * |
| 1093 | * Generates a platform-specific query string that counts all records in |
| 1094 | * the specified database |
| 1095 | * |
| 1096 | * @param string |
| 1097 | * @return int |
| 1098 | */ |
| 1099 | public function count_all($table = '') |
| 1100 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1101 | if ($table === '') |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1102 | { |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | $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] | 1107 | if ($query->num_rows() === 0) |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1108 | { |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | $query = $query->row(); |
| 1113 | $this->_reset_select(); |
| 1114 | return (int) $query->numrows; |
| 1115 | } |
| 1116 | |
| 1117 | // -------------------------------------------------------------------- |
| 1118 | |
| 1119 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1120 | * Returns an array of table names |
| 1121 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1122 | * @param string $constrain_by_prefix = FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1123 | * @return array |
| 1124 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1125 | public function list_tables($constrain_by_prefix = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1126 | { |
| 1127 | // Is there a cached result? |
| 1128 | if (isset($this->data_cache['table_names'])) |
| 1129 | { |
| 1130 | return $this->data_cache['table_names']; |
| 1131 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1132 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1133 | if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) |
| 1134 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1135 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1138 | $this->data_cache['table_names'] = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1139 | $query = $this->query($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1140 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1141 | foreach ($query->result_array() as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1142 | { |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1143 | // Do we know from which column to get the table name? |
| 1144 | if ( ! isset($key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1145 | { |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1146 | if (isset($row['table_name'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1147 | { |
| 1148 | $key = 'table_name'; |
| 1149 | } |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1150 | elseif (isset($row['TABLE_NAME'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1151 | { |
| 1152 | $key = 'TABLE_NAME'; |
| 1153 | } |
| 1154 | else |
| 1155 | { |
| 1156 | /* We have no other choice but to just get the first element's key. |
vlakoff | 3a3d5f6 | 2013-10-17 22:22:16 +0200 | [diff] [blame] | 1157 | * Due to array_shift() accepting its argument by reference, if |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1158 | * E_STRICT is on, this would trigger a warning. So we'll have to |
| 1159 | * assign it first. |
| 1160 | */ |
| 1161 | $key = array_keys($row); |
| 1162 | $key = array_shift($key); |
| 1163 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1166 | $this->data_cache['table_names'][] = $row[$key]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1169 | return $this->data_cache['table_names']; |
| 1170 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1171 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1172 | // -------------------------------------------------------------------- |
| 1173 | |
| 1174 | /** |
| 1175 | * Determine if a particular table exists |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1176 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1177 | * @param string $table_name |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1178 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1179 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1180 | public function table_exists($table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1181 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1182 | 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] | 1183 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1185 | // -------------------------------------------------------------------- |
| 1186 | |
| 1187 | /** |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1188 | * Fetch Field Names |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1189 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1190 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1191 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1192 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1193 | public function list_fields($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1194 | { |
| 1195 | // Is there a cached result? |
| 1196 | if (isset($this->data_cache['field_names'][$table])) |
| 1197 | { |
| 1198 | return $this->data_cache['field_names'][$table]; |
| 1199 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1200 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1201 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1202 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1203 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1204 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1205 | |
Greg Aker | 1edde30 | 2010-01-26 00:17:01 +0000 | [diff] [blame] | 1206 | if (FALSE === ($sql = $this->_list_columns($table))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1207 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1208 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1209 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1210 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1211 | $query = $this->query($sql); |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1212 | $this->data_cache['field_names'][$table] = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1213 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1214 | foreach ($query->result_array() as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1215 | { |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1216 | // Do we know from where to get the column's name? |
| 1217 | if ( ! isset($key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1218 | { |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1219 | if (isset($row['column_name'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1220 | { |
| 1221 | $key = 'column_name'; |
| 1222 | } |
Andrey Andreev | 6781a5f | 2012-03-28 14:50:51 +0300 | [diff] [blame] | 1223 | elseif (isset($row['COLUMN_NAME'])) |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1224 | { |
| 1225 | $key = 'COLUMN_NAME'; |
| 1226 | } |
| 1227 | else |
| 1228 | { |
RJ garcia | 395e2df | 2013-03-21 10:48:24 -0500 | [diff] [blame] | 1229 | // We have no other choice but to just get the first element's key. |
| 1230 | $key = key($row); |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1231 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1232 | } |
Andrey Andreev | 12567e8 | 2012-01-25 22:50:33 +0200 | [diff] [blame] | 1233 | |
| 1234 | $this->data_cache['field_names'][$table][] = $row[$key]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1235 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1236 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1237 | return $this->data_cache['field_names'][$table]; |
| 1238 | } |
| 1239 | |
| 1240 | // -------------------------------------------------------------------- |
| 1241 | |
| 1242 | /** |
| 1243 | * Determine if a particular field exists |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1244 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1245 | * @param string |
| 1246 | * @param string |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1247 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1248 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1249 | public function field_exists($field_name, $table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1250 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1251 | return in_array($field_name, $this->list_fields($table_name)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1252 | } |
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 | |
| 1256 | /** |
| 1257 | * Returns an object with field data |
| 1258 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1259 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1260 | * @return object |
| 1261 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1262 | public function field_data($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1263 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1264 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1265 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1266 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1267 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1268 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1269 | $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] | 1270 | return $query->field_data(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1271 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1272 | |
| 1273 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1275 | /** |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1276 | * Escape the SQL Identifiers |
| 1277 | * |
| 1278 | * This function escapes column and table names |
| 1279 | * |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 1280 | * @param mixed |
| 1281 | * @return mixed |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1282 | */ |
| 1283 | public function escape_identifiers($item) |
| 1284 | { |
Andrey Andreev | a7ddd2d | 2012-11-06 11:53:45 +0200 | [diff] [blame] | 1285 | 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] | 1286 | { |
| 1287 | return $item; |
| 1288 | } |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 1289 | elseif (is_array($item)) |
| 1290 | { |
| 1291 | foreach ($item as $key => $value) |
| 1292 | { |
| 1293 | $item[$key] = $this->escape_identifiers($value); |
| 1294 | } |
| 1295 | |
| 1296 | return $item; |
| 1297 | } |
Andrey Andreev | 49aa45b | 2012-07-06 16:22:21 +0300 | [diff] [blame] | 1298 | // Avoid breaking functions and literal values inside queries |
Andrey Andreev | 9859cb0 | 2012-07-13 13:07:58 +0300 | [diff] [blame] | 1299 | 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] | 1300 | { |
| 1301 | return $item; |
| 1302 | } |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1303 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 1304 | static $preg_ec = array(); |
| 1305 | |
| 1306 | if (empty($preg_ec)) |
| 1307 | { |
| 1308 | if (is_array($this->_escape_char)) |
| 1309 | { |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1310 | $preg_ec = array( |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 1311 | preg_quote($this->_escape_char[0], '/'), preg_quote($this->_escape_char[1], '/'), |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1312 | $this->_escape_char[0], $this->_escape_char[1] |
| 1313 | ); |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 1314 | } |
| 1315 | else |
| 1316 | { |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 1317 | $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/'); |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1318 | $preg_ec[2] = $preg_ec[3] = $this->_escape_char; |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1322 | foreach ($this->_reserved_identifiers as $id) |
| 1323 | { |
| 1324 | if (strpos($item, '.'.$id) !== FALSE) |
| 1325 | { |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1326 | 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] | 1327 | } |
| 1328 | } |
| 1329 | |
Andrey Andreev | 2636c1c | 2012-07-02 14:53:39 +0300 | [diff] [blame] | 1330 | 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] | 1331 | } |
| 1332 | |
| 1333 | // -------------------------------------------------------------------- |
| 1334 | |
| 1335 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1336 | * Generate an insert string |
| 1337 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1338 | * @param string the table upon which the query will be performed |
| 1339 | * @param array an associative array data of key/values |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1340 | * @return string |
| 1341 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1342 | public function insert_string($table, $data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1343 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1344 | $fields = $values = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1345 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1346 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1347 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1348 | $fields[] = $this->escape_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1349 | $values[] = $this->escape($val); |
| 1350 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1351 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1352 | return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1355 | // -------------------------------------------------------------------- |
| 1356 | |
| 1357 | /** |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1358 | * Insert statement |
| 1359 | * |
| 1360 | * Generates a platform-specific insert string from the supplied data |
| 1361 | * |
| 1362 | * @param string the table name |
| 1363 | * @param array the insert keys |
| 1364 | * @param array the insert values |
| 1365 | * @return string |
| 1366 | */ |
| 1367 | protected function _insert($table, $keys, $values) |
| 1368 | { |
| 1369 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
| 1370 | } |
| 1371 | |
| 1372 | // -------------------------------------------------------------------- |
| 1373 | |
| 1374 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1375 | * Generate an update string |
| 1376 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1377 | * @param string the table upon which the query will be performed |
| 1378 | * @param array an associative array data of key/values |
| 1379 | * @param mixed the "where" statement |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1380 | * @return string |
| 1381 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1382 | public function update_string($table, $data, $where) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1383 | { |
Andrey Andreev | 87f4dc2 | 2012-11-01 01:11:22 +0200 | [diff] [blame] | 1384 | if (empty($where)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1385 | { |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1386 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1387 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1388 | |
Andrey Andreev | 87f4dc2 | 2012-11-01 01:11:22 +0200 | [diff] [blame] | 1389 | $this->where($where); |
| 1390 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1391 | $fields = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1392 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1393 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1394 | $fields[$this->protect_identifiers($key)] = $this->escape($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Andrey Andreev | 79f888b | 2013-08-06 13:59:23 +0300 | [diff] [blame] | 1397 | $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields); |
| 1398 | $this->_reset_write(); |
| 1399 | return $sql; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1400 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1401 | |
| 1402 | // -------------------------------------------------------------------- |
| 1403 | |
| 1404 | /** |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1405 | * Update statement |
| 1406 | * |
| 1407 | * Generates a platform-specific update string from the supplied data |
| 1408 | * |
| 1409 | * @param string the table name |
Andrey Andreev | 822317b | 2012-07-19 16:00:32 +0300 | [diff] [blame] | 1410 | * @param array the update data |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1411 | * @return string |
| 1412 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 1413 | protected function _update($table, $values) |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1414 | { |
| 1415 | foreach ($values as $key => $val) |
| 1416 | { |
| 1417 | $valstr[] = $key.' = '.$val; |
| 1418 | } |
| 1419 | |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1420 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr) |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1421 | .$this->_compile_wh('qb_where') |
| 1422 | .$this->_compile_order_by() |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 1423 | .($this->qb_limit ? ' LIMIT '.$this->qb_limit : ''); |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | // -------------------------------------------------------------------- |
| 1427 | |
| 1428 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1429 | * Tests whether the string has an SQL operator |
| 1430 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1431 | * @param string |
| 1432 | * @return bool |
| 1433 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1434 | protected function _has_operator($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1435 | { |
Andrey Andreev | 295cfa9 | 2013-12-20 11:48:38 +0200 | [diff] [blame] | 1436 | return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | // -------------------------------------------------------------------- |
| 1440 | |
| 1441 | /** |
Andrey Andreev | 929fd2d | 2012-06-17 17:29:57 +0300 | [diff] [blame] | 1442 | * Returns the SQL string operator |
| 1443 | * |
| 1444 | * @param string |
| 1445 | * @return string |
| 1446 | */ |
| 1447 | protected function _get_operator($str) |
| 1448 | { |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1449 | static $_operators; |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1450 | |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1451 | if (empty($_operators)) |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 1452 | { |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1453 | $_les = ($this->_like_escape_str !== '') |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 1454 | ? '\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] | 1455 | : ''; |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1456 | $_operators = array( |
| 1457 | '\s*(?:<|>|!)?=\s*', // =, <=, >=, != |
| 1458 | '\s*<>?\s*', // <, <> |
| 1459 | '\s*>\s*', // > |
| 1460 | '\s+IS NULL', // IS NULL |
Andrey Andreev | 295cfa9 | 2013-12-20 11:48:38 +0200 | [diff] [blame] | 1461 | '\s+IS NOT NULL', // IS NOT NULL |
Tufan Barış YILDIRIM | 30893b9 | 2013-12-19 21:30:56 +0200 | [diff] [blame] | 1462 | '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql) |
Andrey Andreev | 295cfa9 | 2013-12-20 11:48:38 +0200 | [diff] [blame] | 1463 | '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql) |
Andrey Andreev | e8be24b | 2012-07-19 16:11:17 +0300 | [diff] [blame] | 1464 | '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value |
| 1465 | '\s+IN\s*\([^\)]+\)', // IN(list) |
| 1466 | '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list) |
| 1467 | '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s'] |
| 1468 | '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s'] |
| 1469 | ); |
| 1470 | |
| 1471 | } |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 1472 | |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1473 | return preg_match('/'.implode('|', $_operators).'/i', $str, $match) |
| 1474 | ? $match[0] : FALSE; |
Andrey Andreev | 929fd2d | 2012-06-17 17:29:57 +0300 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | // -------------------------------------------------------------------- |
| 1478 | |
| 1479 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1480 | * Enables a native PHP function to be run, using a platform agnostic wrapper. |
| 1481 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1482 | * @param string $function Function name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1483 | * @return mixed |
| 1484 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1485 | public function call_function($function) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1486 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1487 | $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1488 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1489 | if (FALSE === strpos($driver, $function)) |
| 1490 | { |
| 1491 | $function = $driver.$function; |
| 1492 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1493 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1494 | if ( ! function_exists($function)) |
| 1495 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1496 | return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1497 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1498 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1499 | return (func_num_args() > 1) |
Kakysha | a3f91b9 | 2013-12-14 05:23:23 +0400 | [diff] [blame] | 1500 | ? call_user_func_array($function, array_slice(func_get_args(), 1)) |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1501 | : call_user_func($function); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | // -------------------------------------------------------------------- |
| 1505 | |
| 1506 | /** |
| 1507 | * Set Cache Directory Path |
| 1508 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1509 | * @param string the path to the cache directory |
| 1510 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1511 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1512 | public function cache_set_path($path = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1513 | { |
| 1514 | $this->cachedir = $path; |
| 1515 | } |
| 1516 | |
| 1517 | // -------------------------------------------------------------------- |
| 1518 | |
| 1519 | /** |
| 1520 | * Enable Query Caching |
| 1521 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1522 | * @return bool cache_on value |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1523 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1524 | public function cache_on() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1525 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1526 | return $this->cache_on = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | // -------------------------------------------------------------------- |
| 1530 | |
| 1531 | /** |
| 1532 | * Disable Query Caching |
| 1533 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1534 | * @return bool cache_on value |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1535 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1536 | public function cache_off() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1537 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1538 | return $this->cache_on = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1539 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1540 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1541 | // -------------------------------------------------------------------- |
| 1542 | |
| 1543 | /** |
| 1544 | * Delete the cache files associated with a particular URI |
| 1545 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1546 | * @param string $segment_one = '' |
| 1547 | * @param string $segment_two = '' |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1548 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1549 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1550 | public function cache_delete($segment_one = '', $segment_two = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1551 | { |
Andrey Andreev | 043f260 | 2012-03-06 20:16:42 +0200 | [diff] [blame] | 1552 | return ($this->_cache_init()) |
| 1553 | ? $this->CACHE->delete($segment_one, $segment_two) |
| 1554 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | // -------------------------------------------------------------------- |
| 1558 | |
| 1559 | /** |
| 1560 | * Delete All cache files |
| 1561 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1562 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1563 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1564 | public function cache_delete_all() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1565 | { |
Andrey Andreev | 043f260 | 2012-03-06 20:16:42 +0200 | [diff] [blame] | 1566 | return ($this->_cache_init()) |
| 1567 | ? $this->CACHE->delete_all() |
| 1568 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | // -------------------------------------------------------------------- |
| 1572 | |
| 1573 | /** |
| 1574 | * Initialize the Cache Class |
| 1575 | * |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1576 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1577 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1578 | protected function _cache_init() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1579 | { |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 1580 | if (class_exists('CI_DB_Cache', FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1581 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1582 | if (is_object($this->CACHE)) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1583 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1584 | return TRUE; |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1585 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1586 | } |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1587 | elseif ( ! @include_once(BASEPATH.'database/DB_cache.php')) |
| 1588 | { |
| 1589 | return $this->cache_off(); |
| 1590 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1591 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1592 | $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects |
| 1593 | return TRUE; |
| 1594 | } |
| 1595 | |
| 1596 | // -------------------------------------------------------------------- |
| 1597 | |
| 1598 | /** |
| 1599 | * Close DB Connection |
| 1600 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1601 | * @return void |
| 1602 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1603 | public function close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1604 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1605 | if ($this->conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1606 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 1607 | $this->_close(); |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1608 | $this->conn_id = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1609 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1610 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1611 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1612 | // -------------------------------------------------------------------- |
| 1613 | |
| 1614 | /** |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 1615 | * Close DB Connection |
| 1616 | * |
| 1617 | * This method would be overriden by most of the drivers. |
| 1618 | * |
| 1619 | * @return void |
| 1620 | */ |
| 1621 | protected function _close() |
| 1622 | { |
| 1623 | $this->conn_id = FALSE; |
| 1624 | } |
| 1625 | |
| 1626 | // -------------------------------------------------------------------- |
| 1627 | |
| 1628 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1629 | * Display an error message |
| 1630 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1631 | * @param string the error message |
| 1632 | * @param string any "swap" values |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1633 | * @param bool whether to localize the message |
vlakoff | 52301c7 | 2013-03-29 14:23:34 +0100 | [diff] [blame] | 1634 | * @return string sends the application/views/errors/error_db.php template |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1635 | */ |
Timothy Warren | e45518d | 2012-03-06 07:38:00 -0500 | [diff] [blame] | 1636 | public function display_error($error = '', $swap = '', $native = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1637 | { |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1638 | $LANG =& load_class('Lang', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1639 | $LANG->load('db'); |
| 1640 | |
| 1641 | $heading = $LANG->line('db_error_heading'); |
| 1642 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1643 | if ($native === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1644 | { |
Andrey Andreev | 85a99cc | 2011-09-24 17:17:37 +0300 | [diff] [blame] | 1645 | $message = (array) $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1646 | } |
| 1647 | else |
| 1648 | { |
Andrey Andreev | 1194ad7 | 2012-10-05 17:05:46 +0300 | [diff] [blame] | 1649 | $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] | 1650 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1651 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1652 | // Find the most likely culprit of the error by going through |
| 1653 | // the backtrace until the source file is no longer in the |
| 1654 | // database folder. |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1655 | $trace = debug_backtrace(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1656 | foreach ($trace as $call) |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1657 | { |
Andrey Andreev | 342bb7e | 2012-11-20 23:17:28 +0200 | [diff] [blame] | 1658 | if (isset($call['file'], $call['class'])) |
Andrey Andreev | 1194ad7 | 2012-10-05 17:05:46 +0300 | [diff] [blame] | 1659 | { |
Andrey Andreev | 342bb7e | 2012-11-20 23:17:28 +0200 | [diff] [blame] | 1660 | // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes |
| 1661 | if (DIRECTORY_SEPARATOR !== '/') |
| 1662 | { |
| 1663 | $call['file'] = str_replace('\\', '/', $call['file']); |
| 1664 | } |
Andrey Andreev | 1194ad7 | 2012-10-05 17:05:46 +0300 | [diff] [blame] | 1665 | |
Andrey Andreev | 342bb7e | 2012-11-20 23:17:28 +0200 | [diff] [blame] | 1666 | if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE) |
| 1667 | { |
| 1668 | // Found it - use a relative path for safety |
| 1669 | $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']); |
| 1670 | $message[] = 'Line Number: '.$call['line']; |
| 1671 | break; |
| 1672 | } |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1673 | } |
| 1674 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1675 | |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1676 | $error =& load_class('Exceptions', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1677 | echo $error->show_error($heading, $message, 'error_db'); |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 1678 | exit(EXIT_DATABASE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | // -------------------------------------------------------------------- |
| 1682 | |
| 1683 | /** |
| 1684 | * Protect Identifiers |
| 1685 | * |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1686 | * This function is used extensively by the Query Builder class, and by |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1687 | * a couple functions in this class. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1688 | * 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] | 1689 | * 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] | 1690 | * column names that include the path. Consider a query like this: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1691 | * |
| 1692 | * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table |
| 1693 | * |
| 1694 | * Or a query with aliasing: |
| 1695 | * |
| 1696 | * SELECT m.member_id, m.member_name FROM members AS m |
| 1697 | * |
| 1698 | * Since the column name can include up to four segments (host, DB, table, column) |
| 1699 | * or also have an alias prefix, we need to do a bit of work to figure this out and |
| 1700 | * insert the table prefix (if it exists) in the proper position, and escape only |
| 1701 | * the correct identifiers. |
| 1702 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1703 | * @param string |
| 1704 | * @param bool |
| 1705 | * @param mixed |
| 1706 | * @param bool |
| 1707 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1708 | */ |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 1709 | 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] | 1710 | { |
| 1711 | if ( ! is_bool($protect_identifiers)) |
| 1712 | { |
Jamie Rumbelow | 0cd8c79 | 2012-03-08 12:52:24 +0000 | [diff] [blame] | 1713 | $protect_identifiers = $this->_protect_identifiers; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1714 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1715 | |
| 1716 | if (is_array($item)) |
| 1717 | { |
| 1718 | $escaped_array = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1719 | foreach ($item as $k => $v) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1720 | { |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1721 | $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] | 1722 | } |
| 1723 | |
| 1724 | return $escaped_array; |
| 1725 | } |
| 1726 | |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1727 | // 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] | 1728 | // 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] | 1729 | // 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] | 1730 | // way to deal with this, but I'm not thinking of it -- Rick |
Andrey Andreev | 3b0c08a | 2013-03-29 15:15:41 +0200 | [diff] [blame] | 1731 | // |
| 1732 | // Added exception for single quotes as well, we don't want to alter |
| 1733 | // literal strings. -- Narf |
| 1734 | if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE) |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1735 | { |
Andrey Andreev | 4db1632 | 2012-06-10 15:12:02 +0300 | [diff] [blame] | 1736 | return $item; |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1739 | // Convert tabs or multiple spaces into single spaces |
| 1740 | $item = preg_replace('/\s+/', ' ', $item); |
| 1741 | |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1742 | // 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] | 1743 | // Note: strripos() is used in order to support spaces in table names |
| 1744 | if ($offset = strripos($item, ' AS ')) |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1745 | { |
Andrey Andreev | 929fd2d | 2012-06-17 17:29:57 +0300 | [diff] [blame] | 1746 | $alias = ($protect_identifiers) |
| 1747 | ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4)) |
| 1748 | : substr($item, $offset); |
| 1749 | $item = substr($item, 0, $offset); |
| 1750 | } |
| 1751 | elseif ($offset = strrpos($item, ' ')) |
| 1752 | { |
| 1753 | $alias = ($protect_identifiers) |
| 1754 | ? ' '.$this->escape_identifiers(substr($item, $offset + 1)) |
| 1755 | : substr($item, $offset); |
| 1756 | $item = substr($item, 0, $offset); |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 1757 | } |
| 1758 | else |
| 1759 | { |
| 1760 | $alias = ''; |
| 1761 | } |
| 1762 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1763 | // Break the string apart if it contains periods, then insert the table prefix |
| 1764 | // in the correct location, assuming the period doesn't indicate that we're dealing |
| 1765 | // with an alias. While we're at it, we will escape the components |
| 1766 | if (strpos($item, '.') !== FALSE) |
| 1767 | { |
| 1768 | $parts = explode('.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1769 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1770 | // Does the first segment of the exploded item match |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1771 | // one of the aliases previously identified? If so, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1772 | // we have nothing more to do other than escape the item |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1773 | if (in_array($parts[0], $this->qb_aliased_tables)) |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1774 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1775 | if ($protect_identifiers === TRUE) |
| 1776 | { |
| 1777 | foreach ($parts as $key => $val) |
| 1778 | { |
| 1779 | if ( ! in_array($val, $this->_reserved_identifiers)) |
| 1780 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1781 | $parts[$key] = $this->escape_identifiers($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1782 | } |
| 1783 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1784 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1785 | $item = implode('.', $parts); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1786 | } |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1787 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1788 | return $item.$alias; |
| 1789 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1790 | |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1791 | // 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] | 1792 | if ($this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1793 | { |
| 1794 | // We now add the table prefix based on some logic. |
| 1795 | // Do we have 4 segments (hostname.database.table.column)? |
| 1796 | // If so, we add the table prefix to the column name in the 3rd segment. |
| 1797 | if (isset($parts[3])) |
| 1798 | { |
| 1799 | $i = 2; |
| 1800 | } |
| 1801 | // Do we have 3 segments (database.table.column)? |
| 1802 | // If so, we add the table prefix to the column name in 2nd position |
| 1803 | elseif (isset($parts[2])) |
| 1804 | { |
| 1805 | $i = 1; |
| 1806 | } |
| 1807 | // Do we have 2 segments (table.column)? |
| 1808 | // If so, we add the table prefix to the column name in 1st segment |
| 1809 | else |
| 1810 | { |
| 1811 | $i = 0; |
| 1812 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1813 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1814 | // This flag is set when the supplied $item does not contain a field name. |
| 1815 | // This can happen when this function is being called from a JOIN. |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1816 | if ($field_exists === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1817 | { |
| 1818 | $i++; |
| 1819 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1820 | |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1821 | // Verify table prefix and replace if necessary |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1822 | if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0) |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1823 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1824 | $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] | 1825 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1826 | // We only add the table prefix if it does not already exist |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1827 | elseif (strpos($parts[$i], $this->dbprefix) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1828 | { |
| 1829 | $parts[$i] = $this->dbprefix.$parts[$i]; |
| 1830 | } |
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 | // Put the parts back together |
| 1833 | $item = implode('.', $parts); |
| 1834 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1835 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1836 | if ($protect_identifiers === TRUE) |
| 1837 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1838 | $item = $this->escape_identifiers($item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1839 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1840 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1841 | return $item.$alias; |
| 1842 | } |
| 1843 | |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1844 | // Is there a table prefix? If not, no need to insert it |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1845 | if ($this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1846 | { |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1847 | // Verify table prefix and replace if necessary |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1848 | if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0) |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1849 | { |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1850 | $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item); |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1851 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1852 | // Do we prefix an item with no segments? |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1853 | elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1854 | { |
| 1855 | $item = $this->dbprefix.$item; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1856 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Andrey Andreev | af5d558 | 2012-01-25 21:34:47 +0200 | [diff] [blame] | 1859 | if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1860 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 1861 | $item = $this->escape_identifiers($item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1862 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1863 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1864 | return $item.$alias; |
| 1865 | } |
Andrey Andreev | 4c20260 | 2012-03-06 20:34:52 +0200 | [diff] [blame] | 1866 | |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1867 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1868 | |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1869 | /** |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 1870 | * Dummy method that allows Query Builder class to be disabled |
Andrey Andreev | 16bb9bd | 2012-05-26 18:21:14 +0300 | [diff] [blame] | 1871 | * and keep count_all() working. |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1872 | * |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1873 | * @return void |
| 1874 | */ |
| 1875 | protected function _reset_select() |
| 1876 | { |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1877 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1878 | |
| 1879 | } |
| 1880 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1881 | /* End of file DB_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 1882 | /* Location: ./system/database/DB_driver.php */ |