blob: 0b47073709a8efdc8c5cd019c3dbe793667ea088 [file] [log] [blame]
Etki640ff4e2014-04-09 23:01:33 +04001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Database Driver Class
42 *
43 * This is the platform-independent base DB implementation class.
44 * This class will not be called directly. Rather, the adapter
45 * class for the specific database will extend and instantiate it.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
Timothy Warren7eeda532012-03-19 16:01:55 -040053abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000054
Andrey Andreevae85eb42012-11-02 01:42:31 +020055 /**
56 * Data Source Name / Connect string
57 *
58 * @var string
59 */
Andrey Andreev738f5342012-03-06 20:43:40 +020060 public $dsn;
Andrey Andreevae85eb42012-11-02 01:42:31 +020061
62 /**
63 * Username
64 *
65 * @var string
66 */
Andrey Andreevd1add432012-03-06 20:26:01 +020067 public $username;
Andrey Andreevae85eb42012-11-02 01:42:31 +020068
69 /**
70 * Password
71 *
72 * @var string
73 */
Andrey Andreevd1add432012-03-06 20:26:01 +020074 public $password;
Andrey Andreevae85eb42012-11-02 01:42:31 +020075
76 /**
77 * Hostname
78 *
79 * @var string
80 */
Andrey Andreevd1add432012-03-06 20:26:01 +020081 public $hostname;
Andrey Andreevae85eb42012-11-02 01:42:31 +020082
83 /**
84 * Database name
85 *
86 * @var string
87 */
Andrey Andreevd1add432012-03-06 20:26:01 +020088 public $database;
Andrey Andreevae85eb42012-11-02 01:42:31 +020089
90 /**
91 * Database driver
92 *
93 * @var string
94 */
Andrey Andreev75546112012-07-05 11:01:29 +030095 public $dbdriver = 'mysqli';
Andrey Andreevae85eb42012-11-02 01:42:31 +020096
97 /**
98 * Sub-driver
99 *
100 * @used-by CI_DB_pdo_driver
101 * @var string
102 */
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300103 public $subdriver;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200104
105 /**
106 * Table prefix
107 *
108 * @var string
109 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200110 public $dbprefix = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200111
112 /**
113 * Character set
114 *
115 * @var string
116 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200117 public $char_set = 'utf8';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200118
119 /**
120 * Collation
121 *
122 * @var string
123 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200124 public $dbcollat = 'utf8_general_ci';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200125
126 /**
127 * Auto-init flag
128 *
129 * Whether to automatically initialize the DB connection.
130 *
131 * @var bool
132 */
133 public $autoinit = TRUE;
134
135 /**
136 * Encryption flag/data
137 *
138 * @var mixed
139 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300140 public $encrypt = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200141
142 /**
143 * Swap Prefix
144 *
145 * @var string
146 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200147 public $swap_pre = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200148
149 /**
150 * Database port
151 *
152 * @var int
153 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200154 public $port = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200155
156 /**
157 * Persistent connection flag
158 *
159 * @var bool
160 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200161 public $pconnect = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200162
163 /**
164 * Connection ID
165 *
166 * @var object|resource
167 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200168 public $conn_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200169
170 /**
171 * Result ID
172 *
173 * @var object|resource
174 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200175 public $result_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200176
177 /**
178 * Debug flag
179 *
180 * Whether to display error messages.
181 *
182 * @var bool
183 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200184 public $db_debug = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200185
186 /**
187 * Benchmark time
188 *
189 * @var int
190 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200191 public $benchmark = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200192
193 /**
194 * Executed queries count
195 *
196 * @var int
197 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200198 public $query_count = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200199
200 /**
201 * Bind marker
202 *
203 * Character used to identify values in a prepared statement.
204 *
205 * @var string
206 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200207 public $bind_marker = '?';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200208
209 /**
210 * Save queries flag
211 *
212 * Whether to keep an in-memory history of queries for debugging purposes.
213 *
214 * @var bool
215 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200216 public $save_queries = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200217
218 /**
219 * Queries list
220 *
221 * @see CI_DB_driver::$save_queries
222 * @var string[]
223 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200224 public $queries = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200225
226 /**
227 * Query times
228 *
229 * A list of times that queries took to execute.
230 *
231 * @var array
232 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200233 public $query_times = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200234
235 /**
236 * Data cache
237 *
238 * An internal generic value cache.
239 *
240 * @var array
241 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200242 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000243
Andrey Andreevae85eb42012-11-02 01:42:31 +0200244 /**
245 * Transaction enabled flag
246 *
247 * @var bool
248 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200249 public $trans_enabled = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200250
251 /**
252 * Strict transaction mode flag
253 *
254 * @var bool
255 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200256 public $trans_strict = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200257
258 /**
259 * Transaction depth level
260 *
261 * @var int
262 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200263 protected $_trans_depth = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264
Andrey Andreevae85eb42012-11-02 01:42:31 +0200265 /**
266 * Transaction status flag
267 *
268 * Used with transactions to determine if a rollback should occur.
269 *
270 * @var bool
271 */
272 protected $_trans_status = TRUE;
273
274 /**
Ahmedul Haque Abida2129772014-05-01 01:57:53 +0600275 * Transaction failure flag
276 *
277 * Used with transactions to determine if a transaction has failed.
278 *
279 * @var bool
280 */
281 protected $_trans_failure = FALSE;
282
283 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200284 * Cache On flag
285 *
286 * @var bool
287 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200288 public $cache_on = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200289
290 /**
291 * Cache directory path
292 *
293 * @var bool
294 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200295 public $cachedir = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200296
297 /**
298 * Cache auto-delete flag
299 *
300 * @var bool
301 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200302 public $cache_autodel = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200303
Andrey Andreevae85eb42012-11-02 01:42:31 +0200304 /**
305 * DB Cache object
306 *
307 * @see CI_DB_cache
308 * @var object
309 */
310 public $CACHE;
311
312 /**
313 * Protect identifiers flag
314 *
315 * @var bool
316 */
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +0000317 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +0200318
Andrey Andreevae85eb42012-11-02 01:42:31 +0200319 /**
320 * List of reserved identifiers
321 *
322 * Identifiers that must NOT be escaped.
323 *
324 * @var string[]
325 */
326 protected $_reserved_identifiers = array('*');
327
328 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200329 * Identifier escape character
330 *
331 * @var string
332 */
333 protected $_escape_char = '"';
334
335 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200336 * ESCAPE statement string
337 *
338 * @var string
339 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300340 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevae85eb42012-11-02 01:42:31 +0200341
342 /**
343 * ESCAPE character
344 *
345 * @var string
346 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300347 protected $_like_escape_chr = '!';
348
Andrey Andreev75546112012-07-05 11:01:29 +0300349 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200350 * ORDER BY random keyword
351 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200352 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200353 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200354 protected $_random_keyword = array('RAND()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200355
356 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200357 * COUNT string
358 *
359 * @used-by CI_DB_driver::count_all()
360 * @used-by CI_DB_query_builder::count_all_results()
361 *
362 * @var string
Andrey Andreev77a5d942012-07-05 15:42:14 +0300363 */
364 protected $_count_string = 'SELECT COUNT(*) AS ';
365
Andrey Andreevae85eb42012-11-02 01:42:31 +0200366 // --------------------------------------------------------------------
367
Andrey Andreev77a5d942012-07-05 15:42:14 +0300368 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200369 * Class constructor
Andrey Andreev75546112012-07-05 11:01:29 +0300370 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200371 * @param array $params
Andrey Andreev75546112012-07-05 11:01:29 +0300372 * @return void
373 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500374 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 if (is_array($params))
377 {
378 foreach ($params as $key => $val)
379 {
380 $this->$key = $val;
381 }
382 }
383
384 log_message('debug', 'Database Driver Class Initialized');
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Gints Murans89f77ee2012-05-23 00:23:50 +0300387 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
390 * Initialize Database Settings
391 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200392 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200393 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200394 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200396 /* If an established connection is available, then there's
397 * no need to connect and select the database.
398 *
399 * Depending on the database driver, conn_id can be either
400 * boolean TRUE, a resource or an object.
401 */
402 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 return TRUE;
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // Connect to the database and set the connection ID
Andrey Andreev7e963512014-02-27 15:43:24 +0200410 $this->conn_id = $this->db_connect($this->pconnect);
Derek Allard2067d1a2008-11-13 22:59:24 +0000411
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200412 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 if ( ! $this->conn_id)
414 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100415 // Check if there is a failover set
416 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100418 // Go over all the failovers
419 foreach ($this->failover as $failover)
420 {
421 // Replace the current settings with those of the failover
422 foreach ($failover as $key => $val)
423 {
424 $this->$key = $val;
425 }
426
427 // Try to connect
Andrey Andreev7e963512014-02-27 15:43:24 +0200428 $this->conn_id = $this->db_connect($this->pconnect);
Felix Balfoort5d581b62011-11-29 15:53:01 +0100429
430 // If a connection is made break the foreach loop
431 if ($this->conn_id)
432 {
433 break;
434 }
435 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100437
438 // We still don't have a connection?
439 if ( ! $this->conn_id)
440 {
441 log_message('error', 'Unable to connect to the database');
442
443 if ($this->db_debug)
444 {
445 $this->display_error('db_unable_to_connect');
446 }
Andrey Andreev7e963512014-02-27 15:43:24 +0200447
Felix Balfoort5d581b62011-11-29 15:53:01 +0100448 return FALSE;
449 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200452 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200453 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // --------------------------------------------------------------------
457
458 /**
Andrey Andreev2e171022014-02-25 15:21:41 +0200459 * Persistent database connection
460 *
461 * @return resource
462 */
463 public function db_pconnect()
464 {
465 return $this->db_connect(TRUE);
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200471 * Reconnect
472 *
473 * Keep / reestablish the db connection if no queries have been
474 * sent for a length of time exceeding the server's idle timeout.
475 *
476 * This is just a dummy method to allow drivers without such
477 * functionality to not declare it, while others will override it.
478 *
479 * @return void
480 */
481 public function reconnect()
482 {
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300488 * Select database
489 *
490 * This is just a dummy method to allow drivers without such
491 * functionality to not declare it, while others will override it.
492 *
493 * @return bool
494 */
495 public function db_select()
496 {
497 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Set client character set
504 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200506 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200508 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200510 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200512 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 if ($this->db_debug)
515 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200516 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 return FALSE;
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 return TRUE;
523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // --------------------------------------------------------------------
526
527 /**
528 * The name of the platform in use (mysql, mssql, etc...)
529 *
Barry Mienydd671972010-10-04 16:33:58 +0200530 * @return string
531 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500532 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 return $this->dbdriver;
535 }
536
537 // --------------------------------------------------------------------
538
539 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200540 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200542 * Returns a string containing the version of the database being used.
543 * Most drivers will override this method.
544 *
Barry Mienydd671972010-10-04 16:33:58 +0200545 * @return string
546 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200547 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200549 if (isset($this->data_cache['version']))
550 {
551 return $this->data_cache['version'];
552 }
553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 if (FALSE === ($sql = $this->_version()))
555 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200556 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Derek Allard3683f772009-12-16 17:32:33 +0000558
Andrey Andreev7e963512014-02-27 15:43:24 +0200559 $query = $this->query($sql)->row();
Andrey Andreev08856b82012-03-03 03:19:28 +0200560 return $this->data_cache['version'] = $query->ver;
561 }
Derek Allard3683f772009-12-16 17:32:33 +0000562
Andrey Andreev08856b82012-03-03 03:19:28 +0200563 // --------------------------------------------------------------------
564
565 /**
566 * Version number query string
567 *
568 * @return string
569 */
570 protected function _version()
571 {
572 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // --------------------------------------------------------------------
576
577 /**
578 * Execute the query
579 *
580 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200581 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * upon successful execution of a "write" type query. Returns boolean
583 * FALSE upon failure, and if the $db_debug variable is set to TRUE
584 * will raise an error.
585 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300586 * @param string $sql
587 * @param array $binds = FALSE An array of binding data
588 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200589 * @return mixed
590 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300591 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100593 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200595 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200596 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300598 elseif ( ! is_bool($return_object))
599 {
600 $return_object = ! $this->is_write_type($sql);
601 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000602
603 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100604 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600605 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200606 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
Derek Jonese7792202010-03-02 17:24:46 -0600608
Ryan Dialef7474c2012-03-01 16:11:36 -0500609 // Compile binds if needed
610 if ($binds !== FALSE)
611 {
612 $sql = $this->compile_binds($sql, $binds);
613 }
614
Andrey Andreev4c202602012-03-06 20:34:52 +0200615 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // we will load the caching class and return the previously
617 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300618 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200620 $this->load_rdriver();
621 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200623 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Andrey Andreevb66664b2012-06-27 14:22:10 +0300627 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100628 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
630 $this->queries[] = $sql;
631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200634 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // Run the Query
637 if (FALSE === ($this->result_id = $this->simple_query($sql)))
638 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100639 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
641 $this->query_times[] = 0;
642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 // This will trigger a rollback if transactions are being used
645 $this->_trans_status = FALSE;
646
Andrey Andreev4be5de12012-03-02 15:45:41 +0200647 // Grab the error now, as we might run some additional queries before displaying the error
648 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200649
650 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300651 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 if ($this->db_debug)
654 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200656 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200657 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 // transactions to remain in limbo.
Andrey Andreev6c854422013-10-21 13:58:15 +0300659 if ($this->_trans_depth !== 0)
660 {
661 do
662 {
663 $this->trans_complete();
664 }
665 while ($this->_trans_depth !== 0);
666 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000667
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200668 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200669 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 return FALSE;
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200676 $time_end = microtime(TRUE);
677 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000678
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100679 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
dixyab3cab52012-04-21 00:58:00 +0200681 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // Increment the query counter
685 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200686
Andrey Andreevb66664b2012-06-27 14:22:10 +0300687 // Will we have a result object instantiated? If not - we'll simply return TRUE
688 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300690 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100691 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 $this->CACHE->delete();
694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 return TRUE;
697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Barry Mienydd671972010-10-04 16:33:58 +0200699 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200700 $driver = $this->load_rdriver();
701 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200702
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200703 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100705 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
707 // We'll create a new instance of the result object
708 // only without the platform specific driver since
709 // we can't use it with cached data (the query result
710 // resource ID won't be any good once we've cached the
711 // result object, so we'll have to compile the data
712 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200713 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 $CR->result_object = $RES->result_object();
715 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200716 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 // Reset these since cached objects can not utilize resource IDs.
719 $CR->conn_id = NULL;
720 $CR->result_id = NULL;
721
722 $this->CACHE->write($sql, $CR);
723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 return $RES;
726 }
727
728 // --------------------------------------------------------------------
729
730 /**
731 * Load the result drivers
732 *
Barry Mienydd671972010-10-04 16:33:58 +0200733 * @return string the name of the result class
734 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500735 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
737 $driver = 'CI_DB_'.$this->dbdriver.'_result';
738
vlakofffadb8222013-05-12 10:57:09 +0200739 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200741 require_once(BASEPATH.'database/DB_result.php');
742 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 return $driver;
746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 // --------------------------------------------------------------------
749
750 /**
751 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200752 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 * we only use it when running transaction commands since they do
754 * not require all the features of the main query() function.
755 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200757 * @return mixed
758 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500759 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
761 if ( ! $this->conn_id)
762 {
763 $this->initialize();
764 }
765
766 return $this->_execute($sql);
767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 // --------------------------------------------------------------------
770
771 /**
772 * Disable Transactions
773 * This permits transactions to be disabled at run-time.
774 *
Barry Mienydd671972010-10-04 16:33:58 +0200775 * @return void
776 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500777 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
779 $this->trans_enabled = FALSE;
780 }
781
782 // --------------------------------------------------------------------
783
784 /**
785 * Enable/disable Transaction Strict Mode
786 * When strict mode is enabled, if you are running multiple groups of
787 * transactions, if one group fails all groups will be rolled back.
788 * If strict mode is disabled, each group is treated autonomously, meaning
789 * a failure of one group will not affect any others
790 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300791 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200792 * @return void
793 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500794 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
796 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 // --------------------------------------------------------------------
800
801 /**
802 * Start Transaction
803 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300804 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200805 * @return void
806 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500807 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200808 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 if ( ! $this->trans_enabled)
810 {
811 return FALSE;
812 }
813
814 // When transactions are nested we only begin/commit/rollback the outermost ones
815 if ($this->_trans_depth > 0)
816 {
817 $this->_trans_depth += 1;
818 return;
819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500822 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 }
824
825 // --------------------------------------------------------------------
826
827 /**
828 * Complete Transaction
829 *
Barry Mienydd671972010-10-04 16:33:58 +0200830 * @return bool
831 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500832 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 {
834 if ( ! $this->trans_enabled)
835 {
836 return FALSE;
837 }
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 // When transactions are nested we only begin/commit/rollback the outermost ones
840 if ($this->_trans_depth > 1)
841 {
842 $this->_trans_depth -= 1;
843 return TRUE;
844 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500845 else
846 {
847 $this->_trans_depth = 0;
848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900851 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 {
853 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 // If we are NOT running in strict mode, we will reset
856 // the _trans_status flag so that subsequent groups of transactions
857 // will be permitted.
858 if ($this->trans_strict === FALSE)
859 {
860 $this->_trans_status = TRUE;
861 }
862
863 log_message('debug', 'DB Transaction Failure');
864 return FALSE;
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 $this->trans_commit();
868 return TRUE;
869 }
870
871 // --------------------------------------------------------------------
872
873 /**
874 * Lets you retrieve the transaction flag to determine if it has failed
875 *
Barry Mienydd671972010-10-04 16:33:58 +0200876 * @return bool
877 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500878 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
880 return $this->_trans_status;
881 }
882
883 // --------------------------------------------------------------------
884
885 /**
886 * Compile Bindings
887 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * @param string the sql statement
889 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200890 * @return string
891 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500892 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300894 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
896 return $sql;
897 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300898 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300900 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300901 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300903 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200904 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300905 // Make sure we're using numeric keys
906 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300907 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
909
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300910 // We'll need the marker length later
911 $ml = strlen($this->bind_marker);
912
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300913 // Make sure not to replace a chunk inside a string that happens to match the bind marker
914 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300916 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300917 str_replace($matches[0],
918 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
919 $sql, $c),
920 $matches, PREG_OFFSET_CAPTURE);
921
922 // Bind values' count must match the count of markers in the query
923 if ($bind_count !== $c)
924 {
925 return $sql;
926 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300927 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300928 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 +0300929 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300930 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 }
932
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300933 do
934 {
935 $c--;
clawoo4a4f5502014-10-20 15:28:08 +0300936 $escaped_value = $this->escape($binds[$c]);
937 if (is_array($escaped_value))
938 {
939 $escaped_value = '('.implode(',', $escaped_value).')';
940 }
941 $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300942 }
943 while ($c !== 0);
944
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300945 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // --------------------------------------------------------------------
949
950 /**
951 * Determines if a query is a "write" type.
952 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200954 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200955 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200956 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200958 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 +0000959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // --------------------------------------------------------------------
962
963 /**
964 * Calculate the aggregate query elapsed time
965 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200966 * @param int The number of decimal places
967 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200968 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500969 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
971 return number_format($this->benchmark, $decimals);
972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 // --------------------------------------------------------------------
975
976 /**
977 * Returns the total number of queries
978 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200979 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200980 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500981 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
983 return $this->query_count;
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 // --------------------------------------------------------------------
987
988 /**
989 * Returns the last query that was executed
990 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200991 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200992 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500993 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
995 return end($this->queries);
996 }
997
998 // --------------------------------------------------------------------
999
1000 /**
1001 * "Smart" Escape String
1002 *
1003 * Escapes data based on type
1004 * Sets boolean and null types
1005 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 * @param string
Barry Mienydd671972010-10-04 16:33:58 +02001007 * @return mixed
1008 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001009 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +02001010 {
clawooa779c482014-10-18 14:47:04 +03001011 if (is_array($str))
1012 {
1013 $str = array_map(array(&$this, 'escape'), $str);
clawoo4a4f5502014-10-20 15:28:08 +03001014 return $str;
clawooa779c482014-10-18 14:47:04 +03001015 }
1016 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001018 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001019 }
1020 elseif (is_bool($str))
1021 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001022 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001023 }
vlakoff1228fe22013-01-14 01:30:09 +01001024 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001025 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001026 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001027 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001028
1029 return $str;
1030 }
1031
1032 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001033
Derek Jonese4ed5832009-02-20 21:44:59 +00001034 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001035 * Escape String
1036 *
1037 * @param string $str
1038 * @param bool $like Whether or not the string will be used in a LIKE condition
1039 * @return string
1040 */
1041 public function escape_str($str, $like = FALSE)
1042 {
1043 if (is_array($str))
1044 {
1045 foreach ($str as $key => $val)
1046 {
1047 $str[$key] = $this->escape_str($val, $like);
1048 }
1049
1050 return $str;
1051 }
1052
1053 $str = $this->_escape_str($str);
1054
1055 // escape LIKE condition wildcards
1056 if ($like === TRUE)
1057 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001058 return str_replace(
1059 array($this->_like_escape_chr, '%', '_'),
1060 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1061 $str
1062 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001063 }
1064
1065 return $str;
1066 }
1067
1068 // --------------------------------------------------------------------
1069
1070 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001071 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001072 *
1073 * Calls the individual driver for platform
1074 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001075 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001076 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001077 * @return mixed
1078 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001079 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001080 {
1081 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001082 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001083
Derek Jonese4ed5832009-02-20 21:44:59 +00001084 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001087 * Platform-dependant string escape
1088 *
1089 * @param string
1090 * @return string
1091 */
1092 protected function _escape_str($str)
1093 {
1094 return str_replace("'", "''", remove_invisible_characters($str));
1095 }
1096
1097 // --------------------------------------------------------------------
1098
1099 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 * Primary
1101 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001102 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 * position is the primary key
1104 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001106 * @return string
1107 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001108 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +02001109 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001111 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 }
1113
1114 // --------------------------------------------------------------------
1115
1116 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001117 * "Count All" query
1118 *
1119 * Generates a platform-specific query string that counts all records in
1120 * the specified database
1121 *
1122 * @param string
1123 * @return int
1124 */
1125 public function count_all($table = '')
1126 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001127 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001128 {
1129 return 0;
1130 }
1131
1132 $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 +01001133 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001134 {
1135 return 0;
1136 }
1137
1138 $query = $query->row();
1139 $this->_reset_select();
1140 return (int) $query->numrows;
1141 }
1142
1143 // --------------------------------------------------------------------
1144
1145 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 * Returns an array of table names
1147 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001148 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001149 * @return array
1150 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001151 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
1153 // Is there a cached result?
1154 if (isset($this->data_cache['table_names']))
1155 {
1156 return $this->data_cache['table_names'];
1157 }
Barry Mienydd671972010-10-04 16:33:58 +02001158
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1160 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001161 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
1163
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001164 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001166
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001167 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001169 // Do we know from which column to get the table name?
1170 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001172 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001173 {
1174 $key = 'table_name';
1175 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001176 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001177 {
1178 $key = 'TABLE_NAME';
1179 }
1180 else
1181 {
1182 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001183 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001184 * E_STRICT is on, this would trigger a warning. So we'll have to
1185 * assign it first.
1186 */
1187 $key = array_keys($row);
1188 $key = array_shift($key);
1189 }
Taufan Aditya18209332012-02-09 16:07:27 +07001190 }
1191
Andrey Andreev12567e82012-01-25 22:50:33 +02001192 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
1194
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 return $this->data_cache['table_names'];
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 // --------------------------------------------------------------------
1199
1200 /**
1201 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001202 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001203 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001204 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001206 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001207 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001208 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 }
Barry Mienydd671972010-10-04 16:33:58 +02001210
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 // --------------------------------------------------------------------
1212
1213 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001214 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001217 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001219 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
1221 // Is there a cached result?
1222 if (isset($this->data_cache['field_names'][$table]))
1223 {
1224 return $this->data_cache['field_names'][$table];
1225 }
Barry Mienydd671972010-10-04 16:33:58 +02001226
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001227 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001229 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Greg Aker1edde302010-01-26 00:17:01 +00001232 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001234 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 }
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001238 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001239
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001240 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001242 // Do we know from where to get the column's name?
1243 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001245 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001246 {
1247 $key = 'column_name';
1248 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001249 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001250 {
1251 $key = 'COLUMN_NAME';
1252 }
1253 else
1254 {
RJ garcia395e2df2013-03-21 10:48:24 -05001255 // We have no other choice but to just get the first element's key.
1256 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001257 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001259
1260 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 }
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 return $this->data_cache['field_names'][$table];
1264 }
1265
1266 // --------------------------------------------------------------------
1267
1268 /**
1269 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001270 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 * @param string
1272 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001273 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001275 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001276 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001277 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 }
Barry Mienydd671972010-10-04 16:33:58 +02001279
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 // --------------------------------------------------------------------
1281
1282 /**
1283 * Returns an object with field data
1284 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +02001286 * @return object
1287 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001288 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001290 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001292 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 }
Barry Mienydd671972010-10-04 16:33:58 +02001294
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001295 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +02001297 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001298
1299 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001302 * Escape the SQL Identifiers
1303 *
1304 * This function escapes column and table names
1305 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001306 * @param mixed
1307 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001308 */
1309 public function escape_identifiers($item)
1310 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001311 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001312 {
1313 return $item;
1314 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001315 elseif (is_array($item))
1316 {
1317 foreach ($item as $key => $value)
1318 {
1319 $item[$key] = $this->escape_identifiers($value);
1320 }
1321
1322 return $item;
1323 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001324 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001325 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001326 {
1327 return $item;
1328 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001329
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001330 static $preg_ec = array();
1331
1332 if (empty($preg_ec))
1333 {
1334 if (is_array($this->_escape_char))
1335 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001336 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001337 preg_quote($this->_escape_char[0], '/'),
1338 preg_quote($this->_escape_char[1], '/'),
1339 $this->_escape_char[0],
1340 $this->_escape_char[1]
1341 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001342 }
1343 else
1344 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001345 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001346 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001347 }
1348 }
1349
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001350 foreach ($this->_reserved_identifiers as $id)
1351 {
1352 if (strpos($item, '.'.$id) !== FALSE)
1353 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001354 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 +03001355 }
1356 }
1357
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001358 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 +03001359 }
1360
1361 // --------------------------------------------------------------------
1362
1363 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 * Generate an insert string
1365 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 * @param string the table upon which the query will be performed
1367 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001368 * @return string
1369 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001370 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001372 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001373
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001374 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001376 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 $values[] = $this->escape($val);
1378 }
Barry Mienydd671972010-10-04 16:33:58 +02001379
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001380 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001381 }
1382
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 // --------------------------------------------------------------------
1384
1385 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001386 * Insert statement
1387 *
1388 * Generates a platform-specific insert string from the supplied data
1389 *
1390 * @param string the table name
1391 * @param array the insert keys
1392 * @param array the insert values
1393 * @return string
1394 */
1395 protected function _insert($table, $keys, $values)
1396 {
1397 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1398 }
1399
1400 // --------------------------------------------------------------------
1401
1402 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 * Generate an update string
1404 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 * @param string the table upon which the query will be performed
1406 * @param array an associative array data of key/values
1407 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001408 * @return string
1409 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001410 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001412 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001414 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 }
Barry Mienydd671972010-10-04 16:33:58 +02001416
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001417 $this->where($where);
1418
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001420 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001421 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001422 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 }
1424
Andrey Andreev79f888b2013-08-06 13:59:23 +03001425 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1426 $this->_reset_write();
1427 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001428 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001429
1430 // --------------------------------------------------------------------
1431
1432 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001433 * Update statement
1434 *
1435 * Generates a platform-specific update string from the supplied data
1436 *
1437 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001438 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001439 * @return string
1440 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001441 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001442 {
1443 foreach ($values as $key => $val)
1444 {
1445 $valstr[] = $key.' = '.$val;
1446 }
1447
Andrey Andreev75546112012-07-05 11:01:29 +03001448 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001449 .$this->_compile_wh('qb_where')
1450 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001451 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001452 }
1453
1454 // --------------------------------------------------------------------
1455
1456 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 * Tests whether the string has an SQL operator
1458 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 * @param string
1460 * @return bool
1461 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001462 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 {
Andrey Andreev2c6cdd72014-09-17 11:13:46 +03001464 return (bool) preg_match('/(<|>|!|=|\sIS\s|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 }
1466
1467 // --------------------------------------------------------------------
1468
1469 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001470 * Returns the SQL string operator
1471 *
1472 * @param string
1473 * @return string
1474 */
1475 protected function _get_operator($str)
1476 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001477 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001478
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001479 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001480 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001481 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001482 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001483 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001484 $_operators = array(
1485 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1486 '\s*<>?\s*', // <, <>
1487 '\s*>\s*', // >
Andrey Andreev2c6cdd72014-09-17 11:13:46 +03001488 '\s+IS(?:\sNOT)?(?:\sNULL)?', // IS[ NOT] NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001489 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001490 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001491 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1492 '\s+IN\s*\([^\)]+\)', // IN(list)
1493 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1494 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1495 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1496 );
1497
1498 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001499
Andrey Andreev6e704752012-07-18 00:46:33 +03001500 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1501 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001502 }
1503
1504 // --------------------------------------------------------------------
1505
1506 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1508 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001509 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001510 * @return mixed
1511 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001512 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001514 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001515
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 if (FALSE === strpos($driver, $function))
1517 {
1518 $function = $driver.$function;
1519 }
Barry Mienydd671972010-10-04 16:33:58 +02001520
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 if ( ! function_exists($function))
1522 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001523 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001525
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001526 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001527 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001528 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 }
1530
1531 // --------------------------------------------------------------------
1532
1533 /**
1534 * Set Cache Directory Path
1535 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 * @param string the path to the cache directory
1537 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001538 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001539 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 {
1541 $this->cachedir = $path;
1542 }
1543
1544 // --------------------------------------------------------------------
1545
1546 /**
1547 * Enable Query Caching
1548 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001549 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001550 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001551 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001552 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001553 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 }
1555
1556 // --------------------------------------------------------------------
1557
1558 /**
1559 * Disable Query Caching
1560 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001561 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001562 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001563 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001565 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 }
Barry Mienydd671972010-10-04 16:33:58 +02001567
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 // --------------------------------------------------------------------
1569
1570 /**
1571 * Delete the cache files associated with a particular URI
1572 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001573 * @param string $segment_one = ''
1574 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001575 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001576 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001577 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001579 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001580 ? $this->CACHE->delete($segment_one, $segment_two)
1581 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 }
1583
1584 // --------------------------------------------------------------------
1585
1586 /**
1587 * Delete All cache files
1588 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001589 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001590 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001591 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001593 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001594 ? $this->CACHE->delete_all()
1595 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 }
1597
1598 // --------------------------------------------------------------------
1599
1600 /**
1601 * Initialize the Cache Class
1602 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001603 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001604 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001605 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001607 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001609 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001611 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001612 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001613 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001614 }
Derek Allarde37ab382009-02-03 16:13:57 +00001615
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1617 return TRUE;
1618 }
1619
1620 // --------------------------------------------------------------------
1621
1622 /**
1623 * Close DB Connection
1624 *
Barry Mienydd671972010-10-04 16:33:58 +02001625 * @return void
1626 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001627 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001629 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001631 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001632 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 }
Barry Mienydd671972010-10-04 16:33:58 +02001635
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 // --------------------------------------------------------------------
1637
1638 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001639 * Close DB Connection
1640 *
1641 * This method would be overriden by most of the drivers.
1642 *
1643 * @return void
1644 */
1645 protected function _close()
1646 {
1647 $this->conn_id = FALSE;
1648 }
1649
1650 // --------------------------------------------------------------------
1651
1652 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 * Display an error message
1654 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 * @param string the error message
1656 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001657 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001658 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001659 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001660 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 {
Derek Jonese7792202010-03-02 17:24:46 -06001662 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 $LANG->load('db');
1664
1665 $heading = $LANG->line('db_error_heading');
1666
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001667 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001669 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 }
1671 else
1672 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001673 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 }
Barry Mienydd671972010-10-04 16:33:58 +02001675
Pascal Kriete60f8c392010-08-25 18:03:28 +02001676 // Find the most likely culprit of the error by going through
1677 // the backtrace until the source file is no longer in the
1678 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001679 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001680 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001681 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001682 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001683 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001684 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1685 if (DIRECTORY_SEPARATOR !== '/')
1686 {
1687 $call['file'] = str_replace('\\', '/', $call['file']);
1688 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001689
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001690 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1691 {
1692 // Found it - use a relative path for safety
1693 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1694 $message[] = 'Line Number: '.$call['line'];
1695 break;
1696 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001697 }
1698 }
Barry Mienydd671972010-10-04 16:33:58 +02001699
Derek Jonese7792202010-03-02 17:24:46 -06001700 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001702 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 }
1704
1705 // --------------------------------------------------------------------
1706
1707 /**
1708 * Protect Identifiers
1709 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001710 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001711 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001713 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001714 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 *
1716 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1717 *
1718 * Or a query with aliasing:
1719 *
1720 * SELECT m.member_id, m.member_name FROM members AS m
1721 *
1722 * Since the column name can include up to four segments (host, DB, table, column)
1723 * or also have an alias prefix, we need to do a bit of work to figure this out and
1724 * insert the table prefix (if it exists) in the proper position, and escape only
1725 * the correct identifiers.
1726 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 * @param string
1728 * @param bool
1729 * @param mixed
1730 * @param bool
1731 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001732 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001733 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 {
1735 if ( ! is_bool($protect_identifiers))
1736 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001737 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 }
Derek Allarde37ab382009-02-03 16:13:57 +00001739
1740 if (is_array($item))
1741 {
1742 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001743 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001744 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001745 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001746 }
1747
1748 return $escaped_array;
1749 }
1750
Derek Allard911d3e02008-12-15 14:08:35 +00001751 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001752 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001753 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001754 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001755 //
1756 // Added exception for single quotes as well, we don't want to alter
1757 // literal strings. -- Narf
1758 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001759 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001760 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001761 }
1762
Andrey Andreevbf940582012-06-10 07:05:05 +03001763 // Convert tabs or multiple spaces into single spaces
1764 $item = preg_replace('/\s+/', ' ', $item);
1765
Andrey Andreevbf940582012-06-10 07:05:05 +03001766 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001767 // Note: strripos() is used in order to support spaces in table names
1768 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001769 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001770 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001771 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1772 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001773 $item = substr($item, 0, $offset);
1774 }
1775 elseif ($offset = strrpos($item, ' '))
1776 {
1777 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001778 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1779 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001780 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001781 }
1782 else
1783 {
1784 $alias = '';
1785 }
1786
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 // Break the string apart if it contains periods, then insert the table prefix
1788 // in the correct location, assuming the period doesn't indicate that we're dealing
1789 // with an alias. While we're at it, we will escape the components
1790 if (strpos($item, '.') !== FALSE)
1791 {
1792 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001793
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001795 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001797 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001798 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 if ($protect_identifiers === TRUE)
1800 {
1801 foreach ($parts as $key => $val)
1802 {
1803 if ( ! in_array($val, $this->_reserved_identifiers))
1804 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001805 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 }
1807 }
Barry Mienydd671972010-10-04 16:33:58 +02001808
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001810 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001811
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 return $item.$alias;
1813 }
Barry Mienydd671972010-10-04 16:33:58 +02001814
Andrey Andreev4c202602012-03-06 20:34:52 +02001815 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001816 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 {
1818 // We now add the table prefix based on some logic.
1819 // Do we have 4 segments (hostname.database.table.column)?
1820 // If so, we add the table prefix to the column name in the 3rd segment.
1821 if (isset($parts[3]))
1822 {
1823 $i = 2;
1824 }
1825 // Do we have 3 segments (database.table.column)?
1826 // If so, we add the table prefix to the column name in 2nd position
1827 elseif (isset($parts[2]))
1828 {
1829 $i = 1;
1830 }
1831 // Do we have 2 segments (table.column)?
1832 // If so, we add the table prefix to the column name in 1st segment
1833 else
1834 {
1835 $i = 0;
1836 }
Barry Mienydd671972010-10-04 16:33:58 +02001837
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 // This flag is set when the supplied $item does not contain a field name.
1839 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001840 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 {
1842 $i++;
1843 }
Barry Mienydd671972010-10-04 16:33:58 +02001844
Derek Jones55acc8b2009-07-11 03:38:13 +00001845 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001846 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001847 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001848 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001849 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001851 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 {
1853 $parts[$i] = $this->dbprefix.$parts[$i];
1854 }
Barry Mienydd671972010-10-04 16:33:58 +02001855
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 // Put the parts back together
1857 $item = implode('.', $parts);
1858 }
Barry Mienydd671972010-10-04 16:33:58 +02001859
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 if ($protect_identifiers === TRUE)
1861 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001862 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 }
Barry Mienydd671972010-10-04 16:33:58 +02001864
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 return $item.$alias;
1866 }
1867
Andrey Andreev4c202602012-03-06 20:34:52 +02001868 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001869 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001871 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001872 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001873 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001874 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001875 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001877 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 {
1879 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001880 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 }
1882
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001883 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001885 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 }
Barry Mienydd671972010-10-04 16:33:58 +02001887
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 return $item.$alias;
1889 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001890
Túbal Martín511f2252011-11-24 14:43:45 +01001891 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001892
Túbal Martín511f2252011-11-24 14:43:45 +01001893 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001894 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001895 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001896 *
Túbal Martín511f2252011-11-24 14:43:45 +01001897 * @return void
1898 */
1899 protected function _reset_select()
1900 {
Túbal Martín511f2252011-11-24 14:43:45 +01001901 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001902
1903}
1904
Derek Allard2067d1a2008-11-13 22:59:24 +00001905/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001906/* Location: ./system/database/DB_driver.php */