blob: cc94edc164f71b39843b9e410198aa534c1129bd [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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
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 /**
508 * Set client character set
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200511 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200513 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200515 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200517 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 if ($this->db_debug)
520 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200521 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 return FALSE;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 return TRUE;
528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 // --------------------------------------------------------------------
531
532 /**
533 * The name of the platform in use (mysql, mssql, etc...)
534 *
Barry Mienydd671972010-10-04 16:33:58 +0200535 * @return string
536 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500537 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 return $this->dbdriver;
540 }
541
542 // --------------------------------------------------------------------
543
544 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200545 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200547 * Returns a string containing the version of the database being used.
548 * Most drivers will override this method.
549 *
Barry Mienydd671972010-10-04 16:33:58 +0200550 * @return string
551 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200552 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200554 if (isset($this->data_cache['version']))
555 {
556 return $this->data_cache['version'];
557 }
558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 if (FALSE === ($sql = $this->_version()))
560 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200561 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
Derek Allard3683f772009-12-16 17:32:33 +0000563
Andrey Andreev7e963512014-02-27 15:43:24 +0200564 $query = $this->query($sql)->row();
Andrey Andreev08856b82012-03-03 03:19:28 +0200565 return $this->data_cache['version'] = $query->ver;
566 }
Derek Allard3683f772009-12-16 17:32:33 +0000567
Andrey Andreev08856b82012-03-03 03:19:28 +0200568 // --------------------------------------------------------------------
569
570 /**
571 * Version number query string
572 *
573 * @return string
574 */
575 protected function _version()
576 {
577 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // --------------------------------------------------------------------
581
582 /**
583 * Execute the query
584 *
585 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200586 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * upon successful execution of a "write" type query. Returns boolean
588 * FALSE upon failure, and if the $db_debug variable is set to TRUE
589 * will raise an error.
590 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300591 * @param string $sql
592 * @param array $binds = FALSE An array of binding data
593 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200594 * @return mixed
595 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300596 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100598 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200600 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200601 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300603 elseif ( ! is_bool($return_object))
604 {
605 $return_object = ! $this->is_write_type($sql);
606 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000607
608 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100609 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600610 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200611 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
Derek Jonese7792202010-03-02 17:24:46 -0600613
Ryan Dialef7474c2012-03-01 16:11:36 -0500614 // Compile binds if needed
615 if ($binds !== FALSE)
616 {
617 $sql = $this->compile_binds($sql, $binds);
618 }
619
Andrey Andreev4c202602012-03-06 20:34:52 +0200620 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // we will load the caching class and return the previously
622 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300623 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200625 $this->load_rdriver();
626 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200628 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Andrey Andreevb66664b2012-06-27 14:22:10 +0300632 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100633 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 $this->queries[] = $sql;
636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200639 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // Run the Query
642 if (FALSE === ($this->result_id = $this->simple_query($sql)))
643 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100644 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
646 $this->query_times[] = 0;
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 // This will trigger a rollback if transactions are being used
Andrey Andreev4e5ff1f2015-08-05 14:32:03 +0300650 if ($this->_trans_depth !== 0)
651 {
652 $this->_trans_status = FALSE;
653 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000654
Andrey Andreev4be5de12012-03-02 15:45:41 +0200655 // Grab the error now, as we might run some additional queries before displaying the error
656 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200657
658 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300659 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 if ($this->db_debug)
662 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200664 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200665 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // transactions to remain in limbo.
Andrey Andreev6c854422013-10-21 13:58:15 +0300667 if ($this->_trans_depth !== 0)
668 {
669 do
670 {
671 $this->trans_complete();
672 }
673 while ($this->_trans_depth !== 0);
674 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000675
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200676 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200677 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 return FALSE;
681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200684 $time_end = microtime(TRUE);
685 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000686
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100687 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
dixyab3cab52012-04-21 00:58:00 +0200689 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 // Increment the query counter
693 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200694
Andrey Andreevb66664b2012-06-27 14:22:10 +0300695 // Will we have a result object instantiated? If not - we'll simply return TRUE
696 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300698 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100699 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
701 $this->CACHE->delete();
702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 return TRUE;
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Barry Mienydd671972010-10-04 16:33:58 +0200707 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200708 $driver = $this->load_rdriver();
709 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200710
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200711 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100713 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
715 // We'll create a new instance of the result object
716 // only without the platform specific driver since
717 // we can't use it with cached data (the query result
718 // resource ID won't be any good once we've cached the
719 // result object, so we'll have to compile the data
720 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200721 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 $CR->result_object = $RES->result_object();
723 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200724 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 // Reset these since cached objects can not utilize resource IDs.
727 $CR->conn_id = NULL;
728 $CR->result_id = NULL;
729
730 $this->CACHE->write($sql, $CR);
731 }
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 return $RES;
734 }
735
736 // --------------------------------------------------------------------
737
738 /**
739 * Load the result drivers
740 *
Barry Mienydd671972010-10-04 16:33:58 +0200741 * @return string the name of the result class
742 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500743 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 {
745 $driver = 'CI_DB_'.$this->dbdriver.'_result';
746
vlakofffadb8222013-05-12 10:57:09 +0200747 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200749 require_once(BASEPATH.'database/DB_result.php');
750 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
Barry Mienydd671972010-10-04 16:33:58 +0200752
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 return $driver;
754 }
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 // --------------------------------------------------------------------
757
758 /**
759 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200760 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 * we only use it when running transaction commands since they do
762 * not require all the features of the main query() function.
763 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200765 * @return mixed
766 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500767 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 if ( ! $this->conn_id)
770 {
771 $this->initialize();
772 }
773
774 return $this->_execute($sql);
775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * Disable Transactions
781 * This permits transactions to be disabled at run-time.
782 *
Barry Mienydd671972010-10-04 16:33:58 +0200783 * @return void
784 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500785 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
787 $this->trans_enabled = FALSE;
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Enable/disable Transaction Strict Mode
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300794 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 * When strict mode is enabled, if you are running multiple groups of
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300796 * transactions, if one group fails all subsequent groups will be
797 * rolled back.
798 *
799 * If strict mode is disabled, each group is treated autonomously,
800 * meaning a failure of one group will not affect any others
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300802 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200803 * @return void
804 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500805 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
807 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 // --------------------------------------------------------------------
811
812 /**
813 * Start Transaction
814 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300815 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200816 * @return void
817 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500818 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200819 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 if ( ! $this->trans_enabled)
821 {
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100822 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 }
824
825 // When transactions are nested we only begin/commit/rollback the outermost ones
826 if ($this->_trans_depth > 0)
827 {
828 $this->_trans_depth += 1;
829 return;
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500833 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
835
836 // --------------------------------------------------------------------
837
838 /**
839 * Complete Transaction
840 *
Barry Mienydd671972010-10-04 16:33:58 +0200841 * @return bool
842 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500843 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
845 if ( ! $this->trans_enabled)
846 {
847 return FALSE;
848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // When transactions are nested we only begin/commit/rollback the outermost ones
851 if ($this->_trans_depth > 1)
852 {
853 $this->_trans_depth -= 1;
854 return TRUE;
855 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500856 else
857 {
858 $this->_trans_depth = 0;
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900862 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
864 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 // If we are NOT running in strict mode, we will reset
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300867 // the _trans_status flag so that subsequent groups of
868 // transactions will be permitted.
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 if ($this->trans_strict === FALSE)
870 {
871 $this->_trans_status = TRUE;
872 }
873
874 log_message('debug', 'DB Transaction Failure');
875 return FALSE;
876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 $this->trans_commit();
879 return TRUE;
880 }
881
882 // --------------------------------------------------------------------
883
884 /**
885 * Lets you retrieve the transaction flag to determine if it has failed
886 *
Barry Mienydd671972010-10-04 16:33:58 +0200887 * @return bool
888 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500889 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
891 return $this->_trans_status;
892 }
893
894 // --------------------------------------------------------------------
895
896 /**
897 * Compile Bindings
898 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 * @param string the sql statement
900 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200901 * @return string
902 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500903 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300905 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
907 return $sql;
908 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300909 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300911 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300912 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300914 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200915 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300916 // Make sure we're using numeric keys
917 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300918 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
920
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300921 // We'll need the marker length later
922 $ml = strlen($this->bind_marker);
923
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300924 // Make sure not to replace a chunk inside a string that happens to match the bind marker
925 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300927 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300928 str_replace($matches[0],
929 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
930 $sql, $c),
931 $matches, PREG_OFFSET_CAPTURE);
932
933 // Bind values' count must match the count of markers in the query
934 if ($bind_count !== $c)
935 {
936 return $sql;
937 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300938 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300939 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 +0300940 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300941 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
943
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300944 do
945 {
946 $c--;
clawoo4a4f5502014-10-20 15:28:08 +0300947 $escaped_value = $this->escape($binds[$c]);
948 if (is_array($escaped_value))
949 {
950 $escaped_value = '('.implode(',', $escaped_value).')';
951 }
952 $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300953 }
954 while ($c !== 0);
955
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300956 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 // --------------------------------------------------------------------
960
961 /**
962 * Determines if a query is a "write" type.
963 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200965 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200966 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200967 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200969 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 Allard2067d1a2008-11-13 22:59:24 +0000970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
973
974 /**
975 * Calculate the aggregate query elapsed time
976 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200977 * @param int The number of decimal places
James L Parry78301732014-12-16 02:14:52 -0800978 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200979 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500980 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 return number_format($this->benchmark, $decimals);
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
986
987 /**
988 * Returns the total number of queries
989 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200990 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200991 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500992 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
994 return $this->query_count;
995 }
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 // --------------------------------------------------------------------
998
999 /**
1000 * Returns the last query that was executed
1001 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001002 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001003 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001004 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
1006 return end($this->queries);
1007 }
1008
1009 // --------------------------------------------------------------------
1010
1011 /**
1012 * "Smart" Escape String
1013 *
1014 * Escapes data based on type
1015 * Sets boolean and null types
1016 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 * @param string
Barry Mienydd671972010-10-04 16:33:58 +02001018 * @return mixed
1019 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001020 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +02001021 {
clawooa779c482014-10-18 14:47:04 +03001022 if (is_array($str))
1023 {
1024 $str = array_map(array(&$this, 'escape'), $str);
clawoo4a4f5502014-10-20 15:28:08 +03001025 return $str;
clawooa779c482014-10-18 14:47:04 +03001026 }
1027 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001029 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001030 }
1031 elseif (is_bool($str))
1032 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001033 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001034 }
vlakoff1228fe22013-01-14 01:30:09 +01001035 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001036 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001037 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001038 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001039
1040 return $str;
1041 }
1042
1043 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001044
Derek Jonese4ed5832009-02-20 21:44:59 +00001045 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001046 * Escape String
1047 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001048 * @param string|string[] $str Input string
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001049 * @param bool $like Whether or not the string will be used in a LIKE condition
1050 * @return string
1051 */
1052 public function escape_str($str, $like = FALSE)
1053 {
1054 if (is_array($str))
1055 {
1056 foreach ($str as $key => $val)
1057 {
1058 $str[$key] = $this->escape_str($val, $like);
1059 }
1060
1061 return $str;
1062 }
1063
1064 $str = $this->_escape_str($str);
1065
1066 // escape LIKE condition wildcards
1067 if ($like === TRUE)
1068 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001069 return str_replace(
1070 array($this->_like_escape_chr, '%', '_'),
1071 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1072 $str
1073 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001074 }
1075
1076 return $str;
1077 }
1078
1079 // --------------------------------------------------------------------
1080
1081 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001082 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001083 *
1084 * Calls the individual driver for platform
1085 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001086 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001087 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001088 * @return mixed
1089 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001090 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001091 {
1092 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001093 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001094
Derek Jonese4ed5832009-02-20 21:44:59 +00001095 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001098 * Platform-dependant string escape
1099 *
1100 * @param string
1101 * @return string
1102 */
1103 protected function _escape_str($str)
1104 {
1105 return str_replace("'", "''", remove_invisible_characters($str));
1106 }
1107
1108 // --------------------------------------------------------------------
1109
1110 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 * Primary
1112 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001113 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 * position is the primary key
1115 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001116 * @param string $table Table name
1117 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001118 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001119 public function primary($table)
Barry Mienydd671972010-10-04 16:33:58 +02001120 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001122 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
1124
1125 // --------------------------------------------------------------------
1126
1127 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001128 * "Count All" query
1129 *
1130 * Generates a platform-specific query string that counts all records in
1131 * the specified database
1132 *
1133 * @param string
1134 * @return int
1135 */
1136 public function count_all($table = '')
1137 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001138 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001139 {
1140 return 0;
1141 }
1142
1143 $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 +01001144 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001145 {
1146 return 0;
1147 }
1148
1149 $query = $query->row();
1150 $this->_reset_select();
1151 return (int) $query->numrows;
1152 }
1153
1154 // --------------------------------------------------------------------
1155
1156 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 * Returns an array of table names
1158 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001159 * @param string $constrain_by_prefix = FALSE
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001160 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001161 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001162 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
1164 // Is there a cached result?
1165 if (isset($this->data_cache['table_names']))
1166 {
1167 return $this->data_cache['table_names'];
1168 }
Barry Mienydd671972010-10-04 16:33:58 +02001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1171 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001172 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
1174
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001175 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001177
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001178 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001180 // Do we know from which column to get the table name?
1181 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001183 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001184 {
1185 $key = 'table_name';
1186 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001187 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001188 {
1189 $key = 'TABLE_NAME';
1190 }
1191 else
1192 {
1193 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001194 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001195 * E_STRICT is on, this would trigger a warning. So we'll have to
1196 * assign it first.
1197 */
1198 $key = array_keys($row);
1199 $key = array_shift($key);
1200 }
Taufan Aditya18209332012-02-09 16:07:27 +07001201 }
1202
Andrey Andreev12567e82012-01-25 22:50:33 +02001203 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 }
1205
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 return $this->data_cache['table_names'];
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // --------------------------------------------------------------------
1210
1211 /**
1212 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001213 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001214 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001215 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001217 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001218 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001219 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 }
Barry Mienydd671972010-10-04 16:33:58 +02001221
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 // --------------------------------------------------------------------
1223
1224 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001225 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 *
Andrey Andreev0f850902015-04-29 12:33:11 +03001227 * @param string $table Table name
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001228 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 */
Andrey Andreev358b0882015-01-05 21:00:18 +02001230 public function list_fields($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 {
1232 // Is there a cached result?
1233 if (isset($this->data_cache['field_names'][$table]))
1234 {
1235 return $this->data_cache['field_names'][$table];
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Greg Aker1edde302010-01-26 00:17:01 +00001238 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001240 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 }
Barry Mienydd671972010-10-04 16:33:58 +02001242
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001244 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001245
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001246 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001248 // Do we know from where to get the column's name?
1249 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001251 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001252 {
1253 $key = 'column_name';
1254 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001255 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001256 {
1257 $key = 'COLUMN_NAME';
1258 }
1259 else
1260 {
RJ garcia395e2df2013-03-21 10:48:24 -05001261 // We have no other choice but to just get the first element's key.
1262 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001263 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001265
1266 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 }
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 return $this->data_cache['field_names'][$table];
1270 }
1271
1272 // --------------------------------------------------------------------
1273
1274 /**
1275 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001276 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 * @param string
1278 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001279 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001281 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001282 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001283 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 }
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 // --------------------------------------------------------------------
1287
1288 /**
1289 * Returns an object with field data
1290 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001291 * @param string $table the table name
1292 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001293 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001294 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001296 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001297 return ($query) ? $query->field_data() : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001298 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001299
1300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001303 * Escape the SQL Identifiers
1304 *
1305 * This function escapes column and table names
1306 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001307 * @param mixed
1308 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001309 */
1310 public function escape_identifiers($item)
1311 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001312 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001313 {
1314 return $item;
1315 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001316 elseif (is_array($item))
1317 {
1318 foreach ($item as $key => $value)
1319 {
1320 $item[$key] = $this->escape_identifiers($value);
1321 }
1322
1323 return $item;
1324 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001325 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001326 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001327 {
1328 return $item;
1329 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001330
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001331 static $preg_ec = array();
1332
1333 if (empty($preg_ec))
1334 {
1335 if (is_array($this->_escape_char))
1336 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001337 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001338 preg_quote($this->_escape_char[0], '/'),
1339 preg_quote($this->_escape_char[1], '/'),
1340 $this->_escape_char[0],
1341 $this->_escape_char[1]
1342 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001343 }
1344 else
1345 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001346 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001347 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001348 }
1349 }
1350
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001351 foreach ($this->_reserved_identifiers as $id)
1352 {
1353 if (strpos($item, '.'.$id) !== FALSE)
1354 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001355 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 +03001356 }
1357 }
1358
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001359 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 +03001360 }
1361
1362 // --------------------------------------------------------------------
1363
1364 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 * Generate an insert string
1366 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 * @param string the table upon which the query will be performed
1368 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001369 * @return string
1370 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001371 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001373 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001374
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001375 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001377 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 $values[] = $this->escape($val);
1379 }
Barry Mienydd671972010-10-04 16:33:58 +02001380
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001381 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001382 }
1383
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 // --------------------------------------------------------------------
1385
1386 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001387 * Insert statement
1388 *
1389 * Generates a platform-specific insert string from the supplied data
1390 *
1391 * @param string the table name
1392 * @param array the insert keys
1393 * @param array the insert values
1394 * @return string
1395 */
1396 protected function _insert($table, $keys, $values)
1397 {
1398 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1399 }
1400
1401 // --------------------------------------------------------------------
1402
1403 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 * Generate an update string
1405 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 * @param string the table upon which the query will be performed
1407 * @param array an associative array data of key/values
1408 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001409 * @return string
1410 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001411 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001413 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001415 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 }
Barry Mienydd671972010-10-04 16:33:58 +02001417
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001418 $this->where($where);
1419
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001421 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001423 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 }
1425
Andrey Andreev79f888b2013-08-06 13:59:23 +03001426 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1427 $this->_reset_write();
1428 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001429 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001430
1431 // --------------------------------------------------------------------
1432
1433 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001434 * Update statement
1435 *
1436 * Generates a platform-specific update string from the supplied data
1437 *
1438 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001439 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001440 * @return string
1441 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001442 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001443 {
1444 foreach ($values as $key => $val)
1445 {
1446 $valstr[] = $key.' = '.$val;
1447 }
1448
Andrey Andreev75546112012-07-05 11:01:29 +03001449 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001450 .$this->_compile_wh('qb_where')
1451 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001452 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001453 }
1454
1455 // --------------------------------------------------------------------
1456
1457 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 * Tests whether the string has an SQL operator
1459 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 * @param string
1461 * @return bool
1462 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001463 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 {
Andrey Andreev5078eb52014-12-02 01:11:54 +02001465 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 +00001466 }
1467
1468 // --------------------------------------------------------------------
1469
1470 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001471 * Returns the SQL string operator
1472 *
1473 * @param string
1474 * @return string
1475 */
1476 protected function _get_operator($str)
1477 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001478 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001479
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001480 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001481 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001482 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001483 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001484 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001485 $_operators = array(
Andrey Andreev348a2d42015-08-31 17:39:04 +03001486 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1487 '\s*<>?\s*', // <, <>
1488 '\s*>\s*', // >
1489 '\s+IS NULL', // IS NULL
1490 '\s+IS NOT NULL', // IS NOT NULL
1491 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
1492 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreev0d0c53c2015-09-03 11:23:44 +03001493 '\s+BETWEEN\s+', // BETWEEN value AND value
Andrey Andreev348a2d42015-08-31 17:39:04 +03001494 '\s+IN\s*\([^\)]+\)', // IN(list)
1495 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1496 '\s+LIKE\s+\S.*('.$_les.')?', // LIKE 'expr'[ ESCAPE '%s']
1497 '\s+NOT LIKE\s+\S.*('.$_les.')?' // NOT LIKE 'expr'[ ESCAPE '%s']
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001498 );
1499
1500 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001501
Andrey Andreev6e704752012-07-18 00:46:33 +03001502 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1503 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001504 }
1505
1506 // --------------------------------------------------------------------
1507
1508 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1510 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001511 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001512 * @return mixed
1513 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001514 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001516 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 if (FALSE === strpos($driver, $function))
1519 {
1520 $function = $driver.$function;
1521 }
Barry Mienydd671972010-10-04 16:33:58 +02001522
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 if ( ! function_exists($function))
1524 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001525 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001527
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001528 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001529 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001530 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 }
1532
1533 // --------------------------------------------------------------------
1534
1535 /**
1536 * Set Cache Directory Path
1537 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 * @param string the path to the cache directory
1539 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001540 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001541 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
1543 $this->cachedir = $path;
1544 }
1545
1546 // --------------------------------------------------------------------
1547
1548 /**
1549 * Enable Query Caching
1550 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001551 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001552 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001553 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001555 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 }
1557
1558 // --------------------------------------------------------------------
1559
1560 /**
1561 * Disable Query Caching
1562 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001563 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001564 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001565 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001567 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 }
Barry Mienydd671972010-10-04 16:33:58 +02001569
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 // --------------------------------------------------------------------
1571
1572 /**
1573 * Delete the cache files associated with a particular URI
1574 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001575 * @param string $segment_one = ''
1576 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001577 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001578 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001579 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001581 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001582 ? $this->CACHE->delete($segment_one, $segment_two)
1583 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 }
1585
1586 // --------------------------------------------------------------------
1587
1588 /**
1589 * Delete All cache files
1590 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001591 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001592 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001593 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001595 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001596 ? $this->CACHE->delete_all()
1597 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 }
1599
1600 // --------------------------------------------------------------------
1601
1602 /**
1603 * Initialize the Cache Class
1604 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001605 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001606 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001607 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001609 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001611 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001613 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001614 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001615 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001616 }
Derek Allarde37ab382009-02-03 16:13:57 +00001617
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1619 return TRUE;
1620 }
1621
1622 // --------------------------------------------------------------------
1623
1624 /**
1625 * Close DB Connection
1626 *
Barry Mienydd671972010-10-04 16:33:58 +02001627 * @return void
1628 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001629 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001631 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001633 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001634 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 }
Barry Mienydd671972010-10-04 16:33:58 +02001637
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 // --------------------------------------------------------------------
1639
1640 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001641 * Close DB Connection
1642 *
Claudio Galdiolob83a3d22015-01-29 11:42:50 -05001643 * This method would be overridden by most of the drivers.
Andrey Andreev79922c02012-05-23 12:27:17 +03001644 *
1645 * @return void
1646 */
1647 protected function _close()
1648 {
1649 $this->conn_id = FALSE;
1650 }
1651
1652 // --------------------------------------------------------------------
1653
1654 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 * Display an error message
1656 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 * @param string the error message
1658 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001659 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001660 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001661 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001662 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 {
Derek Jonese7792202010-03-02 17:24:46 -06001664 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 $LANG->load('db');
1666
1667 $heading = $LANG->line('db_error_heading');
1668
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001669 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001671 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 }
1673 else
1674 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001675 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 }
Barry Mienydd671972010-10-04 16:33:58 +02001677
Pascal Kriete60f8c392010-08-25 18:03:28 +02001678 // Find the most likely culprit of the error by going through
1679 // the backtrace until the source file is no longer in the
1680 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001681 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001682 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001683 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001684 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001685 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001686 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1687 if (DIRECTORY_SEPARATOR !== '/')
1688 {
1689 $call['file'] = str_replace('\\', '/', $call['file']);
1690 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001691
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001692 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1693 {
1694 // Found it - use a relative path for safety
1695 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1696 $message[] = 'Line Number: '.$call['line'];
1697 break;
1698 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001699 }
1700 }
Barry Mienydd671972010-10-04 16:33:58 +02001701
Derek Jonese7792202010-03-02 17:24:46 -06001702 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001704 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 }
1706
1707 // --------------------------------------------------------------------
1708
1709 /**
1710 * Protect Identifiers
1711 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001712 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001713 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001715 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001716 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 *
1718 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1719 *
1720 * Or a query with aliasing:
1721 *
1722 * SELECT m.member_id, m.member_name FROM members AS m
1723 *
1724 * Since the column name can include up to four segments (host, DB, table, column)
1725 * or also have an alias prefix, we need to do a bit of work to figure this out and
1726 * insert the table prefix (if it exists) in the proper position, and escape only
1727 * the correct identifiers.
1728 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 * @param string
1730 * @param bool
1731 * @param mixed
1732 * @param bool
1733 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001734 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001735 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 {
1737 if ( ! is_bool($protect_identifiers))
1738 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001739 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 }
Derek Allarde37ab382009-02-03 16:13:57 +00001741
1742 if (is_array($item))
1743 {
1744 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001745 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001746 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001747 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001748 }
1749
1750 return $escaped_array;
1751 }
1752
Derek Allard911d3e02008-12-15 14:08:35 +00001753 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001754 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001755 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001756 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001757 //
1758 // Added exception for single quotes as well, we don't want to alter
1759 // literal strings. -- Narf
Andrey Andreev49884bc2015-06-29 11:52:34 +03001760 if (strcspn($item, "()'") !== strlen($item))
Derek Allard911d3e02008-12-15 14:08:35 +00001761 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001762 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001763 }
1764
Andrey Andreevbf940582012-06-10 07:05:05 +03001765 // Convert tabs or multiple spaces into single spaces
Andrey Andreevbe178a72015-08-20 13:23:21 +03001766 $item = preg_replace('/\s+/', ' ', trim($item));
Andrey Andreevbf940582012-06-10 07:05:05 +03001767
Andrey Andreevbf940582012-06-10 07:05:05 +03001768 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001769 // Note: strripos() is used in order to support spaces in table names
1770 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001771 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001772 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001773 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1774 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001775 $item = substr($item, 0, $offset);
1776 }
1777 elseif ($offset = strrpos($item, ' '))
1778 {
1779 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001780 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1781 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001782 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001783 }
1784 else
1785 {
1786 $alias = '';
1787 }
1788
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 // Break the string apart if it contains periods, then insert the table prefix
1790 // in the correct location, assuming the period doesn't indicate that we're dealing
1791 // with an alias. While we're at it, we will escape the components
1792 if (strpos($item, '.') !== FALSE)
1793 {
Andrey Andreeva12cf282015-08-07 17:37:05 +03001794 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001795
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001797 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 // we have nothing more to do other than escape the item
Andrey Andreeva12cf282015-08-07 17:37:05 +03001799 //
1800 // NOTE: The ! empty() condition prevents this method
1801 // from breaking when QB isn't enabled.
1802 if ( ! empty($this->qb_aliased_tables) && in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001803 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 if ($protect_identifiers === TRUE)
1805 {
1806 foreach ($parts as $key => $val)
1807 {
1808 if ( ! in_array($val, $this->_reserved_identifiers))
1809 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001810 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 }
1812 }
Barry Mienydd671972010-10-04 16:33:58 +02001813
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001815 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001816
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 return $item.$alias;
1818 }
Barry Mienydd671972010-10-04 16:33:58 +02001819
Andrey Andreev4c202602012-03-06 20:34:52 +02001820 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001821 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
1823 // We now add the table prefix based on some logic.
1824 // Do we have 4 segments (hostname.database.table.column)?
1825 // If so, we add the table prefix to the column name in the 3rd segment.
1826 if (isset($parts[3]))
1827 {
1828 $i = 2;
1829 }
1830 // Do we have 3 segments (database.table.column)?
1831 // If so, we add the table prefix to the column name in 2nd position
1832 elseif (isset($parts[2]))
1833 {
1834 $i = 1;
1835 }
1836 // Do we have 2 segments (table.column)?
1837 // If so, we add the table prefix to the column name in 1st segment
1838 else
1839 {
1840 $i = 0;
1841 }
Barry Mienydd671972010-10-04 16:33:58 +02001842
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 // This flag is set when the supplied $item does not contain a field name.
1844 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001845 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 {
1847 $i++;
1848 }
Barry Mienydd671972010-10-04 16:33:58 +02001849
Derek Jones55acc8b2009-07-11 03:38:13 +00001850 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001851 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001852 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001853 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001854 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001856 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 {
1858 $parts[$i] = $this->dbprefix.$parts[$i];
1859 }
Barry Mienydd671972010-10-04 16:33:58 +02001860
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 // Put the parts back together
1862 $item = implode('.', $parts);
1863 }
Barry Mienydd671972010-10-04 16:33:58 +02001864
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 if ($protect_identifiers === TRUE)
1866 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001867 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 }
Barry Mienydd671972010-10-04 16:33:58 +02001869
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 return $item.$alias;
1871 }
1872
Andrey Andreev4c202602012-03-06 20:34:52 +02001873 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001874 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001876 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001877 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001878 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001879 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001880 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001882 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 {
1884 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001885 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 }
1887
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001888 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001890 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 }
Barry Mienydd671972010-10-04 16:33:58 +02001892
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 return $item.$alias;
1894 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001895
Túbal Martín511f2252011-11-24 14:43:45 +01001896 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001897
Túbal Martín511f2252011-11-24 14:43:45 +01001898 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001899 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001900 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001901 *
Túbal Martín511f2252011-11-24 14:43:45 +01001902 * @return void
1903 */
1904 protected function _reset_select()
1905 {
Túbal Martín511f2252011-11-24 14:43:45 +01001906 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001907
1908}