blob: fc1d9566c1abe1fb6c5d9d2416b77481a436a5e7 [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--;
clawoo4a4f5502014-10-20 15:28:08 +0300925 $escaped_value = $this->escape($binds[$c]);
926 if (is_array($escaped_value))
927 {
928 $escaped_value = '('.implode(',', $escaped_value).')';
929 }
930 $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300931 }
932 while ($c !== 0);
933
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300934 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 // --------------------------------------------------------------------
938
939 /**
940 * Determines if a query is a "write" type.
941 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200943 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200944 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200945 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200947 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 +0000948 }
Barry Mienydd671972010-10-04 16:33:58 +0200949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 // --------------------------------------------------------------------
951
952 /**
953 * Calculate the aggregate query elapsed time
954 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200955 * @param int The number of decimal places
956 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200957 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500958 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
960 return number_format($this->benchmark, $decimals);
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 // --------------------------------------------------------------------
964
965 /**
966 * Returns the total number of queries
967 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200968 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200969 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500970 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
972 return $this->query_count;
973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 // --------------------------------------------------------------------
976
977 /**
978 * Returns the last query that was executed
979 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200980 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200981 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500982 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 return end($this->queries);
985 }
986
987 // --------------------------------------------------------------------
988
989 /**
990 * "Smart" Escape String
991 *
992 * Escapes data based on type
993 * Sets boolean and null types
994 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200996 * @return mixed
997 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500998 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200999 {
clawooa779c482014-10-18 14:47:04 +03001000 if (is_array($str))
1001 {
1002 $str = array_map(array(&$this, 'escape'), $str);
clawoo4a4f5502014-10-20 15:28:08 +03001003 return $str;
clawooa779c482014-10-18 14:47:04 +03001004 }
1005 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001007 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001008 }
1009 elseif (is_bool($str))
1010 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001011 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001012 }
vlakoff1228fe22013-01-14 01:30:09 +01001013 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001014 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001015 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001016 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001017
1018 return $str;
1019 }
1020
1021 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001022
Derek Jonese4ed5832009-02-20 21:44:59 +00001023 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001024 * Escape String
1025 *
1026 * @param string $str
1027 * @param bool $like Whether or not the string will be used in a LIKE condition
1028 * @return string
1029 */
1030 public function escape_str($str, $like = FALSE)
1031 {
1032 if (is_array($str))
1033 {
1034 foreach ($str as $key => $val)
1035 {
1036 $str[$key] = $this->escape_str($val, $like);
1037 }
1038
1039 return $str;
1040 }
1041
1042 $str = $this->_escape_str($str);
1043
1044 // escape LIKE condition wildcards
1045 if ($like === TRUE)
1046 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001047 return str_replace(
1048 array($this->_like_escape_chr, '%', '_'),
1049 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1050 $str
1051 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001052 }
1053
1054 return $str;
1055 }
1056
1057 // --------------------------------------------------------------------
1058
1059 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001060 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001061 *
1062 * Calls the individual driver for platform
1063 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001064 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001065 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001066 * @return mixed
1067 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001068 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001069 {
1070 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001071 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001072
Derek Jonese4ed5832009-02-20 21:44:59 +00001073 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001076 * Platform-dependant string escape
1077 *
1078 * @param string
1079 * @return string
1080 */
1081 protected function _escape_str($str)
1082 {
1083 return str_replace("'", "''", remove_invisible_characters($str));
1084 }
1085
1086 // --------------------------------------------------------------------
1087
1088 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 * Primary
1090 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001091 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * position is the primary key
1093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001095 * @return string
1096 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001097 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +02001098 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001100 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 }
1102
1103 // --------------------------------------------------------------------
1104
1105 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001106 * "Count All" query
1107 *
1108 * Generates a platform-specific query string that counts all records in
1109 * the specified database
1110 *
1111 * @param string
1112 * @return int
1113 */
1114 public function count_all($table = '')
1115 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001116 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001117 {
1118 return 0;
1119 }
1120
1121 $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 +01001122 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001123 {
1124 return 0;
1125 }
1126
1127 $query = $query->row();
1128 $this->_reset_select();
1129 return (int) $query->numrows;
1130 }
1131
1132 // --------------------------------------------------------------------
1133
1134 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 * Returns an array of table names
1136 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001137 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001138 * @return array
1139 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001140 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
1142 // Is there a cached result?
1143 if (isset($this->data_cache['table_names']))
1144 {
1145 return $this->data_cache['table_names'];
1146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1149 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001150 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 }
1152
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001153 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001155
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001156 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001158 // Do we know from which column to get the table name?
1159 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001161 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001162 {
1163 $key = 'table_name';
1164 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001165 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001166 {
1167 $key = 'TABLE_NAME';
1168 }
1169 else
1170 {
1171 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001172 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001173 * E_STRICT is on, this would trigger a warning. So we'll have to
1174 * assign it first.
1175 */
1176 $key = array_keys($row);
1177 $key = array_shift($key);
1178 }
Taufan Aditya18209332012-02-09 16:07:27 +07001179 }
1180
Andrey Andreev12567e82012-01-25 22:50:33 +02001181 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 }
1183
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 return $this->data_cache['table_names'];
1185 }
Barry Mienydd671972010-10-04 16:33:58 +02001186
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 // --------------------------------------------------------------------
1188
1189 /**
1190 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001191 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001192 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001193 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001195 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001196 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001197 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 // --------------------------------------------------------------------
1201
1202 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001203 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001206 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001208 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
1210 // Is there a cached result?
1211 if (isset($this->data_cache['field_names'][$table]))
1212 {
1213 return $this->data_cache['field_names'][$table];
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001216 if ($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_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
Barry Mienydd671972010-10-04 16:33:58 +02001220
Greg Aker1edde302010-01-26 00:17:01 +00001221 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001223 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 }
Barry Mienydd671972010-10-04 16:33:58 +02001225
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001227 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001228
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001229 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001231 // Do we know from where to get the column's name?
1232 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001234 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001235 {
1236 $key = 'column_name';
1237 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001238 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001239 {
1240 $key = 'COLUMN_NAME';
1241 }
1242 else
1243 {
RJ garcia395e2df2013-03-21 10:48:24 -05001244 // We have no other choice but to just get the first element's key.
1245 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001246 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001248
1249 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 }
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 return $this->data_cache['field_names'][$table];
1253 }
1254
1255 // --------------------------------------------------------------------
1256
1257 /**
1258 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001259 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 * @param string
1261 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001262 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001264 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001265 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001266 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 }
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 // --------------------------------------------------------------------
1270
1271 /**
1272 * Returns an object with field data
1273 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001275 * @return object
1276 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001277 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001279 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001281 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 }
Barry Mienydd671972010-10-04 16:33:58 +02001283
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001284 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +02001286 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001287
1288 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001291 * Escape the SQL Identifiers
1292 *
1293 * This function escapes column and table names
1294 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001295 * @param mixed
1296 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001297 */
1298 public function escape_identifiers($item)
1299 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001300 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001301 {
1302 return $item;
1303 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001304 elseif (is_array($item))
1305 {
1306 foreach ($item as $key => $value)
1307 {
1308 $item[$key] = $this->escape_identifiers($value);
1309 }
1310
1311 return $item;
1312 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001313 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001314 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001315 {
1316 return $item;
1317 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001318
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001319 static $preg_ec = array();
1320
1321 if (empty($preg_ec))
1322 {
1323 if (is_array($this->_escape_char))
1324 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001325 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001326 preg_quote($this->_escape_char[0], '/'),
1327 preg_quote($this->_escape_char[1], '/'),
1328 $this->_escape_char[0],
1329 $this->_escape_char[1]
1330 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001331 }
1332 else
1333 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001334 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001335 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001336 }
1337 }
1338
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001339 foreach ($this->_reserved_identifiers as $id)
1340 {
1341 if (strpos($item, '.'.$id) !== FALSE)
1342 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001343 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 +03001344 }
1345 }
1346
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001347 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 +03001348 }
1349
1350 // --------------------------------------------------------------------
1351
1352 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 * Generate an insert string
1354 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 * @param string the table upon which the query will be performed
1356 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001357 * @return string
1358 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001359 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001361 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001362
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001363 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001365 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 $values[] = $this->escape($val);
1367 }
Barry Mienydd671972010-10-04 16:33:58 +02001368
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001369 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001370 }
1371
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 // --------------------------------------------------------------------
1373
1374 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001375 * Insert statement
1376 *
1377 * Generates a platform-specific insert string from the supplied data
1378 *
1379 * @param string the table name
1380 * @param array the insert keys
1381 * @param array the insert values
1382 * @return string
1383 */
1384 protected function _insert($table, $keys, $values)
1385 {
1386 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1387 }
1388
1389 // --------------------------------------------------------------------
1390
1391 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 * Generate an update string
1393 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 * @param string the table upon which the query will be performed
1395 * @param array an associative array data of key/values
1396 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001397 * @return string
1398 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001399 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001401 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001403 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 }
Barry Mienydd671972010-10-04 16:33:58 +02001405
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001406 $this->where($where);
1407
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001409 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001411 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 }
1413
Andrey Andreev79f888b2013-08-06 13:59:23 +03001414 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1415 $this->_reset_write();
1416 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001417 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001418
1419 // --------------------------------------------------------------------
1420
1421 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001422 * Update statement
1423 *
1424 * Generates a platform-specific update string from the supplied data
1425 *
1426 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001427 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001428 * @return string
1429 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001430 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001431 {
1432 foreach ($values as $key => $val)
1433 {
1434 $valstr[] = $key.' = '.$val;
1435 }
1436
Andrey Andreev75546112012-07-05 11:01:29 +03001437 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001438 .$this->_compile_wh('qb_where')
1439 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001440 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001441 }
1442
1443 // --------------------------------------------------------------------
1444
1445 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 * Tests whether the string has an SQL operator
1447 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 * @param string
1449 * @return bool
1450 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001451 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 {
Andrey Andreev2c6cdd72014-09-17 11:13:46 +03001453 return (bool) preg_match('/(<|>|!|=|\sIS\s|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 }
1455
1456 // --------------------------------------------------------------------
1457
1458 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001459 * Returns the SQL string operator
1460 *
1461 * @param string
1462 * @return string
1463 */
1464 protected function _get_operator($str)
1465 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001466 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001467
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001468 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001469 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001470 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001471 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001472 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001473 $_operators = array(
1474 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1475 '\s*<>?\s*', // <, <>
1476 '\s*>\s*', // >
Andrey Andreev2c6cdd72014-09-17 11:13:46 +03001477 '\s+IS(?:\sNOT)?(?:\sNULL)?', // IS[ NOT] NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001478 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001479 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001480 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1481 '\s+IN\s*\([^\)]+\)', // IN(list)
1482 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1483 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1484 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1485 );
1486
1487 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001488
Andrey Andreev6e704752012-07-18 00:46:33 +03001489 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1490 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001491 }
1492
1493 // --------------------------------------------------------------------
1494
1495 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1497 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001498 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001499 * @return mixed
1500 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001501 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001503 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 if (FALSE === strpos($driver, $function))
1506 {
1507 $function = $driver.$function;
1508 }
Barry Mienydd671972010-10-04 16:33:58 +02001509
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 if ( ! function_exists($function))
1511 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001512 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001514
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001515 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001516 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001517 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 }
1519
1520 // --------------------------------------------------------------------
1521
1522 /**
1523 * Set Cache Directory Path
1524 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 * @param string the path to the cache directory
1526 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001527 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001528 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 {
1530 $this->cachedir = $path;
1531 }
1532
1533 // --------------------------------------------------------------------
1534
1535 /**
1536 * Enable Query Caching
1537 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001538 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001539 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001540 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001542 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 }
1544
1545 // --------------------------------------------------------------------
1546
1547 /**
1548 * Disable Query Caching
1549 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001550 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001551 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001552 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001554 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 }
Barry Mienydd671972010-10-04 16:33:58 +02001556
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 // --------------------------------------------------------------------
1558
1559 /**
1560 * Delete the cache files associated with a particular URI
1561 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001562 * @param string $segment_one = ''
1563 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001564 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001565 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001566 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001568 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001569 ? $this->CACHE->delete($segment_one, $segment_two)
1570 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 }
1572
1573 // --------------------------------------------------------------------
1574
1575 /**
1576 * Delete All cache files
1577 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001578 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001579 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001580 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001582 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001583 ? $this->CACHE->delete_all()
1584 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 }
1586
1587 // --------------------------------------------------------------------
1588
1589 /**
1590 * Initialize the Cache Class
1591 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001592 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001593 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001594 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001596 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001598 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001600 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001601 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001602 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001603 }
Derek Allarde37ab382009-02-03 16:13:57 +00001604
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1606 return TRUE;
1607 }
1608
1609 // --------------------------------------------------------------------
1610
1611 /**
1612 * Close DB Connection
1613 *
Barry Mienydd671972010-10-04 16:33:58 +02001614 * @return void
1615 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001616 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001618 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001620 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001621 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 }
Barry Mienydd671972010-10-04 16:33:58 +02001624
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 // --------------------------------------------------------------------
1626
1627 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001628 * Close DB Connection
1629 *
1630 * This method would be overriden by most of the drivers.
1631 *
1632 * @return void
1633 */
1634 protected function _close()
1635 {
1636 $this->conn_id = FALSE;
1637 }
1638
1639 // --------------------------------------------------------------------
1640
1641 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 * Display an error message
1643 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 * @param string the error message
1645 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001646 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001647 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001648 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001649 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 {
Derek Jonese7792202010-03-02 17:24:46 -06001651 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 $LANG->load('db');
1653
1654 $heading = $LANG->line('db_error_heading');
1655
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001656 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001658 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 }
1660 else
1661 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001662 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 }
Barry Mienydd671972010-10-04 16:33:58 +02001664
Pascal Kriete60f8c392010-08-25 18:03:28 +02001665 // Find the most likely culprit of the error by going through
1666 // the backtrace until the source file is no longer in the
1667 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001668 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001669 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001670 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001671 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001672 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001673 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1674 if (DIRECTORY_SEPARATOR !== '/')
1675 {
1676 $call['file'] = str_replace('\\', '/', $call['file']);
1677 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001678
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001679 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1680 {
1681 // Found it - use a relative path for safety
1682 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1683 $message[] = 'Line Number: '.$call['line'];
1684 break;
1685 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001686 }
1687 }
Barry Mienydd671972010-10-04 16:33:58 +02001688
Derek Jonese7792202010-03-02 17:24:46 -06001689 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001691 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 }
1693
1694 // --------------------------------------------------------------------
1695
1696 /**
1697 * Protect Identifiers
1698 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001699 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001700 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001702 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001703 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 *
1705 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1706 *
1707 * Or a query with aliasing:
1708 *
1709 * SELECT m.member_id, m.member_name FROM members AS m
1710 *
1711 * Since the column name can include up to four segments (host, DB, table, column)
1712 * or also have an alias prefix, we need to do a bit of work to figure this out and
1713 * insert the table prefix (if it exists) in the proper position, and escape only
1714 * the correct identifiers.
1715 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 * @param string
1717 * @param bool
1718 * @param mixed
1719 * @param bool
1720 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001721 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001722 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 {
1724 if ( ! is_bool($protect_identifiers))
1725 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001726 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 }
Derek Allarde37ab382009-02-03 16:13:57 +00001728
1729 if (is_array($item))
1730 {
1731 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001732 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001733 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001734 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001735 }
1736
1737 return $escaped_array;
1738 }
1739
Derek Allard911d3e02008-12-15 14:08:35 +00001740 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001741 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001742 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001743 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001744 //
1745 // Added exception for single quotes as well, we don't want to alter
1746 // literal strings. -- Narf
1747 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001748 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001749 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001750 }
1751
Andrey Andreevbf940582012-06-10 07:05:05 +03001752 // Convert tabs or multiple spaces into single spaces
1753 $item = preg_replace('/\s+/', ' ', $item);
1754
Andrey Andreevbf940582012-06-10 07:05:05 +03001755 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001756 // Note: strripos() is used in order to support spaces in table names
1757 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001758 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001759 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001760 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1761 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001762 $item = substr($item, 0, $offset);
1763 }
1764 elseif ($offset = strrpos($item, ' '))
1765 {
1766 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001767 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1768 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001769 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001770 }
1771 else
1772 {
1773 $alias = '';
1774 }
1775
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 // Break the string apart if it contains periods, then insert the table prefix
1777 // in the correct location, assuming the period doesn't indicate that we're dealing
1778 // with an alias. While we're at it, we will escape the components
1779 if (strpos($item, '.') !== FALSE)
1780 {
1781 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001782
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001784 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001786 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001787 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 if ($protect_identifiers === TRUE)
1789 {
1790 foreach ($parts as $key => $val)
1791 {
1792 if ( ! in_array($val, $this->_reserved_identifiers))
1793 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001794 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 }
1796 }
Barry Mienydd671972010-10-04 16:33:58 +02001797
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001799 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001800
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 return $item.$alias;
1802 }
Barry Mienydd671972010-10-04 16:33:58 +02001803
Andrey Andreev4c202602012-03-06 20:34:52 +02001804 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001805 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 {
1807 // We now add the table prefix based on some logic.
1808 // Do we have 4 segments (hostname.database.table.column)?
1809 // If so, we add the table prefix to the column name in the 3rd segment.
1810 if (isset($parts[3]))
1811 {
1812 $i = 2;
1813 }
1814 // Do we have 3 segments (database.table.column)?
1815 // If so, we add the table prefix to the column name in 2nd position
1816 elseif (isset($parts[2]))
1817 {
1818 $i = 1;
1819 }
1820 // Do we have 2 segments (table.column)?
1821 // If so, we add the table prefix to the column name in 1st segment
1822 else
1823 {
1824 $i = 0;
1825 }
Barry Mienydd671972010-10-04 16:33:58 +02001826
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 // This flag is set when the supplied $item does not contain a field name.
1828 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001829 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 {
1831 $i++;
1832 }
Barry Mienydd671972010-10-04 16:33:58 +02001833
Derek Jones55acc8b2009-07-11 03:38:13 +00001834 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001835 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001836 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001837 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001838 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001840 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 {
1842 $parts[$i] = $this->dbprefix.$parts[$i];
1843 }
Barry Mienydd671972010-10-04 16:33:58 +02001844
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 // Put the parts back together
1846 $item = implode('.', $parts);
1847 }
Barry Mienydd671972010-10-04 16:33:58 +02001848
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 if ($protect_identifiers === TRUE)
1850 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001851 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 }
Barry Mienydd671972010-10-04 16:33:58 +02001853
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 return $item.$alias;
1855 }
1856
Andrey Andreev4c202602012-03-06 20:34:52 +02001857 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001858 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001860 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001861 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001862 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001863 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001864 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001866 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 {
1868 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001869 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 }
1871
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001872 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001874 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 }
Barry Mienydd671972010-10-04 16:33:58 +02001876
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 return $item.$alias;
1878 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001879
Túbal Martín511f2252011-11-24 14:43:45 +01001880 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001881
Túbal Martín511f2252011-11-24 14:43:45 +01001882 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001883 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001884 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001885 *
Túbal Martín511f2252011-11-24 14:43:45 +01001886 * @return void
1887 */
1888 protected function _reset_select()
1889 {
Túbal Martín511f2252011-11-24 14:43:45 +01001890 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001891
1892}
1893
Derek Allard2067d1a2008-11-13 22:59:24 +00001894/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001895/* Location: ./system/database/DB_driver.php */