blob: 26791398a0333da7a1c3bba2f58181f1e8bf7432 [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
431 // ----------------------------------------------------------------
432
433 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200434 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200436 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200437
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200438 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200440 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200442 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200445 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200446 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // --------------------------------------------------------------------
450
451 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200452 * Reconnect
453 *
454 * Keep / reestablish the db connection if no queries have been
455 * sent for a length of time exceeding the server's idle timeout.
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 void
461 */
462 public function reconnect()
463 {
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300469 * Select database
470 *
471 * This is just a dummy method to allow drivers without such
472 * functionality to not declare it, while others will override it.
473 *
474 * @return bool
475 */
476 public function db_select()
477 {
478 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480
481 // --------------------------------------------------------------------
482
483 /**
484 * Set client character set
485 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200487 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200489 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200491 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200493 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 if ($this->db_debug)
496 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200497 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 return FALSE;
501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 return TRUE;
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // --------------------------------------------------------------------
507
508 /**
509 * The name of the platform in use (mysql, mssql, etc...)
510 *
Barry Mienydd671972010-10-04 16:33:58 +0200511 * @return string
512 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500513 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 return $this->dbdriver;
516 }
517
518 // --------------------------------------------------------------------
519
520 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200521 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200523 * Returns a string containing the version of the database being used.
524 * Most drivers will override this method.
525 *
Barry Mienydd671972010-10-04 16:33:58 +0200526 * @return string
527 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200528 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200530 if (isset($this->data_cache['version']))
531 {
532 return $this->data_cache['version'];
533 }
534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 if (FALSE === ($sql = $this->_version()))
536 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200537 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
Derek Allard3683f772009-12-16 17:32:33 +0000539
Andrey Andreev08856b82012-03-03 03:19:28 +0200540 $query = $this->query($sql);
541 $query = $query->row();
542 return $this->data_cache['version'] = $query->ver;
543 }
Derek Allard3683f772009-12-16 17:32:33 +0000544
Andrey Andreev08856b82012-03-03 03:19:28 +0200545 // --------------------------------------------------------------------
546
547 /**
548 * Version number query string
549 *
550 * @return string
551 */
552 protected function _version()
553 {
554 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 // --------------------------------------------------------------------
558
559 /**
560 * Execute the query
561 *
562 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200563 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * upon successful execution of a "write" type query. Returns boolean
565 * FALSE upon failure, and if the $db_debug variable is set to TRUE
566 * will raise an error.
567 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300568 * @param string $sql
569 * @param array $binds = FALSE An array of binding data
570 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200571 * @return mixed
572 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300573 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100575 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200577 log_message('error', 'Invalid query: '.$sql);
578
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200579 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300581 elseif ( ! is_bool($return_object))
582 {
583 $return_object = ! $this->is_write_type($sql);
584 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000585
586 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100587 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600588 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200589 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
Derek Jonese7792202010-03-02 17:24:46 -0600591
Ryan Dialef7474c2012-03-01 16:11:36 -0500592 // Compile binds if needed
593 if ($binds !== FALSE)
594 {
595 $sql = $this->compile_binds($sql, $binds);
596 }
597
Andrey Andreev4c202602012-03-06 20:34:52 +0200598 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // we will load the caching class and return the previously
600 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300601 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200603 $this->load_rdriver();
604 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200606 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Andrey Andreevb66664b2012-06-27 14:22:10 +0300610 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100611 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 $this->queries[] = $sql;
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200617 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // Run the Query
620 if (FALSE === ($this->result_id = $this->simple_query($sql)))
621 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100622 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 $this->query_times[] = 0;
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 // This will trigger a rollback if transactions are being used
628 $this->_trans_status = FALSE;
629
Andrey Andreev4be5de12012-03-02 15:45:41 +0200630 // Grab the error now, as we might run some additional queries before displaying the error
631 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200632
633 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300634 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 if ($this->db_debug)
637 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200639 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200640 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // transactions to remain in limbo.
642 $this->trans_complete();
643
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200644 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200645 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 return FALSE;
649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200652 $time_end = microtime(TRUE);
653 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000654
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100655 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 {
dixyab3cab52012-04-21 00:58:00 +0200657 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 // Increment the query counter
661 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200662
Andrey Andreevb66664b2012-06-27 14:22:10 +0300663 // Will we have a result object instantiated? If not - we'll simply return TRUE
664 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300666 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100667 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 $this->CACHE->delete();
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 return TRUE;
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 if ($return_object !== TRUE)
677 {
678 return TRUE;
679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
681 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200682 $driver = $this->load_rdriver();
683 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200684
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200685 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100687 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 // We'll create a new instance of the result object
690 // only without the platform specific driver since
691 // we can't use it with cached data (the query result
692 // resource ID won't be any good once we've cached the
693 // result object, so we'll have to compile the data
694 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200695 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 $CR->result_object = $RES->result_object();
697 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200698 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 // Reset these since cached objects can not utilize resource IDs.
701 $CR->conn_id = NULL;
702 $CR->result_id = NULL;
703
704 $this->CACHE->write($sql, $CR);
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 return $RES;
708 }
709
710 // --------------------------------------------------------------------
711
712 /**
713 * Load the result drivers
714 *
Barry Mienydd671972010-10-04 16:33:58 +0200715 * @return string the name of the result class
716 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500717 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
719 $driver = 'CI_DB_'.$this->dbdriver.'_result';
720
721 if ( ! class_exists($driver))
722 {
Greg Aker3a746652011-04-19 10:59:47 -0500723 include_once(BASEPATH.'database/DB_result.php');
724 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 }
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 return $driver;
728 }
Barry Mienydd671972010-10-04 16:33:58 +0200729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 // --------------------------------------------------------------------
731
732 /**
733 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200734 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 * we only use it when running transaction commands since they do
736 * not require all the features of the main query() function.
737 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200739 * @return mixed
740 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500741 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
743 if ( ! $this->conn_id)
744 {
745 $this->initialize();
746 }
747
748 return $this->_execute($sql);
749 }
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 // --------------------------------------------------------------------
752
753 /**
754 * Disable Transactions
755 * This permits transactions to be disabled at run-time.
756 *
Barry Mienydd671972010-10-04 16:33:58 +0200757 * @return void
758 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500759 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
761 $this->trans_enabled = FALSE;
762 }
763
764 // --------------------------------------------------------------------
765
766 /**
767 * Enable/disable Transaction Strict Mode
768 * When strict mode is enabled, if you are running multiple groups of
769 * transactions, if one group fails all groups will be rolled back.
770 * If strict mode is disabled, each group is treated autonomously, meaning
771 * a failure of one group will not affect any others
772 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300773 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200774 * @return void
775 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500776 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 {
778 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // --------------------------------------------------------------------
782
783 /**
784 * Start Transaction
785 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300786 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200787 * @return void
788 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500789 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200790 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 if ( ! $this->trans_enabled)
792 {
793 return FALSE;
794 }
795
796 // When transactions are nested we only begin/commit/rollback the outermost ones
797 if ($this->_trans_depth > 0)
798 {
799 $this->_trans_depth += 1;
800 return;
801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500804 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
806
807 // --------------------------------------------------------------------
808
809 /**
810 * Complete Transaction
811 *
Barry Mienydd671972010-10-04 16:33:58 +0200812 * @return bool
813 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500814 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
816 if ( ! $this->trans_enabled)
817 {
818 return FALSE;
819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 // When transactions are nested we only begin/commit/rollback the outermost ones
822 if ($this->_trans_depth > 1)
823 {
824 $this->_trans_depth -= 1;
825 return TRUE;
826 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500827 else
828 {
829 $this->_trans_depth = 0;
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 // The query() function will set this flag to FALSE in the event that a query failed
833 if ($this->_trans_status === FALSE)
834 {
835 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 // If we are NOT running in strict mode, we will reset
838 // the _trans_status flag so that subsequent groups of transactions
839 // will be permitted.
840 if ($this->trans_strict === FALSE)
841 {
842 $this->_trans_status = TRUE;
843 }
844
845 log_message('debug', 'DB Transaction Failure');
846 return FALSE;
847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 $this->trans_commit();
850 return TRUE;
851 }
852
853 // --------------------------------------------------------------------
854
855 /**
856 * Lets you retrieve the transaction flag to determine if it has failed
857 *
Barry Mienydd671972010-10-04 16:33:58 +0200858 * @return bool
859 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500860 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
862 return $this->_trans_status;
863 }
864
865 // --------------------------------------------------------------------
866
867 /**
868 * Compile Bindings
869 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @param string the sql statement
871 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200872 * @return string
873 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500874 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300876 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
878 return $sql;
879 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300880 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300882 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300883 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300885 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200886 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300887 // Make sure we're using numeric keys
888 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300889 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 }
891
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300892 // We'll need the marker length later
893 $ml = strlen($this->bind_marker);
894
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300895 // Make sure not to replace a chunk inside a string that happens to match the bind marker
896 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300898 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300899 str_replace($matches[0],
900 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
901 $sql, $c),
902 $matches, PREG_OFFSET_CAPTURE);
903
904 // Bind values' count must match the count of markers in the query
905 if ($bind_count !== $c)
906 {
907 return $sql;
908 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300909 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300910 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 +0300911 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300912 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
914
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300915 do
916 {
917 $c--;
918 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
919 }
920 while ($c !== 0);
921
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300922 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
926
927 /**
928 * Determines if a query is a "write" type.
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200931 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200932 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200933 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Andrey Andreev9e31f8f2012-10-05 17:31:46 +0300935 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 +0000936 }
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 // --------------------------------------------------------------------
939
940 /**
941 * Calculate the aggregate query elapsed time
942 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200943 * @param int The number of decimal places
944 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200945 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500946 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
948 return number_format($this->benchmark, $decimals);
949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 // --------------------------------------------------------------------
952
953 /**
954 * Returns the total number of queries
955 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200956 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200957 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500958 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
960 return $this->query_count;
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 // --------------------------------------------------------------------
964
965 /**
966 * Returns the last query that was executed
967 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200968 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200969 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500970 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
972 return end($this->queries);
973 }
974
975 // --------------------------------------------------------------------
976
977 /**
978 * "Smart" Escape String
979 *
980 * Escapes data based on type
981 * Sets boolean and null types
982 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200984 * @return mixed
985 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500986 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200987 {
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200988 if (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200990 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000991 }
992 elseif (is_bool($str))
993 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200994 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000995 }
vlakoff1228fe22013-01-14 01:30:09 +0100996 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +0000997 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200998 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000999 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001000
1001 return $str;
1002 }
1003
1004 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001005
Derek Jonese4ed5832009-02-20 21:44:59 +00001006 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001007 * Escape String
1008 *
1009 * @param string $str
1010 * @param bool $like Whether or not the string will be used in a LIKE condition
1011 * @return string
1012 */
1013 public function escape_str($str, $like = FALSE)
1014 {
1015 if (is_array($str))
1016 {
1017 foreach ($str as $key => $val)
1018 {
1019 $str[$key] = $this->escape_str($val, $like);
1020 }
1021
1022 return $str;
1023 }
1024
1025 $str = $this->_escape_str($str);
1026
1027 // escape LIKE condition wildcards
1028 if ($like === TRUE)
1029 {
1030 return str_replace(array($this->_like_escape_chr, '%', '_'),
1031 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1032 $str);
1033 }
1034
1035 return $str;
1036 }
1037
1038 // --------------------------------------------------------------------
1039
1040 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001041 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001042 *
1043 * Calls the individual driver for platform
1044 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001045 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001046 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001047 * @return mixed
1048 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001049 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001050 {
1051 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001052 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
Derek Jonese4ed5832009-02-20 21:44:59 +00001054 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001057 * Platform-dependant string escape
1058 *
1059 * @param string
1060 * @return string
1061 */
1062 protected function _escape_str($str)
1063 {
1064 return str_replace("'", "''", remove_invisible_characters($str));
1065 }
1066
1067 // --------------------------------------------------------------------
1068
1069 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 * Primary
1071 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001072 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 * position is the primary key
1074 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001076 * @return string
1077 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001078 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +02001079 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001081 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 }
1083
1084 // --------------------------------------------------------------------
1085
1086 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001087 * "Count All" query
1088 *
1089 * Generates a platform-specific query string that counts all records in
1090 * the specified database
1091 *
1092 * @param string
1093 * @return int
1094 */
1095 public function count_all($table = '')
1096 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001097 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001098 {
1099 return 0;
1100 }
1101
1102 $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 +01001103 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001104 {
1105 return 0;
1106 }
1107
1108 $query = $query->row();
1109 $this->_reset_select();
1110 return (int) $query->numrows;
1111 }
1112
1113 // --------------------------------------------------------------------
1114
1115 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * Returns an array of table names
1117 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001118 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001119 * @return array
1120 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001121 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
1123 // Is there a cached result?
1124 if (isset($this->data_cache['table_names']))
1125 {
1126 return $this->data_cache['table_names'];
1127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1130 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001131 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 }
1133
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001134 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001136
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001137 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001139 // Do we know from which column to get the table name?
1140 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001142 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001143 {
1144 $key = 'table_name';
1145 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001146 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001147 {
1148 $key = 'TABLE_NAME';
1149 }
1150 else
1151 {
1152 /* We have no other choice but to just get the first element's key.
1153 * Due to array_shift() accepting it's argument by reference, if
1154 * E_STRICT is on, this would trigger a warning. So we'll have to
1155 * assign it first.
1156 */
1157 $key = array_keys($row);
1158 $key = array_shift($key);
1159 }
Taufan Aditya18209332012-02-09 16:07:27 +07001160 }
1161
Andrey Andreev12567e82012-01-25 22:50:33 +02001162 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 }
1164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 return $this->data_cache['table_names'];
1166 }
Barry Mienydd671972010-10-04 16:33:58 +02001167
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 // --------------------------------------------------------------------
1169
1170 /**
1171 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001172 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001173 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001174 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001176 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001177 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001178 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 // --------------------------------------------------------------------
1182
1183 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001184 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001187 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001189 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
1191 // Is there a cached result?
1192 if (isset($this->data_cache['field_names'][$table]))
1193 {
1194 return $this->data_cache['field_names'][$table];
1195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001197 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001199 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 }
Barry Mienydd671972010-10-04 16:33:58 +02001201
Greg Aker1edde302010-01-26 00:17:01 +00001202 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001204 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001208 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001209
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001210 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001212 // Do we know from where to get the column's name?
1213 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001215 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001216 {
1217 $key = 'column_name';
1218 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001219 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001220 {
1221 $key = 'COLUMN_NAME';
1222 }
1223 else
1224 {
1225 /* We have no other choice but to just get the first element's key.
1226 * Due to array_shift() accepting it's argument by reference, if
1227 * E_STRICT is on, this would trigger a warning. So we'll have to
1228 * assign it first.
1229 */
1230 $key = array_keys($row);
1231 $key = array_shift($key);
1232 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001234
1235 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 return $this->data_cache['field_names'][$table];
1239 }
1240
1241 // --------------------------------------------------------------------
1242
1243 /**
1244 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001245 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 * @param string
1247 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001248 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001250 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001251 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001252 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 // --------------------------------------------------------------------
1256
1257 /**
1258 * Returns an object with field data
1259 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001261 * @return object
1262 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001263 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001265 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001267 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 }
Barry Mienydd671972010-10-04 16:33:58 +02001269
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001270 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +02001272 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001273
1274 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001277 * Escape the SQL Identifiers
1278 *
1279 * This function escapes column and table names
1280 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001281 * @param mixed
1282 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001283 */
1284 public function escape_identifiers($item)
1285 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001286 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001287 {
1288 return $item;
1289 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001290 elseif (is_array($item))
1291 {
1292 foreach ($item as $key => $value)
1293 {
1294 $item[$key] = $this->escape_identifiers($value);
1295 }
1296
1297 return $item;
1298 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001299 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001300 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001301 {
1302 return $item;
1303 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001304
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001305 static $preg_ec = array();
1306
1307 if (empty($preg_ec))
1308 {
1309 if (is_array($this->_escape_char))
1310 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001311 $preg_ec = array(
Andrey Andreeve4742582012-10-25 13:25:13 +03001312 preg_quote($this->_escape_char[0], '/'), preg_quote($this->_escape_char[1], '/'),
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001313 $this->_escape_char[0], $this->_escape_char[1]
1314 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001315 }
1316 else
1317 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001318 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001319 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001320 }
1321 }
1322
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001323 foreach ($this->_reserved_identifiers as $id)
1324 {
1325 if (strpos($item, '.'.$id) !== FALSE)
1326 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001327 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 +03001328 }
1329 }
1330
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001331 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 +03001332 }
1333
1334 // --------------------------------------------------------------------
1335
1336 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 * Generate an insert string
1338 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 * @param string the table upon which the query will be performed
1340 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001341 * @return string
1342 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001343 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001345 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001346
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001347 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001349 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 $values[] = $this->escape($val);
1351 }
Barry Mienydd671972010-10-04 16:33:58 +02001352
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001353 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001354 }
1355
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 // --------------------------------------------------------------------
1357
1358 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001359 * Insert statement
1360 *
1361 * Generates a platform-specific insert string from the supplied data
1362 *
1363 * @param string the table name
1364 * @param array the insert keys
1365 * @param array the insert values
1366 * @return string
1367 */
1368 protected function _insert($table, $keys, $values)
1369 {
1370 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1371 }
1372
1373 // --------------------------------------------------------------------
1374
1375 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 * Generate an update string
1377 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 * @param string the table upon which the query will be performed
1379 * @param array an associative array data of key/values
1380 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001381 * @return string
1382 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001383 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001385 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001387 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 }
Barry Mienydd671972010-10-04 16:33:58 +02001389
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001390 $this->where($where);
1391
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001393 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001395 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 }
1397
Andrey Andreevd337a262012-11-20 19:24:10 +02001398 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
Barry Mienydd671972010-10-04 16:33:58 +02001399 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001400
1401 // --------------------------------------------------------------------
1402
1403 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001404 * Update statement
1405 *
1406 * Generates a platform-specific update string from the supplied data
1407 *
1408 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001409 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001410 * @return string
1411 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001412 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001413 {
1414 foreach ($values as $key => $val)
1415 {
1416 $valstr[] = $key.' = '.$val;
1417 }
1418
Andrey Andreev75546112012-07-05 11:01:29 +03001419 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001420 .$this->_compile_wh('qb_where')
1421 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001422 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001423 }
1424
1425 // --------------------------------------------------------------------
1426
1427 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 * Tests whether the string has an SQL operator
1429 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 * @param string
1431 * @return bool
1432 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001433 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001435 return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 }
1437
1438 // --------------------------------------------------------------------
1439
1440 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001441 * Returns the SQL string operator
1442 *
1443 * @param string
1444 * @return string
1445 */
1446 protected function _get_operator($str)
1447 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001448 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001449
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001450 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001451 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001452 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001453 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001454 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001455 $_operators = array(
1456 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1457 '\s*<>?\s*', // <, <>
1458 '\s*>\s*', // >
1459 '\s+IS NULL', // IS NULL
1460 '\s+IS NOT NULL', // IS NOT NULL
1461 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1462 '\s+IN\s*\([^\)]+\)', // IN(list)
1463 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1464 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1465 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1466 );
1467
1468 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001469
Andrey Andreev6e704752012-07-18 00:46:33 +03001470 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1471 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001472 }
1473
1474 // --------------------------------------------------------------------
1475
1476 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1478 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001479 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001480 * @return mixed
1481 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001482 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001484 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001485
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 if (FALSE === strpos($driver, $function))
1487 {
1488 $function = $driver.$function;
1489 }
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 if ( ! function_exists($function))
1492 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001493 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001495
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001496 return (func_num_args() > 1)
1497 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1498 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 }
1500
1501 // --------------------------------------------------------------------
1502
1503 /**
1504 * Set Cache Directory Path
1505 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 * @param string the path to the cache directory
1507 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001508 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001509 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 {
1511 $this->cachedir = $path;
1512 }
1513
1514 // --------------------------------------------------------------------
1515
1516 /**
1517 * Enable Query Caching
1518 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001519 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001520 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001521 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001523 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 }
1525
1526 // --------------------------------------------------------------------
1527
1528 /**
1529 * Disable Query Caching
1530 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001531 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001532 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001533 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001535 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 }
Barry Mienydd671972010-10-04 16:33:58 +02001537
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 // --------------------------------------------------------------------
1539
1540 /**
1541 * Delete the cache files associated with a particular URI
1542 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001543 * @param string $segment_one = ''
1544 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001545 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001546 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001547 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001549 return ($this->_cache_init())
1550 ? $this->CACHE->delete($segment_one, $segment_two)
1551 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001552 }
1553
1554 // --------------------------------------------------------------------
1555
1556 /**
1557 * Delete All cache files
1558 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001559 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001560 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001561 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001563 return ($this->_cache_init())
1564 ? $this->CACHE->delete_all()
1565 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 }
1567
1568 // --------------------------------------------------------------------
1569
1570 /**
1571 * Initialize the Cache Class
1572 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001573 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001574 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001575 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001577 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001579 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001580 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001581 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001582 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001583 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001584 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1585 {
1586 return $this->cache_off();
1587 }
Derek Allarde37ab382009-02-03 16:13:57 +00001588
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1590 return TRUE;
1591 }
1592
1593 // --------------------------------------------------------------------
1594
1595 /**
1596 * Close DB Connection
1597 *
Barry Mienydd671972010-10-04 16:33:58 +02001598 * @return void
1599 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001600 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001602 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001604 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001605 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 }
Barry Mienydd671972010-10-04 16:33:58 +02001608
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 // --------------------------------------------------------------------
1610
1611 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001612 * Close DB Connection
1613 *
1614 * This method would be overriden by most of the drivers.
1615 *
1616 * @return void
1617 */
1618 protected function _close()
1619 {
1620 $this->conn_id = FALSE;
1621 }
1622
1623 // --------------------------------------------------------------------
1624
1625 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 * Display an error message
1627 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 * @param string the error message
1629 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001630 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001631 * @return string sends the application/error_db.php template
1632 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001633 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 {
Derek Jonese7792202010-03-02 17:24:46 -06001635 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 $LANG->load('db');
1637
1638 $heading = $LANG->line('db_error_heading');
1639
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001640 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001642 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 }
1644 else
1645 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001646 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 }
Barry Mienydd671972010-10-04 16:33:58 +02001648
Pascal Kriete60f8c392010-08-25 18:03:28 +02001649 // Find the most likely culprit of the error by going through
1650 // the backtrace until the source file is no longer in the
1651 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001652 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001653 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001654 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001655 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001656 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001657 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1658 if (DIRECTORY_SEPARATOR !== '/')
1659 {
1660 $call['file'] = str_replace('\\', '/', $call['file']);
1661 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001662
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001663 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1664 {
1665 // Found it - use a relative path for safety
1666 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1667 $message[] = 'Line Number: '.$call['line'];
1668 break;
1669 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001670 }
1671 }
Barry Mienydd671972010-10-04 16:33:58 +02001672
Derek Jonese7792202010-03-02 17:24:46 -06001673 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 echo $error->show_error($heading, $message, 'error_db');
1675 exit;
1676 }
1677
1678 // --------------------------------------------------------------------
1679
1680 /**
1681 * Protect Identifiers
1682 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001683 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001684 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001686 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001687 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 *
1689 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1690 *
1691 * Or a query with aliasing:
1692 *
1693 * SELECT m.member_id, m.member_name FROM members AS m
1694 *
1695 * Since the column name can include up to four segments (host, DB, table, column)
1696 * or also have an alias prefix, we need to do a bit of work to figure this out and
1697 * insert the table prefix (if it exists) in the proper position, and escape only
1698 * the correct identifiers.
1699 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 * @param string
1701 * @param bool
1702 * @param mixed
1703 * @param bool
1704 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001705 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001706 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 {
1708 if ( ! is_bool($protect_identifiers))
1709 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001710 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 }
Derek Allarde37ab382009-02-03 16:13:57 +00001712
1713 if (is_array($item))
1714 {
1715 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001716 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001717 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001718 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001719 }
1720
1721 return $escaped_array;
1722 }
1723
Derek Allard911d3e02008-12-15 14:08:35 +00001724 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001725 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001726 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001727 // way to deal with this, but I'm not thinking of it -- Rick
1728 if (strpos($item, '(') !== FALSE)
1729 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001730 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001731 }
1732
Andrey Andreevbf940582012-06-10 07:05:05 +03001733 // Convert tabs or multiple spaces into single spaces
1734 $item = preg_replace('/\s+/', ' ', $item);
1735
Andrey Andreevbf940582012-06-10 07:05:05 +03001736 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001737 // Note: strripos() is used in order to support spaces in table names
1738 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001739 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001740 $alias = ($protect_identifiers)
1741 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1742 : substr($item, $offset);
1743 $item = substr($item, 0, $offset);
1744 }
1745 elseif ($offset = strrpos($item, ' '))
1746 {
1747 $alias = ($protect_identifiers)
1748 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1749 : substr($item, $offset);
1750 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001751 }
1752 else
1753 {
1754 $alias = '';
1755 }
1756
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 // Break the string apart if it contains periods, then insert the table prefix
1758 // in the correct location, assuming the period doesn't indicate that we're dealing
1759 // with an alias. While we're at it, we will escape the components
1760 if (strpos($item, '.') !== FALSE)
1761 {
1762 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001763
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001765 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001767 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001768 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 if ($protect_identifiers === TRUE)
1770 {
1771 foreach ($parts as $key => $val)
1772 {
1773 if ( ! in_array($val, $this->_reserved_identifiers))
1774 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001775 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 }
1777 }
Barry Mienydd671972010-10-04 16:33:58 +02001778
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001780 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001781
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 return $item.$alias;
1783 }
Barry Mienydd671972010-10-04 16:33:58 +02001784
Andrey Andreev4c202602012-03-06 20:34:52 +02001785 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001786 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 {
1788 // We now add the table prefix based on some logic.
1789 // Do we have 4 segments (hostname.database.table.column)?
1790 // If so, we add the table prefix to the column name in the 3rd segment.
1791 if (isset($parts[3]))
1792 {
1793 $i = 2;
1794 }
1795 // Do we have 3 segments (database.table.column)?
1796 // If so, we add the table prefix to the column name in 2nd position
1797 elseif (isset($parts[2]))
1798 {
1799 $i = 1;
1800 }
1801 // Do we have 2 segments (table.column)?
1802 // If so, we add the table prefix to the column name in 1st segment
1803 else
1804 {
1805 $i = 0;
1806 }
Barry Mienydd671972010-10-04 16:33:58 +02001807
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 // This flag is set when the supplied $item does not contain a field name.
1809 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001810 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 {
1812 $i++;
1813 }
Barry Mienydd671972010-10-04 16:33:58 +02001814
Derek Jones55acc8b2009-07-11 03:38:13 +00001815 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001816 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001817 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001818 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001819 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001821 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
1823 $parts[$i] = $this->dbprefix.$parts[$i];
1824 }
Barry Mienydd671972010-10-04 16:33:58 +02001825
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 // Put the parts back together
1827 $item = implode('.', $parts);
1828 }
Barry Mienydd671972010-10-04 16:33:58 +02001829
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 if ($protect_identifiers === TRUE)
1831 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001832 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001833 }
Barry Mienydd671972010-10-04 16:33:58 +02001834
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 return $item.$alias;
1836 }
1837
Andrey Andreev4c202602012-03-06 20:34:52 +02001838 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001839 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001841 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001842 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001843 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001844 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001845 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001847 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 {
1849 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001850 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 }
1852
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001853 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001855 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 }
Barry Mienydd671972010-10-04 16:33:58 +02001857
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 return $item.$alias;
1859 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001860
Túbal Martín511f2252011-11-24 14:43:45 +01001861 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001862
Túbal Martín511f2252011-11-24 14:43:45 +01001863 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001864 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001865 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001866 *
Túbal Martín511f2252011-11-24 14:43:45 +01001867 * @return void
1868 */
1869 protected function _reset_select()
1870 {
Túbal Martín511f2252011-11-24 14:43:45 +01001871 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001872
1873}
1874
Derek Allard2067d1a2008-11-13 22:59:24 +00001875/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001876/* Location: ./system/database/DB_driver.php */