blob: b004de355070ab9f768478dd5083a63aa6ab9521 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?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 /**
264 * Cache On flag
265 *
266 * @var bool
267 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200268 public $cache_on = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200269
270 /**
271 * Cache directory path
272 *
273 * @var bool
274 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200275 public $cachedir = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200276
277 /**
278 * Cache auto-delete flag
279 *
280 * @var bool
281 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200282 public $cache_autodel = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200283
Andrey Andreevae85eb42012-11-02 01:42:31 +0200284 /**
285 * DB Cache object
286 *
287 * @see CI_DB_cache
288 * @var object
289 */
290 public $CACHE;
291
292 /**
293 * Protect identifiers flag
294 *
295 * @var bool
296 */
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +0000297 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +0200298
Andrey Andreevae85eb42012-11-02 01:42:31 +0200299 /**
300 * List of reserved identifiers
301 *
302 * Identifiers that must NOT be escaped.
303 *
304 * @var string[]
305 */
306 protected $_reserved_identifiers = array('*');
307
308 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200309 * Identifier escape character
310 *
311 * @var string
312 */
313 protected $_escape_char = '"';
314
315 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200316 * ESCAPE statement string
317 *
318 * @var string
319 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300320 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevae85eb42012-11-02 01:42:31 +0200321
322 /**
323 * ESCAPE character
324 *
325 * @var string
326 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300327 protected $_like_escape_chr = '!';
328
Andrey Andreev75546112012-07-05 11:01:29 +0300329 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200330 * ORDER BY random keyword
331 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200332 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200333 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200334 protected $_random_keyword = array('RAND()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200335
336 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200337 * COUNT string
338 *
339 * @used-by CI_DB_driver::count_all()
340 * @used-by CI_DB_query_builder::count_all_results()
341 *
342 * @var string
Andrey Andreev77a5d942012-07-05 15:42:14 +0300343 */
344 protected $_count_string = 'SELECT COUNT(*) AS ';
345
Andrey Andreevae85eb42012-11-02 01:42:31 +0200346 // --------------------------------------------------------------------
347
Andrey Andreev77a5d942012-07-05 15:42:14 +0300348 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200349 * Class constructor
Andrey Andreev75546112012-07-05 11:01:29 +0300350 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200351 * @param array $params
Andrey Andreev75546112012-07-05 11:01:29 +0300352 * @return void
353 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500354 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 if (is_array($params))
357 {
358 foreach ($params as $key => $val)
359 {
360 $this->$key = $val;
361 }
362 }
363
364 log_message('debug', 'Database Driver Class Initialized');
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Gints Murans89f77ee2012-05-23 00:23:50 +0300367 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 /**
370 * Initialize Database Settings
371 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200372 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200373 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200374 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200376 /* If an established connection is available, then there's
377 * no need to connect and select the database.
378 *
379 * Depending on the database driver, conn_id can be either
380 * boolean TRUE, a resource or an object.
381 */
382 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 return TRUE;
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // Connect to the database and set the connection ID
Andrey Andreev7e963512014-02-27 15:43:24 +0200390 $this->conn_id = $this->db_connect($this->pconnect);
Derek Allard2067d1a2008-11-13 22:59:24 +0000391
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200392 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 if ( ! $this->conn_id)
394 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100395 // Check if there is a failover set
396 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100398 // Go over all the failovers
399 foreach ($this->failover as $failover)
400 {
401 // Replace the current settings with those of the failover
402 foreach ($failover as $key => $val)
403 {
404 $this->$key = $val;
405 }
406
407 // Try to connect
Andrey Andreev7e963512014-02-27 15:43:24 +0200408 $this->conn_id = $this->db_connect($this->pconnect);
Felix Balfoort5d581b62011-11-29 15:53:01 +0100409
410 // If a connection is made break the foreach loop
411 if ($this->conn_id)
412 {
413 break;
414 }
415 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100417
418 // We still don't have a connection?
419 if ( ! $this->conn_id)
420 {
421 log_message('error', 'Unable to connect to the database');
422
423 if ($this->db_debug)
424 {
425 $this->display_error('db_unable_to_connect');
426 }
Andrey Andreev7e963512014-02-27 15:43:24 +0200427
Felix Balfoort5d581b62011-11-29 15:53:01 +0100428 return FALSE;
429 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200432 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200433 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
437
438 /**
Andrey Andreev2e171022014-02-25 15:21:41 +0200439 * Persistent database connection
440 *
441 * @return resource
442 */
443 public function db_pconnect()
444 {
445 return $this->db_connect(TRUE);
446 }
447
448 // --------------------------------------------------------------------
449
450 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200451 * Reconnect
452 *
453 * Keep / reestablish the db connection if no queries have been
454 * sent for a length of time exceeding the server's idle timeout.
455 *
456 * This is just a dummy method to allow drivers without such
457 * functionality to not declare it, while others will override it.
458 *
459 * @return void
460 */
461 public function reconnect()
462 {
463 }
464
465 // --------------------------------------------------------------------
466
467 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300468 * Select database
469 *
470 * This is just a dummy method to allow drivers without such
471 * functionality to not declare it, while others will override it.
472 *
473 * @return bool
474 */
475 public function db_select()
476 {
477 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
479
480 // --------------------------------------------------------------------
481
482 /**
483 * Set client character set
484 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200486 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200488 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200490 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200492 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 if ($this->db_debug)
495 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200496 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 return FALSE;
500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 return TRUE;
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
508 * The name of the platform in use (mysql, mssql, etc...)
509 *
Barry Mienydd671972010-10-04 16:33:58 +0200510 * @return string
511 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500512 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 return $this->dbdriver;
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200520 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200522 * Returns a string containing the version of the database being used.
523 * Most drivers will override this method.
524 *
Barry Mienydd671972010-10-04 16:33:58 +0200525 * @return string
526 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200527 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200529 if (isset($this->data_cache['version']))
530 {
531 return $this->data_cache['version'];
532 }
533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 if (FALSE === ($sql = $this->_version()))
535 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200536 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
Derek Allard3683f772009-12-16 17:32:33 +0000538
Andrey Andreev7e963512014-02-27 15:43:24 +0200539 $query = $this->query($sql)->row();
Andrey Andreev08856b82012-03-03 03:19:28 +0200540 return $this->data_cache['version'] = $query->ver;
541 }
Derek Allard3683f772009-12-16 17:32:33 +0000542
Andrey Andreev08856b82012-03-03 03:19:28 +0200543 // --------------------------------------------------------------------
544
545 /**
546 * Version number query string
547 *
548 * @return string
549 */
550 protected function _version()
551 {
552 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
556
557 /**
558 * Execute the query
559 *
560 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200561 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * upon successful execution of a "write" type query. Returns boolean
563 * FALSE upon failure, and if the $db_debug variable is set to TRUE
564 * will raise an error.
565 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300566 * @param string $sql
567 * @param array $binds = FALSE An array of binding data
568 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200569 * @return mixed
570 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300571 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100573 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200575 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200576 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300578 elseif ( ! is_bool($return_object))
579 {
580 $return_object = ! $this->is_write_type($sql);
581 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000582
583 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100584 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600585 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200586 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Derek Jonese7792202010-03-02 17:24:46 -0600588
Ryan Dialef7474c2012-03-01 16:11:36 -0500589 // Compile binds if needed
590 if ($binds !== FALSE)
591 {
592 $sql = $this->compile_binds($sql, $binds);
593 }
594
Andrey Andreev4c202602012-03-06 20:34:52 +0200595 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // we will load the caching class and return the previously
597 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300598 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200600 $this->load_rdriver();
601 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200603 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
605 }
Barry Mienydd671972010-10-04 16:33:58 +0200606
Andrey Andreevb66664b2012-06-27 14:22:10 +0300607 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100608 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 $this->queries[] = $sql;
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200614 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // Run the Query
617 if (FALSE === ($this->result_id = $this->simple_query($sql)))
618 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100619 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
621 $this->query_times[] = 0;
622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // This will trigger a rollback if transactions are being used
625 $this->_trans_status = FALSE;
626
Andrey Andreev4be5de12012-03-02 15:45:41 +0200627 // Grab the error now, as we might run some additional queries before displaying the error
628 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200629
630 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300631 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 if ($this->db_debug)
634 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200636 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200637 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // transactions to remain in limbo.
Andrey Andreev6c854422013-10-21 13:58:15 +0300639 if ($this->_trans_depth !== 0)
640 {
641 do
642 {
643 $this->trans_complete();
644 }
645 while ($this->_trans_depth !== 0);
646 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000647
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200648 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200649 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 return FALSE;
653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200656 $time_end = microtime(TRUE);
657 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000658
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100659 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
dixyab3cab52012-04-21 00:58:00 +0200661 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 // Increment the query counter
665 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200666
Andrey Andreevb66664b2012-06-27 14:22:10 +0300667 // Will we have a result object instantiated? If not - we'll simply return TRUE
668 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300670 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100671 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 $this->CACHE->delete();
674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 return TRUE;
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 if ($return_object !== TRUE)
681 {
682 return TRUE;
683 }
Barry Mienydd671972010-10-04 16:33:58 +0200684
685 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200686 $driver = $this->load_rdriver();
687 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200688
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200689 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100691 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 // We'll create a new instance of the result object
694 // only without the platform specific driver since
695 // we can't use it with cached data (the query result
696 // resource ID won't be any good once we've cached the
697 // result object, so we'll have to compile the data
698 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200699 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 $CR->result_object = $RES->result_object();
701 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200702 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 // Reset these since cached objects can not utilize resource IDs.
705 $CR->conn_id = NULL;
706 $CR->result_id = NULL;
707
708 $this->CACHE->write($sql, $CR);
709 }
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 return $RES;
712 }
713
714 // --------------------------------------------------------------------
715
716 /**
717 * Load the result drivers
718 *
Barry Mienydd671972010-10-04 16:33:58 +0200719 * @return string the name of the result class
720 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500721 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
723 $driver = 'CI_DB_'.$this->dbdriver.'_result';
724
vlakofffadb8222013-05-12 10:57:09 +0200725 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200727 require_once(BASEPATH.'database/DB_result.php');
728 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 return $driver;
732 }
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 // --------------------------------------------------------------------
735
736 /**
737 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200738 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 * we only use it when running transaction commands since they do
740 * not require all the features of the main query() function.
741 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200743 * @return mixed
744 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500745 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
747 if ( ! $this->conn_id)
748 {
749 $this->initialize();
750 }
751
752 return $this->_execute($sql);
753 }
Barry Mienydd671972010-10-04 16:33:58 +0200754
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 // --------------------------------------------------------------------
756
757 /**
758 * Disable Transactions
759 * This permits transactions to be disabled at run-time.
760 *
Barry Mienydd671972010-10-04 16:33:58 +0200761 * @return void
762 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500763 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 {
765 $this->trans_enabled = FALSE;
766 }
767
768 // --------------------------------------------------------------------
769
770 /**
771 * Enable/disable Transaction Strict Mode
772 * When strict mode is enabled, if you are running multiple groups of
773 * transactions, if one group fails all groups will be rolled back.
774 * If strict mode is disabled, each group is treated autonomously, meaning
775 * a failure of one group will not affect any others
776 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300777 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200778 * @return void
779 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500780 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
782 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
783 }
Barry Mienydd671972010-10-04 16:33:58 +0200784
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 // --------------------------------------------------------------------
786
787 /**
788 * Start Transaction
789 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300790 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200791 * @return void
792 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500793 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200794 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 if ( ! $this->trans_enabled)
796 {
797 return FALSE;
798 }
799
800 // When transactions are nested we only begin/commit/rollback the outermost ones
801 if ($this->_trans_depth > 0)
802 {
803 $this->_trans_depth += 1;
804 return;
805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500808 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 }
810
811 // --------------------------------------------------------------------
812
813 /**
814 * Complete Transaction
815 *
Barry Mienydd671972010-10-04 16:33:58 +0200816 * @return bool
817 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500818 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
820 if ( ! $this->trans_enabled)
821 {
822 return FALSE;
823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 // When transactions are nested we only begin/commit/rollback the outermost ones
826 if ($this->_trans_depth > 1)
827 {
828 $this->_trans_depth -= 1;
829 return TRUE;
830 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500831 else
832 {
833 $this->_trans_depth = 0;
834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900837 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 // If we are NOT running in strict mode, we will reset
842 // the _trans_status flag so that subsequent groups of transactions
843 // will be permitted.
844 if ($this->trans_strict === FALSE)
845 {
846 $this->_trans_status = TRUE;
847 }
848
849 log_message('debug', 'DB Transaction Failure');
850 return FALSE;
851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 $this->trans_commit();
854 return TRUE;
855 }
856
857 // --------------------------------------------------------------------
858
859 /**
860 * Lets you retrieve the transaction flag to determine if it has failed
861 *
Barry Mienydd671972010-10-04 16:33:58 +0200862 * @return bool
863 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500864 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
866 return $this->_trans_status;
867 }
868
869 // --------------------------------------------------------------------
870
871 /**
872 * Compile Bindings
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @param string the sql statement
875 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200876 * @return string
877 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500878 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300880 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
882 return $sql;
883 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300884 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300886 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300887 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300889 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200890 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300891 // Make sure we're using numeric keys
892 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300893 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
895
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300896 // We'll need the marker length later
897 $ml = strlen($this->bind_marker);
898
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300899 // Make sure not to replace a chunk inside a string that happens to match the bind marker
900 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300902 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300903 str_replace($matches[0],
904 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
905 $sql, $c),
906 $matches, PREG_OFFSET_CAPTURE);
907
908 // Bind values' count must match the count of markers in the query
909 if ($bind_count !== $c)
910 {
911 return $sql;
912 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300913 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300914 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 +0300915 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300916 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
918
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300919 do
920 {
921 $c--;
922 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
923 }
924 while ($c !== 0);
925
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300926 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 // --------------------------------------------------------------------
930
931 /**
932 * Determines if a query is a "write" type.
933 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200935 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200936 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200937 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200939 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 +0000940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 // --------------------------------------------------------------------
943
944 /**
945 * Calculate the aggregate query elapsed time
946 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200947 * @param int The number of decimal places
948 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200949 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500950 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 return number_format($this->benchmark, $decimals);
953 }
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 // --------------------------------------------------------------------
956
957 /**
958 * Returns the total number of queries
959 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200960 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200961 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500962 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
964 return $this->query_count;
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // --------------------------------------------------------------------
968
969 /**
970 * Returns the last query that was executed
971 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200972 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200973 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500974 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
976 return end($this->queries);
977 }
978
979 // --------------------------------------------------------------------
980
981 /**
982 * "Smart" Escape String
983 *
984 * Escapes data based on type
985 * Sets boolean and null types
986 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200988 * @return mixed
989 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500990 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200991 {
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200992 if (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200994 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000995 }
996 elseif (is_bool($str))
997 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200998 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000999 }
vlakoff1228fe22013-01-14 01:30:09 +01001000 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001001 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001002 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001003 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001004
1005 return $str;
1006 }
1007
1008 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001009
Derek Jonese4ed5832009-02-20 21:44:59 +00001010 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001011 * Escape String
1012 *
1013 * @param string $str
1014 * @param bool $like Whether or not the string will be used in a LIKE condition
1015 * @return string
1016 */
1017 public function escape_str($str, $like = FALSE)
1018 {
1019 if (is_array($str))
1020 {
1021 foreach ($str as $key => $val)
1022 {
1023 $str[$key] = $this->escape_str($val, $like);
1024 }
1025
1026 return $str;
1027 }
1028
1029 $str = $this->_escape_str($str);
1030
1031 // escape LIKE condition wildcards
1032 if ($like === TRUE)
1033 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001034 return str_replace(
1035 array($this->_like_escape_chr, '%', '_'),
1036 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1037 $str
1038 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001039 }
1040
1041 return $str;
1042 }
1043
1044 // --------------------------------------------------------------------
1045
1046 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001047 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001048 *
1049 * Calls the individual driver for platform
1050 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001051 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001052 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001053 * @return mixed
1054 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001055 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001056 {
1057 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001058 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001059
Derek Jonese4ed5832009-02-20 21:44:59 +00001060 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001063 * Platform-dependant string escape
1064 *
1065 * @param string
1066 * @return string
1067 */
1068 protected function _escape_str($str)
1069 {
1070 return str_replace("'", "''", remove_invisible_characters($str));
1071 }
1072
1073 // --------------------------------------------------------------------
1074
1075 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 * Primary
1077 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001078 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * position is the primary key
1080 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001082 * @return string
1083 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001084 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +02001085 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001087 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
1089
1090 // --------------------------------------------------------------------
1091
1092 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001093 * "Count All" query
1094 *
1095 * Generates a platform-specific query string that counts all records in
1096 * the specified database
1097 *
1098 * @param string
1099 * @return int
1100 */
1101 public function count_all($table = '')
1102 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001103 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001104 {
1105 return 0;
1106 }
1107
1108 $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 +01001109 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001110 {
1111 return 0;
1112 }
1113
1114 $query = $query->row();
1115 $this->_reset_select();
1116 return (int) $query->numrows;
1117 }
1118
1119 // --------------------------------------------------------------------
1120
1121 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 * Returns an array of table names
1123 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001124 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001125 * @return array
1126 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001127 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
1129 // Is there a cached result?
1130 if (isset($this->data_cache['table_names']))
1131 {
1132 return $this->data_cache['table_names'];
1133 }
Barry Mienydd671972010-10-04 16:33:58 +02001134
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1136 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001137 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
1139
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001140 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001142
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001143 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001145 // Do we know from which column to get the table name?
1146 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001148 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001149 {
1150 $key = 'table_name';
1151 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001152 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001153 {
1154 $key = 'TABLE_NAME';
1155 }
1156 else
1157 {
1158 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001159 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001160 * E_STRICT is on, this would trigger a warning. So we'll have to
1161 * assign it first.
1162 */
1163 $key = array_keys($row);
1164 $key = array_shift($key);
1165 }
Taufan Aditya18209332012-02-09 16:07:27 +07001166 }
1167
Andrey Andreev12567e82012-01-25 22:50:33 +02001168 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 }
1170
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 return $this->data_cache['table_names'];
1172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 // --------------------------------------------------------------------
1175
1176 /**
1177 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001178 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001179 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001180 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001182 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001183 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001184 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 }
Barry Mienydd671972010-10-04 16:33:58 +02001186
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 // --------------------------------------------------------------------
1188
1189 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001190 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001193 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001195 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 // Is there a cached result?
1198 if (isset($this->data_cache['field_names'][$table]))
1199 {
1200 return $this->data_cache['field_names'][$table];
1201 }
Barry Mienydd671972010-10-04 16:33:58 +02001202
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001203 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001205 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Greg Aker1edde302010-01-26 00:17:01 +00001208 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001210 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001214 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001215
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001216 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001218 // Do we know from where to get the column's name?
1219 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001221 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001222 {
1223 $key = 'column_name';
1224 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001225 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001226 {
1227 $key = 'COLUMN_NAME';
1228 }
1229 else
1230 {
RJ garcia395e2df2013-03-21 10:48:24 -05001231 // We have no other choice but to just get the first element's key.
1232 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001233 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001235
1236 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 return $this->data_cache['field_names'][$table];
1240 }
1241
1242 // --------------------------------------------------------------------
1243
1244 /**
1245 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001246 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 * @param string
1248 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001249 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001251 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001252 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001253 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 }
Barry Mienydd671972010-10-04 16:33:58 +02001255
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 // --------------------------------------------------------------------
1257
1258 /**
1259 * Returns an object with field data
1260 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001262 * @return object
1263 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001264 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001266 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001268 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 }
Barry Mienydd671972010-10-04 16:33:58 +02001270
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001271 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +02001273 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001274
1275 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001278 * Escape the SQL Identifiers
1279 *
1280 * This function escapes column and table names
1281 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001282 * @param mixed
1283 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001284 */
1285 public function escape_identifiers($item)
1286 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001287 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001288 {
1289 return $item;
1290 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001291 elseif (is_array($item))
1292 {
1293 foreach ($item as $key => $value)
1294 {
1295 $item[$key] = $this->escape_identifiers($value);
1296 }
1297
1298 return $item;
1299 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001300 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001301 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001302 {
1303 return $item;
1304 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001305
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001306 static $preg_ec = array();
1307
1308 if (empty($preg_ec))
1309 {
1310 if (is_array($this->_escape_char))
1311 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001312 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001313 preg_quote($this->_escape_char[0], '/'),
1314 preg_quote($this->_escape_char[1], '/'),
1315 $this->_escape_char[0],
1316 $this->_escape_char[1]
1317 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001318 }
1319 else
1320 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001321 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001322 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001323 }
1324 }
1325
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001326 foreach ($this->_reserved_identifiers as $id)
1327 {
1328 if (strpos($item, '.'.$id) !== FALSE)
1329 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001330 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 +03001331 }
1332 }
1333
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001334 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 +03001335 }
1336
1337 // --------------------------------------------------------------------
1338
1339 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 * Generate an insert string
1341 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 * @param string the table upon which the query will be performed
1343 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001344 * @return string
1345 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001346 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001348 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001349
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001350 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001352 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 $values[] = $this->escape($val);
1354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001356 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001357 }
1358
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 // --------------------------------------------------------------------
1360
1361 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001362 * Insert statement
1363 *
1364 * Generates a platform-specific insert string from the supplied data
1365 *
1366 * @param string the table name
1367 * @param array the insert keys
1368 * @param array the insert values
1369 * @return string
1370 */
1371 protected function _insert($table, $keys, $values)
1372 {
1373 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1374 }
1375
1376 // --------------------------------------------------------------------
1377
1378 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 * Generate an update string
1380 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 * @param string the table upon which the query will be performed
1382 * @param array an associative array data of key/values
1383 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001384 * @return string
1385 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001386 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001388 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001390 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 }
Barry Mienydd671972010-10-04 16:33:58 +02001392
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001393 $this->where($where);
1394
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001396 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001398 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 }
1400
Andrey Andreev79f888b2013-08-06 13:59:23 +03001401 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1402 $this->_reset_write();
1403 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001404 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001405
1406 // --------------------------------------------------------------------
1407
1408 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001409 * Update statement
1410 *
1411 * Generates a platform-specific update string from the supplied data
1412 *
1413 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001414 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001415 * @return string
1416 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001417 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001418 {
1419 foreach ($values as $key => $val)
1420 {
1421 $valstr[] = $key.' = '.$val;
1422 }
1423
Andrey Andreev75546112012-07-05 11:01:29 +03001424 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001425 .$this->_compile_wh('qb_where')
1426 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001427 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001428 }
1429
1430 // --------------------------------------------------------------------
1431
1432 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 * Tests whether the string has an SQL operator
1434 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 * @param string
1436 * @return bool
1437 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001438 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 {
Andrey Andreev295cfa92013-12-20 11:48:38 +02001440 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 +00001441 }
1442
1443 // --------------------------------------------------------------------
1444
1445 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001446 * Returns the SQL string operator
1447 *
1448 * @param string
1449 * @return string
1450 */
1451 protected function _get_operator($str)
1452 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001453 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001454
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001455 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001456 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001457 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001458 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001459 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001460 $_operators = array(
1461 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1462 '\s*<>?\s*', // <, <>
1463 '\s*>\s*', // >
1464 '\s+IS NULL', // IS NULL
Andrey Andreev295cfa92013-12-20 11:48:38 +02001465 '\s+IS NOT NULL', // IS NOT NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001466 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001467 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001468 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1469 '\s+IN\s*\([^\)]+\)', // IN(list)
1470 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1471 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1472 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1473 );
1474
1475 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001476
Andrey Andreev6e704752012-07-18 00:46:33 +03001477 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1478 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001479 }
1480
1481 // --------------------------------------------------------------------
1482
1483 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1485 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001486 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001487 * @return mixed
1488 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001489 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001491 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001492
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 if (FALSE === strpos($driver, $function))
1494 {
1495 $function = $driver.$function;
1496 }
Barry Mienydd671972010-10-04 16:33:58 +02001497
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 if ( ! function_exists($function))
1499 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001500 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001502
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001503 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001504 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001505 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 }
1507
1508 // --------------------------------------------------------------------
1509
1510 /**
1511 * Set Cache Directory Path
1512 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 * @param string the path to the cache directory
1514 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001515 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001516 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 {
1518 $this->cachedir = $path;
1519 }
1520
1521 // --------------------------------------------------------------------
1522
1523 /**
1524 * Enable Query Caching
1525 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001526 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001527 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001528 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001530 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 }
1532
1533 // --------------------------------------------------------------------
1534
1535 /**
1536 * Disable 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_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001542 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 }
Barry Mienydd671972010-10-04 16:33:58 +02001544
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 // --------------------------------------------------------------------
1546
1547 /**
1548 * Delete the cache files associated with a particular URI
1549 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001550 * @param string $segment_one = ''
1551 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001552 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001553 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001554 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001556 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001557 ? $this->CACHE->delete($segment_one, $segment_two)
1558 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 }
1560
1561 // --------------------------------------------------------------------
1562
1563 /**
1564 * Delete All cache files
1565 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001566 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001567 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001568 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001570 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001571 ? $this->CACHE->delete_all()
1572 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001573 }
1574
1575 // --------------------------------------------------------------------
1576
1577 /**
1578 * Initialize the Cache Class
1579 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001580 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001581 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001582 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001583 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001584 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001586 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001587 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001588 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001589 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001590 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001591 }
Derek Allarde37ab382009-02-03 16:13:57 +00001592
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1594 return TRUE;
1595 }
1596
1597 // --------------------------------------------------------------------
1598
1599 /**
1600 * Close DB Connection
1601 *
Barry Mienydd671972010-10-04 16:33:58 +02001602 * @return void
1603 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001604 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001606 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001608 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001609 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 }
Barry Mienydd671972010-10-04 16:33:58 +02001612
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 // --------------------------------------------------------------------
1614
1615 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001616 * Close DB Connection
1617 *
1618 * This method would be overriden by most of the drivers.
1619 *
1620 * @return void
1621 */
1622 protected function _close()
1623 {
1624 $this->conn_id = FALSE;
1625 }
1626
1627 // --------------------------------------------------------------------
1628
1629 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 * Display an error message
1631 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 * @param string the error message
1633 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001634 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001635 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001636 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001637 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 {
Derek Jonese7792202010-03-02 17:24:46 -06001639 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001640 $LANG->load('db');
1641
1642 $heading = $LANG->line('db_error_heading');
1643
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001644 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001646 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 }
1648 else
1649 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001650 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 }
Barry Mienydd671972010-10-04 16:33:58 +02001652
Pascal Kriete60f8c392010-08-25 18:03:28 +02001653 // Find the most likely culprit of the error by going through
1654 // the backtrace until the source file is no longer in the
1655 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001656 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001657 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001658 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001659 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001660 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001661 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1662 if (DIRECTORY_SEPARATOR !== '/')
1663 {
1664 $call['file'] = str_replace('\\', '/', $call['file']);
1665 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001666
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001667 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1668 {
1669 // Found it - use a relative path for safety
1670 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1671 $message[] = 'Line Number: '.$call['line'];
1672 break;
1673 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001674 }
1675 }
Barry Mienydd671972010-10-04 16:33:58 +02001676
Derek Jonese7792202010-03-02 17:24:46 -06001677 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 echo $error->show_error($heading, $message, 'error_db');
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -07001679 exit(EXIT_DATABASE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 }
1681
1682 // --------------------------------------------------------------------
1683
1684 /**
1685 * Protect Identifiers
1686 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001687 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001688 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001690 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001691 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 *
1693 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1694 *
1695 * Or a query with aliasing:
1696 *
1697 * SELECT m.member_id, m.member_name FROM members AS m
1698 *
1699 * Since the column name can include up to four segments (host, DB, table, column)
1700 * or also have an alias prefix, we need to do a bit of work to figure this out and
1701 * insert the table prefix (if it exists) in the proper position, and escape only
1702 * the correct identifiers.
1703 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 * @param string
1705 * @param bool
1706 * @param mixed
1707 * @param bool
1708 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001709 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001710 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 {
1712 if ( ! is_bool($protect_identifiers))
1713 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001714 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 }
Derek Allarde37ab382009-02-03 16:13:57 +00001716
1717 if (is_array($item))
1718 {
1719 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001720 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001721 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001722 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001723 }
1724
1725 return $escaped_array;
1726 }
1727
Derek Allard911d3e02008-12-15 14:08:35 +00001728 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001729 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001730 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001731 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001732 //
1733 // Added exception for single quotes as well, we don't want to alter
1734 // literal strings. -- Narf
1735 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001736 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001737 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001738 }
1739
Andrey Andreevbf940582012-06-10 07:05:05 +03001740 // Convert tabs or multiple spaces into single spaces
1741 $item = preg_replace('/\s+/', ' ', $item);
1742
Andrey Andreevbf940582012-06-10 07:05:05 +03001743 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001744 // Note: strripos() is used in order to support spaces in table names
1745 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001746 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001747 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001748 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1749 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001750 $item = substr($item, 0, $offset);
1751 }
1752 elseif ($offset = strrpos($item, ' '))
1753 {
1754 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001755 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1756 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001757 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001758 }
1759 else
1760 {
1761 $alias = '';
1762 }
1763
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 // Break the string apart if it contains periods, then insert the table prefix
1765 // in the correct location, assuming the period doesn't indicate that we're dealing
1766 // with an alias. While we're at it, we will escape the components
1767 if (strpos($item, '.') !== FALSE)
1768 {
1769 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001770
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001772 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001774 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001775 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 if ($protect_identifiers === TRUE)
1777 {
1778 foreach ($parts as $key => $val)
1779 {
1780 if ( ! in_array($val, $this->_reserved_identifiers))
1781 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001782 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 }
1784 }
Barry Mienydd671972010-10-04 16:33:58 +02001785
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001787 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001788
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 return $item.$alias;
1790 }
Barry Mienydd671972010-10-04 16:33:58 +02001791
Andrey Andreev4c202602012-03-06 20:34:52 +02001792 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001793 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 {
1795 // We now add the table prefix based on some logic.
1796 // Do we have 4 segments (hostname.database.table.column)?
1797 // If so, we add the table prefix to the column name in the 3rd segment.
1798 if (isset($parts[3]))
1799 {
1800 $i = 2;
1801 }
1802 // Do we have 3 segments (database.table.column)?
1803 // If so, we add the table prefix to the column name in 2nd position
1804 elseif (isset($parts[2]))
1805 {
1806 $i = 1;
1807 }
1808 // Do we have 2 segments (table.column)?
1809 // If so, we add the table prefix to the column name in 1st segment
1810 else
1811 {
1812 $i = 0;
1813 }
Barry Mienydd671972010-10-04 16:33:58 +02001814
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 // This flag is set when the supplied $item does not contain a field name.
1816 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001817 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 {
1819 $i++;
1820 }
Barry Mienydd671972010-10-04 16:33:58 +02001821
Derek Jones55acc8b2009-07-11 03:38:13 +00001822 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001823 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001824 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001825 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001826 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001828 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 {
1830 $parts[$i] = $this->dbprefix.$parts[$i];
1831 }
Barry Mienydd671972010-10-04 16:33:58 +02001832
Derek Allard2067d1a2008-11-13 22:59:24 +00001833 // Put the parts back together
1834 $item = implode('.', $parts);
1835 }
Barry Mienydd671972010-10-04 16:33:58 +02001836
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 if ($protect_identifiers === TRUE)
1838 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001839 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 }
Barry Mienydd671972010-10-04 16:33:58 +02001841
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 return $item.$alias;
1843 }
1844
Andrey Andreev4c202602012-03-06 20:34:52 +02001845 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001846 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001848 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001849 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001850 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001851 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001852 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001854 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 {
1856 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001857 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 }
1859
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001860 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001862 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 }
Barry Mienydd671972010-10-04 16:33:58 +02001864
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 return $item.$alias;
1866 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001867
Túbal Martín511f2252011-11-24 14:43:45 +01001868 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001869
Túbal Martín511f2252011-11-24 14:43:45 +01001870 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001871 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001872 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001873 *
Túbal Martín511f2252011-11-24 14:43:45 +01001874 * @return void
1875 */
1876 protected function _reset_select()
1877 {
Túbal Martín511f2252011-11-24 14:43:45 +01001878 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001879
1880}
1881
Derek Allard2067d1a2008-11-13 22:59:24 +00001882/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001883/* Location: ./system/database/DB_driver.php */