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