blob: 12ab5bb2adacea75abfedc6d3bf5ae30973b29dd [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 {
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200995 if (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200997 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000998 }
999 elseif (is_bool($str))
1000 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001001 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001002 }
vlakoff1228fe22013-01-14 01:30:09 +01001003 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001004 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001005 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001006 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001007
1008 return $str;
1009 }
1010
1011 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001012
Derek Jonese4ed5832009-02-20 21:44:59 +00001013 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001014 * Escape String
1015 *
1016 * @param string $str
1017 * @param bool $like Whether or not the string will be used in a LIKE condition
1018 * @return string
1019 */
1020 public function escape_str($str, $like = FALSE)
1021 {
1022 if (is_array($str))
1023 {
1024 foreach ($str as $key => $val)
1025 {
1026 $str[$key] = $this->escape_str($val, $like);
1027 }
1028
1029 return $str;
1030 }
1031
1032 $str = $this->_escape_str($str);
1033
1034 // escape LIKE condition wildcards
1035 if ($like === TRUE)
1036 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001037 return str_replace(
1038 array($this->_like_escape_chr, '%', '_'),
1039 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1040 $str
1041 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001042 }
1043
1044 return $str;
1045 }
1046
1047 // --------------------------------------------------------------------
1048
1049 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001050 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001051 *
1052 * Calls the individual driver for platform
1053 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001054 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001055 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001056 * @return mixed
1057 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001058 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001059 {
1060 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001062
Derek Jonese4ed5832009-02-20 21:44:59 +00001063 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001066 * Platform-dependant string escape
1067 *
1068 * @param string
1069 * @return string
1070 */
1071 protected function _escape_str($str)
1072 {
1073 return str_replace("'", "''", remove_invisible_characters($str));
1074 }
1075
1076 // --------------------------------------------------------------------
1077
1078 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * Primary
1080 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001081 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 * position is the primary key
1083 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001085 * @return string
1086 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001087 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +02001088 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001090 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 }
1092
1093 // --------------------------------------------------------------------
1094
1095 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001096 * "Count All" query
1097 *
1098 * Generates a platform-specific query string that counts all records in
1099 * the specified database
1100 *
1101 * @param string
1102 * @return int
1103 */
1104 public function count_all($table = '')
1105 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001106 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001107 {
1108 return 0;
1109 }
1110
1111 $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 +01001112 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001113 {
1114 return 0;
1115 }
1116
1117 $query = $query->row();
1118 $this->_reset_select();
1119 return (int) $query->numrows;
1120 }
1121
1122 // --------------------------------------------------------------------
1123
1124 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 * Returns an array of table names
1126 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001127 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001128 * @return array
1129 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001130 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
1132 // Is there a cached result?
1133 if (isset($this->data_cache['table_names']))
1134 {
1135 return $this->data_cache['table_names'];
1136 }
Barry Mienydd671972010-10-04 16:33:58 +02001137
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1139 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001140 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
1142
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001143 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001145
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001146 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001148 // Do we know from which column to get the table name?
1149 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001151 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001152 {
1153 $key = 'table_name';
1154 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001155 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001156 {
1157 $key = 'TABLE_NAME';
1158 }
1159 else
1160 {
1161 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001162 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001163 * E_STRICT is on, this would trigger a warning. So we'll have to
1164 * assign it first.
1165 */
1166 $key = array_keys($row);
1167 $key = array_shift($key);
1168 }
Taufan Aditya18209332012-02-09 16:07:27 +07001169 }
1170
Andrey Andreev12567e82012-01-25 22:50:33 +02001171 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 }
1173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 return $this->data_cache['table_names'];
1175 }
Barry Mienydd671972010-10-04 16:33:58 +02001176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 // --------------------------------------------------------------------
1178
1179 /**
1180 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001181 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001182 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001183 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001185 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001186 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001187 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 }
Barry Mienydd671972010-10-04 16:33:58 +02001189
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 // --------------------------------------------------------------------
1191
1192 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001193 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001196 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001198 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
1200 // Is there a cached result?
1201 if (isset($this->data_cache['field_names'][$table]))
1202 {
1203 return $this->data_cache['field_names'][$table];
1204 }
Barry Mienydd671972010-10-04 16:33:58 +02001205
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001206 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001208 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 }
Barry Mienydd671972010-10-04 16:33:58 +02001210
Greg Aker1edde302010-01-26 00:17:01 +00001211 if (FALSE === ($sql = $this->_list_columns($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_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001217 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001218
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001219 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001221 // Do we know from where to get the column's name?
1222 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001224 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001225 {
1226 $key = 'column_name';
1227 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001228 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001229 {
1230 $key = 'COLUMN_NAME';
1231 }
1232 else
1233 {
RJ garcia395e2df2013-03-21 10:48:24 -05001234 // We have no other choice but to just get the first element's key.
1235 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001236 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001238
1239 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 }
Barry Mienydd671972010-10-04 16:33:58 +02001241
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 return $this->data_cache['field_names'][$table];
1243 }
1244
1245 // --------------------------------------------------------------------
1246
1247 /**
1248 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001249 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 * @param string
1251 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001252 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001254 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001255 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001256 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // --------------------------------------------------------------------
1260
1261 /**
1262 * Returns an object with field data
1263 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001265 * @return object
1266 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001267 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001269 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001271 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 }
Barry Mienydd671972010-10-04 16:33:58 +02001273
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001274 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +02001276 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001277
1278 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001279
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001281 * Escape the SQL Identifiers
1282 *
1283 * This function escapes column and table names
1284 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001285 * @param mixed
1286 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001287 */
1288 public function escape_identifiers($item)
1289 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001290 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001291 {
1292 return $item;
1293 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001294 elseif (is_array($item))
1295 {
1296 foreach ($item as $key => $value)
1297 {
1298 $item[$key] = $this->escape_identifiers($value);
1299 }
1300
1301 return $item;
1302 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001303 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001304 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001305 {
1306 return $item;
1307 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001308
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001309 static $preg_ec = array();
1310
1311 if (empty($preg_ec))
1312 {
1313 if (is_array($this->_escape_char))
1314 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001315 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001316 preg_quote($this->_escape_char[0], '/'),
1317 preg_quote($this->_escape_char[1], '/'),
1318 $this->_escape_char[0],
1319 $this->_escape_char[1]
1320 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001321 }
1322 else
1323 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001324 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001325 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001326 }
1327 }
1328
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001329 foreach ($this->_reserved_identifiers as $id)
1330 {
1331 if (strpos($item, '.'.$id) !== FALSE)
1332 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001333 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 +03001334 }
1335 }
1336
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001337 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 +03001338 }
1339
1340 // --------------------------------------------------------------------
1341
1342 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 * Generate an insert string
1344 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 * @param string the table upon which the query will be performed
1346 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001347 * @return string
1348 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001349 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001351 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001352
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001353 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001355 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 $values[] = $this->escape($val);
1357 }
Barry Mienydd671972010-10-04 16:33:58 +02001358
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001359 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001360 }
1361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 // --------------------------------------------------------------------
1363
1364 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001365 * Insert statement
1366 *
1367 * Generates a platform-specific insert string from the supplied data
1368 *
1369 * @param string the table name
1370 * @param array the insert keys
1371 * @param array the insert values
1372 * @return string
1373 */
1374 protected function _insert($table, $keys, $values)
1375 {
1376 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1377 }
1378
1379 // --------------------------------------------------------------------
1380
1381 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 * Generate an update string
1383 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 * @param string the table upon which the query will be performed
1385 * @param array an associative array data of key/values
1386 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001387 * @return string
1388 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001389 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001391 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001393 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 }
Barry Mienydd671972010-10-04 16:33:58 +02001395
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001396 $this->where($where);
1397
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001399 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001401 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 }
1403
Andrey Andreev79f888b2013-08-06 13:59:23 +03001404 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1405 $this->_reset_write();
1406 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001407 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001408
1409 // --------------------------------------------------------------------
1410
1411 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001412 * Update statement
1413 *
1414 * Generates a platform-specific update string from the supplied data
1415 *
1416 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001417 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001418 * @return string
1419 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001420 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001421 {
1422 foreach ($values as $key => $val)
1423 {
1424 $valstr[] = $key.' = '.$val;
1425 }
1426
Andrey Andreev75546112012-07-05 11:01:29 +03001427 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001428 .$this->_compile_wh('qb_where')
1429 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001430 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001431 }
1432
1433 // --------------------------------------------------------------------
1434
1435 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 * Tests whether the string has an SQL operator
1437 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 * @param string
1439 * @return bool
1440 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001441 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 {
Andrey Andreev295cfa92013-12-20 11:48:38 +02001443 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 +00001444 }
1445
1446 // --------------------------------------------------------------------
1447
1448 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001449 * Returns the SQL string operator
1450 *
1451 * @param string
1452 * @return string
1453 */
1454 protected function _get_operator($str)
1455 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001456 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001457
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001458 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001459 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001460 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001461 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001462 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001463 $_operators = array(
1464 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1465 '\s*<>?\s*', // <, <>
1466 '\s*>\s*', // >
1467 '\s+IS NULL', // IS NULL
Andrey Andreev295cfa92013-12-20 11:48:38 +02001468 '\s+IS NOT NULL', // IS NOT NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001469 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001470 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001471 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1472 '\s+IN\s*\([^\)]+\)', // IN(list)
1473 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1474 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1475 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1476 );
1477
1478 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001479
Andrey Andreev6e704752012-07-18 00:46:33 +03001480 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1481 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001482 }
1483
1484 // --------------------------------------------------------------------
1485
1486 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1488 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001489 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001490 * @return mixed
1491 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001492 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001494 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001495
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 if (FALSE === strpos($driver, $function))
1497 {
1498 $function = $driver.$function;
1499 }
Barry Mienydd671972010-10-04 16:33:58 +02001500
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 if ( ! function_exists($function))
1502 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001503 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001505
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001506 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001507 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001508 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 }
1510
1511 // --------------------------------------------------------------------
1512
1513 /**
1514 * Set Cache Directory Path
1515 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 * @param string the path to the cache directory
1517 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001518 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001519 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 {
1521 $this->cachedir = $path;
1522 }
1523
1524 // --------------------------------------------------------------------
1525
1526 /**
1527 * Enable Query Caching
1528 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001529 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001530 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001531 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001533 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 }
1535
1536 // --------------------------------------------------------------------
1537
1538 /**
1539 * Disable Query Caching
1540 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001541 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001542 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001543 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001544 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001545 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 }
Barry Mienydd671972010-10-04 16:33:58 +02001547
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 // --------------------------------------------------------------------
1549
1550 /**
1551 * Delete the cache files associated with a particular URI
1552 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001553 * @param string $segment_one = ''
1554 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001555 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001556 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001557 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001559 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001560 ? $this->CACHE->delete($segment_one, $segment_two)
1561 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 }
1563
1564 // --------------------------------------------------------------------
1565
1566 /**
1567 * Delete All cache files
1568 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001569 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001570 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001571 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001572 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001573 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001574 ? $this->CACHE->delete_all()
1575 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 }
1577
1578 // --------------------------------------------------------------------
1579
1580 /**
1581 * Initialize the Cache Class
1582 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001583 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001584 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001585 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001586 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001587 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001589 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001591 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001592 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001593 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001594 }
Derek Allarde37ab382009-02-03 16:13:57 +00001595
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1597 return TRUE;
1598 }
1599
1600 // --------------------------------------------------------------------
1601
1602 /**
1603 * Close DB Connection
1604 *
Barry Mienydd671972010-10-04 16:33:58 +02001605 * @return void
1606 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001607 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001609 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001611 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001612 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001614 }
Barry Mienydd671972010-10-04 16:33:58 +02001615
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 // --------------------------------------------------------------------
1617
1618 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001619 * Close DB Connection
1620 *
1621 * This method would be overriden by most of the drivers.
1622 *
1623 * @return void
1624 */
1625 protected function _close()
1626 {
1627 $this->conn_id = FALSE;
1628 }
1629
1630 // --------------------------------------------------------------------
1631
1632 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 * Display an error message
1634 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 * @param string the error message
1636 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001637 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001638 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001639 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001640 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 {
Derek Jonese7792202010-03-02 17:24:46 -06001642 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 $LANG->load('db');
1644
1645 $heading = $LANG->line('db_error_heading');
1646
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001647 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001649 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 }
1651 else
1652 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001653 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 }
Barry Mienydd671972010-10-04 16:33:58 +02001655
Pascal Kriete60f8c392010-08-25 18:03:28 +02001656 // Find the most likely culprit of the error by going through
1657 // the backtrace until the source file is no longer in the
1658 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001659 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001660 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001661 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001662 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001663 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001664 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1665 if (DIRECTORY_SEPARATOR !== '/')
1666 {
1667 $call['file'] = str_replace('\\', '/', $call['file']);
1668 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001669
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001670 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1671 {
1672 // Found it - use a relative path for safety
1673 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1674 $message[] = 'Line Number: '.$call['line'];
1675 break;
1676 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001677 }
1678 }
Barry Mienydd671972010-10-04 16:33:58 +02001679
Derek Jonese7792202010-03-02 17:24:46 -06001680 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001682 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 }
1684
1685 // --------------------------------------------------------------------
1686
1687 /**
1688 * Protect Identifiers
1689 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001690 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001691 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001693 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001694 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 *
1696 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1697 *
1698 * Or a query with aliasing:
1699 *
1700 * SELECT m.member_id, m.member_name FROM members AS m
1701 *
1702 * Since the column name can include up to four segments (host, DB, table, column)
1703 * or also have an alias prefix, we need to do a bit of work to figure this out and
1704 * insert the table prefix (if it exists) in the proper position, and escape only
1705 * the correct identifiers.
1706 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 * @param string
1708 * @param bool
1709 * @param mixed
1710 * @param bool
1711 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001712 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001713 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 {
1715 if ( ! is_bool($protect_identifiers))
1716 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001717 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 }
Derek Allarde37ab382009-02-03 16:13:57 +00001719
1720 if (is_array($item))
1721 {
1722 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001723 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001724 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001725 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001726 }
1727
1728 return $escaped_array;
1729 }
1730
Derek Allard911d3e02008-12-15 14:08:35 +00001731 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001732 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001733 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001734 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001735 //
1736 // Added exception for single quotes as well, we don't want to alter
1737 // literal strings. -- Narf
1738 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001739 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001740 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001741 }
1742
Andrey Andreevbf940582012-06-10 07:05:05 +03001743 // Convert tabs or multiple spaces into single spaces
1744 $item = preg_replace('/\s+/', ' ', $item);
1745
Andrey Andreevbf940582012-06-10 07:05:05 +03001746 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001747 // Note: strripos() is used in order to support spaces in table names
1748 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001749 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001750 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001751 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1752 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001753 $item = substr($item, 0, $offset);
1754 }
1755 elseif ($offset = strrpos($item, ' '))
1756 {
1757 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001758 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1759 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001760 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001761 }
1762 else
1763 {
1764 $alias = '';
1765 }
1766
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 // Break the string apart if it contains periods, then insert the table prefix
1768 // in the correct location, assuming the period doesn't indicate that we're dealing
1769 // with an alias. While we're at it, we will escape the components
1770 if (strpos($item, '.') !== FALSE)
1771 {
1772 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001773
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001775 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001777 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001778 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 if ($protect_identifiers === TRUE)
1780 {
1781 foreach ($parts as $key => $val)
1782 {
1783 if ( ! in_array($val, $this->_reserved_identifiers))
1784 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001785 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 }
1787 }
Barry Mienydd671972010-10-04 16:33:58 +02001788
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001790 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001791
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 return $item.$alias;
1793 }
Barry Mienydd671972010-10-04 16:33:58 +02001794
Andrey Andreev4c202602012-03-06 20:34:52 +02001795 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001796 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 {
1798 // We now add the table prefix based on some logic.
1799 // Do we have 4 segments (hostname.database.table.column)?
1800 // If so, we add the table prefix to the column name in the 3rd segment.
1801 if (isset($parts[3]))
1802 {
1803 $i = 2;
1804 }
1805 // Do we have 3 segments (database.table.column)?
1806 // If so, we add the table prefix to the column name in 2nd position
1807 elseif (isset($parts[2]))
1808 {
1809 $i = 1;
1810 }
1811 // Do we have 2 segments (table.column)?
1812 // If so, we add the table prefix to the column name in 1st segment
1813 else
1814 {
1815 $i = 0;
1816 }
Barry Mienydd671972010-10-04 16:33:58 +02001817
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 // This flag is set when the supplied $item does not contain a field name.
1819 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001820 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 {
1822 $i++;
1823 }
Barry Mienydd671972010-10-04 16:33:58 +02001824
Derek Jones55acc8b2009-07-11 03:38:13 +00001825 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001826 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001827 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001828 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001829 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001831 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 {
1833 $parts[$i] = $this->dbprefix.$parts[$i];
1834 }
Barry Mienydd671972010-10-04 16:33:58 +02001835
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 // Put the parts back together
1837 $item = implode('.', $parts);
1838 }
Barry Mienydd671972010-10-04 16:33:58 +02001839
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 if ($protect_identifiers === TRUE)
1841 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001842 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 }
Barry Mienydd671972010-10-04 16:33:58 +02001844
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 return $item.$alias;
1846 }
1847
Andrey Andreev4c202602012-03-06 20:34:52 +02001848 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001849 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001851 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001852 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001853 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001854 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001855 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001857 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 {
1859 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001860 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 }
1862
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001863 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001864 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001865 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 }
Barry Mienydd671972010-10-04 16:33:58 +02001867
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 return $item.$alias;
1869 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001870
Túbal Martín511f2252011-11-24 14:43:45 +01001871 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001872
Túbal Martín511f2252011-11-24 14:43:45 +01001873 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001874 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001875 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001876 *
Túbal Martín511f2252011-11-24 14:43:45 +01001877 * @return void
1878 */
1879 protected function _reset_select()
1880 {
Túbal Martín511f2252011-11-24 14:43:45 +01001881 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001882
1883}
1884
Derek Allard2067d1a2008-11-13 22:59:24 +00001885/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001886/* Location: ./system/database/DB_driver.php */