blob: 25e70ec3f2bd62d6003077d6d742bb9d5a30ed97 [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 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300671 $trans_depth = $this->_trans_depth;
Andrey Andreev6c854422013-10-21 13:58:15 +0300672 $this->trans_complete();
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300673 if ($trans_depth === $this->_trans_depth)
674 {
675 log_message('error', 'Database: Failure during an automated transaction commit/rollback!');
676 break;
677 }
Andrey Andreev6c854422013-10-21 13:58:15 +0300678 }
679 while ($this->_trans_depth !== 0);
680 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000681
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200682 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200683 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 return FALSE;
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200690 $time_end = microtime(TRUE);
691 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000692
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100693 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
dixyab3cab52012-04-21 00:58:00 +0200695 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 }
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 // Increment the query counter
699 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200700
Andrey Andreevb66664b2012-06-27 14:22:10 +0300701 // Will we have a result object instantiated? If not - we'll simply return TRUE
702 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300704 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100705 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
707 $this->CACHE->delete();
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 return TRUE;
711 }
Barry Mienydd671972010-10-04 16:33:58 +0200712
Barry Mienydd671972010-10-04 16:33:58 +0200713 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200714 $driver = $this->load_rdriver();
715 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200716
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200717 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100719 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
721 // We'll create a new instance of the result object
722 // only without the platform specific driver since
723 // we can't use it with cached data (the query result
724 // resource ID won't be any good once we've cached the
725 // result object, so we'll have to compile the data
726 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200727 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 $CR->result_object = $RES->result_object();
729 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200730 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // Reset these since cached objects can not utilize resource IDs.
733 $CR->conn_id = NULL;
734 $CR->result_id = NULL;
735
736 $this->CACHE->write($sql, $CR);
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 return $RES;
740 }
741
742 // --------------------------------------------------------------------
743
744 /**
745 * Load the result drivers
746 *
Barry Mienydd671972010-10-04 16:33:58 +0200747 * @return string the name of the result class
748 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500749 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
751 $driver = 'CI_DB_'.$this->dbdriver.'_result';
752
vlakofffadb8222013-05-12 10:57:09 +0200753 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200755 require_once(BASEPATH.'database/DB_result.php');
756 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 }
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 return $driver;
760 }
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 // --------------------------------------------------------------------
763
764 /**
765 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200766 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 * we only use it when running transaction commands since they do
768 * not require all the features of the main query() function.
769 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200771 * @return mixed
772 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500773 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
775 if ( ! $this->conn_id)
776 {
Andrey Andreev39105002015-11-09 14:05:02 +0200777 if ( ! $this->initialize())
778 {
779 return FALSE;
780 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 }
782
783 return $this->_execute($sql);
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 // --------------------------------------------------------------------
787
788 /**
789 * Disable Transactions
790 * This permits transactions to be disabled at run-time.
791 *
Barry Mienydd671972010-10-04 16:33:58 +0200792 * @return void
793 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500794 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
796 $this->trans_enabled = FALSE;
797 }
798
799 // --------------------------------------------------------------------
800
801 /**
802 * Enable/disable Transaction Strict Mode
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300803 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 * When strict mode is enabled, if you are running multiple groups of
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300805 * transactions, if one group fails all subsequent groups will be
806 * rolled back.
807 *
808 * If strict mode is disabled, each group is treated autonomously,
809 * meaning a failure of one group will not affect any others
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300811 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200812 * @return void
813 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500814 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
816 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 // --------------------------------------------------------------------
820
821 /**
822 * Start Transaction
823 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300824 * @param bool $test_mode = FALSE
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300825 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200826 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500827 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200828 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 if ( ! $this->trans_enabled)
830 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300831 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 }
833
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300834 return $this->trans_begin($test_mode);
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 }
836
837 // --------------------------------------------------------------------
838
839 /**
840 * Complete Transaction
841 *
Barry Mienydd671972010-10-04 16:33:58 +0200842 * @return bool
843 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500844 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 {
846 if ( ! $this->trans_enabled)
847 {
848 return FALSE;
849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900852 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // If we are NOT running in strict mode, we will reset
Andrey Andreevc08f27b2015-09-14 14:03:39 +0300857 // the _trans_status flag so that subsequent groups of
858 // transactions will be permitted.
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 if ($this->trans_strict === FALSE)
860 {
861 $this->_trans_status = TRUE;
862 }
863
864 log_message('debug', 'DB Transaction Failure');
865 return FALSE;
866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300868 return $this->trans_commit();
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
870
871 // --------------------------------------------------------------------
872
873 /**
874 * Lets you retrieve the transaction flag to determine if it has failed
875 *
Barry Mienydd671972010-10-04 16:33:58 +0200876 * @return bool
877 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500878 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
880 return $this->_trans_status;
881 }
882
883 // --------------------------------------------------------------------
884
885 /**
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300886 * Begin Transaction
887 *
888 * @param bool $test_mode
889 * @return bool
890 */
891 public function trans_begin($test_mode = FALSE)
892 {
893 if ( ! $this->trans_enabled)
894 {
895 return FALSE;
896 }
897 // When transactions are nested we only begin/commit/rollback the outermost ones
898 elseif ($this->_trans_depth > 0)
899 {
900 $this->_trans_depth++;
901 return TRUE;
902 }
903
904 // Reset the transaction failure flag.
905 // If the $test_mode flag is set to TRUE transactions will be rolled back
906 // even if the queries produce a successful result.
907 $this->_trans_failure = ($test_mode === TRUE);
908
909 if ($this->_trans_begin())
910 {
911 $this->_trans_depth++;
912 return TRUE;
913 }
914
915 return FALSE;
916 }
917
918 // --------------------------------------------------------------------
919
920 /**
921 * Commit Transaction
922 *
923 * @return bool
924 */
925 public function trans_commit()
926 {
927 if ( ! $this->trans_enabled OR $this->_trans_depth === 0)
928 {
929 return FALSE;
930 }
931 // When transactions are nested we only begin/commit/rollback the outermost ones
932 elseif ($this->_trans_depth > 1 OR $this->_trans_commit())
933 {
934 $this->_trans_depth--;
935 return TRUE;
936 }
937
938 return FALSE;
939 }
940
941 // --------------------------------------------------------------------
942
943 /**
944 * Rollback Transaction
945 *
946 * @return bool
947 */
948 public function trans_rollback()
949 {
950 if ( ! $this->trans_enabled OR $this->_trans_depth === 0)
951 {
952 return FALSE;
953 }
954 // When transactions are nested we only begin/commit/rollback the outermost ones
955 elseif ($this->_trans_depth > 1 OR $this->_trans_rollback())
956 {
957 $this->_trans_depth--;
958 return TRUE;
959 }
960
961 return FALSE;
962 }
963
964 // --------------------------------------------------------------------
965
966 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 * Compile Bindings
968 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 * @param string the sql statement
970 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200971 * @return string
972 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500973 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300975 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
977 return $sql;
978 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300979 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300981 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300982 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300984 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200985 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300986 // Make sure we're using numeric keys
987 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300988 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
990
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300991 // We'll need the marker length later
992 $ml = strlen($this->bind_marker);
993
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300994 // Make sure not to replace a chunk inside a string that happens to match the bind marker
995 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300997 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300998 str_replace($matches[0],
999 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
1000 $sql, $c),
1001 $matches, PREG_OFFSET_CAPTURE);
1002
1003 // Bind values' count must match the count of markers in the query
1004 if ($bind_count !== $c)
1005 {
1006 return $sql;
1007 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +03001008 }
Andrey Andreeve4742582012-10-25 13:25:13 +03001009 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 +03001010 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +03001011 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 }
1013
Andrey Andreevaf915ce2012-06-13 19:03:06 +03001014 do
1015 {
1016 $c--;
clawoo4a4f5502014-10-20 15:28:08 +03001017 $escaped_value = $this->escape($binds[$c]);
1018 if (is_array($escaped_value))
1019 {
1020 $escaped_value = '('.implode(',', $escaped_value).')';
1021 }
1022 $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
Andrey Andreevaf915ce2012-06-13 19:03:06 +03001023 }
1024 while ($c !== 0);
1025
Andrey Andreev4e9538f2012-06-12 14:16:53 +03001026 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 }
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 // --------------------------------------------------------------------
1030
1031 /**
1032 * Determines if a query is a "write" type.
1033 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +02001035 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001036 */
Andrey Andreev67f71a42012-03-01 16:18:42 +02001037 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +02001039 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 +00001040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 // --------------------------------------------------------------------
1043
1044 /**
1045 * Calculate the aggregate query elapsed time
1046 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001047 * @param int The number of decimal places
James L Parry78301732014-12-16 02:14:52 -08001048 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001049 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001050 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
1052 return number_format($this->benchmark, $decimals);
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 // --------------------------------------------------------------------
1056
1057 /**
1058 * Returns the total number of queries
1059 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001060 * @return int
Barry Mienydd671972010-10-04 16:33:58 +02001061 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001062 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 {
1064 return $this->query_count;
1065 }
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 // --------------------------------------------------------------------
1068
1069 /**
1070 * Returns the last query that was executed
1071 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001072 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001073 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001074 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
1076 return end($this->queries);
1077 }
1078
1079 // --------------------------------------------------------------------
1080
1081 /**
1082 * "Smart" Escape String
1083 *
1084 * Escapes data based on type
1085 * Sets boolean and null types
1086 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * @param string
Barry Mienydd671972010-10-04 16:33:58 +02001088 * @return mixed
1089 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001090 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +02001091 {
clawooa779c482014-10-18 14:47:04 +03001092 if (is_array($str))
1093 {
1094 $str = array_map(array(&$this, 'escape'), $str);
clawoo4a4f5502014-10-20 15:28:08 +03001095 return $str;
clawooa779c482014-10-18 14:47:04 +03001096 }
1097 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001099 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001100 }
1101 elseif (is_bool($str))
1102 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001103 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001104 }
vlakoff1228fe22013-01-14 01:30:09 +01001105 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001106 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001107 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001108 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001109
1110 return $str;
1111 }
1112
1113 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001114
Derek Jonese4ed5832009-02-20 21:44:59 +00001115 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001116 * Escape String
1117 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001118 * @param string|string[] $str Input string
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001119 * @param bool $like Whether or not the string will be used in a LIKE condition
1120 * @return string
1121 */
1122 public function escape_str($str, $like = FALSE)
1123 {
1124 if (is_array($str))
1125 {
1126 foreach ($str as $key => $val)
1127 {
1128 $str[$key] = $this->escape_str($val, $like);
1129 }
1130
1131 return $str;
1132 }
1133
1134 $str = $this->_escape_str($str);
1135
1136 // escape LIKE condition wildcards
1137 if ($like === TRUE)
1138 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001139 return str_replace(
1140 array($this->_like_escape_chr, '%', '_'),
1141 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1142 $str
1143 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001144 }
1145
1146 return $str;
1147 }
1148
1149 // --------------------------------------------------------------------
1150
1151 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001152 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001153 *
1154 * Calls the individual driver for platform
1155 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001156 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001157 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001158 * @return mixed
1159 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001160 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001161 {
1162 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001163 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001164
Derek Jonese4ed5832009-02-20 21:44:59 +00001165 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001168 * Platform-dependant string escape
1169 *
1170 * @param string
1171 * @return string
1172 */
1173 protected function _escape_str($str)
1174 {
1175 return str_replace("'", "''", remove_invisible_characters($str));
1176 }
1177
1178 // --------------------------------------------------------------------
1179
1180 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 * Primary
1182 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001183 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 * position is the primary key
1185 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001186 * @param string $table Table name
1187 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001188 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001189 public function primary($table)
Barry Mienydd671972010-10-04 16:33:58 +02001190 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001192 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
1194
1195 // --------------------------------------------------------------------
1196
1197 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001198 * "Count All" query
1199 *
1200 * Generates a platform-specific query string that counts all records in
1201 * the specified database
1202 *
1203 * @param string
1204 * @return int
1205 */
1206 public function count_all($table = '')
1207 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001208 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001209 {
1210 return 0;
1211 }
1212
1213 $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 +01001214 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001215 {
1216 return 0;
1217 }
1218
1219 $query = $query->row();
1220 $this->_reset_select();
1221 return (int) $query->numrows;
1222 }
1223
1224 // --------------------------------------------------------------------
1225
1226 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 * Returns an array of table names
1228 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001229 * @param string $constrain_by_prefix = FALSE
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001230 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001231 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001232 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
1234 // Is there a cached result?
1235 if (isset($this->data_cache['table_names']))
1236 {
1237 return $this->data_cache['table_names'];
1238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1241 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001242 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 }
1244
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001245 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001247
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001248 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001250 // Do we know from which column to get the table name?
1251 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001253 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001254 {
1255 $key = 'table_name';
1256 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001257 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001258 {
1259 $key = 'TABLE_NAME';
1260 }
1261 else
1262 {
1263 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001264 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001265 * E_STRICT is on, this would trigger a warning. So we'll have to
1266 * assign it first.
1267 */
1268 $key = array_keys($row);
1269 $key = array_shift($key);
1270 }
Taufan Aditya18209332012-02-09 16:07:27 +07001271 }
1272
Andrey Andreev12567e82012-01-25 22:50:33 +02001273 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 }
1275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 return $this->data_cache['table_names'];
1277 }
Barry Mienydd671972010-10-04 16:33:58 +02001278
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 // --------------------------------------------------------------------
1280
1281 /**
1282 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001283 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001284 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001285 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001287 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001288 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001289 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 }
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 // --------------------------------------------------------------------
1293
1294 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001295 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 *
Andrey Andreev0f850902015-04-29 12:33:11 +03001297 * @param string $table Table name
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001298 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 */
Andrey Andreev358b0882015-01-05 21:00:18 +02001300 public function list_fields($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 {
1302 // Is there a cached result?
1303 if (isset($this->data_cache['field_names'][$table]))
1304 {
1305 return $this->data_cache['field_names'][$table];
1306 }
Barry Mienydd671972010-10-04 16:33:58 +02001307
Greg Aker1edde302010-01-26 00:17:01 +00001308 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001310 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001314 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001315
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001316 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001318 // Do we know from where to get the column's name?
1319 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001321 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001322 {
1323 $key = 'column_name';
1324 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001325 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001326 {
1327 $key = 'COLUMN_NAME';
1328 }
1329 else
1330 {
RJ garcia395e2df2013-03-21 10:48:24 -05001331 // We have no other choice but to just get the first element's key.
1332 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001333 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001335
1336 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 }
Barry Mienydd671972010-10-04 16:33:58 +02001338
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 return $this->data_cache['field_names'][$table];
1340 }
1341
1342 // --------------------------------------------------------------------
1343
1344 /**
1345 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001346 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 * @param string
1348 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001349 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001351 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001352 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001353 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 // --------------------------------------------------------------------
1357
1358 /**
1359 * Returns an object with field data
1360 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001361 * @param string $table the table name
1362 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001363 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001364 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001366 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001367 return ($query) ? $query->field_data() : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001368 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001369
1370 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001371
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001373 * Escape the SQL Identifiers
1374 *
1375 * This function escapes column and table names
1376 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001377 * @param mixed
1378 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001379 */
1380 public function escape_identifiers($item)
1381 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001382 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001383 {
1384 return $item;
1385 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001386 elseif (is_array($item))
1387 {
1388 foreach ($item as $key => $value)
1389 {
1390 $item[$key] = $this->escape_identifiers($value);
1391 }
1392
1393 return $item;
1394 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001395 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001396 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001397 {
1398 return $item;
1399 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001400
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001401 static $preg_ec = array();
1402
1403 if (empty($preg_ec))
1404 {
1405 if (is_array($this->_escape_char))
1406 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001407 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001408 preg_quote($this->_escape_char[0], '/'),
1409 preg_quote($this->_escape_char[1], '/'),
1410 $this->_escape_char[0],
1411 $this->_escape_char[1]
1412 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001413 }
1414 else
1415 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001416 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001417 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001418 }
1419 }
1420
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001421 foreach ($this->_reserved_identifiers as $id)
1422 {
1423 if (strpos($item, '.'.$id) !== FALSE)
1424 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001425 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 +03001426 }
1427 }
1428
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001429 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 +03001430 }
1431
1432 // --------------------------------------------------------------------
1433
1434 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 * Generate an insert string
1436 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 * @param string the table upon which the query will be performed
1438 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001439 * @return string
1440 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001441 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001443 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001444
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001445 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001447 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 $values[] = $this->escape($val);
1449 }
Barry Mienydd671972010-10-04 16:33:58 +02001450
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001451 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001452 }
1453
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 // --------------------------------------------------------------------
1455
1456 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001457 * Insert statement
1458 *
1459 * Generates a platform-specific insert string from the supplied data
1460 *
1461 * @param string the table name
1462 * @param array the insert keys
1463 * @param array the insert values
1464 * @return string
1465 */
1466 protected function _insert($table, $keys, $values)
1467 {
1468 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1469 }
1470
1471 // --------------------------------------------------------------------
1472
1473 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 * Generate an update string
1475 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 * @param string the table upon which the query will be performed
1477 * @param array an associative array data of key/values
1478 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001479 * @return string
1480 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001481 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001483 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001485 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 }
Barry Mienydd671972010-10-04 16:33:58 +02001487
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001488 $this->where($where);
1489
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001491 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001493 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 }
1495
Andrey Andreev79f888b2013-08-06 13:59:23 +03001496 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1497 $this->_reset_write();
1498 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001499 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001500
1501 // --------------------------------------------------------------------
1502
1503 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001504 * Update statement
1505 *
1506 * Generates a platform-specific update string from the supplied data
1507 *
1508 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001509 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001510 * @return string
1511 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001512 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001513 {
1514 foreach ($values as $key => $val)
1515 {
1516 $valstr[] = $key.' = '.$val;
1517 }
1518
Andrey Andreev75546112012-07-05 11:01:29 +03001519 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001520 .$this->_compile_wh('qb_where')
1521 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001522 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001523 }
1524
1525 // --------------------------------------------------------------------
1526
1527 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 * Tests whether the string has an SQL operator
1529 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 * @param string
1531 * @return bool
1532 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001533 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 {
Andrey Andreev5078eb52014-12-02 01:11:54 +02001535 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 +00001536 }
1537
1538 // --------------------------------------------------------------------
1539
1540 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001541 * Returns the SQL string operator
1542 *
1543 * @param string
1544 * @return string
1545 */
1546 protected function _get_operator($str)
1547 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001548 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001549
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001550 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001551 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001552 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001553 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001554 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001555 $_operators = array(
Andrey Andreev348a2d42015-08-31 17:39:04 +03001556 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1557 '\s*<>?\s*', // <, <>
1558 '\s*>\s*', // >
1559 '\s+IS NULL', // IS NULL
1560 '\s+IS NOT NULL', // IS NOT NULL
1561 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
1562 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreev0d0c53c2015-09-03 11:23:44 +03001563 '\s+BETWEEN\s+', // BETWEEN value AND value
Andrey Andreev348a2d42015-08-31 17:39:04 +03001564 '\s+IN\s*\([^\)]+\)', // IN(list)
1565 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1566 '\s+LIKE\s+\S.*('.$_les.')?', // LIKE 'expr'[ ESCAPE '%s']
1567 '\s+NOT LIKE\s+\S.*('.$_les.')?' // NOT LIKE 'expr'[ ESCAPE '%s']
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001568 );
1569
1570 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001571
Andrey Andreev6e704752012-07-18 00:46:33 +03001572 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1573 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001574 }
1575
1576 // --------------------------------------------------------------------
1577
1578 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001579 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1580 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001581 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001582 * @return mixed
1583 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001584 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001586 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001587
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 if (FALSE === strpos($driver, $function))
1589 {
1590 $function = $driver.$function;
1591 }
Barry Mienydd671972010-10-04 16:33:58 +02001592
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 if ( ! function_exists($function))
1594 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001595 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001597
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001598 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001599 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001600 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 }
1602
1603 // --------------------------------------------------------------------
1604
1605 /**
1606 * Set Cache Directory Path
1607 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 * @param string the path to the cache directory
1609 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001610 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001611 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 {
1613 $this->cachedir = $path;
1614 }
1615
1616 // --------------------------------------------------------------------
1617
1618 /**
1619 * Enable Query Caching
1620 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001621 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001622 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001623 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001625 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 }
1627
1628 // --------------------------------------------------------------------
1629
1630 /**
1631 * Disable Query Caching
1632 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001633 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001634 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001635 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001637 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 }
Barry Mienydd671972010-10-04 16:33:58 +02001639
Derek Allard2067d1a2008-11-13 22:59:24 +00001640 // --------------------------------------------------------------------
1641
1642 /**
1643 * Delete the cache files associated with a particular URI
1644 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001645 * @param string $segment_one = ''
1646 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001647 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001648 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001649 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001651 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001652 ? $this->CACHE->delete($segment_one, $segment_two)
1653 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 }
1655
1656 // --------------------------------------------------------------------
1657
1658 /**
1659 * Delete All cache files
1660 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001661 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001662 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001663 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001665 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001666 ? $this->CACHE->delete_all()
1667 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 }
1669
1670 // --------------------------------------------------------------------
1671
1672 /**
1673 * Initialize the Cache Class
1674 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001675 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001676 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001677 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001679 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001681 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001683 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001684 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001685 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001686 }
Derek Allarde37ab382009-02-03 16:13:57 +00001687
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1689 return TRUE;
1690 }
1691
1692 // --------------------------------------------------------------------
1693
1694 /**
1695 * Close DB Connection
1696 *
Barry Mienydd671972010-10-04 16:33:58 +02001697 * @return void
1698 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001699 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001701 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001703 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001704 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 }
Barry Mienydd671972010-10-04 16:33:58 +02001707
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 // --------------------------------------------------------------------
1709
1710 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001711 * Close DB Connection
1712 *
Claudio Galdiolob83a3d22015-01-29 11:42:50 -05001713 * This method would be overridden by most of the drivers.
Andrey Andreev79922c02012-05-23 12:27:17 +03001714 *
1715 * @return void
1716 */
1717 protected function _close()
1718 {
1719 $this->conn_id = FALSE;
1720 }
1721
1722 // --------------------------------------------------------------------
1723
1724 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 * Display an error message
1726 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 * @param string the error message
1728 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001729 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001730 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001731 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001732 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
Derek Jonese7792202010-03-02 17:24:46 -06001734 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 $LANG->load('db');
1736
1737 $heading = $LANG->line('db_error_heading');
1738
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001739 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001741 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 }
1743 else
1744 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001745 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 }
Barry Mienydd671972010-10-04 16:33:58 +02001747
Pascal Kriete60f8c392010-08-25 18:03:28 +02001748 // Find the most likely culprit of the error by going through
1749 // the backtrace until the source file is no longer in the
1750 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001751 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001752 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001753 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001754 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001755 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001756 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1757 if (DIRECTORY_SEPARATOR !== '/')
1758 {
1759 $call['file'] = str_replace('\\', '/', $call['file']);
1760 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001761
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001762 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1763 {
1764 // Found it - use a relative path for safety
1765 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1766 $message[] = 'Line Number: '.$call['line'];
1767 break;
1768 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001769 }
1770 }
Barry Mienydd671972010-10-04 16:33:58 +02001771
Derek Jonese7792202010-03-02 17:24:46 -06001772 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001774 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 }
1776
1777 // --------------------------------------------------------------------
1778
1779 /**
1780 * Protect Identifiers
1781 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001782 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001783 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001785 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001786 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 *
1788 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1789 *
1790 * Or a query with aliasing:
1791 *
1792 * SELECT m.member_id, m.member_name FROM members AS m
1793 *
1794 * Since the column name can include up to four segments (host, DB, table, column)
1795 * or also have an alias prefix, we need to do a bit of work to figure this out and
1796 * insert the table prefix (if it exists) in the proper position, and escape only
1797 * the correct identifiers.
1798 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 * @param string
1800 * @param bool
1801 * @param mixed
1802 * @param bool
1803 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001804 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001805 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 {
1807 if ( ! is_bool($protect_identifiers))
1808 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001809 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 }
Derek Allarde37ab382009-02-03 16:13:57 +00001811
1812 if (is_array($item))
1813 {
1814 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001815 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001816 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001817 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001818 }
1819
1820 return $escaped_array;
1821 }
1822
Derek Allard911d3e02008-12-15 14:08:35 +00001823 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001824 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001825 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001826 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001827 //
1828 // Added exception for single quotes as well, we don't want to alter
1829 // literal strings. -- Narf
Andrey Andreev49884bc2015-06-29 11:52:34 +03001830 if (strcspn($item, "()'") !== strlen($item))
Derek Allard911d3e02008-12-15 14:08:35 +00001831 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001832 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001833 }
1834
Andrey Andreevbf940582012-06-10 07:05:05 +03001835 // Convert tabs or multiple spaces into single spaces
Andrey Andreevbe178a72015-08-20 13:23:21 +03001836 $item = preg_replace('/\s+/', ' ', trim($item));
Andrey Andreevbf940582012-06-10 07:05:05 +03001837
Andrey Andreevbf940582012-06-10 07:05:05 +03001838 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001839 // Note: strripos() is used in order to support spaces in table names
1840 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001841 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001842 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001843 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1844 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001845 $item = substr($item, 0, $offset);
1846 }
1847 elseif ($offset = strrpos($item, ' '))
1848 {
1849 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001850 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1851 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001852 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001853 }
1854 else
1855 {
1856 $alias = '';
1857 }
1858
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 // Break the string apart if it contains periods, then insert the table prefix
1860 // in the correct location, assuming the period doesn't indicate that we're dealing
1861 // with an alias. While we're at it, we will escape the components
1862 if (strpos($item, '.') !== FALSE)
1863 {
Andrey Andreeva12cf282015-08-07 17:37:05 +03001864 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001865
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001867 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 // we have nothing more to do other than escape the item
Andrey Andreeva12cf282015-08-07 17:37:05 +03001869 //
1870 // NOTE: The ! empty() condition prevents this method
1871 // from breaking when QB isn't enabled.
1872 if ( ! empty($this->qb_aliased_tables) && in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001873 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001874 if ($protect_identifiers === TRUE)
1875 {
1876 foreach ($parts as $key => $val)
1877 {
1878 if ( ! in_array($val, $this->_reserved_identifiers))
1879 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001880 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 }
1882 }
Barry Mienydd671972010-10-04 16:33:58 +02001883
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001885 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001886
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 return $item.$alias;
1888 }
Barry Mienydd671972010-10-04 16:33:58 +02001889
Andrey Andreev4c202602012-03-06 20:34:52 +02001890 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001891 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 {
1893 // We now add the table prefix based on some logic.
1894 // Do we have 4 segments (hostname.database.table.column)?
1895 // If so, we add the table prefix to the column name in the 3rd segment.
1896 if (isset($parts[3]))
1897 {
1898 $i = 2;
1899 }
1900 // Do we have 3 segments (database.table.column)?
1901 // If so, we add the table prefix to the column name in 2nd position
1902 elseif (isset($parts[2]))
1903 {
1904 $i = 1;
1905 }
1906 // Do we have 2 segments (table.column)?
1907 // If so, we add the table prefix to the column name in 1st segment
1908 else
1909 {
1910 $i = 0;
1911 }
Barry Mienydd671972010-10-04 16:33:58 +02001912
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 // This flag is set when the supplied $item does not contain a field name.
1914 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001915 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001916 {
1917 $i++;
1918 }
Barry Mienydd671972010-10-04 16:33:58 +02001919
Derek Jones55acc8b2009-07-11 03:38:13 +00001920 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001921 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001922 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001923 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001924 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001926 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 {
1928 $parts[$i] = $this->dbprefix.$parts[$i];
1929 }
Barry Mienydd671972010-10-04 16:33:58 +02001930
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 // Put the parts back together
1932 $item = implode('.', $parts);
1933 }
Barry Mienydd671972010-10-04 16:33:58 +02001934
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 if ($protect_identifiers === TRUE)
1936 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001937 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 }
Barry Mienydd671972010-10-04 16:33:58 +02001939
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 return $item.$alias;
1941 }
1942
Andrey Andreev4c202602012-03-06 20:34:52 +02001943 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001944 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001946 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001947 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001948 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001949 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001950 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001952 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 {
1954 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001955 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 }
1957
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001958 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001960 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 }
Barry Mienydd671972010-10-04 16:33:58 +02001962
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 return $item.$alias;
1964 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001965
Túbal Martín511f2252011-11-24 14:43:45 +01001966 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001967
Túbal Martín511f2252011-11-24 14:43:45 +01001968 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001969 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001970 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001971 *
Túbal Martín511f2252011-11-24 14:43:45 +01001972 * @return void
1973 */
1974 protected function _reset_select()
1975 {
Túbal Martín511f2252011-11-24 14:43:45 +01001976 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001977
1978}