blob: f066b58de105e203a2b28a5571a4db203df45b1b [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100390 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_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
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100408 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_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 }
427 return FALSE;
428 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200431 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200432 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // --------------------------------------------------------------------
436
437 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200438 * Reconnect
439 *
440 * Keep / reestablish the db connection if no queries have been
441 * sent for a length of time exceeding the server's idle timeout.
442 *
443 * This is just a dummy method to allow drivers without such
444 * functionality to not declare it, while others will override it.
445 *
446 * @return void
447 */
448 public function reconnect()
449 {
450 }
451
452 // --------------------------------------------------------------------
453
454 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300455 * Select database
456 *
457 * This is just a dummy method to allow drivers without such
458 * functionality to not declare it, while others will override it.
459 *
460 * @return bool
461 */
462 public function db_select()
463 {
464 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Set client character set
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200473 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200475 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200477 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200479 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 if ($this->db_debug)
482 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200483 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 return FALSE;
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 return TRUE;
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 // --------------------------------------------------------------------
493
494 /**
495 * The name of the platform in use (mysql, mssql, etc...)
496 *
Barry Mienydd671972010-10-04 16:33:58 +0200497 * @return string
498 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500499 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 return $this->dbdriver;
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200507 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200509 * Returns a string containing the version of the database being used.
510 * Most drivers will override this method.
511 *
Barry Mienydd671972010-10-04 16:33:58 +0200512 * @return string
513 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200514 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200516 if (isset($this->data_cache['version']))
517 {
518 return $this->data_cache['version'];
519 }
520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 if (FALSE === ($sql = $this->_version()))
522 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200523 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
Derek Allard3683f772009-12-16 17:32:33 +0000525
Andrey Andreev08856b82012-03-03 03:19:28 +0200526 $query = $this->query($sql);
527 $query = $query->row();
528 return $this->data_cache['version'] = $query->ver;
529 }
Derek Allard3683f772009-12-16 17:32:33 +0000530
Andrey Andreev08856b82012-03-03 03:19:28 +0200531 // --------------------------------------------------------------------
532
533 /**
534 * Version number query string
535 *
536 * @return string
537 */
538 protected function _version()
539 {
540 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 // --------------------------------------------------------------------
544
545 /**
546 * Execute the query
547 *
548 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200549 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * upon successful execution of a "write" type query. Returns boolean
551 * FALSE upon failure, and if the $db_debug variable is set to TRUE
552 * will raise an error.
553 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300554 * @param string $sql
555 * @param array $binds = FALSE An array of binding data
556 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200557 * @return mixed
558 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300559 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100561 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200563 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200564 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300566 elseif ( ! is_bool($return_object))
567 {
568 $return_object = ! $this->is_write_type($sql);
569 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000570
571 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100572 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600573 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200574 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
Derek Jonese7792202010-03-02 17:24:46 -0600576
Ryan Dialef7474c2012-03-01 16:11:36 -0500577 // Compile binds if needed
578 if ($binds !== FALSE)
579 {
580 $sql = $this->compile_binds($sql, $binds);
581 }
582
Andrey Andreev4c202602012-03-06 20:34:52 +0200583 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // we will load the caching class and return the previously
585 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300586 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200588 $this->load_rdriver();
589 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200591 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 }
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Andrey Andreevb66664b2012-06-27 14:22:10 +0300595 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100596 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 $this->queries[] = $sql;
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200602 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // Run the Query
605 if (FALSE === ($this->result_id = $this->simple_query($sql)))
606 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100607 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 $this->query_times[] = 0;
610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // This will trigger a rollback if transactions are being used
613 $this->_trans_status = FALSE;
614
Andrey Andreev4be5de12012-03-02 15:45:41 +0200615 // Grab the error now, as we might run some additional queries before displaying the error
616 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200617
618 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300619 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 if ($this->db_debug)
622 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200624 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200625 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // transactions to remain in limbo.
Andrey Andreev6c854422013-10-21 13:58:15 +0300627 if ($this->_trans_depth !== 0)
628 {
629 do
630 {
631 $this->trans_complete();
632 }
633 while ($this->_trans_depth !== 0);
634 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000635
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200636 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200637 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 return FALSE;
641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200644 $time_end = microtime(TRUE);
645 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000646
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100647 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
dixyab3cab52012-04-21 00:58:00 +0200649 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // Increment the query counter
653 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200654
Andrey Andreevb66664b2012-06-27 14:22:10 +0300655 // Will we have a result object instantiated? If not - we'll simply return TRUE
656 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300658 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100659 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 $this->CACHE->delete();
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 return TRUE;
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 if ($return_object !== TRUE)
669 {
670 return TRUE;
671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
673 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200674 $driver = $this->load_rdriver();
675 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200676
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200677 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100679 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
681 // We'll create a new instance of the result object
682 // only without the platform specific driver since
683 // we can't use it with cached data (the query result
684 // resource ID won't be any good once we've cached the
685 // result object, so we'll have to compile the data
686 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200687 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 $CR->result_object = $RES->result_object();
689 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200690 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 // Reset these since cached objects can not utilize resource IDs.
693 $CR->conn_id = NULL;
694 $CR->result_id = NULL;
695
696 $this->CACHE->write($sql, $CR);
697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 return $RES;
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * Load the result drivers
706 *
Barry Mienydd671972010-10-04 16:33:58 +0200707 * @return string the name of the result class
708 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500709 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 $driver = 'CI_DB_'.$this->dbdriver.'_result';
712
vlakofffadb8222013-05-12 10:57:09 +0200713 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
Greg Aker3a746652011-04-19 10:59:47 -0500715 include_once(BASEPATH.'database/DB_result.php');
716 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 }
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 return $driver;
720 }
Barry Mienydd671972010-10-04 16:33:58 +0200721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 // --------------------------------------------------------------------
723
724 /**
725 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200726 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 * we only use it when running transaction commands since they do
728 * not require all the features of the main query() function.
729 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200731 * @return mixed
732 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500733 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
735 if ( ! $this->conn_id)
736 {
737 $this->initialize();
738 }
739
740 return $this->_execute($sql);
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
744
745 /**
746 * Disable Transactions
747 * This permits transactions to be disabled at run-time.
748 *
Barry Mienydd671972010-10-04 16:33:58 +0200749 * @return void
750 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500751 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
753 $this->trans_enabled = FALSE;
754 }
755
756 // --------------------------------------------------------------------
757
758 /**
759 * Enable/disable Transaction Strict Mode
760 * When strict mode is enabled, if you are running multiple groups of
761 * transactions, if one group fails all groups will be rolled back.
762 * If strict mode is disabled, each group is treated autonomously, meaning
763 * a failure of one group will not affect any others
764 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300765 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200766 * @return void
767 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500768 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 {
770 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 // --------------------------------------------------------------------
774
775 /**
776 * Start Transaction
777 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300778 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200779 * @return void
780 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500781 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200782 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 if ( ! $this->trans_enabled)
784 {
785 return FALSE;
786 }
787
788 // When transactions are nested we only begin/commit/rollback the outermost ones
789 if ($this->_trans_depth > 0)
790 {
791 $this->_trans_depth += 1;
792 return;
793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500796 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
798
799 // --------------------------------------------------------------------
800
801 /**
802 * Complete Transaction
803 *
Barry Mienydd671972010-10-04 16:33:58 +0200804 * @return bool
805 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500806 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 {
808 if ( ! $this->trans_enabled)
809 {
810 return FALSE;
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // When transactions are nested we only begin/commit/rollback the outermost ones
814 if ($this->_trans_depth > 1)
815 {
816 $this->_trans_depth -= 1;
817 return TRUE;
818 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500819 else
820 {
821 $this->_trans_depth = 0;
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900825 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 // If we are NOT running in strict mode, we will reset
830 // the _trans_status flag so that subsequent groups of transactions
831 // will be permitted.
832 if ($this->trans_strict === FALSE)
833 {
834 $this->_trans_status = TRUE;
835 }
836
837 log_message('debug', 'DB Transaction Failure');
838 return FALSE;
839 }
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 $this->trans_commit();
842 return TRUE;
843 }
844
845 // --------------------------------------------------------------------
846
847 /**
848 * Lets you retrieve the transaction flag to determine if it has failed
849 *
Barry Mienydd671972010-10-04 16:33:58 +0200850 * @return bool
851 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500852 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 return $this->_trans_status;
855 }
856
857 // --------------------------------------------------------------------
858
859 /**
860 * Compile Bindings
861 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 * @param string the sql statement
863 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200864 * @return string
865 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500866 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300868 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
870 return $sql;
871 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300872 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300874 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300875 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300877 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200878 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300879 // Make sure we're using numeric keys
880 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300881 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 }
883
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300884 // We'll need the marker length later
885 $ml = strlen($this->bind_marker);
886
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300887 // Make sure not to replace a chunk inside a string that happens to match the bind marker
888 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300890 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300891 str_replace($matches[0],
892 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
893 $sql, $c),
894 $matches, PREG_OFFSET_CAPTURE);
895
896 // Bind values' count must match the count of markers in the query
897 if ($bind_count !== $c)
898 {
899 return $sql;
900 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300901 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300902 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 +0300903 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300904 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 }
906
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300907 do
908 {
909 $c--;
910 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
911 }
912 while ($c !== 0);
913
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300914 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 // --------------------------------------------------------------------
918
919 /**
920 * Determines if a query is a "write" type.
921 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200923 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200924 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200925 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200927 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 +0000928 }
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 // --------------------------------------------------------------------
931
932 /**
933 * Calculate the aggregate query elapsed time
934 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200935 * @param int The number of decimal places
936 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200937 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500938 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 return number_format($this->benchmark, $decimals);
941 }
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 // --------------------------------------------------------------------
944
945 /**
946 * Returns the total number of queries
947 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200948 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200949 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500950 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 return $this->query_count;
953 }
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 // --------------------------------------------------------------------
956
957 /**
958 * Returns the last query that was executed
959 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200960 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200961 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500962 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
964 return end($this->queries);
965 }
966
967 // --------------------------------------------------------------------
968
969 /**
970 * "Smart" Escape String
971 *
972 * Escapes data based on type
973 * Sets boolean and null types
974 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200976 * @return mixed
977 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500978 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200979 {
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200980 if (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200982 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000983 }
984 elseif (is_bool($str))
985 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200986 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000987 }
vlakoff1228fe22013-01-14 01:30:09 +0100988 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +0000989 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200990 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000991 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000992
993 return $str;
994 }
995
996 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600997
Derek Jonese4ed5832009-02-20 21:44:59 +0000998 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200999 * Escape String
1000 *
1001 * @param string $str
1002 * @param bool $like Whether or not the string will be used in a LIKE condition
1003 * @return string
1004 */
1005 public function escape_str($str, $like = FALSE)
1006 {
1007 if (is_array($str))
1008 {
1009 foreach ($str as $key => $val)
1010 {
1011 $str[$key] = $this->escape_str($val, $like);
1012 }
1013
1014 return $str;
1015 }
1016
1017 $str = $this->_escape_str($str);
1018
1019 // escape LIKE condition wildcards
1020 if ($like === TRUE)
1021 {
1022 return str_replace(array($this->_like_escape_chr, '%', '_'),
1023 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1024 $str);
1025 }
1026
1027 return $str;
1028 }
1029
1030 // --------------------------------------------------------------------
1031
1032 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001033 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001034 *
1035 * Calls the individual driver for platform
1036 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001037 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001038 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001039 * @return mixed
1040 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001041 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001042 {
1043 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001044 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001045
Derek Jonese4ed5832009-02-20 21:44:59 +00001046 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001049 * Platform-dependant string escape
1050 *
1051 * @param string
1052 * @return string
1053 */
1054 protected function _escape_str($str)
1055 {
1056 return str_replace("'", "''", remove_invisible_characters($str));
1057 }
1058
1059 // --------------------------------------------------------------------
1060
1061 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * Primary
1063 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001064 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * position is the primary key
1066 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001068 * @return string
1069 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001070 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +02001071 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001073 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
1075
1076 // --------------------------------------------------------------------
1077
1078 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001079 * "Count All" query
1080 *
1081 * Generates a platform-specific query string that counts all records in
1082 * the specified database
1083 *
1084 * @param string
1085 * @return int
1086 */
1087 public function count_all($table = '')
1088 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001089 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001090 {
1091 return 0;
1092 }
1093
1094 $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 +01001095 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001096 {
1097 return 0;
1098 }
1099
1100 $query = $query->row();
1101 $this->_reset_select();
1102 return (int) $query->numrows;
1103 }
1104
1105 // --------------------------------------------------------------------
1106
1107 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 * Returns an array of table names
1109 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001110 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001111 * @return array
1112 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001113 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 {
1115 // Is there a cached result?
1116 if (isset($this->data_cache['table_names']))
1117 {
1118 return $this->data_cache['table_names'];
1119 }
Barry Mienydd671972010-10-04 16:33:58 +02001120
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1122 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001123 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 }
1125
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001126 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001128
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001129 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001131 // Do we know from which column to get the table name?
1132 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001134 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001135 {
1136 $key = 'table_name';
1137 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001138 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001139 {
1140 $key = 'TABLE_NAME';
1141 }
1142 else
1143 {
1144 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001145 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001146 * E_STRICT is on, this would trigger a warning. So we'll have to
1147 * assign it first.
1148 */
1149 $key = array_keys($row);
1150 $key = array_shift($key);
1151 }
Taufan Aditya18209332012-02-09 16:07:27 +07001152 }
1153
Andrey Andreev12567e82012-01-25 22:50:33 +02001154 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 }
1156
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 return $this->data_cache['table_names'];
1158 }
Barry Mienydd671972010-10-04 16:33:58 +02001159
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 // --------------------------------------------------------------------
1161
1162 /**
1163 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001164 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001165 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001166 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001168 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001169 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001170 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 }
Barry Mienydd671972010-10-04 16:33:58 +02001172
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 // --------------------------------------------------------------------
1174
1175 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001176 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001179 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001181 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
1183 // Is there a cached result?
1184 if (isset($this->data_cache['field_names'][$table]))
1185 {
1186 return $this->data_cache['field_names'][$table];
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001189 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001191 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 }
Barry Mienydd671972010-10-04 16:33:58 +02001193
Greg Aker1edde302010-01-26 00:17:01 +00001194 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001196 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 }
Barry Mienydd671972010-10-04 16:33:58 +02001198
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001200 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001201
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001202 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001204 // Do we know from where to get the column's name?
1205 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001207 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001208 {
1209 $key = 'column_name';
1210 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001211 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001212 {
1213 $key = 'COLUMN_NAME';
1214 }
1215 else
1216 {
RJ garcia395e2df2013-03-21 10:48:24 -05001217 // We have no other choice but to just get the first element's key.
1218 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001219 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001221
1222 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 return $this->data_cache['field_names'][$table];
1226 }
1227
1228 // --------------------------------------------------------------------
1229
1230 /**
1231 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001232 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 * @param string
1234 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001235 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001237 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001238 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001239 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 }
Barry Mienydd671972010-10-04 16:33:58 +02001241
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 // --------------------------------------------------------------------
1243
1244 /**
1245 * Returns an object with field data
1246 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001248 * @return object
1249 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001250 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001252 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001254 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 }
Barry Mienydd671972010-10-04 16:33:58 +02001256
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001257 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +02001259 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001260
1261 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001264 * Escape the SQL Identifiers
1265 *
1266 * This function escapes column and table names
1267 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001268 * @param mixed
1269 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001270 */
1271 public function escape_identifiers($item)
1272 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001273 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001274 {
1275 return $item;
1276 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001277 elseif (is_array($item))
1278 {
1279 foreach ($item as $key => $value)
1280 {
1281 $item[$key] = $this->escape_identifiers($value);
1282 }
1283
1284 return $item;
1285 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001286 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001287 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001288 {
1289 return $item;
1290 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001291
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001292 static $preg_ec = array();
1293
1294 if (empty($preg_ec))
1295 {
1296 if (is_array($this->_escape_char))
1297 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001298 $preg_ec = array(
Andrey Andreeve4742582012-10-25 13:25:13 +03001299 preg_quote($this->_escape_char[0], '/'), preg_quote($this->_escape_char[1], '/'),
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001300 $this->_escape_char[0], $this->_escape_char[1]
1301 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001302 }
1303 else
1304 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001305 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001306 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001307 }
1308 }
1309
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001310 foreach ($this->_reserved_identifiers as $id)
1311 {
1312 if (strpos($item, '.'.$id) !== FALSE)
1313 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001314 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 +03001315 }
1316 }
1317
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001318 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 +03001319 }
1320
1321 // --------------------------------------------------------------------
1322
1323 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 * Generate an insert string
1325 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 * @param string the table upon which the query will be performed
1327 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001328 * @return string
1329 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001330 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001332 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001333
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001334 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001336 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 $values[] = $this->escape($val);
1338 }
Barry Mienydd671972010-10-04 16:33:58 +02001339
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001340 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001341 }
1342
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 // --------------------------------------------------------------------
1344
1345 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001346 * Insert statement
1347 *
1348 * Generates a platform-specific insert string from the supplied data
1349 *
1350 * @param string the table name
1351 * @param array the insert keys
1352 * @param array the insert values
1353 * @return string
1354 */
1355 protected function _insert($table, $keys, $values)
1356 {
1357 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1358 }
1359
1360 // --------------------------------------------------------------------
1361
1362 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 * Generate an update string
1364 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 * @param string the table upon which the query will be performed
1366 * @param array an associative array data of key/values
1367 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001368 * @return string
1369 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001370 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001372 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001374 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 }
Barry Mienydd671972010-10-04 16:33:58 +02001376
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001377 $this->where($where);
1378
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001380 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001382 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 }
1384
Andrey Andreev79f888b2013-08-06 13:59:23 +03001385 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1386 $this->_reset_write();
1387 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001388 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001389
1390 // --------------------------------------------------------------------
1391
1392 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001393 * Update statement
1394 *
1395 * Generates a platform-specific update string from the supplied data
1396 *
1397 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001398 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001399 * @return string
1400 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001401 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001402 {
1403 foreach ($values as $key => $val)
1404 {
1405 $valstr[] = $key.' = '.$val;
1406 }
1407
Andrey Andreev75546112012-07-05 11:01:29 +03001408 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001409 .$this->_compile_wh('qb_where')
1410 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001411 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001412 }
1413
1414 // --------------------------------------------------------------------
1415
1416 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 * Tests whether the string has an SQL operator
1418 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 * @param string
1420 * @return bool
1421 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001422 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 {
Andrey Andreev295cfa92013-12-20 11:48:38 +02001424 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 +00001425 }
1426
1427 // --------------------------------------------------------------------
1428
1429 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001430 * Returns the SQL string operator
1431 *
1432 * @param string
1433 * @return string
1434 */
1435 protected function _get_operator($str)
1436 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001437 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001438
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001439 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001440 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001441 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001442 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001443 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001444 $_operators = array(
1445 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1446 '\s*<>?\s*', // <, <>
1447 '\s*>\s*', // >
1448 '\s+IS NULL', // IS NULL
Andrey Andreev295cfa92013-12-20 11:48:38 +02001449 '\s+IS NOT NULL', // IS NOT NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001450 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001451 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001452 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1453 '\s+IN\s*\([^\)]+\)', // IN(list)
1454 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1455 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1456 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1457 );
1458
1459 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001460
Andrey Andreev6e704752012-07-18 00:46:33 +03001461 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1462 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001463 }
1464
1465 // --------------------------------------------------------------------
1466
1467 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1469 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001470 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001471 * @return mixed
1472 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001473 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001475 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001476
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 if (FALSE === strpos($driver, $function))
1478 {
1479 $function = $driver.$function;
1480 }
Barry Mienydd671972010-10-04 16:33:58 +02001481
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 if ( ! function_exists($function))
1483 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001484 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001486
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001487 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001488 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001489 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 }
1491
1492 // --------------------------------------------------------------------
1493
1494 /**
1495 * Set Cache Directory Path
1496 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001497 * @param string the path to the cache directory
1498 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001499 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001500 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 {
1502 $this->cachedir = $path;
1503 }
1504
1505 // --------------------------------------------------------------------
1506
1507 /**
1508 * Enable Query Caching
1509 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001510 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001511 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001512 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001514 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 }
1516
1517 // --------------------------------------------------------------------
1518
1519 /**
1520 * Disable Query Caching
1521 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001522 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001523 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001524 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001526 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 }
Barry Mienydd671972010-10-04 16:33:58 +02001528
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 // --------------------------------------------------------------------
1530
1531 /**
1532 * Delete the cache files associated with a particular URI
1533 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001534 * @param string $segment_one = ''
1535 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001536 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001537 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001538 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001540 return ($this->_cache_init())
1541 ? $this->CACHE->delete($segment_one, $segment_two)
1542 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 }
1544
1545 // --------------------------------------------------------------------
1546
1547 /**
1548 * Delete All cache files
1549 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001550 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001551 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001552 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001554 return ($this->_cache_init())
1555 ? $this->CACHE->delete_all()
1556 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 }
1558
1559 // --------------------------------------------------------------------
1560
1561 /**
1562 * Initialize the Cache Class
1563 *
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 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 {
Andrey Andreev49e68de2013-02-21 16:30:55 +02001568 if (class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001570 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001571 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001572 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001573 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001575 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1576 {
1577 return $this->cache_off();
1578 }
Derek Allarde37ab382009-02-03 16:13:57 +00001579
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1581 return TRUE;
1582 }
1583
1584 // --------------------------------------------------------------------
1585
1586 /**
1587 * Close DB Connection
1588 *
Barry Mienydd671972010-10-04 16:33:58 +02001589 * @return void
1590 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001591 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001593 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001595 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001596 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 }
Barry Mienydd671972010-10-04 16:33:58 +02001599
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 // --------------------------------------------------------------------
1601
1602 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001603 * Close DB Connection
1604 *
1605 * This method would be overriden by most of the drivers.
1606 *
1607 * @return void
1608 */
1609 protected function _close()
1610 {
1611 $this->conn_id = FALSE;
1612 }
1613
1614 // --------------------------------------------------------------------
1615
1616 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 * Display an error message
1618 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 * @param string the error message
1620 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001621 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001622 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001623 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001624 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 {
Derek Jonese7792202010-03-02 17:24:46 -06001626 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 $LANG->load('db');
1628
1629 $heading = $LANG->line('db_error_heading');
1630
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001631 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001633 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 }
1635 else
1636 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001637 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 }
Barry Mienydd671972010-10-04 16:33:58 +02001639
Pascal Kriete60f8c392010-08-25 18:03:28 +02001640 // Find the most likely culprit of the error by going through
1641 // the backtrace until the source file is no longer in the
1642 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001643 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001644 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001645 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001646 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001647 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001648 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1649 if (DIRECTORY_SEPARATOR !== '/')
1650 {
1651 $call['file'] = str_replace('\\', '/', $call['file']);
1652 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001653
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001654 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1655 {
1656 // Found it - use a relative path for safety
1657 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1658 $message[] = 'Line Number: '.$call['line'];
1659 break;
1660 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001661 }
1662 }
Barry Mienydd671972010-10-04 16:33:58 +02001663
Derek Jonese7792202010-03-02 17:24:46 -06001664 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 echo $error->show_error($heading, $message, 'error_db');
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -07001666 exit(EXIT_DATABASE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 }
1668
1669 // --------------------------------------------------------------------
1670
1671 /**
1672 * Protect Identifiers
1673 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001674 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001675 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001677 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001678 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 *
1680 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1681 *
1682 * Or a query with aliasing:
1683 *
1684 * SELECT m.member_id, m.member_name FROM members AS m
1685 *
1686 * Since the column name can include up to four segments (host, DB, table, column)
1687 * or also have an alias prefix, we need to do a bit of work to figure this out and
1688 * insert the table prefix (if it exists) in the proper position, and escape only
1689 * the correct identifiers.
1690 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 * @param string
1692 * @param bool
1693 * @param mixed
1694 * @param bool
1695 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001696 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001697 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 {
1699 if ( ! is_bool($protect_identifiers))
1700 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001701 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 }
Derek Allarde37ab382009-02-03 16:13:57 +00001703
1704 if (is_array($item))
1705 {
1706 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001707 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001708 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001709 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001710 }
1711
1712 return $escaped_array;
1713 }
1714
Derek Allard911d3e02008-12-15 14:08:35 +00001715 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001716 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001717 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001718 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001719 //
1720 // Added exception for single quotes as well, we don't want to alter
1721 // literal strings. -- Narf
1722 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001723 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001724 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001725 }
1726
Andrey Andreevbf940582012-06-10 07:05:05 +03001727 // Convert tabs or multiple spaces into single spaces
1728 $item = preg_replace('/\s+/', ' ', $item);
1729
Andrey Andreevbf940582012-06-10 07:05:05 +03001730 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001731 // Note: strripos() is used in order to support spaces in table names
1732 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001733 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001734 $alias = ($protect_identifiers)
1735 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1736 : substr($item, $offset);
1737 $item = substr($item, 0, $offset);
1738 }
1739 elseif ($offset = strrpos($item, ' '))
1740 {
1741 $alias = ($protect_identifiers)
1742 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1743 : substr($item, $offset);
1744 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001745 }
1746 else
1747 {
1748 $alias = '';
1749 }
1750
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 // Break the string apart if it contains periods, then insert the table prefix
1752 // in the correct location, assuming the period doesn't indicate that we're dealing
1753 // with an alias. While we're at it, we will escape the components
1754 if (strpos($item, '.') !== FALSE)
1755 {
1756 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001757
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001759 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001761 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001762 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 if ($protect_identifiers === TRUE)
1764 {
1765 foreach ($parts as $key => $val)
1766 {
1767 if ( ! in_array($val, $this->_reserved_identifiers))
1768 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001769 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 }
1771 }
Barry Mienydd671972010-10-04 16:33:58 +02001772
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001774 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001775
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 return $item.$alias;
1777 }
Barry Mienydd671972010-10-04 16:33:58 +02001778
Andrey Andreev4c202602012-03-06 20:34:52 +02001779 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001780 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 {
1782 // We now add the table prefix based on some logic.
1783 // Do we have 4 segments (hostname.database.table.column)?
1784 // If so, we add the table prefix to the column name in the 3rd segment.
1785 if (isset($parts[3]))
1786 {
1787 $i = 2;
1788 }
1789 // Do we have 3 segments (database.table.column)?
1790 // If so, we add the table prefix to the column name in 2nd position
1791 elseif (isset($parts[2]))
1792 {
1793 $i = 1;
1794 }
1795 // Do we have 2 segments (table.column)?
1796 // If so, we add the table prefix to the column name in 1st segment
1797 else
1798 {
1799 $i = 0;
1800 }
Barry Mienydd671972010-10-04 16:33:58 +02001801
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 // This flag is set when the supplied $item does not contain a field name.
1803 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001804 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 {
1806 $i++;
1807 }
Barry Mienydd671972010-10-04 16:33:58 +02001808
Derek Jones55acc8b2009-07-11 03:38:13 +00001809 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001810 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001811 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001812 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001813 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001815 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 {
1817 $parts[$i] = $this->dbprefix.$parts[$i];
1818 }
Barry Mienydd671972010-10-04 16:33:58 +02001819
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 // Put the parts back together
1821 $item = implode('.', $parts);
1822 }
Barry Mienydd671972010-10-04 16:33:58 +02001823
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 if ($protect_identifiers === TRUE)
1825 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001826 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 }
Barry Mienydd671972010-10-04 16:33:58 +02001828
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 return $item.$alias;
1830 }
1831
Andrey Andreev4c202602012-03-06 20:34:52 +02001832 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001833 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001835 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001836 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001837 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001838 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001839 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001841 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 {
1843 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001844 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 }
1846
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001847 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001849 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 }
Barry Mienydd671972010-10-04 16:33:58 +02001851
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 return $item.$alias;
1853 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001854
Túbal Martín511f2252011-11-24 14:43:45 +01001855 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001856
Túbal Martín511f2252011-11-24 14:43:45 +01001857 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001858 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001859 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001860 *
Túbal Martín511f2252011-11-24 14:43:45 +01001861 * @return void
1862 */
1863 protected function _reset_select()
1864 {
Túbal Martín511f2252011-11-24 14:43:45 +01001865 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001866
1867}
1868
Derek Allard2067d1a2008-11-13 22:59:24 +00001869/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001870/* Location: ./system/database/DB_driver.php */