blob: 059849771e0c0f670e5653dcfba18ed606c149d6 [file] [log] [blame]
Marco Monteiro89726cd2015-09-01 09:50:21 +01001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Database Driver Class
42 *
43 * This is the platform-independent base DB implementation class.
44 * This class will not be called directly. Rather, the adapter
45 * class for the specific database will extend and instantiate it.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
Timothy Warren7eeda532012-03-19 16:01:55 -040053abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000054
Andrey Andreevae85eb42012-11-02 01:42:31 +020055 /**
56 * Data Source Name / Connect string
57 *
58 * @var string
59 */
Andrey Andreev738f5342012-03-06 20:43:40 +020060 public $dsn;
Andrey Andreevae85eb42012-11-02 01:42:31 +020061
62 /**
63 * Username
64 *
65 * @var string
66 */
Andrey Andreevd1add432012-03-06 20:26:01 +020067 public $username;
Andrey Andreevae85eb42012-11-02 01:42:31 +020068
69 /**
70 * Password
71 *
72 * @var string
73 */
Andrey Andreevd1add432012-03-06 20:26:01 +020074 public $password;
Andrey Andreevae85eb42012-11-02 01:42:31 +020075
76 /**
77 * Hostname
78 *
79 * @var string
80 */
Andrey Andreevd1add432012-03-06 20:26:01 +020081 public $hostname;
Andrey Andreevae85eb42012-11-02 01:42:31 +020082
83 /**
84 * Database name
85 *
86 * @var string
87 */
Andrey Andreevd1add432012-03-06 20:26:01 +020088 public $database;
Andrey Andreevae85eb42012-11-02 01:42:31 +020089
90 /**
91 * Database driver
92 *
93 * @var string
94 */
Andrey Andreev75546112012-07-05 11:01:29 +030095 public $dbdriver = 'mysqli';
Andrey Andreevae85eb42012-11-02 01:42:31 +020096
97 /**
98 * Sub-driver
99 *
100 * @used-by CI_DB_pdo_driver
101 * @var string
102 */
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300103 public $subdriver;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200104
105 /**
106 * Table prefix
107 *
108 * @var string
109 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200110 public $dbprefix = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200111
112 /**
113 * Character set
114 *
115 * @var string
116 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200117 public $char_set = 'utf8';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200118
119 /**
120 * Collation
121 *
122 * @var string
123 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200124 public $dbcollat = 'utf8_general_ci';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200125
126 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200127 * Encryption flag/data
128 *
129 * @var mixed
130 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300131 public $encrypt = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200132
133 /**
134 * Swap Prefix
135 *
136 * @var string
137 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200138 public $swap_pre = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200139
140 /**
141 * Database port
142 *
143 * @var int
144 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200145 public $port = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200146
147 /**
148 * Persistent connection flag
149 *
150 * @var bool
151 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200152 public $pconnect = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200153
154 /**
155 * Connection ID
156 *
157 * @var object|resource
158 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200159 public $conn_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200160
161 /**
162 * Result ID
163 *
164 * @var object|resource
165 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200166 public $result_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200167
168 /**
169 * Debug flag
170 *
171 * Whether to display error messages.
172 *
173 * @var bool
174 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200175 public $db_debug = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200176
177 /**
178 * Benchmark time
179 *
180 * @var int
181 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200182 public $benchmark = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200183
184 /**
185 * Executed queries count
186 *
187 * @var int
188 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200189 public $query_count = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200190
191 /**
192 * Bind marker
193 *
194 * Character used to identify values in a prepared statement.
195 *
196 * @var string
197 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200198 public $bind_marker = '?';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200199
200 /**
201 * Save queries flag
202 *
203 * Whether to keep an in-memory history of queries for debugging purposes.
204 *
205 * @var bool
206 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200207 public $save_queries = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200208
209 /**
210 * Queries list
211 *
212 * @see CI_DB_driver::$save_queries
213 * @var string[]
214 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200215 public $queries = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200216
217 /**
218 * Query times
219 *
220 * A list of times that queries took to execute.
221 *
222 * @var array
223 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200224 public $query_times = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200225
226 /**
227 * Data cache
228 *
229 * An internal generic value cache.
230 *
231 * @var array
232 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200233 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000234
Andrey Andreevae85eb42012-11-02 01:42:31 +0200235 /**
236 * Transaction enabled flag
237 *
238 * @var bool
239 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200240 public $trans_enabled = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200241
242 /**
243 * Strict transaction mode flag
244 *
245 * @var bool
246 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200247 public $trans_strict = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200248
249 /**
250 * Transaction depth level
251 *
252 * @var int
253 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200254 protected $_trans_depth = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255
Andrey Andreevae85eb42012-11-02 01:42:31 +0200256 /**
257 * Transaction status flag
258 *
259 * Used with transactions to determine if a rollback should occur.
260 *
261 * @var bool
262 */
263 protected $_trans_status = TRUE;
264
265 /**
Ahmedul Haque Abida2129772014-05-01 01:57:53 +0600266 * Transaction failure flag
267 *
268 * Used with transactions to determine if a transaction has failed.
269 *
270 * @var bool
271 */
272 protected $_trans_failure = FALSE;
273
274 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200275 * Cache On flag
276 *
277 * @var bool
278 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200279 public $cache_on = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200280
281 /**
282 * Cache directory path
283 *
284 * @var bool
285 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200286 public $cachedir = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200287
288 /**
289 * Cache auto-delete flag
290 *
291 * @var bool
292 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200293 public $cache_autodel = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200294
Andrey Andreevae85eb42012-11-02 01:42:31 +0200295 /**
296 * DB Cache object
297 *
298 * @see CI_DB_cache
299 * @var object
300 */
301 public $CACHE;
302
303 /**
304 * Protect identifiers flag
305 *
306 * @var bool
307 */
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +0000308 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +0200309
Andrey Andreevae85eb42012-11-02 01:42:31 +0200310 /**
311 * List of reserved identifiers
312 *
313 * Identifiers that must NOT be escaped.
314 *
315 * @var string[]
316 */
317 protected $_reserved_identifiers = array('*');
318
319 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200320 * Identifier escape character
321 *
322 * @var string
323 */
324 protected $_escape_char = '"';
325
326 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200327 * ESCAPE statement string
328 *
329 * @var string
330 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300331 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevae85eb42012-11-02 01:42:31 +0200332
333 /**
334 * ESCAPE character
335 *
336 * @var string
337 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300338 protected $_like_escape_chr = '!';
339
Andrey Andreev75546112012-07-05 11:01:29 +0300340 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200341 * ORDER BY random keyword
342 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200343 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200344 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200345 protected $_random_keyword = array('RAND()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200346
347 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200348 * COUNT string
349 *
350 * @used-by CI_DB_driver::count_all()
351 * @used-by CI_DB_query_builder::count_all_results()
352 *
353 * @var string
Andrey Andreev77a5d942012-07-05 15:42:14 +0300354 */
355 protected $_count_string = 'SELECT COUNT(*) AS ';
356
Andrey Andreevae85eb42012-11-02 01:42:31 +0200357 // --------------------------------------------------------------------
358
Andrey Andreev77a5d942012-07-05 15:42:14 +0300359 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200360 * Class constructor
Andrey Andreev75546112012-07-05 11:01:29 +0300361 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200362 * @param array $params
Andrey Andreev75546112012-07-05 11:01:29 +0300363 * @return void
364 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500365 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 if (is_array($params))
368 {
369 foreach ($params as $key => $val)
370 {
371 $this->$key = $val;
372 }
373 }
374
Andrey Andreev90726b82015-01-20 12:39:22 +0200375 log_message('info', 'Database Driver Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Gints Murans89f77ee2012-05-23 00:23:50 +0300378 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 /**
381 * Initialize Database Settings
382 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200383 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200384 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200385 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200387 /* If an established connection is available, then there's
388 * no need to connect and select the database.
389 *
390 * Depending on the database driver, conn_id can be either
391 * boolean TRUE, a resource or an object.
392 */
393 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 return TRUE;
396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // Connect to the database and set the connection ID
Andrey Andreev7e963512014-02-27 15:43:24 +0200401 $this->conn_id = $this->db_connect($this->pconnect);
Derek Allard2067d1a2008-11-13 22:59:24 +0000402
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200403 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 if ( ! $this->conn_id)
405 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100406 // Check if there is a failover set
407 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100409 // Go over all the failovers
410 foreach ($this->failover as $failover)
411 {
412 // Replace the current settings with those of the failover
413 foreach ($failover as $key => $val)
414 {
415 $this->$key = $val;
416 }
417
418 // Try to connect
Andrey Andreev7e963512014-02-27 15:43:24 +0200419 $this->conn_id = $this->db_connect($this->pconnect);
Felix Balfoort5d581b62011-11-29 15:53:01 +0100420
421 // If a connection is made break the foreach loop
422 if ($this->conn_id)
423 {
424 break;
425 }
426 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100428
429 // We still don't have a connection?
430 if ( ! $this->conn_id)
431 {
432 log_message('error', 'Unable to connect to the database');
433
434 if ($this->db_debug)
435 {
436 $this->display_error('db_unable_to_connect');
437 }
Andrey Andreev7e963512014-02-27 15:43:24 +0200438
Felix Balfoort5d581b62011-11-29 15:53:01 +0100439 return FALSE;
440 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200443 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200444 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // --------------------------------------------------------------------
448
449 /**
Andrey Andreev0d3fde22015-01-05 16:56:41 +0200450 * DB connect
451 *
452 * This is just a dummy method that all drivers will override.
453 *
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300454 * @return mixed
Andrey Andreev0d3fde22015-01-05 16:56:41 +0200455 */
456 public function db_connect()
457 {
458 return TRUE;
459 }
460
461 // --------------------------------------------------------------------
462
463 /**
Andrey Andreev2e171022014-02-25 15:21:41 +0200464 * Persistent database connection
465 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +0200466 * @return mixed
Andrey Andreev2e171022014-02-25 15:21:41 +0200467 */
468 public function db_pconnect()
469 {
470 return $this->db_connect(TRUE);
471 }
472
473 // --------------------------------------------------------------------
474
475 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200476 * Reconnect
477 *
478 * Keep / reestablish the db connection if no queries have been
479 * sent for a length of time exceeding the server's idle timeout.
480 *
481 * This is just a dummy method to allow drivers without such
482 * functionality to not declare it, while others will override it.
483 *
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300484 * @return void
Andrey Andreev2cae1932012-03-23 16:08:41 +0200485 */
486 public function reconnect()
487 {
488 }
489
490 // --------------------------------------------------------------------
491
492 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300493 * Select database
494 *
495 * This is just a dummy method to allow drivers without such
496 * functionality to not declare it, while others will override it.
497 *
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300498 * @return bool
Andrey Andreevbee66442012-03-28 15:28:19 +0300499 */
500 public function db_select()
501 {
502 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
504
505 // --------------------------------------------------------------------
506
507 /**
Andrey Andreev27ba5e62015-12-14 13:05:33 +0200508 * Last error
509 *
510 * @return array
511 */
512 public function error()
513 {
514 return array('code' => NULL, 'message' => NULL);
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * Set client character set
521 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200523 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200525 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200527 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200529 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 if ($this->db_debug)
532 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200533 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 return FALSE;
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 return TRUE;
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // --------------------------------------------------------------------
543
544 /**
545 * The name of the platform in use (mysql, mssql, etc...)
546 *
Barry Mienydd671972010-10-04 16:33:58 +0200547 * @return string
548 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500549 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
551 return $this->dbdriver;
552 }
553
554 // --------------------------------------------------------------------
555
556 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200557 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200559 * Returns a string containing the version of the database being used.
560 * Most drivers will override this method.
561 *
Barry Mienydd671972010-10-04 16:33:58 +0200562 * @return string
563 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200564 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200566 if (isset($this->data_cache['version']))
567 {
568 return $this->data_cache['version'];
569 }
570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 if (FALSE === ($sql = $this->_version()))
572 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200573 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
Derek Allard3683f772009-12-16 17:32:33 +0000575
Andrey Andreev7e963512014-02-27 15:43:24 +0200576 $query = $this->query($sql)->row();
Andrey Andreev08856b82012-03-03 03:19:28 +0200577 return $this->data_cache['version'] = $query->ver;
578 }
Derek Allard3683f772009-12-16 17:32:33 +0000579
Andrey Andreev08856b82012-03-03 03:19:28 +0200580 // --------------------------------------------------------------------
581
582 /**
583 * Version number query string
584 *
585 * @return string
586 */
587 protected function _version()
588 {
589 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // --------------------------------------------------------------------
593
594 /**
595 * Execute the query
596 *
597 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200598 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 * upon successful execution of a "write" type query. Returns boolean
600 * FALSE upon failure, and if the $db_debug variable is set to TRUE
601 * will raise an error.
602 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300603 * @param string $sql
604 * @param array $binds = FALSE An array of binding data
605 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200606 * @return mixed
607 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300608 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100610 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200612 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200613 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300615 elseif ( ! is_bool($return_object))
616 {
617 $return_object = ! $this->is_write_type($sql);
618 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000619
620 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100621 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600622 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200623 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
Derek Jonese7792202010-03-02 17:24:46 -0600625
Ryan Dialef7474c2012-03-01 16:11:36 -0500626 // Compile binds if needed
627 if ($binds !== FALSE)
628 {
629 $sql = $this->compile_binds($sql, $binds);
630 }
631
Andrey Andreev4c202602012-03-06 20:34:52 +0200632 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 // we will load the caching class and return the previously
634 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300635 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200637 $this->load_rdriver();
638 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200640 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Andrey Andreevb66664b2012-06-27 14:22:10 +0300644 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100645 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
647 $this->queries[] = $sql;
648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200651 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // Run the Query
654 if (FALSE === ($this->result_id = $this->simple_query($sql)))
655 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100656 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
658 $this->query_times[] = 0;
659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // This will trigger a rollback if transactions are being used
Andrey Andreev4e5ff1f2015-08-05 14:32:03 +0300662 if ($this->_trans_depth !== 0)
663 {
664 $this->_trans_status = FALSE;
665 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000666
Andrey Andreev4be5de12012-03-02 15:45:41 +0200667 // Grab the error now, as we might run some additional queries before displaying the error
668 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200669
670 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300671 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 if ($this->db_debug)
674 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200676 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200677 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 // transactions to remain in limbo.
Andrey Andreev803cc122016-01-11 11:15:27 +0200679 while ($this->_trans_depth !== 0)
Andrey Andreev6c854422013-10-21 13:58:15 +0300680 {
Andrey Andreev803cc122016-01-11 11:15:27 +0200681 $trans_depth = $this->_trans_depth;
682 $this->trans_complete();
683 if ($trans_depth === $this->_trans_depth)
Andrey Andreev6c854422013-10-21 13:58:15 +0300684 {
Andrey Andreev803cc122016-01-11 11:15:27 +0200685 log_message('error', 'Database: Failure during an automated transaction commit/rollback!');
686 break;
Andrey Andreev6c854422013-10-21 13:58:15 +0300687 }
Andrey Andreev6c854422013-10-21 13:58:15 +0300688 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000689
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200690 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200691 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 return FALSE;
695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200698 $time_end = microtime(TRUE);
699 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000700
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100701 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
dixyab3cab52012-04-21 00:58:00 +0200703 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 // Increment the query counter
707 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200708
Andrey Andreevb66664b2012-06-27 14:22:10 +0300709 // Will we have a result object instantiated? If not - we'll simply return TRUE
710 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300712 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100713 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
715 $this->CACHE->delete();
716 }
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 return TRUE;
719 }
Barry Mienydd671972010-10-04 16:33:58 +0200720
Barry Mienydd671972010-10-04 16:33:58 +0200721 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200722 $driver = $this->load_rdriver();
723 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200724
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200725 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100727 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
729 // We'll create a new instance of the result object
730 // only without the platform specific driver since
731 // we can't use it with cached data (the query result
732 // resource ID won't be any good once we've cached the
733 // result object, so we'll have to compile the data
734 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200735 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 $CR->result_object = $RES->result_object();
737 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200738 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 // Reset these since cached objects can not utilize resource IDs.
741 $CR->conn_id = NULL;
742 $CR->result_id = NULL;
743
744 $this->CACHE->write($sql, $CR);
745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 return $RES;
748 }
749
750 // --------------------------------------------------------------------
751
752 /**
753 * Load the result drivers
754 *
Barry Mienydd671972010-10-04 16:33:58 +0200755 * @return string the name of the result class
756 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500757 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
759 $driver = 'CI_DB_'.$this->dbdriver.'_result';
760
vlakofffadb8222013-05-12 10:57:09 +0200761 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200763 require_once(BASEPATH.'database/DB_result.php');
764 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 }
Barry Mienydd671972010-10-04 16:33:58 +0200766
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 return $driver;
768 }
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 // --------------------------------------------------------------------
771
772 /**
773 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200774 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 * we only use it when running transaction commands since they do
776 * not require all the features of the main query() function.
777 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200779 * @return mixed
780 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500781 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
783 if ( ! $this->conn_id)
784 {
Andrey Andreev39105002015-11-09 14:05:02 +0200785 if ( ! $this->initialize())
786 {
787 return FALSE;
788 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
790
791 return $this->_execute($sql);
792 }
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 // --------------------------------------------------------------------
795
796 /**
797 * Disable Transactions
798 * This permits transactions to be disabled at run-time.
799 *
Barry Mienydd671972010-10-04 16:33:58 +0200800 * @return void
801 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500802 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
804 $this->trans_enabled = FALSE;
805 }
806
807 // --------------------------------------------------------------------
808
809 /**
810 * Enable/disable Transaction Strict Mode
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300811 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 * When strict mode is enabled, if you are running multiple groups of
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300813 * transactions, if one group fails all subsequent groups will be
814 * rolled back.
815 *
816 * If strict mode is disabled, each group is treated autonomously,
817 * meaning a failure of one group will not affect any others
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300819 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200820 * @return void
821 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500822 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
824 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 // --------------------------------------------------------------------
828
829 /**
830 * Start Transaction
831 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300832 * @param bool $test_mode = FALSE
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300833 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200834 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500835 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200836 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 if ( ! $this->trans_enabled)
838 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300839 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
841
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300842 return $this->trans_begin($test_mode);
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 }
844
845 // --------------------------------------------------------------------
846
847 /**
848 * Complete Transaction
849 *
Barry Mienydd671972010-10-04 16:33:58 +0200850 * @return bool
851 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500852 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 if ( ! $this->trans_enabled)
855 {
856 return FALSE;
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900860 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
862 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // If we are NOT running in strict mode, we will reset
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300865 // the _trans_status flag so that subsequent groups of
866 // transactions will be permitted.
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 if ($this->trans_strict === FALSE)
868 {
869 $this->_trans_status = TRUE;
870 }
871
872 log_message('debug', 'DB Transaction Failure');
873 return FALSE;
874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300876 return $this->trans_commit();
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 }
878
879 // --------------------------------------------------------------------
880
881 /**
882 * Lets you retrieve the transaction flag to determine if it has failed
883 *
Barry Mienydd671972010-10-04 16:33:58 +0200884 * @return bool
885 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500886 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 return $this->_trans_status;
889 }
890
891 // --------------------------------------------------------------------
892
893 /**
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300894 * Begin Transaction
895 *
896 * @param bool $test_mode
897 * @return bool
898 */
899 public function trans_begin($test_mode = FALSE)
900 {
901 if ( ! $this->trans_enabled)
902 {
903 return FALSE;
904 }
905 // When transactions are nested we only begin/commit/rollback the outermost ones
906 elseif ($this->_trans_depth > 0)
907 {
908 $this->_trans_depth++;
909 return TRUE;
910 }
911
912 // Reset the transaction failure flag.
913 // If the $test_mode flag is set to TRUE transactions will be rolled back
914 // even if the queries produce a successful result.
915 $this->_trans_failure = ($test_mode === TRUE);
916
917 if ($this->_trans_begin())
918 {
Andrey Andreev5e88ea52017-09-04 15:13:37 +0300919 $this->_trans_status = TRUE;
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300920 $this->_trans_depth++;
921 return TRUE;
922 }
923
924 return FALSE;
925 }
926
927 // --------------------------------------------------------------------
928
929 /**
930 * Commit Transaction
931 *
932 * @return bool
933 */
934 public function trans_commit()
935 {
936 if ( ! $this->trans_enabled OR $this->_trans_depth === 0)
937 {
938 return FALSE;
939 }
940 // When transactions are nested we only begin/commit/rollback the outermost ones
941 elseif ($this->_trans_depth > 1 OR $this->_trans_commit())
942 {
943 $this->_trans_depth--;
944 return TRUE;
945 }
946
947 return FALSE;
948 }
949
950 // --------------------------------------------------------------------
951
952 /**
953 * Rollback Transaction
954 *
955 * @return bool
956 */
957 public function trans_rollback()
958 {
959 if ( ! $this->trans_enabled OR $this->_trans_depth === 0)
960 {
961 return FALSE;
962 }
963 // When transactions are nested we only begin/commit/rollback the outermost ones
964 elseif ($this->_trans_depth > 1 OR $this->_trans_rollback())
965 {
966 $this->_trans_depth--;
967 return TRUE;
968 }
969
970 return FALSE;
971 }
972
973 // --------------------------------------------------------------------
974
975 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 * Compile Bindings
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @param string the sql statement
979 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200980 * @return string
981 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500982 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
Andrey Andreevdae08b52016-10-22 15:37:15 +0300984 if (empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 return $sql;
987 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300988 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300990 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300991 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300993 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200994 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300995 // Make sure we're using numeric keys
996 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300997 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
999
Andrey Andreevaf915ce2012-06-13 19:03:06 +03001000 // We'll need the marker length later
1001 $ml = strlen($this->bind_marker);
1002
Andrey Andreev10cbdf02012-06-13 13:32:30 +03001003 // Make sure not to replace a chunk inside a string that happens to match the bind marker
Andrey Andreev6b5464c2016-10-31 13:09:33 +02001004 if ($c = preg_match_all("/'[^']*'|\"[^\"]*\"/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001006 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +03001007 str_replace($matches[0],
1008 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
1009 $sql, $c),
1010 $matches, PREG_OFFSET_CAPTURE);
1011
1012 // Bind values' count must match the count of markers in the query
1013 if ($bind_count !== $c)
1014 {
1015 return $sql;
1016 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +03001017 }
Andrey Andreeve4742582012-10-25 13:25:13 +03001018 elseif (($c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i', $sql, $matches, PREG_OFFSET_CAPTURE)) !== $bind_count)
Andrey Andreev10cbdf02012-06-13 13:32:30 +03001019 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +03001020 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 }
1022
Andrey Andreevaf915ce2012-06-13 19:03:06 +03001023 do
1024 {
1025 $c--;
clawoo4a4f5502014-10-20 15:28:08 +03001026 $escaped_value = $this->escape($binds[$c]);
1027 if (is_array($escaped_value))
1028 {
1029 $escaped_value = '('.implode(',', $escaped_value).')';
1030 }
1031 $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
Andrey Andreevaf915ce2012-06-13 19:03:06 +03001032 }
1033 while ($c !== 0);
1034
Andrey Andreev4e9538f2012-06-12 14:16:53 +03001035 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 }
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 // --------------------------------------------------------------------
1039
1040 /**
1041 * Determines if a query is a "write" type.
1042 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +02001044 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001045 */
Andrey Andreev67f71a42012-03-01 16:18:42 +02001046 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Andrey Andreev56e452a2017-07-14 13:05:48 +03001048 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX|MERGE)\s/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 // --------------------------------------------------------------------
1052
1053 /**
1054 * Calculate the aggregate query elapsed time
1055 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001056 * @param int The number of decimal places
James L Parry78301732014-12-16 02:14:52 -08001057 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001058 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001059 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
1061 return number_format($this->benchmark, $decimals);
1062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 // --------------------------------------------------------------------
1065
1066 /**
1067 * Returns the total number of queries
1068 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001069 * @return int
Barry Mienydd671972010-10-04 16:33:58 +02001070 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001071 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
1073 return $this->query_count;
1074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 // --------------------------------------------------------------------
1077
1078 /**
1079 * Returns the last query that was executed
1080 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001081 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001082 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001083 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
1085 return end($this->queries);
1086 }
1087
1088 // --------------------------------------------------------------------
1089
1090 /**
1091 * "Smart" Escape String
1092 *
1093 * Escapes data based on type
1094 * Sets boolean and null types
1095 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 * @param string
Barry Mienydd671972010-10-04 16:33:58 +02001097 * @return mixed
1098 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001099 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +02001100 {
clawooa779c482014-10-18 14:47:04 +03001101 if (is_array($str))
1102 {
1103 $str = array_map(array(&$this, 'escape'), $str);
clawoo4a4f5502014-10-20 15:28:08 +03001104 return $str;
clawooa779c482014-10-18 14:47:04 +03001105 }
1106 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001108 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001109 }
1110 elseif (is_bool($str))
1111 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001112 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001113 }
vlakoff1228fe22013-01-14 01:30:09 +01001114 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001115 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001116 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001117 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001118
1119 return $str;
1120 }
1121
1122 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001123
Derek Jonese4ed5832009-02-20 21:44:59 +00001124 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001125 * Escape String
1126 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001127 * @param string|string[] $str Input string
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001128 * @param bool $like Whether or not the string will be used in a LIKE condition
1129 * @return string
1130 */
1131 public function escape_str($str, $like = FALSE)
1132 {
1133 if (is_array($str))
1134 {
1135 foreach ($str as $key => $val)
1136 {
1137 $str[$key] = $this->escape_str($val, $like);
1138 }
1139
1140 return $str;
1141 }
1142
1143 $str = $this->_escape_str($str);
1144
1145 // escape LIKE condition wildcards
1146 if ($like === TRUE)
1147 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001148 return str_replace(
1149 array($this->_like_escape_chr, '%', '_'),
1150 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1151 $str
1152 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001153 }
1154
1155 return $str;
1156 }
1157
1158 // --------------------------------------------------------------------
1159
1160 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001161 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001162 *
1163 * Calls the individual driver for platform
1164 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001165 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001166 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001167 * @return mixed
1168 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001169 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001170 {
1171 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001172 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001173
Derek Jonese4ed5832009-02-20 21:44:59 +00001174 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 /**
Andrey Andreev71d8f722017-01-17 12:01:00 +02001177 * Platform-dependent string escape
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001178 *
1179 * @param string
1180 * @return string
1181 */
1182 protected function _escape_str($str)
1183 {
vlakofff0093f92017-02-16 01:31:59 +01001184 return str_replace("'", "''", remove_invisible_characters($str, FALSE));
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001185 }
1186
1187 // --------------------------------------------------------------------
1188
1189 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 * Primary
1191 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001192 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 * position is the primary key
1194 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001195 * @param string $table Table name
1196 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001197 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001198 public function primary($table)
Barry Mienydd671972010-10-04 16:33:58 +02001199 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001201 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 }
1203
1204 // --------------------------------------------------------------------
1205
1206 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001207 * "Count All" query
1208 *
1209 * Generates a platform-specific query string that counts all records in
1210 * the specified database
1211 *
1212 * @param string
1213 * @return int
1214 */
1215 public function count_all($table = '')
1216 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001217 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001218 {
1219 return 0;
1220 }
1221
1222 $query = $this->query($this->_count_string.$this->escape_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001223 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001224 {
1225 return 0;
1226 }
1227
1228 $query = $query->row();
1229 $this->_reset_select();
1230 return (int) $query->numrows;
1231 }
1232
1233 // --------------------------------------------------------------------
1234
1235 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 * Returns an array of table names
1237 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001238 * @param string $constrain_by_prefix = FALSE
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001239 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001240 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001241 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
1243 // Is there a cached result?
1244 if (isset($this->data_cache['table_names']))
1245 {
1246 return $this->data_cache['table_names'];
1247 }
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1250 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001251 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 }
1253
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001254 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001256
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001257 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001259 // Do we know from which column to get the table name?
1260 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001262 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001263 {
1264 $key = 'table_name';
1265 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001266 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001267 {
1268 $key = 'TABLE_NAME';
1269 }
1270 else
1271 {
1272 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001273 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001274 * E_STRICT is on, this would trigger a warning. So we'll have to
1275 * assign it first.
1276 */
1277 $key = array_keys($row);
1278 $key = array_shift($key);
1279 }
Taufan Aditya18209332012-02-09 16:07:27 +07001280 }
1281
Andrey Andreev12567e82012-01-25 22:50:33 +02001282 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 }
1284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 return $this->data_cache['table_names'];
1286 }
Barry Mienydd671972010-10-04 16:33:58 +02001287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 // --------------------------------------------------------------------
1289
1290 /**
1291 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001292 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001293 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001294 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001296 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001297 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001298 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 }
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 // --------------------------------------------------------------------
1302
1303 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001304 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 *
Andrey Andreev0f850902015-04-29 12:33:11 +03001306 * @param string $table Table name
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001307 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 */
Andrey Andreev358b0882015-01-05 21:00:18 +02001309 public function list_fields($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 {
1311 // Is there a cached result?
1312 if (isset($this->data_cache['field_names'][$table]))
1313 {
1314 return $this->data_cache['field_names'][$table];
1315 }
Barry Mienydd671972010-10-04 16:33:58 +02001316
Greg Aker1edde302010-01-26 00:17:01 +00001317 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001319 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 }
Barry Mienydd671972010-10-04 16:33:58 +02001321
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001323 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001324
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001325 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001327 // Do we know from where to get the column's name?
1328 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001330 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001331 {
1332 $key = 'column_name';
1333 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001334 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001335 {
1336 $key = 'COLUMN_NAME';
1337 }
1338 else
1339 {
RJ garcia395e2df2013-03-21 10:48:24 -05001340 // We have no other choice but to just get the first element's key.
1341 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001342 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001344
1345 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 return $this->data_cache['field_names'][$table];
1349 }
1350
1351 // --------------------------------------------------------------------
1352
1353 /**
1354 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001355 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 * @param string
1357 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001358 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001360 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001361 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001362 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 }
Barry Mienydd671972010-10-04 16:33:58 +02001364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 // --------------------------------------------------------------------
1366
1367 /**
1368 * Returns an object with field data
1369 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001370 * @param string $table the table name
1371 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001372 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001373 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001375 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001376 return ($query) ? $query->field_data() : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001377 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001378
1379 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001380
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001382 * Escape the SQL Identifiers
1383 *
1384 * This function escapes column and table names
1385 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001386 * @param mixed
1387 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001388 */
1389 public function escape_identifiers($item)
1390 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001391 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001392 {
1393 return $item;
1394 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001395 elseif (is_array($item))
1396 {
1397 foreach ($item as $key => $value)
1398 {
1399 $item[$key] = $this->escape_identifiers($value);
1400 }
1401
1402 return $item;
1403 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001404 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001405 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001406 {
1407 return $item;
1408 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001409
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001410 static $preg_ec = array();
1411
1412 if (empty($preg_ec))
1413 {
1414 if (is_array($this->_escape_char))
1415 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001416 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001417 preg_quote($this->_escape_char[0], '/'),
1418 preg_quote($this->_escape_char[1], '/'),
1419 $this->_escape_char[0],
1420 $this->_escape_char[1]
1421 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001422 }
1423 else
1424 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001425 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001426 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001427 }
1428 }
1429
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001430 foreach ($this->_reserved_identifiers as $id)
1431 {
1432 if (strpos($item, '.'.$id) !== FALSE)
1433 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001434 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\./i', $preg_ec[2].'$1'.$preg_ec[3].'.', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001435 }
1436 }
1437
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001438 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?(\.)?/i', $preg_ec[2].'$1'.$preg_ec[3].'$2', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001439 }
1440
1441 // --------------------------------------------------------------------
1442
1443 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 * Generate an insert string
1445 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 * @param string the table upon which the query will be performed
1447 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001448 * @return string
1449 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001450 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001452 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001453
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001454 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001456 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 $values[] = $this->escape($val);
1458 }
Barry Mienydd671972010-10-04 16:33:58 +02001459
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001460 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001461 }
1462
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 // --------------------------------------------------------------------
1464
1465 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001466 * Insert statement
1467 *
1468 * Generates a platform-specific insert string from the supplied data
1469 *
1470 * @param string the table name
1471 * @param array the insert keys
1472 * @param array the insert values
1473 * @return string
1474 */
1475 protected function _insert($table, $keys, $values)
1476 {
1477 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1478 }
1479
1480 // --------------------------------------------------------------------
1481
1482 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 * Generate an update string
1484 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 * @param string the table upon which the query will be performed
1486 * @param array an associative array data of key/values
1487 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001488 * @return string
1489 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001490 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001492 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001494 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001495 }
Barry Mienydd671972010-10-04 16:33:58 +02001496
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001497 $this->where($where);
1498
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001500 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001502 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 }
1504
Andrey Andreev79f888b2013-08-06 13:59:23 +03001505 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1506 $this->_reset_write();
1507 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001508 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001509
1510 // --------------------------------------------------------------------
1511
1512 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001513 * Update statement
1514 *
1515 * Generates a platform-specific update string from the supplied data
1516 *
1517 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001518 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001519 * @return string
1520 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001521 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001522 {
1523 foreach ($values as $key => $val)
1524 {
1525 $valstr[] = $key.' = '.$val;
1526 }
1527
Andrey Andreev75546112012-07-05 11:01:29 +03001528 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001529 .$this->_compile_wh('qb_where')
1530 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001531 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001532 }
1533
1534 // --------------------------------------------------------------------
1535
1536 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 * Tests whether the string has an SQL operator
1538 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 * @param string
1540 * @return bool
1541 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001542 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 {
Andrey Andreev5078eb52014-12-02 01:11:54 +02001544 return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 }
1546
1547 // --------------------------------------------------------------------
1548
1549 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001550 * Returns the SQL string operator
1551 *
1552 * @param string
1553 * @return string
1554 */
1555 protected function _get_operator($str)
1556 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001557 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001558
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001559 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001560 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001561 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001562 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001563 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001564 $_operators = array(
Andrey Andreev348a2d42015-08-31 17:39:04 +03001565 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1566 '\s*<>?\s*', // <, <>
1567 '\s*>\s*', // >
1568 '\s+IS NULL', // IS NULL
1569 '\s+IS NOT NULL', // IS NOT NULL
Andrey Andreev1089a862016-01-27 16:15:01 +02001570 '\s+EXISTS\s*\(.*\)', // EXISTS(sql)
1571 '\s+NOT EXISTS\s*\(.*\)', // NOT EXISTS(sql)
Andrey Andreev0d0c53c2015-09-03 11:23:44 +03001572 '\s+BETWEEN\s+', // BETWEEN value AND value
Andrey Andreev1089a862016-01-27 16:15:01 +02001573 '\s+IN\s*\(.*\)', // IN(list)
1574 '\s+NOT IN\s*\(.*\)', // NOT IN (list)
Andrey Andreev348a2d42015-08-31 17:39:04 +03001575 '\s+LIKE\s+\S.*('.$_les.')?', // LIKE 'expr'[ ESCAPE '%s']
1576 '\s+NOT LIKE\s+\S.*('.$_les.')?' // NOT LIKE 'expr'[ ESCAPE '%s']
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001577 );
1578
1579 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001580
Andrey Andreev6e704752012-07-18 00:46:33 +03001581 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1582 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001583 }
1584
1585 // --------------------------------------------------------------------
1586
1587 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1589 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001590 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001591 * @return mixed
1592 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001593 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001595 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001596
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 if (FALSE === strpos($driver, $function))
1598 {
1599 $function = $driver.$function;
1600 }
Barry Mienydd671972010-10-04 16:33:58 +02001601
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 if ( ! function_exists($function))
1603 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001604 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001606
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001607 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001608 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001609 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 }
1611
1612 // --------------------------------------------------------------------
1613
1614 /**
1615 * Set Cache Directory Path
1616 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 * @param string the path to the cache directory
1618 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001619 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001620 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 {
1622 $this->cachedir = $path;
1623 }
1624
1625 // --------------------------------------------------------------------
1626
1627 /**
1628 * Enable Query Caching
1629 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001630 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001631 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001632 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001634 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 }
1636
1637 // --------------------------------------------------------------------
1638
1639 /**
1640 * Disable Query Caching
1641 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001642 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001643 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001644 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001646 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 }
Barry Mienydd671972010-10-04 16:33:58 +02001648
Derek Allard2067d1a2008-11-13 22:59:24 +00001649 // --------------------------------------------------------------------
1650
1651 /**
1652 * Delete the cache files associated with a particular URI
1653 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001654 * @param string $segment_one = ''
1655 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001656 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001657 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001658 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001660 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001661 ? $this->CACHE->delete($segment_one, $segment_two)
1662 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 }
1664
1665 // --------------------------------------------------------------------
1666
1667 /**
1668 * Delete All cache files
1669 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001670 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001671 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001672 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001674 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001675 ? $this->CACHE->delete_all()
1676 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 }
1678
1679 // --------------------------------------------------------------------
1680
1681 /**
1682 * Initialize the Cache Class
1683 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001684 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001685 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001686 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001688 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001690 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001692 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001693 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001694 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001695 }
Derek Allarde37ab382009-02-03 16:13:57 +00001696
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1698 return TRUE;
1699 }
1700
1701 // --------------------------------------------------------------------
1702
1703 /**
1704 * Close DB Connection
1705 *
Barry Mienydd671972010-10-04 16:33:58 +02001706 * @return void
1707 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001708 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001710 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001712 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001713 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 }
Barry Mienydd671972010-10-04 16:33:58 +02001716
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 // --------------------------------------------------------------------
1718
1719 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001720 * Close DB Connection
1721 *
Claudio Galdiolob83a3d22015-01-29 11:42:50 -05001722 * This method would be overridden by most of the drivers.
Andrey Andreev79922c02012-05-23 12:27:17 +03001723 *
1724 * @return void
1725 */
1726 protected function _close()
1727 {
1728 $this->conn_id = FALSE;
1729 }
1730
1731 // --------------------------------------------------------------------
1732
1733 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 * Display an error message
1735 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 * @param string the error message
1737 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001738 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001739 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001740 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001741 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 {
Derek Jonese7792202010-03-02 17:24:46 -06001743 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 $LANG->load('db');
1745
1746 $heading = $LANG->line('db_error_heading');
1747
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001748 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001750 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 }
1752 else
1753 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001754 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 }
Barry Mienydd671972010-10-04 16:33:58 +02001756
Pascal Kriete60f8c392010-08-25 18:03:28 +02001757 // Find the most likely culprit of the error by going through
1758 // the backtrace until the source file is no longer in the
1759 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001760 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001761 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001762 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001763 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001764 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001765 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1766 if (DIRECTORY_SEPARATOR !== '/')
1767 {
1768 $call['file'] = str_replace('\\', '/', $call['file']);
1769 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001770
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001771 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1772 {
1773 // Found it - use a relative path for safety
1774 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1775 $message[] = 'Line Number: '.$call['line'];
1776 break;
1777 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001778 }
1779 }
Barry Mienydd671972010-10-04 16:33:58 +02001780
Derek Jonese7792202010-03-02 17:24:46 -06001781 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001783 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 }
1785
1786 // --------------------------------------------------------------------
1787
1788 /**
1789 * Protect Identifiers
1790 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001791 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001792 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001794 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001795 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 *
Claudio Galdiolo27eb5c92016-02-04 13:02:39 -05001797 * SELECT hostname.database.table.column AS c FROM hostname.database.table
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 *
1799 * Or a query with aliasing:
1800 *
1801 * SELECT m.member_id, m.member_name FROM members AS m
1802 *
1803 * Since the column name can include up to four segments (host, DB, table, column)
1804 * or also have an alias prefix, we need to do a bit of work to figure this out and
1805 * insert the table prefix (if it exists) in the proper position, and escape only
1806 * the correct identifiers.
1807 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 * @param string
1809 * @param bool
1810 * @param mixed
1811 * @param bool
1812 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001813 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001814 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 {
1816 if ( ! is_bool($protect_identifiers))
1817 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001818 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 }
Derek Allarde37ab382009-02-03 16:13:57 +00001820
1821 if (is_array($item))
1822 {
1823 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001824 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001825 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001826 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001827 }
1828
1829 return $escaped_array;
1830 }
1831
Derek Allard911d3e02008-12-15 14:08:35 +00001832 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001833 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001834 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001835 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001836 //
1837 // Added exception for single quotes as well, we don't want to alter
1838 // literal strings. -- Narf
Andrey Andreev49884bc2015-06-29 11:52:34 +03001839 if (strcspn($item, "()'") !== strlen($item))
Derek Allard911d3e02008-12-15 14:08:35 +00001840 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001841 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001842 }
1843
Andrey Andreevbf940582012-06-10 07:05:05 +03001844 // Convert tabs or multiple spaces into single spaces
Andrey Andreevbe178a72015-08-20 13:23:21 +03001845 $item = preg_replace('/\s+/', ' ', trim($item));
Andrey Andreevbf940582012-06-10 07:05:05 +03001846
Andrey Andreevbf940582012-06-10 07:05:05 +03001847 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001848 // Note: strripos() is used in order to support spaces in table names
1849 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001850 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001851 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001852 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1853 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001854 $item = substr($item, 0, $offset);
1855 }
1856 elseif ($offset = strrpos($item, ' '))
1857 {
1858 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001859 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1860 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001861 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001862 }
1863 else
1864 {
1865 $alias = '';
1866 }
1867
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 // Break the string apart if it contains periods, then insert the table prefix
1869 // in the correct location, assuming the period doesn't indicate that we're dealing
1870 // with an alias. While we're at it, we will escape the components
1871 if (strpos($item, '.') !== FALSE)
1872 {
Andrey Andreeva12cf282015-08-07 17:37:05 +03001873 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001874
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001876 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 // we have nothing more to do other than escape the item
Andrey Andreeva12cf282015-08-07 17:37:05 +03001878 //
1879 // NOTE: The ! empty() condition prevents this method
1880 // from breaking when QB isn't enabled.
1881 if ( ! empty($this->qb_aliased_tables) && in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001882 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 if ($protect_identifiers === TRUE)
1884 {
1885 foreach ($parts as $key => $val)
1886 {
1887 if ( ! in_array($val, $this->_reserved_identifiers))
1888 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001889 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 }
1891 }
Barry Mienydd671972010-10-04 16:33:58 +02001892
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001894 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001895
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 return $item.$alias;
1897 }
Barry Mienydd671972010-10-04 16:33:58 +02001898
Andrey Andreev4c202602012-03-06 20:34:52 +02001899 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001900 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 {
1902 // We now add the table prefix based on some logic.
1903 // Do we have 4 segments (hostname.database.table.column)?
1904 // If so, we add the table prefix to the column name in the 3rd segment.
1905 if (isset($parts[3]))
1906 {
1907 $i = 2;
1908 }
1909 // Do we have 3 segments (database.table.column)?
1910 // If so, we add the table prefix to the column name in 2nd position
1911 elseif (isset($parts[2]))
1912 {
1913 $i = 1;
1914 }
1915 // Do we have 2 segments (table.column)?
1916 // If so, we add the table prefix to the column name in 1st segment
1917 else
1918 {
1919 $i = 0;
1920 }
Barry Mienydd671972010-10-04 16:33:58 +02001921
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 // This flag is set when the supplied $item does not contain a field name.
1923 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001924 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 {
1926 $i++;
1927 }
Barry Mienydd671972010-10-04 16:33:58 +02001928
Andrey Andreevfa2a0c52017-10-13 17:04:35 +03001929 // dbprefix may've already been applied, with or without the identifier escaped
1930 $ec = '(?<ec>'.preg_quote(is_array($this->_escape_char) ? $this->_escape_char[0] : $this->_escape_char).')?';
1931 isset($ec[0]) && $ec .= '?'; // Just in case someone has disabled escaping by forcing an empty escape character
1932
Derek Jones55acc8b2009-07-11 03:38:13 +00001933 // Verify table prefix and replace if necessary
Andrey Andreevfa2a0c52017-10-13 17:04:35 +03001934 if ($this->swap_pre !== '' && preg_match('#^'.$ec.preg_quote($this->swap_pre).'#', $parts[$i]))
Derek Jones55acc8b2009-07-11 03:38:13 +00001935 {
Andrey Andreevfa2a0c52017-10-13 17:04:35 +03001936 $parts[$i] = preg_replace('#^'.$ec.preg_quote($this->swap_pre).'(\S+?)#', '\\1'.$this->dbprefix.'\\2', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001937 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 // We only add the table prefix if it does not already exist
Andrey Andreevfa2a0c52017-10-13 17:04:35 +03001939 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 {
Andrey Andreevfa2a0c52017-10-13 17:04:35 +03001941 preg_match('#^'.$ec.preg_quote($this->dbprefix).'#', $parts[$i]) OR $parts[$i] = $this->dbprefix.$parts[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 }
Barry Mienydd671972010-10-04 16:33:58 +02001943
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 // Put the parts back together
1945 $item = implode('.', $parts);
1946 }
Barry Mienydd671972010-10-04 16:33:58 +02001947
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 if ($protect_identifiers === TRUE)
1949 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001950 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 }
Barry Mienydd671972010-10-04 16:33:58 +02001952
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 return $item.$alias;
1954 }
1955
Andrey Andreev4c202602012-03-06 20:34:52 +02001956 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001957 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001959 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001960 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001961 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001962 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001963 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001965 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001966 {
1967 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001968 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 }
1970
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001971 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001972 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001973 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 }
Barry Mienydd671972010-10-04 16:33:58 +02001975
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 return $item.$alias;
1977 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001978
Túbal Martín511f2252011-11-24 14:43:45 +01001979 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001980
Túbal Martín511f2252011-11-24 14:43:45 +01001981 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001982 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001983 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001984 *
Túbal Martín511f2252011-11-24 14:43:45 +01001985 * @return void
1986 */
1987 protected function _reset_select()
1988 {
Túbal Martín511f2252011-11-24 14:43:45 +01001989 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001990
1991}