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