blob: 68e5a28336195be70cb4462dcddc8cb1d6d16aaa [file] [log] [blame]
Etki640ff4e2014-04-09 23:01:33 +04001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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 Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @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 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200127 * Encryption flag/data
128 *
129 * @var mixed
130 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300131 public $encrypt = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200132
133 /**
134 * Swap Prefix
135 *
136 * @var string
137 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200138 public $swap_pre = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200139
140 /**
141 * Database port
142 *
143 * @var int
144 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200145 public $port = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200146
147 /**
148 * Persistent connection flag
149 *
150 * @var bool
151 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200152 public $pconnect = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200153
154 /**
155 * Connection ID
156 *
157 * @var object|resource
158 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200159 public $conn_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200160
161 /**
162 * Result ID
163 *
164 * @var object|resource
165 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200166 public $result_id = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200167
168 /**
169 * Debug flag
170 *
171 * Whether to display error messages.
172 *
173 * @var bool
174 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200175 public $db_debug = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200176
177 /**
178 * Benchmark time
179 *
180 * @var int
181 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200182 public $benchmark = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200183
184 /**
185 * Executed queries count
186 *
187 * @var int
188 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200189 public $query_count = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200190
191 /**
192 * Bind marker
193 *
194 * Character used to identify values in a prepared statement.
195 *
196 * @var string
197 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200198 public $bind_marker = '?';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200199
200 /**
201 * Save queries flag
202 *
203 * Whether to keep an in-memory history of queries for debugging purposes.
204 *
205 * @var bool
206 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200207 public $save_queries = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200208
209 /**
210 * Queries list
211 *
212 * @see CI_DB_driver::$save_queries
213 * @var string[]
214 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200215 public $queries = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200216
217 /**
218 * Query times
219 *
220 * A list of times that queries took to execute.
221 *
222 * @var array
223 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200224 public $query_times = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200225
226 /**
227 * Data cache
228 *
229 * An internal generic value cache.
230 *
231 * @var array
232 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200233 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000234
Andrey Andreevae85eb42012-11-02 01:42:31 +0200235 /**
236 * Transaction enabled flag
237 *
238 * @var bool
239 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200240 public $trans_enabled = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200241
242 /**
243 * Strict transaction mode flag
244 *
245 * @var bool
246 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200247 public $trans_strict = TRUE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200248
249 /**
250 * Transaction depth level
251 *
252 * @var int
253 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200254 protected $_trans_depth = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255
Andrey Andreevae85eb42012-11-02 01:42:31 +0200256 /**
257 * Transaction status flag
258 *
259 * Used with transactions to determine if a rollback should occur.
260 *
261 * @var bool
262 */
263 protected $_trans_status = TRUE;
264
265 /**
Ahmedul Haque Abida2129772014-05-01 01:57:53 +0600266 * Transaction failure flag
267 *
268 * Used with transactions to determine if a transaction has failed.
269 *
270 * @var bool
271 */
272 protected $_trans_failure = FALSE;
273
274 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200275 * Cache On flag
276 *
277 * @var bool
278 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200279 public $cache_on = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200280
281 /**
282 * Cache directory path
283 *
284 * @var bool
285 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200286 public $cachedir = '';
Andrey Andreevae85eb42012-11-02 01:42:31 +0200287
288 /**
289 * Cache auto-delete flag
290 *
291 * @var bool
292 */
Andrey Andreevd1add432012-03-06 20:26:01 +0200293 public $cache_autodel = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200294
Andrey Andreevae85eb42012-11-02 01:42:31 +0200295 /**
296 * DB Cache object
297 *
298 * @see CI_DB_cache
299 * @var object
300 */
301 public $CACHE;
302
303 /**
304 * Protect identifiers flag
305 *
306 * @var bool
307 */
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +0000308 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +0200309
Andrey Andreevae85eb42012-11-02 01:42:31 +0200310 /**
311 * List of reserved identifiers
312 *
313 * Identifiers that must NOT be escaped.
314 *
315 * @var string[]
316 */
317 protected $_reserved_identifiers = array('*');
318
319 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200320 * Identifier escape character
321 *
322 * @var string
323 */
324 protected $_escape_char = '"';
325
326 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200327 * ESCAPE statement string
328 *
329 * @var string
330 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300331 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevae85eb42012-11-02 01:42:31 +0200332
333 /**
334 * ESCAPE character
335 *
336 * @var string
337 */
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300338 protected $_like_escape_chr = '!';
339
Andrey Andreev75546112012-07-05 11:01:29 +0300340 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200341 * ORDER BY random keyword
342 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200343 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200344 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200345 protected $_random_keyword = array('RAND()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200346
347 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200348 * COUNT string
349 *
350 * @used-by CI_DB_driver::count_all()
351 * @used-by CI_DB_query_builder::count_all_results()
352 *
353 * @var string
Andrey Andreev77a5d942012-07-05 15:42:14 +0300354 */
355 protected $_count_string = 'SELECT COUNT(*) AS ';
356
Andrey Andreevae85eb42012-11-02 01:42:31 +0200357 // --------------------------------------------------------------------
358
Andrey Andreev77a5d942012-07-05 15:42:14 +0300359 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200360 * Class constructor
Andrey Andreev75546112012-07-05 11:01:29 +0300361 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200362 * @param array $params
Andrey Andreev75546112012-07-05 11:01:29 +0300363 * @return void
364 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500365 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 if (is_array($params))
368 {
369 foreach ($params as $key => $val)
370 {
371 $this->$key = $val;
372 }
373 }
374
Andrey Andreev6c7c8912015-02-19 14:44:18 +0200375 $this->initialize();
Andrey Andreev90726b82015-01-20 12:39:22 +0200376 log_message('info', 'Database Driver Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Gints Murans89f77ee2012-05-23 00:23:50 +0300379 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 /**
382 * Initialize Database Settings
383 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200384 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200385 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200386 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200388 /* If an established connection is available, then there's
389 * no need to connect and select the database.
390 *
391 * Depending on the database driver, conn_id can be either
392 * boolean TRUE, a resource or an object.
393 */
394 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 return TRUE;
397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 // Connect to the database and set the connection ID
Andrey Andreev7e963512014-02-27 15:43:24 +0200402 $this->conn_id = $this->db_connect($this->pconnect);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200404 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 if ( ! $this->conn_id)
406 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100407 // Check if there is a failover set
408 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100410 // Go over all the failovers
411 foreach ($this->failover as $failover)
412 {
413 // Replace the current settings with those of the failover
414 foreach ($failover as $key => $val)
415 {
416 $this->$key = $val;
417 }
418
419 // Try to connect
Andrey Andreev7e963512014-02-27 15:43:24 +0200420 $this->conn_id = $this->db_connect($this->pconnect);
Felix Balfoort5d581b62011-11-29 15:53:01 +0100421
422 // If a connection is made break the foreach loop
423 if ($this->conn_id)
424 {
425 break;
426 }
427 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100429
430 // We still don't have a connection?
431 if ( ! $this->conn_id)
432 {
433 log_message('error', 'Unable to connect to the database');
434
435 if ($this->db_debug)
436 {
437 $this->display_error('db_unable_to_connect');
438 }
Andrey Andreev7e963512014-02-27 15:43:24 +0200439
Felix Balfoort5d581b62011-11-29 15:53:01 +0100440 return FALSE;
441 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200444 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200445 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
449
450 /**
Andrey Andreev0d3fde22015-01-05 16:56:41 +0200451 * DB connect
452 *
453 * This is just a dummy method that all drivers will override.
454 *
455 * @return mixed
456 */
457 public function db_connect()
458 {
459 return TRUE;
460 }
461
462 // --------------------------------------------------------------------
463
464 /**
Andrey Andreev2e171022014-02-25 15:21:41 +0200465 * Persistent database connection
466 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +0200467 * @return mixed
Andrey Andreev2e171022014-02-25 15:21:41 +0200468 */
469 public function db_pconnect()
470 {
471 return $this->db_connect(TRUE);
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200477 * Reconnect
478 *
479 * Keep / reestablish the db connection if no queries have been
480 * sent for a length of time exceeding the server's idle timeout.
481 *
482 * This is just a dummy method to allow drivers without such
483 * functionality to not declare it, while others will override it.
484 *
485 * @return void
486 */
487 public function reconnect()
488 {
489 }
490
491 // --------------------------------------------------------------------
492
493 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300494 * Select database
495 *
496 * This is just a dummy method to allow drivers without such
497 * functionality to not declare it, while others will override it.
498 *
499 * @return bool
500 */
501 public function db_select()
502 {
503 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
506 // --------------------------------------------------------------------
507
508 /**
509 * Set client character set
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200512 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200514 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200516 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200518 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 if ($this->db_debug)
521 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200522 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 return FALSE;
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 return TRUE;
529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 // --------------------------------------------------------------------
532
533 /**
534 * The name of the platform in use (mysql, mssql, etc...)
535 *
Barry Mienydd671972010-10-04 16:33:58 +0200536 * @return string
537 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500538 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
540 return $this->dbdriver;
541 }
542
543 // --------------------------------------------------------------------
544
545 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200546 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200548 * Returns a string containing the version of the database being used.
549 * Most drivers will override this method.
550 *
Barry Mienydd671972010-10-04 16:33:58 +0200551 * @return string
552 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200553 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200555 if (isset($this->data_cache['version']))
556 {
557 return $this->data_cache['version'];
558 }
559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 if (FALSE === ($sql = $this->_version()))
561 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200562 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
Derek Allard3683f772009-12-16 17:32:33 +0000564
Andrey Andreev7e963512014-02-27 15:43:24 +0200565 $query = $this->query($sql)->row();
Andrey Andreev08856b82012-03-03 03:19:28 +0200566 return $this->data_cache['version'] = $query->ver;
567 }
Derek Allard3683f772009-12-16 17:32:33 +0000568
Andrey Andreev08856b82012-03-03 03:19:28 +0200569 // --------------------------------------------------------------------
570
571 /**
572 * Version number query string
573 *
574 * @return string
575 */
576 protected function _version()
577 {
578 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // --------------------------------------------------------------------
582
583 /**
584 * Execute the query
585 *
586 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200587 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * upon successful execution of a "write" type query. Returns boolean
589 * FALSE upon failure, and if the $db_debug variable is set to TRUE
590 * will raise an error.
591 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300592 * @param string $sql
593 * @param array $binds = FALSE An array of binding data
594 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200595 * @return mixed
596 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300597 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100599 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200601 log_message('error', 'Invalid query: '.$sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200602 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300604 elseif ( ! is_bool($return_object))
605 {
606 $return_object = ! $this->is_write_type($sql);
607 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000608
609 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100610 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600611 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200612 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 }
Derek Jonese7792202010-03-02 17:24:46 -0600614
Ryan Dialef7474c2012-03-01 16:11:36 -0500615 // Compile binds if needed
616 if ($binds !== FALSE)
617 {
618 $sql = $this->compile_binds($sql, $binds);
619 }
620
Andrey Andreev4c202602012-03-06 20:34:52 +0200621 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // we will load the caching class and return the previously
623 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300624 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200626 $this->load_rdriver();
627 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200629 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Andrey Andreevb66664b2012-06-27 14:22:10 +0300633 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100634 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 $this->queries[] = $sql;
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200640 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 // Run the Query
643 if (FALSE === ($this->result_id = $this->simple_query($sql)))
644 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100645 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
647 $this->query_times[] = 0;
648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 // This will trigger a rollback if transactions are being used
651 $this->_trans_status = FALSE;
652
Andrey Andreev4be5de12012-03-02 15:45:41 +0200653 // Grab the error now, as we might run some additional queries before displaying the error
654 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200655
656 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300657 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 if ($this->db_debug)
660 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200662 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200663 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 // transactions to remain in limbo.
Andrey Andreev6c854422013-10-21 13:58:15 +0300665 if ($this->_trans_depth !== 0)
666 {
667 do
668 {
669 $this->trans_complete();
670 }
671 while ($this->_trans_depth !== 0);
672 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000673
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200674 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200675 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 return FALSE;
679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200682 $time_end = microtime(TRUE);
683 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000684
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100685 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
dixyab3cab52012-04-21 00:58:00 +0200687 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // Increment the query counter
691 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200692
Andrey Andreevb66664b2012-06-27 14:22:10 +0300693 // Will we have a result object instantiated? If not - we'll simply return TRUE
694 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300696 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100697 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
699 $this->CACHE->delete();
700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 return TRUE;
703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Barry Mienydd671972010-10-04 16:33:58 +0200705 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200706 $driver = $this->load_rdriver();
707 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200708
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200709 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100711 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 {
713 // We'll create a new instance of the result object
714 // only without the platform specific driver since
715 // we can't use it with cached data (the query result
716 // resource ID won't be any good once we've cached the
717 // result object, so we'll have to compile the data
718 // and save it)
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200719 $CR = new CI_DB_result($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 $CR->result_object = $RES->result_object();
721 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200722 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200723
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 // Reset these since cached objects can not utilize resource IDs.
725 $CR->conn_id = NULL;
726 $CR->result_id = NULL;
727
728 $this->CACHE->write($sql, $CR);
729 }
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 return $RES;
732 }
733
734 // --------------------------------------------------------------------
735
736 /**
737 * Load the result drivers
738 *
Barry Mienydd671972010-10-04 16:33:58 +0200739 * @return string the name of the result class
740 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500741 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
743 $driver = 'CI_DB_'.$this->dbdriver.'_result';
744
vlakofffadb8222013-05-12 10:57:09 +0200745 if ( ! class_exists($driver, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
Andrey Andreev7e963512014-02-27 15:43:24 +0200747 require_once(BASEPATH.'database/DB_result.php');
748 require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 }
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 return $driver;
752 }
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 // --------------------------------------------------------------------
755
756 /**
757 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200758 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 * we only use it when running transaction commands since they do
760 * not require all the features of the main query() function.
761 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200763 * @return mixed
764 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500765 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 {
767 if ( ! $this->conn_id)
768 {
769 $this->initialize();
770 }
771
772 return $this->_execute($sql);
773 }
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 // --------------------------------------------------------------------
776
777 /**
778 * Disable Transactions
779 * This permits transactions to be disabled at run-time.
780 *
Barry Mienydd671972010-10-04 16:33:58 +0200781 * @return void
782 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500783 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
785 $this->trans_enabled = FALSE;
786 }
787
788 // --------------------------------------------------------------------
789
790 /**
791 * Enable/disable Transaction Strict Mode
792 * When strict mode is enabled, if you are running multiple groups of
793 * transactions, if one group fails all groups will be rolled back.
794 * If strict mode is disabled, each group is treated autonomously, meaning
795 * a failure of one group will not affect any others
796 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300797 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200798 * @return void
799 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500800 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
802 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 // --------------------------------------------------------------------
806
807 /**
808 * Start Transaction
809 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300810 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200811 * @return void
812 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500813 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200814 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 if ( ! $this->trans_enabled)
816 {
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100817 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
819
820 // When transactions are nested we only begin/commit/rollback the outermost ones
821 if ($this->_trans_depth > 0)
822 {
823 $this->_trans_depth += 1;
824 return;
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500828 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 }
830
831 // --------------------------------------------------------------------
832
833 /**
834 * Complete Transaction
835 *
Barry Mienydd671972010-10-04 16:33:58 +0200836 * @return bool
837 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500838 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
840 if ( ! $this->trans_enabled)
841 {
842 return FALSE;
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 // When transactions are nested we only begin/commit/rollback the outermost ones
846 if ($this->_trans_depth > 1)
847 {
848 $this->_trans_depth -= 1;
849 return TRUE;
850 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500851 else
852 {
853 $this->_trans_depth = 0;
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // The query() function will set this flag to FALSE in the event that a query failed
Katsumi Hondafa99dc62013-04-03 22:47:34 +0900857 if ($this->_trans_status === FALSE OR $this->_trans_failure === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
859 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // If we are NOT running in strict mode, we will reset
862 // the _trans_status flag so that subsequent groups of transactions
863 // will be permitted.
864 if ($this->trans_strict === FALSE)
865 {
866 $this->_trans_status = TRUE;
867 }
868
869 log_message('debug', 'DB Transaction Failure');
870 return FALSE;
871 }
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 $this->trans_commit();
874 return TRUE;
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Lets you retrieve the transaction flag to determine if it has failed
881 *
Barry Mienydd671972010-10-04 16:33:58 +0200882 * @return bool
883 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500884 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
886 return $this->_trans_status;
887 }
888
889 // --------------------------------------------------------------------
890
891 /**
892 * Compile Bindings
893 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 * @param string the sql statement
895 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200896 * @return string
897 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500898 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300900 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
902 return $sql;
903 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300904 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300906 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300907 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300909 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200910 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300911 // Make sure we're using numeric keys
912 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300913 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
915
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300916 // We'll need the marker length later
917 $ml = strlen($this->bind_marker);
918
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300919 // Make sure not to replace a chunk inside a string that happens to match the bind marker
920 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300922 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300923 str_replace($matches[0],
924 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
925 $sql, $c),
926 $matches, PREG_OFFSET_CAPTURE);
927
928 // Bind values' count must match the count of markers in the query
929 if ($bind_count !== $c)
930 {
931 return $sql;
932 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300933 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300934 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 +0300935 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300936 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 }
938
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300939 do
940 {
941 $c--;
clawoo4a4f5502014-10-20 15:28:08 +0300942 $escaped_value = $this->escape($binds[$c]);
943 if (is_array($escaped_value))
944 {
945 $escaped_value = '('.implode(',', $escaped_value).')';
946 }
947 $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300948 }
949 while ($c !== 0);
950
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300951 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 }
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 // --------------------------------------------------------------------
955
956 /**
957 * Determines if a query is a "write" type.
958 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200960 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200961 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200962 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200964 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 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // --------------------------------------------------------------------
968
969 /**
970 * Calculate the aggregate query elapsed time
971 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200972 * @param int The number of decimal places
James L Parry78301732014-12-16 02:14:52 -0800973 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200974 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500975 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
977 return number_format($this->benchmark, $decimals);
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // --------------------------------------------------------------------
981
982 /**
983 * Returns the total number of queries
984 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200985 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200986 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500987 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
989 return $this->query_count;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 // --------------------------------------------------------------------
993
994 /**
995 * Returns the last query that was executed
996 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200997 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200998 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500999 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
1001 return end($this->queries);
1002 }
1003
1004 // --------------------------------------------------------------------
1005
1006 /**
1007 * "Smart" Escape String
1008 *
1009 * Escapes data based on type
1010 * Sets boolean and null types
1011 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @param string
Barry Mienydd671972010-10-04 16:33:58 +02001013 * @return mixed
1014 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001015 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +02001016 {
clawooa779c482014-10-18 14:47:04 +03001017 if (is_array($str))
1018 {
1019 $str = array_map(array(&$this, 'escape'), $str);
clawoo4a4f5502014-10-20 15:28:08 +03001020 return $str;
clawooa779c482014-10-18 14:47:04 +03001021 }
1022 elseif (is_string($str) OR (is_object($str) && method_exists($str, '__toString')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001024 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +00001025 }
1026 elseif (is_bool($str))
1027 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001028 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +00001029 }
vlakoff1228fe22013-01-14 01:30:09 +01001030 elseif ($str === NULL)
Derek Jonesa377bdd2009-02-11 18:55:24 +00001031 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001032 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +00001033 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001034
1035 return $str;
1036 }
1037
1038 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001039
Derek Jonese4ed5832009-02-20 21:44:59 +00001040 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001041 * Escape String
1042 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001043 * @param string|string[] $str Input string
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001044 * @param bool $like Whether or not the string will be used in a LIKE condition
1045 * @return string
1046 */
1047 public function escape_str($str, $like = FALSE)
1048 {
1049 if (is_array($str))
1050 {
1051 foreach ($str as $key => $val)
1052 {
1053 $str[$key] = $this->escape_str($val, $like);
1054 }
1055
1056 return $str;
1057 }
1058
1059 $str = $this->_escape_str($str);
1060
1061 // escape LIKE condition wildcards
1062 if ($like === TRUE)
1063 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001064 return str_replace(
1065 array($this->_like_escape_chr, '%', '_'),
1066 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
1067 $str
1068 );
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001069 }
1070
1071 return $str;
1072 }
1073
1074 // --------------------------------------------------------------------
1075
1076 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +00001077 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +00001078 *
1079 * Calls the individual driver for platform
1080 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +02001081 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001082 * @param string|string[]
Derek Jonese4ed5832009-02-20 21:44:59 +00001083 * @return mixed
1084 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001085 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +02001086 {
1087 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +00001088 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001089
Derek Jonese4ed5832009-02-20 21:44:59 +00001090 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -06001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +02001093 * Platform-dependant string escape
1094 *
1095 * @param string
1096 * @return string
1097 */
1098 protected function _escape_str($str)
1099 {
1100 return str_replace("'", "''", remove_invisible_characters($str));
1101 }
1102
1103 // --------------------------------------------------------------------
1104
1105 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 * Primary
1107 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001108 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 * position is the primary key
1110 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001111 * @param string $table Table name
1112 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001113 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001114 public function primary($table)
Barry Mienydd671972010-10-04 16:33:58 +02001115 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001117 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 }
1119
1120 // --------------------------------------------------------------------
1121
1122 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001123 * "Count All" query
1124 *
1125 * Generates a platform-specific query string that counts all records in
1126 * the specified database
1127 *
1128 * @param string
1129 * @return int
1130 */
1131 public function count_all($table = '')
1132 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001133 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001134 {
1135 return 0;
1136 }
1137
1138 $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 +01001139 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001140 {
1141 return 0;
1142 }
1143
1144 $query = $query->row();
1145 $this->_reset_select();
1146 return (int) $query->numrows;
1147 }
1148
1149 // --------------------------------------------------------------------
1150
1151 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * Returns an array of table names
1153 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001154 * @param string $constrain_by_prefix = FALSE
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001155 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001156 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001157 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
1159 // Is there a cached result?
1160 if (isset($this->data_cache['table_names']))
1161 {
1162 return $this->data_cache['table_names'];
1163 }
Barry Mienydd671972010-10-04 16:33:58 +02001164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
1166 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001167 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 }
1169
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001170 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +02001172
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001173 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001175 // Do we know from which column to get the table name?
1176 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001178 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001179 {
1180 $key = 'table_name';
1181 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001182 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001183 {
1184 $key = 'TABLE_NAME';
1185 }
1186 else
1187 {
1188 /* We have no other choice but to just get the first element's key.
vlakoff3a3d5f62013-10-17 22:22:16 +02001189 * Due to array_shift() accepting its argument by reference, if
Andrey Andreev12567e82012-01-25 22:50:33 +02001190 * E_STRICT is on, this would trigger a warning. So we'll have to
1191 * assign it first.
1192 */
1193 $key = array_keys($row);
1194 $key = array_shift($key);
1195 }
Taufan Aditya18209332012-02-09 16:07:27 +07001196 }
1197
Andrey Andreev12567e82012-01-25 22:50:33 +02001198 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 }
1200
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 return $this->data_cache['table_names'];
1202 }
Barry Mienydd671972010-10-04 16:33:58 +02001203
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 // --------------------------------------------------------------------
1205
1206 /**
1207 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001208 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001209 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +02001210 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001212 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001213 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001214 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 }
Barry Mienydd671972010-10-04 16:33:58 +02001216
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 // --------------------------------------------------------------------
1218
1219 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001220 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 * @param string the table name
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001223 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 */
Andrey Andreev358b0882015-01-05 21:00:18 +02001225 public function list_fields($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
1227 // Is there a cached result?
1228 if (isset($this->data_cache['field_names'][$table]))
1229 {
1230 return $this->data_cache['field_names'][$table];
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Greg Aker1edde302010-01-26 00:17:01 +00001233 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001235 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001239 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001240
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001241 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Andrey Andreev12567e82012-01-25 22:50:33 +02001243 // Do we know from where to get the column's name?
1244 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001246 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001247 {
1248 $key = 'column_name';
1249 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +03001250 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +02001251 {
1252 $key = 'COLUMN_NAME';
1253 }
1254 else
1255 {
RJ garcia395e2df2013-03-21 10:48:24 -05001256 // We have no other choice but to just get the first element's key.
1257 $key = key($row);
Andrey Andreev12567e82012-01-25 22:50:33 +02001258 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 }
Andrey Andreev12567e82012-01-25 22:50:33 +02001260
1261 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 }
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 return $this->data_cache['field_names'][$table];
1265 }
1266
1267 // --------------------------------------------------------------------
1268
1269 /**
1270 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -05001271 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 * @param string
1273 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +02001274 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001276 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +02001277 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001278 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 }
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 // --------------------------------------------------------------------
1282
1283 /**
1284 * Returns an object with field data
1285 *
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001286 * @param string $table the table name
1287 * @return array
Barry Mienydd671972010-10-04 16:33:58 +02001288 */
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001289 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001291 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev0d3fde22015-01-05 16:56:41 +02001292 return ($query) ? $query->field_data() : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001293 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001294
1295 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001298 * Escape the SQL Identifiers
1299 *
1300 * This function escapes column and table names
1301 *
Andrey Andreev9637b402012-06-08 15:39:24 +03001302 * @param mixed
1303 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001304 */
1305 public function escape_identifiers($item)
1306 {
Andrey Andreeva7ddd2d2012-11-06 11:53:45 +02001307 if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001308 {
1309 return $item;
1310 }
Andrey Andreev9637b402012-06-08 15:39:24 +03001311 elseif (is_array($item))
1312 {
1313 foreach ($item as $key => $value)
1314 {
1315 $item[$key] = $this->escape_identifiers($value);
1316 }
1317
1318 return $item;
1319 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +03001320 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +03001321 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001322 {
1323 return $item;
1324 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001325
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001326 static $preg_ec = array();
1327
1328 if (empty($preg_ec))
1329 {
1330 if (is_array($this->_escape_char))
1331 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001332 $preg_ec = array(
Andrey Andreev7e963512014-02-27 15:43:24 +02001333 preg_quote($this->_escape_char[0], '/'),
1334 preg_quote($this->_escape_char[1], '/'),
1335 $this->_escape_char[0],
1336 $this->_escape_char[1]
1337 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001338 }
1339 else
1340 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001341 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001342 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001343 }
1344 }
1345
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001346 foreach ($this->_reserved_identifiers as $id)
1347 {
1348 if (strpos($item, '.'.$id) !== FALSE)
1349 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001350 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 +03001351 }
1352 }
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].'$2', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001355 }
1356
1357 // --------------------------------------------------------------------
1358
1359 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 * Generate an insert string
1361 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 * @param string the table upon which the query will be performed
1363 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001364 * @return string
1365 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001366 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001368 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001369
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001370 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001372 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 $values[] = $this->escape($val);
1374 }
Barry Mienydd671972010-10-04 16:33:58 +02001375
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001376 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001377 }
1378
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 // --------------------------------------------------------------------
1380
1381 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001382 * Insert statement
1383 *
1384 * Generates a platform-specific insert string from the supplied data
1385 *
1386 * @param string the table name
1387 * @param array the insert keys
1388 * @param array the insert values
1389 * @return string
1390 */
1391 protected function _insert($table, $keys, $values)
1392 {
1393 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1394 }
1395
1396 // --------------------------------------------------------------------
1397
1398 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 * Generate an update string
1400 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 * @param string the table upon which the query will be performed
1402 * @param array an associative array data of key/values
1403 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001404 * @return string
1405 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001406 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001408 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001410 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 }
Barry Mienydd671972010-10-04 16:33:58 +02001412
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001413 $this->where($where);
1414
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001416 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001418 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 }
1420
Andrey Andreev79f888b2013-08-06 13:59:23 +03001421 $sql = $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields);
1422 $this->_reset_write();
1423 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +02001424 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001425
1426 // --------------------------------------------------------------------
1427
1428 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001429 * Update statement
1430 *
1431 * Generates a platform-specific update string from the supplied data
1432 *
1433 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001434 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001435 * @return string
1436 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001437 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001438 {
1439 foreach ($values as $key => $val)
1440 {
1441 $valstr[] = $key.' = '.$val;
1442 }
1443
Andrey Andreev75546112012-07-05 11:01:29 +03001444 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001445 .$this->_compile_wh('qb_where')
1446 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001447 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001448 }
1449
1450 // --------------------------------------------------------------------
1451
1452 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 * Tests whether the string has an SQL operator
1454 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 * @param string
1456 * @return bool
1457 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001458 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 {
Andrey Andreev5078eb52014-12-02 01:11:54 +02001460 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 +00001461 }
1462
1463 // --------------------------------------------------------------------
1464
1465 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001466 * Returns the SQL string operator
1467 *
1468 * @param string
1469 * @return string
1470 */
1471 protected function _get_operator($str)
1472 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001473 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001474
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001475 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001476 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001477 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001478 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001479 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001480 $_operators = array(
1481 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1482 '\s*<>?\s*', // <, <>
1483 '\s*>\s*', // >
Andrey Andreev5078eb52014-12-02 01:11:54 +02001484 '\s+IS NULL', // IS NULL
1485 '\s+IS NOT NULL', // IS NOT NULL
Tufan Barış YILDIRIM30893b92013-12-19 21:30:56 +02001486 '\s+EXISTS\s*\([^\)]+\)', // EXISTS(sql)
Andrey Andreev295cfa92013-12-20 11:48:38 +02001487 '\s+NOT EXISTS\s*\([^\)]+\)', // NOT EXISTS(sql)
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001488 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1489 '\s+IN\s*\([^\)]+\)', // IN(list)
1490 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1491 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1492 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1493 );
1494
1495 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001496
Andrey Andreev6e704752012-07-18 00:46:33 +03001497 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1498 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001499 }
1500
1501 // --------------------------------------------------------------------
1502
1503 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1505 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001506 * @param string $function Function name
Barry Mienydd671972010-10-04 16:33:58 +02001507 * @return mixed
1508 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001509 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001511 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001512
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 if (FALSE === strpos($driver, $function))
1514 {
1515 $function = $driver.$function;
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 if ( ! function_exists($function))
1519 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001520 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001522
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001523 return (func_num_args() > 1)
Kakyshaa3f91b92013-12-14 05:23:23 +04001524 ? call_user_func_array($function, array_slice(func_get_args(), 1))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001525 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 }
1527
1528 // --------------------------------------------------------------------
1529
1530 /**
1531 * Set Cache Directory Path
1532 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 * @param string the path to the cache directory
1534 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001535 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001536 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
1538 $this->cachedir = $path;
1539 }
1540
1541 // --------------------------------------------------------------------
1542
1543 /**
1544 * Enable Query Caching
1545 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001546 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001547 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001548 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001550 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 }
1552
1553 // --------------------------------------------------------------------
1554
1555 /**
1556 * Disable Query Caching
1557 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001558 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001559 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001560 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001562 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 }
Barry Mienydd671972010-10-04 16:33:58 +02001564
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 // --------------------------------------------------------------------
1566
1567 /**
1568 * Delete the cache files associated with a particular URI
1569 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001570 * @param string $segment_one = ''
1571 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001572 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001573 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001574 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001575 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001576 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001577 ? $this->CACHE->delete($segment_one, $segment_two)
1578 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001579 }
1580
1581 // --------------------------------------------------------------------
1582
1583 /**
1584 * Delete All cache files
1585 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001586 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001587 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001588 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 {
Andrey Andreev7e963512014-02-27 15:43:24 +02001590 return $this->_cache_init()
Andrey Andreev043f2602012-03-06 20:16:42 +02001591 ? $this->CACHE->delete_all()
1592 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 }
1594
1595 // --------------------------------------------------------------------
1596
1597 /**
1598 * Initialize the Cache Class
1599 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001600 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001601 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001602 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001604 if ( ! class_exists('CI_DB_Cache', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001606 require_once(BASEPATH.'database/DB_cache.php');
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 }
Andrey Andreev58e1c002014-02-26 18:51:18 +02001608 elseif (is_object($this->CACHE))
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001609 {
Andrey Andreev58e1c002014-02-26 18:51:18 +02001610 return TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001611 }
Derek Allarde37ab382009-02-03 16:13:57 +00001612
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1614 return TRUE;
1615 }
1616
1617 // --------------------------------------------------------------------
1618
1619 /**
1620 * Close DB Connection
1621 *
Barry Mienydd671972010-10-04 16:33:58 +02001622 * @return void
1623 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001624 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001626 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001628 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001629 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 }
Barry Mienydd671972010-10-04 16:33:58 +02001632
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 // --------------------------------------------------------------------
1634
1635 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001636 * Close DB Connection
1637 *
Claudio Galdiolob83a3d22015-01-29 11:42:50 -05001638 * This method would be overridden by most of the drivers.
Andrey Andreev79922c02012-05-23 12:27:17 +03001639 *
1640 * @return void
1641 */
1642 protected function _close()
1643 {
1644 $this->conn_id = FALSE;
1645 }
1646
1647 // --------------------------------------------------------------------
1648
1649 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 * Display an error message
1651 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 * @param string the error message
1653 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001654 * @param bool whether to localize the message
vlakoff52301c72013-03-29 14:23:34 +01001655 * @return string sends the application/views/errors/error_db.php template
Barry Mienydd671972010-10-04 16:33:58 +02001656 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001657 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 {
Derek Jonese7792202010-03-02 17:24:46 -06001659 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 $LANG->load('db');
1661
1662 $heading = $LANG->line('db_error_heading');
1663
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001664 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001666 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 }
1668 else
1669 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001670 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 }
Barry Mienydd671972010-10-04 16:33:58 +02001672
Pascal Kriete60f8c392010-08-25 18:03:28 +02001673 // Find the most likely culprit of the error by going through
1674 // the backtrace until the source file is no longer in the
1675 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001676 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001677 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001678 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001679 if (isset($call['file'], $call['class']))
Andrey Andreev1194ad72012-10-05 17:05:46 +03001680 {
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001681 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1682 if (DIRECTORY_SEPARATOR !== '/')
1683 {
1684 $call['file'] = str_replace('\\', '/', $call['file']);
1685 }
Andrey Andreev1194ad72012-10-05 17:05:46 +03001686
Andrey Andreev342bb7e2012-11-20 23:17:28 +02001687 if (strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') === FALSE)
1688 {
1689 // Found it - use a relative path for safety
1690 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
1691 $message[] = 'Line Number: '.$call['line'];
1692 break;
1693 }
Pascal Kriete60f8c392010-08-25 18:03:28 +02001694 }
1695 }
Barry Mienydd671972010-10-04 16:33:58 +02001696
Derek Jonese7792202010-03-02 17:24:46 -06001697 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 echo $error->show_error($heading, $message, 'error_db');
Andrey Andreev7cf682a2014-03-13 14:55:45 +02001699 exit(8); // EXIT_DATABASE
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 }
1701
1702 // --------------------------------------------------------------------
1703
1704 /**
1705 * Protect Identifiers
1706 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001707 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001708 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001710 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001711 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 *
1713 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1714 *
1715 * Or a query with aliasing:
1716 *
1717 * SELECT m.member_id, m.member_name FROM members AS m
1718 *
1719 * Since the column name can include up to four segments (host, DB, table, column)
1720 * or also have an alias prefix, we need to do a bit of work to figure this out and
1721 * insert the table prefix (if it exists) in the proper position, and escape only
1722 * the correct identifiers.
1723 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 * @param string
1725 * @param bool
1726 * @param mixed
1727 * @param bool
1728 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001729 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001730 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 {
1732 if ( ! is_bool($protect_identifiers))
1733 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001734 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 }
Derek Allarde37ab382009-02-03 16:13:57 +00001736
1737 if (is_array($item))
1738 {
1739 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001740 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001741 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001742 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001743 }
1744
1745 return $escaped_array;
1746 }
1747
Derek Allard911d3e02008-12-15 14:08:35 +00001748 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001749 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001750 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001751 // way to deal with this, but I'm not thinking of it -- Rick
Andrey Andreev3b0c08a2013-03-29 15:15:41 +02001752 //
1753 // Added exception for single quotes as well, we don't want to alter
1754 // literal strings. -- Narf
1755 if (strpos($item, '(') !== FALSE OR strpos($item, "'") !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001756 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001757 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001758 }
1759
Andrey Andreevbf940582012-06-10 07:05:05 +03001760 // Convert tabs or multiple spaces into single spaces
1761 $item = preg_replace('/\s+/', ' ', $item);
1762
Andrey Andreevbf940582012-06-10 07:05:05 +03001763 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001764 // Note: strripos() is used in order to support spaces in table names
1765 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001766 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001767 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001768 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1769 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001770 $item = substr($item, 0, $offset);
1771 }
1772 elseif ($offset = strrpos($item, ' '))
1773 {
1774 $alias = ($protect_identifiers)
Andrey Andreev7e963512014-02-27 15:43:24 +02001775 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1776 : substr($item, $offset);
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001777 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001778 }
1779 else
1780 {
1781 $alias = '';
1782 }
1783
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 // Break the string apart if it contains periods, then insert the table prefix
1785 // in the correct location, assuming the period doesn't indicate that we're dealing
1786 // with an alias. While we're at it, we will escape the components
1787 if (strpos($item, '.') !== FALSE)
1788 {
1789 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001790
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001792 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001794 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001795 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 if ($protect_identifiers === TRUE)
1797 {
1798 foreach ($parts as $key => $val)
1799 {
1800 if ( ! in_array($val, $this->_reserved_identifiers))
1801 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001802 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 }
1804 }
Barry Mienydd671972010-10-04 16:33:58 +02001805
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001807 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001808
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 return $item.$alias;
1810 }
Barry Mienydd671972010-10-04 16:33:58 +02001811
Andrey Andreev4c202602012-03-06 20:34:52 +02001812 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001813 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 {
1815 // We now add the table prefix based on some logic.
1816 // Do we have 4 segments (hostname.database.table.column)?
1817 // If so, we add the table prefix to the column name in the 3rd segment.
1818 if (isset($parts[3]))
1819 {
1820 $i = 2;
1821 }
1822 // Do we have 3 segments (database.table.column)?
1823 // If so, we add the table prefix to the column name in 2nd position
1824 elseif (isset($parts[2]))
1825 {
1826 $i = 1;
1827 }
1828 // Do we have 2 segments (table.column)?
1829 // If so, we add the table prefix to the column name in 1st segment
1830 else
1831 {
1832 $i = 0;
1833 }
Barry Mienydd671972010-10-04 16:33:58 +02001834
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 // This flag is set when the supplied $item does not contain a field name.
1836 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001837 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 {
1839 $i++;
1840 }
Barry Mienydd671972010-10-04 16:33:58 +02001841
Derek Jones55acc8b2009-07-11 03:38:13 +00001842 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001843 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001844 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001845 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001846 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001848 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 {
1850 $parts[$i] = $this->dbprefix.$parts[$i];
1851 }
Barry Mienydd671972010-10-04 16:33:58 +02001852
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 // Put the parts back together
1854 $item = implode('.', $parts);
1855 }
Barry Mienydd671972010-10-04 16:33:58 +02001856
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 if ($protect_identifiers === TRUE)
1858 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001859 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 }
Barry Mienydd671972010-10-04 16:33:58 +02001861
Derek Allard2067d1a2008-11-13 22:59:24 +00001862 return $item.$alias;
1863 }
1864
Andrey Andreev4c202602012-03-06 20:34:52 +02001865 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001866 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001868 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001869 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001870 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001871 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001872 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001874 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 {
1876 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001877 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 }
1879
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001880 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001882 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 }
Barry Mienydd671972010-10-04 16:33:58 +02001884
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 return $item.$alias;
1886 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001887
Túbal Martín511f2252011-11-24 14:43:45 +01001888 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001889
Túbal Martín511f2252011-11-24 14:43:45 +01001890 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001891 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001892 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001893 *
Túbal Martín511f2252011-11-24 14:43:45 +01001894 * @return void
1895 */
1896 protected function _reset_select()
1897 {
Túbal Martín511f2252011-11-24 14:43:45 +01001898 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001899
1900}