blob: 9ef197cd15f09de79a70b4a3d0b40ffb2d757252 [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 Andreev0d3fde22015-01-05 16:56:41 +0200459 * DB connect
460 *
461 * This is just a dummy method that all drivers will override.
462 *
463 * @return mixed
464 */
465 public function db_connect()
466 {
467 return TRUE;
468 }
469
470 // --------------------------------------------------------------------
471
472 /**
Andrey Andreev2e171022014-02-25 15:21:41 +0200473 * Persistent database connection
474 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +0200475 * @return mixed
Andrey Andreev2e171022014-02-25 15:21:41 +0200476 */
477 public function db_pconnect()
478 {
479 return $this->db_connect(TRUE);
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200485 * Reconnect
486 *
487 * Keep / reestablish the db connection if no queries have been
488 * sent for a length of time exceeding the server's idle timeout.
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 void
494 */
495 public function reconnect()
496 {
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300502 * Select database
503 *
504 * This is just a dummy method to allow drivers without such
505 * functionality to not declare it, while others will override it.
506 *
507 * @return bool
508 */
509 public function db_select()
510 {
511 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
514 // --------------------------------------------------------------------
515
516 /**
517 * Set client character set
518 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200520 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200522 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200524 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200526 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 if ($this->db_debug)
529 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200530 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 return FALSE;
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 return TRUE;
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // --------------------------------------------------------------------
540
541 /**
542 * The name of the platform in use (mysql, mssql, etc...)
543 *
Barry Mienydd671972010-10-04 16:33:58 +0200544 * @return string
545 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500546 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
548 return $this->dbdriver;
549 }
550
551 // --------------------------------------------------------------------
552
553 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200554 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200556 * Returns a string containing the version of the database being used.
557 * Most drivers will override this method.
558 *
Barry Mienydd671972010-10-04 16:33:58 +0200559 * @return string
560 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200561 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200563 if (isset($this->data_cache['version']))
564 {
565 return $this->data_cache['version'];
566 }
567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 if (FALSE === ($sql = $this->_version()))
569 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200570 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
Derek Allard3683f772009-12-16 17:32:33 +0000572
Andrey Andreev7e963512014-02-27 15:43:24 +0200573 $query = $this->query($sql)->row();
Andrey Andreev08856b82012-03-03 03:19:28 +0200574 return $this->data_cache['version'] = $query->ver;
575 }
Derek Allard3683f772009-12-16 17:32:33 +0000576
Andrey Andreev08856b82012-03-03 03:19:28 +0200577 // --------------------------------------------------------------------
578
579 /**
580 * Version number query string
581 *
582 * @return string
583 */
584 protected function _version()
585 {
586 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // --------------------------------------------------------------------
590
591 /**
592 * Execute the query
593 *
594 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200595 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 * upon successful execution of a "write" type query. Returns boolean
597 * FALSE upon failure, and if the $db_debug variable is set to TRUE
598 * will raise an error.
599 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300600 * @param string $sql
601 * @param array $binds = FALSE An array of binding data
602 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200603 * @return mixed
604 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300605 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100607 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200609 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200610 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300612 elseif ( ! is_bool($return_object))
613 {
614 $return_object = ! $this->is_write_type($sql);
615 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000616
617 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100618 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600619 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200620 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
Derek Jonese7792202010-03-02 17:24:46 -0600622
Ryan Dialef7474c2012-03-01 16:11:36 -0500623 // Compile binds if needed
624 if ($binds !== FALSE)
625 {
626 $sql = $this->compile_binds($sql, $binds);
627 }
628
Andrey Andreev4c202602012-03-06 20:34:52 +0200629 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 // we will load the caching class and return the previously
631 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300632 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200634 $this->load_rdriver();
635 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200637 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Andrey Andreevb66664b2012-06-27 14:22:10 +0300641 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100642 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 $this->queries[] = $sql;
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200648 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 // Run the Query
651 if (FALSE === ($this->result_id = $this->simple_query($sql)))
652 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100653 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 $this->query_times[] = 0;
656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 // This will trigger a rollback if transactions are being used
659 $this->_trans_status = FALSE;
660
Andrey Andreev4be5de12012-03-02 15:45:41 +0200661 // Grab the error now, as we might run some additional queries before displaying the error
662 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200663
664 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300665 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 if ($this->db_debug)
668 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200670 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200671 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 // transactions to remain in limbo.
Andrey Andreev6c854422013-10-21 13:58:15 +0300673 if ($this->_trans_depth !== 0)
674 {
675 do
676 {
677 $this->trans_complete();
678 }
679 while ($this->_trans_depth !== 0);
680 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000681
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200682 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200683 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 return FALSE;
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200690 $time_end = microtime(TRUE);
691 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000692
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100693 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
dixyab3cab52012-04-21 00:58:00 +0200695 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 }
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 // Increment the query counter
699 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200700
Andrey Andreevb66664b2012-06-27 14:22:10 +0300701 // Will we have a result object instantiated? If not - we'll simply return TRUE
702 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300704 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100705 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
707 $this->CACHE->delete();
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 return TRUE;
711 }
Barry Mienydd671972010-10-04 16:33:58 +0200712
Barry Mienydd671972010-10-04 16:33:58 +0200713 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200714 $driver = $this->load_rdriver();
715 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200716
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200717 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100719 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
721 // We'll create a new instance of the result object
722 // only without the platform specific driver since
723 // we can't use it with cached data (the query result
724 // resource ID won't be any good once we've cached the
725 // result object, so we'll have to compile the data
726 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200727 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 $CR->result_object = $RES->result_object();
729 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200730 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // Reset these since cached objects can not utilize resource IDs.
733 $CR->conn_id = NULL;
734 $CR->result_id = NULL;
735
736 $this->CACHE->write($sql, $CR);
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 return $RES;
740 }
741
742 // --------------------------------------------------------------------
743
744 /**
745 * Load the result drivers
746 *
Barry Mienydd671972010-10-04 16:33:58 +0200747 * @return string the name of the result class
748 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500749 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
751 $driver = 'CI_DB_'.$this->dbdriver.'_result';
752
vlakofffadb8222013-05-12 10:57:09 +0200753 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200755 require_once(BASEPATH.'database/DB_result.php');
756 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 }
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 return $driver;
760 }
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 // --------------------------------------------------------------------
763
764 /**
765 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200766 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 * we only use it when running transaction commands since they do
768 * not require all the features of the main query() function.
769 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200771 * @return mixed
772 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500773 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
775 if ( ! $this->conn_id)
776 {
777 $this->initialize();
778 }
779
780 return $this->_execute($sql);
781 }
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 // --------------------------------------------------------------------
784
785 /**
786 * Disable Transactions
787 * This permits transactions to be disabled at run-time.
788 *
Barry Mienydd671972010-10-04 16:33:58 +0200789 * @return void
790 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500791 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
793 $this->trans_enabled = FALSE;
794 }
795
796 // --------------------------------------------------------------------
797
798 /**
799 * Enable/disable Transaction Strict Mode
800 * When strict mode is enabled, if you are running multiple groups of
801 * transactions, if one group fails all groups will be rolled back.
802 * If strict mode is disabled, each group is treated autonomously, meaning
803 * a failure of one group will not affect any others
804 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300805 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200806 * @return void
807 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500808 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
810 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // --------------------------------------------------------------------
814
815 /**
816 * Start Transaction
817 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300818 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200819 * @return void
820 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500821 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200822 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 if ( ! $this->trans_enabled)
824 {
825 return FALSE;
826 }
827
828 // When transactions are nested we only begin/commit/rollback the outermost ones
829 if ($this->_trans_depth > 0)
830 {
831 $this->_trans_depth += 1;
832 return;
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500836 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 }
838
839 // --------------------------------------------------------------------
840
841 /**
842 * Complete Transaction
843 *
Barry Mienydd671972010-10-04 16:33:58 +0200844 * @return bool
845 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500846 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 {
848 if ( ! $this->trans_enabled)
849 {
850 return FALSE;
851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 // When transactions are nested we only begin/commit/rollback the outermost ones
854 if ($this->_trans_depth > 1)
855 {
856 $this->_trans_depth -= 1;
857 return TRUE;
858 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500859 else
860 {
861 $this->_trans_depth = 0;
862 }
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900865 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
867 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 // If we are NOT running in strict mode, we will reset
870 // the _trans_status flag so that subsequent groups of transactions
871 // will be permitted.
872 if ($this->trans_strict === FALSE)
873 {
874 $this->_trans_status = TRUE;
875 }
876
877 log_message('debug', 'DB Transaction Failure');
878 return FALSE;
879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $this->trans_commit();
882 return TRUE;
883 }
884
885 // --------------------------------------------------------------------
886
887 /**
888 * Lets you retrieve the transaction flag to determine if it has failed
889 *
Barry Mienydd671972010-10-04 16:33:58 +0200890 * @return bool
891 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500892 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
894 return $this->_trans_status;
895 }
896
897 // --------------------------------------------------------------------
898
899 /**
900 * Compile Bindings
901 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 * @param string the sql statement
903 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200904 * @return string
905 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500906 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300908 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
910 return $sql;
911 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300912 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300914 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300915 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300917 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200918 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300919 // Make sure we're using numeric keys
920 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300921 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
923
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300924 // We'll need the marker length later
925 $ml = strlen($this->bind_marker);
926
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300927 // Make sure not to replace a chunk inside a string that happens to match the bind marker
928 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300930 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300931 str_replace($matches[0],
932 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
933 $sql, $c),
934 $matches, PREG_OFFSET_CAPTURE);
935
936 // Bind values' count must match the count of markers in the query
937 if ($bind_count !== $c)
938 {
939 return $sql;
940 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300941 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300942 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 +0300943 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300944 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
946
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300947 do
948 {
949 $c--;
clawoo4a4f5502014-10-20 15:28:08 +0300950 $escaped_value = $this->escape($binds[$c]);
951 if (is_array($escaped_value))
952 {
953 $escaped_value = '('.implode(',', $escaped_value).')';
954 }
955 $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300956 }
957 while ($c !== 0);
958
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300959 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 // --------------------------------------------------------------------
963
964 /**
965 * Determines if a query is a "write" type.
966 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200968 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200969 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200970 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200972 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 +0000973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 // --------------------------------------------------------------------
976
977 /**
978 * Calculate the aggregate query elapsed time
979 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200980 * @param int The number of decimal places
James L Parry78301732014-12-16 02:14:52 -0800981 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200982 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500983 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 return number_format($this->benchmark, $decimals);
986 }
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 // --------------------------------------------------------------------
989
990 /**
991 * Returns the total number of queries
992 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200993 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200994 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500995 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
997 return $this->query_count;
998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Returns the last query that was executed
1004 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001005 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001006 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001007 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 {
1009 return end($this->queries);
1010 }
1011
1012 // --------------------------------------------------------------------
1013
1014 /**
1015 * "Smart" Escape String
1016 *
1017 * Escapes data based on type
1018 * Sets boolean and null types
1019 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * @param string
Barry Mienydd671972010-10-04 16:33:58 +02001021 * @return mixed
1022 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001023 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +02001024 {
clawooa779c482014-10-18 14:47:04 +03001025 if (is_array($str))
1026 {
1027 $str = array_map(array(&$this, 'escape'), $str);
clawoo4a4f5502014-10-20 15:28:08 +03001028 return $str;
clawooa779c482014-10-18 14:47:04 +03001029 }
1030 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001032 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001033 }
1034 elseif (is_bool($str))
1035 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001036 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001037 }
vlakoff1228fe22013-01-14 01:30:09 +01001038 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001039 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001040 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001041 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
1043 return $str;
1044 }
1045
1046 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001047
Derek Jonese4ed5832009-02-20 21:44:59 +00001048 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001049 * Escape String
1050 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001051 * @param string|string[] $str Input string
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001052 * @param bool $like Whether or not the string will be used in a LIKE condition
1053 * @return string
1054 */
1055 public function escape_str($str, $like = FALSE)
1056 {
1057 if (is_array($str))
1058 {
1059 foreach ($str as $key => $val)
1060 {
1061 $str[$key] = $this->escape_str($val, $like);
1062 }
1063
1064 return $str;
1065 }
1066
1067 $str = $this->_escape_str($str);
1068
1069 // escape LIKE condition wildcards
1070 if ($like === TRUE)
1071 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001072 return str_replace(
1073 array($this->_like_escape_chr, '%', '_'),
1074 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1075 $str
1076 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001077 }
1078
1079 return $str;
1080 }
1081
1082 // --------------------------------------------------------------------
1083
1084 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001085 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001086 *
1087 * Calls the individual driver for platform
1088 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001089 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001090 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001091 * @return mixed
1092 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001093 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001094 {
1095 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001096 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001097
Derek Jonese4ed5832009-02-20 21:44:59 +00001098 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001101 * Platform-dependant string escape
1102 *
1103 * @param string
1104 * @return string
1105 */
1106 protected function _escape_str($str)
1107 {
1108 return str_replace("'", "''", remove_invisible_characters($str));
1109 }
1110
1111 // --------------------------------------------------------------------
1112
1113 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 * Primary
1115 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001116 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * position is the primary key
1118 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001119 * @param string $table Table name
1120 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001121 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001122 public function primary($table)
Barry Mienydd671972010-10-04 16:33:58 +02001123 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001125 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 }
1127
1128 // --------------------------------------------------------------------
1129
1130 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001131 * "Count All" query
1132 *
1133 * Generates a platform-specific query string that counts all records in
1134 * the specified database
1135 *
1136 * @param string
1137 * @return int
1138 */
1139 public function count_all($table = '')
1140 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001141 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001142 {
1143 return 0;
1144 }
1145
1146 $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 +01001147 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001148 {
1149 return 0;
1150 }
1151
1152 $query = $query->row();
1153 $this->_reset_select();
1154 return (int) $query->numrows;
1155 }
1156
1157 // --------------------------------------------------------------------
1158
1159 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 * Returns an array of table names
1161 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001162 * @param string $constrain_by_prefix = FALSE
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001163 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001164 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001165 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 {
1167 // Is there a cached result?
1168 if (isset($this->data_cache['table_names']))
1169 {
1170 return $this->data_cache['table_names'];
1171 }
Barry Mienydd671972010-10-04 16:33:58 +02001172
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1174 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001175 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 }
1177
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001178 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001180
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001181 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001183 // Do we know from which column to get the table name?
1184 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001186 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001187 {
1188 $key = 'table_name';
1189 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001190 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001191 {
1192 $key = 'TABLE_NAME';
1193 }
1194 else
1195 {
1196 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001197 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001198 * E_STRICT is on, this would trigger a warning. So we'll have to
1199 * assign it first.
1200 */
1201 $key = array_keys($row);
1202 $key = array_shift($key);
1203 }
Taufan Aditya18209332012-02-09 16:07:27 +07001204 }
1205
Andrey Andreev12567e82012-01-25 22:50:33 +02001206 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 }
1208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 return $this->data_cache['table_names'];
1210 }
Barry Mienydd671972010-10-04 16:33:58 +02001211
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 // --------------------------------------------------------------------
1213
1214 /**
1215 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001216 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001217 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001218 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001220 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001221 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001222 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // --------------------------------------------------------------------
1226
1227 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001228 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 * @param string the table name
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001231 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001233 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
1235 // Is there a cached result?
1236 if (isset($this->data_cache['field_names'][$table]))
1237 {
1238 return $this->data_cache['field_names'][$table];
1239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240
Greg Aker1edde302010-01-26 00:17:01 +00001241 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001243 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001247 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001248
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001249 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001251 // Do we know from where to get the column's name?
1252 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001254 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001255 {
1256 $key = 'column_name';
1257 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001258 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001259 {
1260 $key = 'COLUMN_NAME';
1261 }
1262 else
1263 {
RJ garcia395e2df2013-03-21 10:48:24 -05001264 // We have no other choice but to just get the first element's key.
1265 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001266 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001268
1269 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 }
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 return $this->data_cache['field_names'][$table];
1273 }
1274
1275 // --------------------------------------------------------------------
1276
1277 /**
1278 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001279 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 * @param string
1281 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001282 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001284 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001285 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001286 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 }
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 // --------------------------------------------------------------------
1290
1291 /**
1292 * Returns an object with field data
1293 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001294 * @param string $table the table name
1295 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001296 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001297 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001299 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001300 return ($query) ? $query->field_data() : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001301 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001302
1303 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001306 * Escape the SQL Identifiers
1307 *
1308 * This function escapes column and table names
1309 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001310 * @param mixed
1311 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001312 */
1313 public function escape_identifiers($item)
1314 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001315 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001316 {
1317 return $item;
1318 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001319 elseif (is_array($item))
1320 {
1321 foreach ($item as $key => $value)
1322 {
1323 $item[$key] = $this->escape_identifiers($value);
1324 }
1325
1326 return $item;
1327 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001328 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001329 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001330 {
1331 return $item;
1332 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001333
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001334 static $preg_ec = array();
1335
1336 if (empty($preg_ec))
1337 {
1338 if (is_array($this->_escape_char))
1339 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001340 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001341 preg_quote($this->_escape_char[0], '/'),
1342 preg_quote($this->_escape_char[1], '/'),
1343 $this->_escape_char[0],
1344 $this->_escape_char[1]
1345 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001346 }
1347 else
1348 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001349 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001350 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001351 }
1352 }
1353
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001354 foreach ($this->_reserved_identifiers as $id)
1355 {
1356 if (strpos($item, '.'.$id) !== FALSE)
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].'.', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001359 }
1360 }
1361
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001362 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 +03001363 }
1364
1365 // --------------------------------------------------------------------
1366
1367 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 * Generate an insert string
1369 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 * @param string the table upon which the query will be performed
1371 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001372 * @return string
1373 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001374 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001376 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001377
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001378 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001380 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 $values[] = $this->escape($val);
1382 }
Barry Mienydd671972010-10-04 16:33:58 +02001383
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001384 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001385 }
1386
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 // --------------------------------------------------------------------
1388
1389 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001390 * Insert statement
1391 *
1392 * Generates a platform-specific insert string from the supplied data
1393 *
1394 * @param string the table name
1395 * @param array the insert keys
1396 * @param array the insert values
1397 * @return string
1398 */
1399 protected function _insert($table, $keys, $values)
1400 {
1401 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1402 }
1403
1404 // --------------------------------------------------------------------
1405
1406 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 * Generate an update string
1408 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 * @param string the table upon which the query will be performed
1410 * @param array an associative array data of key/values
1411 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001412 * @return string
1413 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001414 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001416 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001418 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 }
Barry Mienydd671972010-10-04 16:33:58 +02001420
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001421 $this->where($where);
1422
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001424 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001426 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 }
1428
Andrey Andreev79f888b2013-08-06 13:59:23 +03001429 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1430 $this->_reset_write();
1431 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001432 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001433
1434 // --------------------------------------------------------------------
1435
1436 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001437 * Update statement
1438 *
1439 * Generates a platform-specific update string from the supplied data
1440 *
1441 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001442 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001443 * @return string
1444 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001445 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001446 {
1447 foreach ($values as $key => $val)
1448 {
1449 $valstr[] = $key.' = '.$val;
1450 }
1451
Andrey Andreev75546112012-07-05 11:01:29 +03001452 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001453 .$this->_compile_wh('qb_where')
1454 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001455 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001456 }
1457
1458 // --------------------------------------------------------------------
1459
1460 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 * Tests whether the string has an SQL operator
1462 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 * @param string
1464 * @return bool
1465 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001466 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 {
Andrey Andreev5078eb52014-12-02 01:11:54 +02001468 return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 }
1470
1471 // --------------------------------------------------------------------
1472
1473 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001474 * Returns the SQL string operator
1475 *
1476 * @param string
1477 * @return string
1478 */
1479 protected function _get_operator($str)
1480 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001481 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001482
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001483 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001484 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001485 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001486 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001487 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001488 $_operators = array(
1489 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1490 '\s*<>?\s*', // <, <>
1491 '\s*>\s*', // >
Andrey Andreev5078eb52014-12-02 01:11:54 +02001492 '\s+IS NULL', // IS NULL
1493 '\s+IS NOT NULL', // IS NOT NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001494 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001495 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001496 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1497 '\s+IN\s*\([^\)]+\)', // IN(list)
1498 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1499 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1500 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1501 );
1502
1503 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001504
Andrey Andreev6e704752012-07-18 00:46:33 +03001505 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1506 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001507 }
1508
1509 // --------------------------------------------------------------------
1510
1511 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1513 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001514 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001515 * @return mixed
1516 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001517 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001519 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001520
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 if (FALSE === strpos($driver, $function))
1522 {
1523 $function = $driver.$function;
1524 }
Barry Mienydd671972010-10-04 16:33:58 +02001525
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 if ( ! function_exists($function))
1527 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001528 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001530
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001531 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001532 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001533 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 }
1535
1536 // --------------------------------------------------------------------
1537
1538 /**
1539 * Set Cache Directory Path
1540 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 * @param string the path to the cache directory
1542 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001543 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001544 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 {
1546 $this->cachedir = $path;
1547 }
1548
1549 // --------------------------------------------------------------------
1550
1551 /**
1552 * Enable Query Caching
1553 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001554 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001555 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001556 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001558 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 }
1560
1561 // --------------------------------------------------------------------
1562
1563 /**
1564 * Disable Query Caching
1565 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001566 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001567 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001568 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001570 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 }
Barry Mienydd671972010-10-04 16:33:58 +02001572
Derek Allard2067d1a2008-11-13 22:59:24 +00001573 // --------------------------------------------------------------------
1574
1575 /**
1576 * Delete the cache files associated with a particular URI
1577 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001578 * @param string $segment_one = ''
1579 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001580 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001581 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001582 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001583 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001584 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001585 ? $this->CACHE->delete($segment_one, $segment_two)
1586 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001587 }
1588
1589 // --------------------------------------------------------------------
1590
1591 /**
1592 * Delete All cache files
1593 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001594 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001595 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001596 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001598 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001599 ? $this->CACHE->delete_all()
1600 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 }
1602
1603 // --------------------------------------------------------------------
1604
1605 /**
1606 * Initialize the Cache Class
1607 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001608 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001609 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001610 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001612 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001614 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001616 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001617 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001618 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001619 }
Derek Allarde37ab382009-02-03 16:13:57 +00001620
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1622 return TRUE;
1623 }
1624
1625 // --------------------------------------------------------------------
1626
1627 /**
1628 * Close DB Connection
1629 *
Barry Mienydd671972010-10-04 16:33:58 +02001630 * @return void
1631 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001632 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001634 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001636 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001637 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 }
Barry Mienydd671972010-10-04 16:33:58 +02001640
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 // --------------------------------------------------------------------
1642
1643 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001644 * Close DB Connection
1645 *
1646 * This method would be overriden by most of the drivers.
1647 *
1648 * @return void
1649 */
1650 protected function _close()
1651 {
1652 $this->conn_id = FALSE;
1653 }
1654
1655 // --------------------------------------------------------------------
1656
1657 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 * Display an error message
1659 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 * @param string the error message
1661 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001662 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001663 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001664 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001665 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 {
Derek Jonese7792202010-03-02 17:24:46 -06001667 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 $LANG->load('db');
1669
1670 $heading = $LANG->line('db_error_heading');
1671
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001672 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001674 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 }
1676 else
1677 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001678 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 }
Barry Mienydd671972010-10-04 16:33:58 +02001680
Pascal Kriete60f8c392010-08-25 18:03:28 +02001681 // Find the most likely culprit of the error by going through
1682 // the backtrace until the source file is no longer in the
1683 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001684 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001685 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001686 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001687 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001688 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001689 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1690 if (DIRECTORY_SEPARATOR !== '/')
1691 {
1692 $call['file'] = str_replace('\\', '/', $call['file']);
1693 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001694
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001695 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1696 {
1697 // Found it - use a relative path for safety
1698 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1699 $message[] = 'Line Number: '.$call['line'];
1700 break;
1701 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001702 }
1703 }
Barry Mienydd671972010-10-04 16:33:58 +02001704
Derek Jonese7792202010-03-02 17:24:46 -06001705 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001707 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 }
1709
1710 // --------------------------------------------------------------------
1711
1712 /**
1713 * Protect Identifiers
1714 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001715 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001716 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001718 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001719 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 *
1721 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1722 *
1723 * Or a query with aliasing:
1724 *
1725 * SELECT m.member_id, m.member_name FROM members AS m
1726 *
1727 * Since the column name can include up to four segments (host, DB, table, column)
1728 * or also have an alias prefix, we need to do a bit of work to figure this out and
1729 * insert the table prefix (if it exists) in the proper position, and escape only
1730 * the correct identifiers.
1731 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 * @param string
1733 * @param bool
1734 * @param mixed
1735 * @param bool
1736 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001737 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001738 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
1740 if ( ! is_bool($protect_identifiers))
1741 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001742 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 }
Derek Allarde37ab382009-02-03 16:13:57 +00001744
1745 if (is_array($item))
1746 {
1747 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001748 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001749 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001750 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001751 }
1752
1753 return $escaped_array;
1754 }
1755
Derek Allard911d3e02008-12-15 14:08:35 +00001756 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001757 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001758 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001759 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001760 //
1761 // Added exception for single quotes as well, we don't want to alter
1762 // literal strings. -- Narf
1763 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001764 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001765 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001766 }
1767
Andrey Andreevbf940582012-06-10 07:05:05 +03001768 // Convert tabs or multiple spaces into single spaces
1769 $item = preg_replace('/\s+/', ' ', $item);
1770
Andrey Andreevbf940582012-06-10 07:05:05 +03001771 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001772 // Note: strripos() is used in order to support spaces in table names
1773 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001774 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001775 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001776 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1777 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001778 $item = substr($item, 0, $offset);
1779 }
1780 elseif ($offset = strrpos($item, ' '))
1781 {
1782 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001783 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1784 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001785 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001786 }
1787 else
1788 {
1789 $alias = '';
1790 }
1791
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 // Break the string apart if it contains periods, then insert the table prefix
1793 // in the correct location, assuming the period doesn't indicate that we're dealing
1794 // with an alias. While we're at it, we will escape the components
1795 if (strpos($item, '.') !== FALSE)
1796 {
1797 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001798
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001800 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001802 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001803 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 if ($protect_identifiers === TRUE)
1805 {
1806 foreach ($parts as $key => $val)
1807 {
1808 if ( ! in_array($val, $this->_reserved_identifiers))
1809 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001810 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 }
1812 }
Barry Mienydd671972010-10-04 16:33:58 +02001813
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001815 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001816
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 return $item.$alias;
1818 }
Barry Mienydd671972010-10-04 16:33:58 +02001819
Andrey Andreev4c202602012-03-06 20:34:52 +02001820 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001821 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
1823 // We now add the table prefix based on some logic.
1824 // Do we have 4 segments (hostname.database.table.column)?
1825 // If so, we add the table prefix to the column name in the 3rd segment.
1826 if (isset($parts[3]))
1827 {
1828 $i = 2;
1829 }
1830 // Do we have 3 segments (database.table.column)?
1831 // If so, we add the table prefix to the column name in 2nd position
1832 elseif (isset($parts[2]))
1833 {
1834 $i = 1;
1835 }
1836 // Do we have 2 segments (table.column)?
1837 // If so, we add the table prefix to the column name in 1st segment
1838 else
1839 {
1840 $i = 0;
1841 }
Barry Mienydd671972010-10-04 16:33:58 +02001842
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 // This flag is set when the supplied $item does not contain a field name.
1844 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001845 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 {
1847 $i++;
1848 }
Barry Mienydd671972010-10-04 16:33:58 +02001849
Derek Jones55acc8b2009-07-11 03:38:13 +00001850 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001851 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001852 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001853 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001854 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001856 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 {
1858 $parts[$i] = $this->dbprefix.$parts[$i];
1859 }
Barry Mienydd671972010-10-04 16:33:58 +02001860
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 // Put the parts back together
1862 $item = implode('.', $parts);
1863 }
Barry Mienydd671972010-10-04 16:33:58 +02001864
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 if ($protect_identifiers === TRUE)
1866 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001867 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 }
Barry Mienydd671972010-10-04 16:33:58 +02001869
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 return $item.$alias;
1871 }
1872
Andrey Andreev4c202602012-03-06 20:34:52 +02001873 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001874 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001876 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001877 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001878 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001879 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001880 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001882 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 {
1884 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001885 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 }
1887
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001888 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001890 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 }
Barry Mienydd671972010-10-04 16:33:58 +02001892
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 return $item.$alias;
1894 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001895
Túbal Martín511f2252011-11-24 14:43:45 +01001896 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001897
Túbal Martín511f2252011-11-24 14:43:45 +01001898 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001899 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001900 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001901 *
Túbal Martín511f2252011-11-24 14:43:45 +01001902 * @return void
1903 */
1904 protected function _reset_select()
1905 {
Túbal Martín511f2252011-11-24 14:43:45 +01001906 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001907
1908}
1909
Derek Allard2067d1a2008-11-13 22:59:24 +00001910/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001911/* Location: ./system/database/DB_driver.php */