blob: 09435696567877266b6551034db839b76998718d [file] [log] [blame]
Etki640ff4e2014-04-09 23:01:33 +04001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Database Driver Class
31 *
32 * This is the platform-independent base DB implementation class.
33 * This class will not be called directly. Rather, the adapter
34 * class for the specific database will extend and instantiate it.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
Timothy Warren7eeda532012-03-19 16:01:55 -040042abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000043
Andrey Andreevae85eb42012-11-02 01:42:31 +020044 /**
45 * Data Source Name / Connect string
46 *
47 * @var string
48 */
Andrey Andreev738f5342012-03-06 20:43:40 +020049 public $dsn;
Andrey Andreevae85eb42012-11-02 01:42:31 +020050
51 /**
52 * Username
53 *
54 * @var string
55 */
Andrey Andreevd1add432012-03-06 20:26:01 +020056 public $username;
Andrey Andreevae85eb42012-11-02 01:42:31 +020057
58 /**
59 * Password
60 *
61 * @var string
62 */
Andrey Andreevd1add432012-03-06 20:26:01 +020063 public $password;
Andrey Andreevae85eb42012-11-02 01:42:31 +020064
65 /**
66 * Hostname
67 *
68 * @var string
69 */
Andrey Andreevd1add432012-03-06 20:26:01 +020070 public $hostname;
Andrey Andreevae85eb42012-11-02 01:42:31 +020071
72 /**
73 * Database name
74 *
75 * @var string
76 */
Andrey Andreevd1add432012-03-06 20:26:01 +020077 public $database;
Andrey Andreevae85eb42012-11-02 01:42:31 +020078
79 /**
80 * Database driver
81 *
82 * @var string
83 */
Andrey Andreev75546112012-07-05 11:01:29 +030084 public $dbdriver = 'mysqli';
Andrey Andreevae85eb42012-11-02 01:42:31 +020085
86 /**
87 * Sub-driver
88 *
89 * @used-by CI_DB_pdo_driver
90 * @var string
91 */
Andrey Andreevfbba54e2012-06-23 20:26:31 +030092 public $subdriver;
Andrey Andreevae85eb42012-11-02 01:42:31 +020093
94 /**
95 * Table prefix
96 *
97 * @var string
98 */
Andrey Andreevd1add432012-03-06 20:26:01 +020099 public $dbprefix = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200100
101 /**
102 * Character set
103 *
104 * @var string
105 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200106 public $char_set = 'utf8';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200107
108 /**
109 * Collation
110 *
111 * @var string
112 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200113 public $dbcollat = 'utf8_general_ci';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200114
115 /**
116 * Auto-init flag
117 *
118 * Whether to automatically initialize the DB connection.
119 *
120 * @var bool
121 */
122 public $autoinit = TRUE;
123
124 /**
125 * Encryption flag/data
126 *
127 * @var mixed
128 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300129 public $encrypt = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200130
131 /**
132 * Swap Prefix
133 *
134 * @var string
135 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200136 public $swap_pre = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200137
138 /**
139 * Database port
140 *
141 * @var int
142 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200143 public $port = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200144
145 /**
146 * Persistent connection flag
147 *
148 * @var bool
149 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200150 public $pconnect = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200151
152 /**
153 * Connection ID
154 *
155 * @var object|resource
156 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200157 public $conn_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200158
159 /**
160 * Result ID
161 *
162 * @var object|resource
163 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200164 public $result_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200165
166 /**
167 * Debug flag
168 *
169 * Whether to display error messages.
170 *
171 * @var bool
172 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200173 public $db_debug = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200174
175 /**
176 * Benchmark time
177 *
178 * @var int
179 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200180 public $benchmark = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200181
182 /**
183 * Executed queries count
184 *
185 * @var int
186 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200187 public $query_count = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200188
189 /**
190 * Bind marker
191 *
192 * Character used to identify values in a prepared statement.
193 *
194 * @var string
195 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200196 public $bind_marker = '?';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200197
198 /**
199 * Save queries flag
200 *
201 * Whether to keep an in-memory history of queries for debugging purposes.
202 *
203 * @var bool
204 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200205 public $save_queries = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200206
207 /**
208 * Queries list
209 *
210 * @see CI_DB_driver::$save_queries
211 * @var string[]
212 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200213 public $queries = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200214
215 /**
216 * Query times
217 *
218 * A list of times that queries took to execute.
219 *
220 * @var array
221 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200222 public $query_times = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200223
224 /**
225 * Data cache
226 *
227 * An internal generic value cache.
228 *
229 * @var array
230 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200231 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000232
Andrey Andreevae85eb42012-11-02 01:42:31 +0200233 /**
234 * Transaction enabled flag
235 *
236 * @var bool
237 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200238 public $trans_enabled = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200239
240 /**
241 * Strict transaction mode flag
242 *
243 * @var bool
244 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200245 public $trans_strict = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200246
247 /**
248 * Transaction depth level
249 *
250 * @var int
251 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200252 protected $_trans_depth = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
Andrey Andreevae85eb42012-11-02 01:42:31 +0200254 /**
255 * Transaction status flag
256 *
257 * Used with transactions to determine if a rollback should occur.
258 *
259 * @var bool
260 */
261 protected $_trans_status = TRUE;
262
263 /**
Ahmedul Haque Abida2129772014-05-01 01:57:53 +0600264 * Transaction failure flag
265 *
266 * Used with transactions to determine if a transaction has failed.
267 *
268 * @var bool
269 */
270 protected $_trans_failure = FALSE;
271
272 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200273 * Cache On flag
274 *
275 * @var bool
276 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200277 public $cache_on = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200278
279 /**
280 * Cache directory path
281 *
282 * @var bool
283 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200284 public $cachedir = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200285
286 /**
287 * Cache auto-delete flag
288 *
289 * @var bool
290 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200291 public $cache_autodel = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200292
Andrey Andreevae85eb42012-11-02 01:42:31 +0200293 /**
294 * DB Cache object
295 *
296 * @see CI_DB_cache
297 * @var object
298 */
299 public $CACHE;
300
301 /**
302 * Protect identifiers flag
303 *
304 * @var bool
305 */
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +0000306 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +0200307
Andrey Andreevae85eb42012-11-02 01:42:31 +0200308 /**
309 * List of reserved identifiers
310 *
311 * Identifiers that must NOT be escaped.
312 *
313 * @var string[]
314 */
315 protected $_reserved_identifiers = array('*');
316
317 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200318 * Identifier escape character
319 *
320 * @var string
321 */
322 protected $_escape_char = '"';
323
324 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200325 * ESCAPE statement string
326 *
327 * @var string
328 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300329 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevae85eb42012-11-02 01:42:31 +0200330
331 /**
332 * ESCAPE character
333 *
334 * @var string
335 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300336 protected $_like_escape_chr = '!';
337
Andrey Andreev75546112012-07-05 11:01:29 +0300338 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200339 * ORDER BY random keyword
340 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200341 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200342 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200343 protected $_random_keyword = array('RAND()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200344
345 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200346 * COUNT string
347 *
348 * @used-by CI_DB_driver::count_all()
349 * @used-by CI_DB_query_builder::count_all_results()
350 *
351 * @var string
Andrey Andreev77a5d942012-07-05 15:42:14 +0300352 */
353 protected $_count_string = 'SELECT COUNT(*) AS ';
354
Andrey Andreevae85eb42012-11-02 01:42:31 +0200355 // --------------------------------------------------------------------
356
Andrey Andreev77a5d942012-07-05 15:42:14 +0300357 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200358 * Class constructor
Andrey Andreev75546112012-07-05 11:01:29 +0300359 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200360 * @param array $params
Andrey Andreev75546112012-07-05 11:01:29 +0300361 * @return void
362 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500363 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
365 if (is_array($params))
366 {
367 foreach ($params as $key => $val)
368 {
369 $this->$key = $val;
370 }
371 }
372
373 log_message('debug', 'Database Driver Class Initialized');
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Gints Murans89f77ee2012-05-23 00:23:50 +0300376 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 /**
379 * Initialize Database Settings
380 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200381 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200382 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200383 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200385 /* If an established connection is available, then there's
386 * no need to connect and select the database.
387 *
388 * Depending on the database driver, conn_id can be either
389 * boolean TRUE, a resource or an object.
390 */
391 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 return TRUE;
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // Connect to the database and set the connection ID
Andrey Andreev7e963512014-02-27 15:43:24 +0200399 $this->conn_id = $this->db_connect($this->pconnect);
Derek Allard2067d1a2008-11-13 22:59:24 +0000400
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200401 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 if ( ! $this->conn_id)
403 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100404 // Check if there is a failover set
405 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100407 // Go over all the failovers
408 foreach ($this->failover as $failover)
409 {
410 // Replace the current settings with those of the failover
411 foreach ($failover as $key => $val)
412 {
413 $this->$key = $val;
414 }
415
416 // Try to connect
Andrey Andreev7e963512014-02-27 15:43:24 +0200417 $this->conn_id = $this->db_connect($this->pconnect);
Felix Balfoort5d581b62011-11-29 15:53:01 +0100418
419 // If a connection is made break the foreach loop
420 if ($this->conn_id)
421 {
422 break;
423 }
424 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100426
427 // We still don't have a connection?
428 if ( ! $this->conn_id)
429 {
430 log_message('error', 'Unable to connect to the database');
431
432 if ($this->db_debug)
433 {
434 $this->display_error('db_unable_to_connect');
435 }
Andrey Andreev7e963512014-02-27 15:43:24 +0200436
Felix Balfoort5d581b62011-11-29 15:53:01 +0100437 return FALSE;
438 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200441 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200442 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // --------------------------------------------------------------------
446
447 /**
Andrey Andreev2e171022014-02-25 15:21:41 +0200448 * Persistent database connection
449 *
450 * @return resource
451 */
452 public function db_pconnect()
453 {
454 return $this->db_connect(TRUE);
455 }
456
457 // --------------------------------------------------------------------
458
459 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200460 * Reconnect
461 *
462 * Keep / reestablish the db connection if no queries have been
463 * sent for a length of time exceeding the server's idle timeout.
464 *
465 * This is just a dummy method to allow drivers without such
466 * functionality to not declare it, while others will override it.
467 *
468 * @return void
469 */
470 public function reconnect()
471 {
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300477 * Select database
478 *
479 * This is just a dummy method to allow drivers without such
480 * functionality to not declare it, while others will override it.
481 *
482 * @return bool
483 */
484 public function db_select()
485 {
486 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Set client character set
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200495 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200497 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200499 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200501 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if ($this->db_debug)
504 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200505 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 return FALSE;
509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 return TRUE;
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 /**
517 * The name of the platform in use (mysql, mssql, etc...)
518 *
Barry Mienydd671972010-10-04 16:33:58 +0200519 * @return string
520 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500521 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 return $this->dbdriver;
524 }
525
526 // --------------------------------------------------------------------
527
528 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200529 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200531 * Returns a string containing the version of the database being used.
532 * Most drivers will override this method.
533 *
Barry Mienydd671972010-10-04 16:33:58 +0200534 * @return string
535 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200536 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200538 if (isset($this->data_cache['version']))
539 {
540 return $this->data_cache['version'];
541 }
542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 if (FALSE === ($sql = $this->_version()))
544 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200545 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 }
Derek Allard3683f772009-12-16 17:32:33 +0000547
Andrey Andreev7e963512014-02-27 15:43:24 +0200548 $query = $this->query($sql)->row();
Andrey Andreev08856b82012-03-03 03:19:28 +0200549 return $this->data_cache['version'] = $query->ver;
550 }
Derek Allard3683f772009-12-16 17:32:33 +0000551
Andrey Andreev08856b82012-03-03 03:19:28 +0200552 // --------------------------------------------------------------------
553
554 /**
555 * Version number query string
556 *
557 * @return string
558 */
559 protected function _version()
560 {
561 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 // --------------------------------------------------------------------
565
566 /**
567 * Execute the query
568 *
569 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200570 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * upon successful execution of a "write" type query. Returns boolean
572 * FALSE upon failure, and if the $db_debug variable is set to TRUE
573 * will raise an error.
574 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300575 * @param string $sql
576 * @param array $binds = FALSE An array of binding data
577 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200578 * @return mixed
579 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300580 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100582 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200584 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200585 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300587 elseif ( ! is_bool($return_object))
588 {
589 $return_object = ! $this->is_write_type($sql);
590 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000591
592 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100593 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600594 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200595 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
Derek Jonese7792202010-03-02 17:24:46 -0600597
Ryan Dialef7474c2012-03-01 16:11:36 -0500598 // Compile binds if needed
599 if ($binds !== FALSE)
600 {
601 $sql = $this->compile_binds($sql, $binds);
602 }
603
Andrey Andreev4c202602012-03-06 20:34:52 +0200604 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // we will load the caching class and return the previously
606 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300607 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200609 $this->load_rdriver();
610 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200612 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 }
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Andrey Andreevb66664b2012-06-27 14:22:10 +0300616 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100617 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 $this->queries[] = $sql;
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200623 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 // Run the Query
626 if (FALSE === ($this->result_id = $this->simple_query($sql)))
627 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100628 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
630 $this->query_times[] = 0;
631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 // This will trigger a rollback if transactions are being used
634 $this->_trans_status = FALSE;
635
Andrey Andreev4be5de12012-03-02 15:45:41 +0200636 // Grab the error now, as we might run some additional queries before displaying the error
637 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200638
639 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300640 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 if ($this->db_debug)
643 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200645 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200646 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // transactions to remain in limbo.
Andrey Andreev6c854422013-10-21 13:58:15 +0300648 if ($this->_trans_depth !== 0)
649 {
650 do
651 {
652 $this->trans_complete();
653 }
654 while ($this->_trans_depth !== 0);
655 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000656
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200657 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200658 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 return FALSE;
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200665 $time_end = microtime(TRUE);
666 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000667
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100668 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
dixyab3cab52012-04-21 00:58:00 +0200670 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // Increment the query counter
674 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200675
Andrey Andreevb66664b2012-06-27 14:22:10 +0300676 // Will we have a result object instantiated? If not - we'll simply return TRUE
677 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300679 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100680 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
682 $this->CACHE->delete();
683 }
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 return TRUE;
686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Barry Mienydd671972010-10-04 16:33:58 +0200688 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200689 $driver = $this->load_rdriver();
690 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200691
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200692 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100694 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
696 // We'll create a new instance of the result object
697 // only without the platform specific driver since
698 // we can't use it with cached data (the query result
699 // resource ID won't be any good once we've cached the
700 // result object, so we'll have to compile the data
701 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200702 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 $CR->result_object = $RES->result_object();
704 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200705 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // Reset these since cached objects can not utilize resource IDs.
708 $CR->conn_id = NULL;
709 $CR->result_id = NULL;
710
711 $this->CACHE->write($sql, $CR);
712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 return $RES;
715 }
716
717 // --------------------------------------------------------------------
718
719 /**
720 * Load the result drivers
721 *
Barry Mienydd671972010-10-04 16:33:58 +0200722 * @return string the name of the result class
723 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500724 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
726 $driver = 'CI_DB_'.$this->dbdriver.'_result';
727
vlakofffadb8222013-05-12 10:57:09 +0200728 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200730 require_once(BASEPATH.'database/DB_result.php');
731 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 }
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 return $driver;
735 }
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 // --------------------------------------------------------------------
738
739 /**
740 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200741 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * we only use it when running transaction commands since they do
743 * not require all the features of the main query() function.
744 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200746 * @return mixed
747 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500748 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
750 if ( ! $this->conn_id)
751 {
752 $this->initialize();
753 }
754
755 return $this->_execute($sql);
756 }
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 // --------------------------------------------------------------------
759
760 /**
761 * Disable Transactions
762 * This permits transactions to be disabled at run-time.
763 *
Barry Mienydd671972010-10-04 16:33:58 +0200764 * @return void
765 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500766 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
768 $this->trans_enabled = FALSE;
769 }
770
771 // --------------------------------------------------------------------
772
773 /**
774 * Enable/disable Transaction Strict Mode
775 * When strict mode is enabled, if you are running multiple groups of
776 * transactions, if one group fails all groups will be rolled back.
777 * If strict mode is disabled, each group is treated autonomously, meaning
778 * a failure of one group will not affect any others
779 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300780 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200781 * @return void
782 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500783 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
785 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
786 }
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 // --------------------------------------------------------------------
789
790 /**
791 * Start Transaction
792 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300793 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200794 * @return void
795 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500796 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200797 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 if ( ! $this->trans_enabled)
799 {
800 return FALSE;
801 }
802
803 // When transactions are nested we only begin/commit/rollback the outermost ones
804 if ($this->_trans_depth > 0)
805 {
806 $this->_trans_depth += 1;
807 return;
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500811 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813
814 // --------------------------------------------------------------------
815
816 /**
817 * Complete Transaction
818 *
Barry Mienydd671972010-10-04 16:33:58 +0200819 * @return bool
820 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500821 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
823 if ( ! $this->trans_enabled)
824 {
825 return FALSE;
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 // When transactions are nested we only begin/commit/rollback the outermost ones
829 if ($this->_trans_depth > 1)
830 {
831 $this->_trans_depth -= 1;
832 return TRUE;
833 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500834 else
835 {
836 $this->_trans_depth = 0;
837 }
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900840 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
842 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 // If we are NOT running in strict mode, we will reset
845 // the _trans_status flag so that subsequent groups of transactions
846 // will be permitted.
847 if ($this->trans_strict === FALSE)
848 {
849 $this->_trans_status = TRUE;
850 }
851
852 log_message('debug', 'DB Transaction Failure');
853 return FALSE;
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 $this->trans_commit();
857 return TRUE;
858 }
859
860 // --------------------------------------------------------------------
861
862 /**
863 * Lets you retrieve the transaction flag to determine if it has failed
864 *
Barry Mienydd671972010-10-04 16:33:58 +0200865 * @return bool
866 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500867 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 {
869 return $this->_trans_status;
870 }
871
872 // --------------------------------------------------------------------
873
874 /**
875 * Compile Bindings
876 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 * @param string the sql statement
878 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200879 * @return string
880 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500881 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300883 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 {
885 return $sql;
886 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300887 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300889 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300890 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300892 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200893 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300894 // Make sure we're using numeric keys
895 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300896 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 }
898
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300899 // We'll need the marker length later
900 $ml = strlen($this->bind_marker);
901
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300902 // Make sure not to replace a chunk inside a string that happens to match the bind marker
903 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300905 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300906 str_replace($matches[0],
907 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
908 $sql, $c),
909 $matches, PREG_OFFSET_CAPTURE);
910
911 // Bind values' count must match the count of markers in the query
912 if ($bind_count !== $c)
913 {
914 return $sql;
915 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300916 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300917 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 +0300918 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300919 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
921
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300922 do
923 {
924 $c--;
925 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
926 }
927 while ($c !== 0);
928
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300929 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 }
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 // --------------------------------------------------------------------
933
934 /**
935 * Determines if a query is a "write" type.
936 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200938 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200939 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200940 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200942 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 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 // --------------------------------------------------------------------
946
947 /**
948 * Calculate the aggregate query elapsed time
949 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200950 * @param int The number of decimal places
951 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200952 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500953 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
955 return number_format($this->benchmark, $decimals);
956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 // --------------------------------------------------------------------
959
960 /**
961 * Returns the total number of queries
962 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200963 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200964 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500965 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
967 return $this->query_count;
968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 // --------------------------------------------------------------------
971
972 /**
973 * Returns the last query that was executed
974 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200975 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200976 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500977 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 {
979 return end($this->queries);
980 }
981
982 // --------------------------------------------------------------------
983
984 /**
985 * "Smart" Escape String
986 *
987 * Escapes data based on type
988 * Sets boolean and null types
989 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200991 * @return mixed
992 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500993 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200994 {
clawooa779c482014-10-18 14:47:04 +0300995 if (is_array($str))
996 {
997 $str = array_map(array(&$this, 'escape'), $str);
998 return '('.implode(',', $str).')';
999 }
1000 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001002 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001003 }
1004 elseif (is_bool($str))
1005 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001006 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001007 }
vlakoff1228fe22013-01-14 01:30:09 +01001008 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001009 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001010 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001011 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
1013 return $str;
1014 }
1015
1016 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001017
Derek Jonese4ed5832009-02-20 21:44:59 +00001018 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001019 * Escape String
1020 *
1021 * @param string $str
1022 * @param bool $like Whether or not the string will be used in a LIKE condition
1023 * @return string
1024 */
1025 public function escape_str($str, $like = FALSE)
1026 {
1027 if (is_array($str))
1028 {
1029 foreach ($str as $key => $val)
1030 {
1031 $str[$key] = $this->escape_str($val, $like);
1032 }
1033
1034 return $str;
1035 }
1036
1037 $str = $this->_escape_str($str);
1038
1039 // escape LIKE condition wildcards
1040 if ($like === TRUE)
1041 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001042 return str_replace(
1043 array($this->_like_escape_chr, '%', '_'),
1044 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1045 $str
1046 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001047 }
1048
1049 return $str;
1050 }
1051
1052 // --------------------------------------------------------------------
1053
1054 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001055 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001056 *
1057 * Calls the individual driver for platform
1058 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001059 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001060 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001061 * @return mixed
1062 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001063 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001064 {
1065 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001066 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001067
Derek Jonese4ed5832009-02-20 21:44:59 +00001068 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001071 * Platform-dependant string escape
1072 *
1073 * @param string
1074 * @return string
1075 */
1076 protected function _escape_str($str)
1077 {
1078 return str_replace("'", "''", remove_invisible_characters($str));
1079 }
1080
1081 // --------------------------------------------------------------------
1082
1083 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 * Primary
1085 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001086 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * position is the primary key
1088 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001090 * @return string
1091 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001092 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +02001093 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001095 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 }
1097
1098 // --------------------------------------------------------------------
1099
1100 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001101 * "Count All" query
1102 *
1103 * Generates a platform-specific query string that counts all records in
1104 * the specified database
1105 *
1106 * @param string
1107 * @return int
1108 */
1109 public function count_all($table = '')
1110 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001111 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001112 {
1113 return 0;
1114 }
1115
1116 $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 +01001117 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001118 {
1119 return 0;
1120 }
1121
1122 $query = $query->row();
1123 $this->_reset_select();
1124 return (int) $query->numrows;
1125 }
1126
1127 // --------------------------------------------------------------------
1128
1129 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 * Returns an array of table names
1131 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001132 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001133 * @return array
1134 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001135 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
1137 // Is there a cached result?
1138 if (isset($this->data_cache['table_names']))
1139 {
1140 return $this->data_cache['table_names'];
1141 }
Barry Mienydd671972010-10-04 16:33:58 +02001142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1144 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001145 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 }
1147
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001148 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001150
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001151 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001153 // Do we know from which column to get the table name?
1154 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001156 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001157 {
1158 $key = 'table_name';
1159 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001160 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001161 {
1162 $key = 'TABLE_NAME';
1163 }
1164 else
1165 {
1166 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001167 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001168 * E_STRICT is on, this would trigger a warning. So we'll have to
1169 * assign it first.
1170 */
1171 $key = array_keys($row);
1172 $key = array_shift($key);
1173 }
Taufan Aditya18209332012-02-09 16:07:27 +07001174 }
1175
Andrey Andreev12567e82012-01-25 22:50:33 +02001176 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 }
1178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 return $this->data_cache['table_names'];
1180 }
Barry Mienydd671972010-10-04 16:33:58 +02001181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 // --------------------------------------------------------------------
1183
1184 /**
1185 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001186 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001187 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001188 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001190 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001191 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001192 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
Barry Mienydd671972010-10-04 16:33:58 +02001194
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 // --------------------------------------------------------------------
1196
1197 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001198 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001201 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001203 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
1205 // Is there a cached result?
1206 if (isset($this->data_cache['field_names'][$table]))
1207 {
1208 return $this->data_cache['field_names'][$table];
1209 }
Barry Mienydd671972010-10-04 16:33:58 +02001210
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001211 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001213 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Greg Aker1edde302010-01-26 00:17:01 +00001216 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001218 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
Barry Mienydd671972010-10-04 16:33:58 +02001220
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001222 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001223
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001224 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001226 // Do we know from where to get the column's name?
1227 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001229 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001230 {
1231 $key = 'column_name';
1232 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001233 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001234 {
1235 $key = 'COLUMN_NAME';
1236 }
1237 else
1238 {
RJ garcia395e2df2013-03-21 10:48:24 -05001239 // We have no other choice but to just get the first element's key.
1240 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001241 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001243
1244 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 }
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 return $this->data_cache['field_names'][$table];
1248 }
1249
1250 // --------------------------------------------------------------------
1251
1252 /**
1253 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001254 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 * @param string
1256 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001257 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001259 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001260 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001261 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 }
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 // --------------------------------------------------------------------
1265
1266 /**
1267 * Returns an object with field data
1268 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001270 * @return object
1271 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001272 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001274 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001276 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 }
Barry Mienydd671972010-10-04 16:33:58 +02001278
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001279 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +02001281 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001282
1283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001286 * Escape the SQL Identifiers
1287 *
1288 * This function escapes column and table names
1289 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001290 * @param mixed
1291 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001292 */
1293 public function escape_identifiers($item)
1294 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001295 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001296 {
1297 return $item;
1298 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001299 elseif (is_array($item))
1300 {
1301 foreach ($item as $key => $value)
1302 {
1303 $item[$key] = $this->escape_identifiers($value);
1304 }
1305
1306 return $item;
1307 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001308 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001309 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001310 {
1311 return $item;
1312 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001313
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001314 static $preg_ec = array();
1315
1316 if (empty($preg_ec))
1317 {
1318 if (is_array($this->_escape_char))
1319 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001320 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001321 preg_quote($this->_escape_char[0], '/'),
1322 preg_quote($this->_escape_char[1], '/'),
1323 $this->_escape_char[0],
1324 $this->_escape_char[1]
1325 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001326 }
1327 else
1328 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001329 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001330 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001331 }
1332 }
1333
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001334 foreach ($this->_reserved_identifiers as $id)
1335 {
1336 if (strpos($item, '.'.$id) !== FALSE)
1337 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001338 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 +03001339 }
1340 }
1341
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001342 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 +03001343 }
1344
1345 // --------------------------------------------------------------------
1346
1347 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 * Generate an insert string
1349 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 * @param string the table upon which the query will be performed
1351 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001352 * @return string
1353 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001354 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001356 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001357
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001358 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001360 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 $values[] = $this->escape($val);
1362 }
Barry Mienydd671972010-10-04 16:33:58 +02001363
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001364 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001365 }
1366
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 // --------------------------------------------------------------------
1368
1369 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001370 * Insert statement
1371 *
1372 * Generates a platform-specific insert string from the supplied data
1373 *
1374 * @param string the table name
1375 * @param array the insert keys
1376 * @param array the insert values
1377 * @return string
1378 */
1379 protected function _insert($table, $keys, $values)
1380 {
1381 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1382 }
1383
1384 // --------------------------------------------------------------------
1385
1386 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 * Generate an update string
1388 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 * @param string the table upon which the query will be performed
1390 * @param array an associative array data of key/values
1391 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001392 * @return string
1393 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001394 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001396 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001398 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001401 $this->where($where);
1402
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001404 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001406 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 }
1408
Andrey Andreev79f888b2013-08-06 13:59:23 +03001409 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1410 $this->_reset_write();
1411 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001412 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001413
1414 // --------------------------------------------------------------------
1415
1416 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001417 * Update statement
1418 *
1419 * Generates a platform-specific update string from the supplied data
1420 *
1421 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001422 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001423 * @return string
1424 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001425 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001426 {
1427 foreach ($values as $key => $val)
1428 {
1429 $valstr[] = $key.' = '.$val;
1430 }
1431
Andrey Andreev75546112012-07-05 11:01:29 +03001432 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001433 .$this->_compile_wh('qb_where')
1434 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001435 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001436 }
1437
1438 // --------------------------------------------------------------------
1439
1440 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001441 * Tests whether the string has an SQL operator
1442 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 * @param string
1444 * @return bool
1445 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001446 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 {
Andrey Andreev2c6cdd72014-09-17 11:13:46 +03001448 return (bool) preg_match('/(<|>|!|=|\sIS\s|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 }
1450
1451 // --------------------------------------------------------------------
1452
1453 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001454 * Returns the SQL string operator
1455 *
1456 * @param string
1457 * @return string
1458 */
1459 protected function _get_operator($str)
1460 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001461 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001462
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001463 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001464 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001465 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001466 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001467 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001468 $_operators = array(
1469 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1470 '\s*<>?\s*', // <, <>
1471 '\s*>\s*', // >
Andrey Andreev2c6cdd72014-09-17 11:13:46 +03001472 '\s+IS(?:\sNOT)?(?:\sNULL)?', // IS[ NOT] NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001473 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001474 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001475 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1476 '\s+IN\s*\([^\)]+\)', // IN(list)
1477 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1478 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1479 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1480 );
1481
1482 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001483
Andrey Andreev6e704752012-07-18 00:46:33 +03001484 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1485 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001486 }
1487
1488 // --------------------------------------------------------------------
1489
1490 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1492 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001493 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001494 * @return mixed
1495 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001496 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001497 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001498 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001499
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 if (FALSE === strpos($driver, $function))
1501 {
1502 $function = $driver.$function;
1503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 if ( ! function_exists($function))
1506 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001507 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001509
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001510 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001511 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001512 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 }
1514
1515 // --------------------------------------------------------------------
1516
1517 /**
1518 * Set Cache Directory Path
1519 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 * @param string the path to the cache directory
1521 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001522 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001523 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 {
1525 $this->cachedir = $path;
1526 }
1527
1528 // --------------------------------------------------------------------
1529
1530 /**
1531 * Enable Query Caching
1532 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001533 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001534 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001535 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001537 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 }
1539
1540 // --------------------------------------------------------------------
1541
1542 /**
1543 * Disable Query Caching
1544 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001545 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001546 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001547 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001549 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 }
Barry Mienydd671972010-10-04 16:33:58 +02001551
Derek Allard2067d1a2008-11-13 22:59:24 +00001552 // --------------------------------------------------------------------
1553
1554 /**
1555 * Delete the cache files associated with a particular URI
1556 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001557 * @param string $segment_one = ''
1558 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001559 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001560 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001561 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001563 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001564 ? $this->CACHE->delete($segment_one, $segment_two)
1565 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 }
1567
1568 // --------------------------------------------------------------------
1569
1570 /**
1571 * Delete All cache files
1572 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001573 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001574 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001575 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001577 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001578 ? $this->CACHE->delete_all()
1579 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 }
1581
1582 // --------------------------------------------------------------------
1583
1584 /**
1585 * Initialize the Cache Class
1586 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001587 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001588 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001589 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001591 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001593 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001595 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001596 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001597 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001598 }
Derek Allarde37ab382009-02-03 16:13:57 +00001599
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1601 return TRUE;
1602 }
1603
1604 // --------------------------------------------------------------------
1605
1606 /**
1607 * Close DB Connection
1608 *
Barry Mienydd671972010-10-04 16:33:58 +02001609 * @return void
1610 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001611 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001613 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001614 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001615 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001616 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 }
Barry Mienydd671972010-10-04 16:33:58 +02001619
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 // --------------------------------------------------------------------
1621
1622 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001623 * Close DB Connection
1624 *
1625 * This method would be overriden by most of the drivers.
1626 *
1627 * @return void
1628 */
1629 protected function _close()
1630 {
1631 $this->conn_id = FALSE;
1632 }
1633
1634 // --------------------------------------------------------------------
1635
1636 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 * Display an error message
1638 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 * @param string the error message
1640 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001641 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001642 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001643 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001644 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 {
Derek Jonese7792202010-03-02 17:24:46 -06001646 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 $LANG->load('db');
1648
1649 $heading = $LANG->line('db_error_heading');
1650
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001651 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001653 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 }
1655 else
1656 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001657 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 }
Barry Mienydd671972010-10-04 16:33:58 +02001659
Pascal Kriete60f8c392010-08-25 18:03:28 +02001660 // Find the most likely culprit of the error by going through
1661 // the backtrace until the source file is no longer in the
1662 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001663 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001664 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001665 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001666 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001667 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001668 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1669 if (DIRECTORY_SEPARATOR !== '/')
1670 {
1671 $call['file'] = str_replace('\\', '/', $call['file']);
1672 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001673
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001674 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1675 {
1676 // Found it - use a relative path for safety
1677 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1678 $message[] = 'Line Number: '.$call['line'];
1679 break;
1680 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001681 }
1682 }
Barry Mienydd671972010-10-04 16:33:58 +02001683
Derek Jonese7792202010-03-02 17:24:46 -06001684 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001686 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 }
1688
1689 // --------------------------------------------------------------------
1690
1691 /**
1692 * Protect Identifiers
1693 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001694 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001695 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001697 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001698 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 *
1700 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1701 *
1702 * Or a query with aliasing:
1703 *
1704 * SELECT m.member_id, m.member_name FROM members AS m
1705 *
1706 * Since the column name can include up to four segments (host, DB, table, column)
1707 * or also have an alias prefix, we need to do a bit of work to figure this out and
1708 * insert the table prefix (if it exists) in the proper position, and escape only
1709 * the correct identifiers.
1710 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 * @param string
1712 * @param bool
1713 * @param mixed
1714 * @param bool
1715 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001716 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001717 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 {
1719 if ( ! is_bool($protect_identifiers))
1720 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001721 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 }
Derek Allarde37ab382009-02-03 16:13:57 +00001723
1724 if (is_array($item))
1725 {
1726 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001727 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001728 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001729 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001730 }
1731
1732 return $escaped_array;
1733 }
1734
Derek Allard911d3e02008-12-15 14:08:35 +00001735 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001736 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001737 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001738 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001739 //
1740 // Added exception for single quotes as well, we don't want to alter
1741 // literal strings. -- Narf
1742 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001743 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001744 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001745 }
1746
Andrey Andreevbf940582012-06-10 07:05:05 +03001747 // Convert tabs or multiple spaces into single spaces
1748 $item = preg_replace('/\s+/', ' ', $item);
1749
Andrey Andreevbf940582012-06-10 07:05:05 +03001750 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001751 // Note: strripos() is used in order to support spaces in table names
1752 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001753 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001754 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001755 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1756 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001757 $item = substr($item, 0, $offset);
1758 }
1759 elseif ($offset = strrpos($item, ' '))
1760 {
1761 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001762 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1763 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001764 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001765 }
1766 else
1767 {
1768 $alias = '';
1769 }
1770
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 // Break the string apart if it contains periods, then insert the table prefix
1772 // in the correct location, assuming the period doesn't indicate that we're dealing
1773 // with an alias. While we're at it, we will escape the components
1774 if (strpos($item, '.') !== FALSE)
1775 {
1776 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001777
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001779 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001781 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001782 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 if ($protect_identifiers === TRUE)
1784 {
1785 foreach ($parts as $key => $val)
1786 {
1787 if ( ! in_array($val, $this->_reserved_identifiers))
1788 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001789 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 }
1791 }
Barry Mienydd671972010-10-04 16:33:58 +02001792
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001794 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001795
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 return $item.$alias;
1797 }
Barry Mienydd671972010-10-04 16:33:58 +02001798
Andrey Andreev4c202602012-03-06 20:34:52 +02001799 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001800 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 {
1802 // We now add the table prefix based on some logic.
1803 // Do we have 4 segments (hostname.database.table.column)?
1804 // If so, we add the table prefix to the column name in the 3rd segment.
1805 if (isset($parts[3]))
1806 {
1807 $i = 2;
1808 }
1809 // Do we have 3 segments (database.table.column)?
1810 // If so, we add the table prefix to the column name in 2nd position
1811 elseif (isset($parts[2]))
1812 {
1813 $i = 1;
1814 }
1815 // Do we have 2 segments (table.column)?
1816 // If so, we add the table prefix to the column name in 1st segment
1817 else
1818 {
1819 $i = 0;
1820 }
Barry Mienydd671972010-10-04 16:33:58 +02001821
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 // This flag is set when the supplied $item does not contain a field name.
1823 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001824 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
1826 $i++;
1827 }
Barry Mienydd671972010-10-04 16:33:58 +02001828
Derek Jones55acc8b2009-07-11 03:38:13 +00001829 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001830 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001831 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001832 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001833 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001835 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 {
1837 $parts[$i] = $this->dbprefix.$parts[$i];
1838 }
Barry Mienydd671972010-10-04 16:33:58 +02001839
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 // Put the parts back together
1841 $item = implode('.', $parts);
1842 }
Barry Mienydd671972010-10-04 16:33:58 +02001843
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 if ($protect_identifiers === TRUE)
1845 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001846 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 }
Barry Mienydd671972010-10-04 16:33:58 +02001848
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 return $item.$alias;
1850 }
1851
Andrey Andreev4c202602012-03-06 20:34:52 +02001852 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001853 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001855 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001856 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001857 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001858 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001859 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001861 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001862 {
1863 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001864 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 }
1866
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001867 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001869 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 }
Barry Mienydd671972010-10-04 16:33:58 +02001871
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 return $item.$alias;
1873 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001874
Túbal Martín511f2252011-11-24 14:43:45 +01001875 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001876
Túbal Martín511f2252011-11-24 14:43:45 +01001877 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001878 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001879 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001880 *
Túbal Martín511f2252011-11-24 14:43:45 +01001881 * @return void
1882 */
1883 protected function _reset_select()
1884 {
Túbal Martín511f2252011-11-24 14:43:45 +01001885 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001886
1887}
1888
Derek Allard2067d1a2008-11-13 22:59:24 +00001889/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001890/* Location: ./system/database/DB_driver.php */