blob: c757277ced70883a09c2e567a91521c8e8899521 [file] [log] [blame]
Andrey Andreev4c202602012-03-06 20:34:52 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Timothy Warren7eeda532012-03-19 16:01:55 -040041abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreev738f5342012-03-06 20:43:40 +020043 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
48 public $dbdriver = 'mysql';
49 public $dbprefix = '';
50 public $char_set = 'utf8';
51 public $dbcollat = 'utf8_general_ci';
52 public $autoinit = TRUE; // Whether to automatically initialize the DB
53 public $swap_pre = '';
54 public $port = '';
55 public $pconnect = FALSE;
56 public $conn_id = FALSE;
57 public $result_id = FALSE;
58 public $db_debug = FALSE;
59 public $benchmark = 0;
60 public $query_count = 0;
61 public $bind_marker = '?';
62 public $save_queries = TRUE;
63 public $queries = array();
64 public $query_times = array();
65 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000066
Andrey Andreevd1add432012-03-06 20:26:01 +020067 public $trans_enabled = TRUE;
68 public $trans_strict = TRUE;
69 protected $_trans_depth = 0;
70 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000071
Andrey Andreevd1add432012-03-06 20:26:01 +020072 public $cache_on = FALSE;
73 public $cachedir = '';
74 public $cache_autodel = FALSE;
75 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020076
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000077 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020078 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
Timothy Warrene45518d2012-03-06 07:38:00 -050080 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if (is_array($params))
83 {
84 foreach ($params as $key => $val)
85 {
86 $this->$key = $val;
87 }
88 }
89
90 log_message('debug', 'Database Driver Class Initialized');
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
Root36237d82012-05-21 18:30:00 -040095 public function __destruct()
96 {
97 $this->close();
98 }
99
100 // --------------------------------------------------------------------
101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 /**
103 * Initialize Database Settings
104 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200105 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200106 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200107 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200109 /* If an established connection is available, then there's
110 * no need to connect and select the database.
111 *
112 * Depending on the database driver, conn_id can be either
113 * boolean TRUE, a resource or an object.
114 */
115 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 return TRUE;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // Connect to the database and set the connection ID
123 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
124
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200125 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 if ( ! $this->conn_id)
127 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100128 // Check if there is a failover set
129 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100131 // Go over all the failovers
132 foreach ($this->failover as $failover)
133 {
134 // Replace the current settings with those of the failover
135 foreach ($failover as $key => $val)
136 {
137 $this->$key = $val;
138 }
139
140 // Try to connect
141 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
142
143 // If a connection is made break the foreach loop
144 if ($this->conn_id)
145 {
146 break;
147 }
148 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100150
151 // We still don't have a connection?
152 if ( ! $this->conn_id)
153 {
154 log_message('error', 'Unable to connect to the database');
155
156 if ($this->db_debug)
157 {
158 $this->display_error('db_unable_to_connect');
159 }
160 return FALSE;
161 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 // ----------------------------------------------------------------
165
166 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200167 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200169 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200170
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200171 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200173 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200175 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200178 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200179 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 // --------------------------------------------------------------------
183
184 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200185 * Reconnect
186 *
187 * Keep / reestablish the db connection if no queries have been
188 * sent for a length of time exceeding the server's idle timeout.
189 *
190 * This is just a dummy method to allow drivers without such
191 * functionality to not declare it, while others will override it.
192 *
193 * @return void
194 */
195 public function reconnect()
196 {
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300202 * Select database
203 *
204 * This is just a dummy method to allow drivers without such
205 * functionality to not declare it, while others will override it.
206 *
207 * @return bool
208 */
209 public function db_select()
210 {
211 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Set client character set
218 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200220 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200222 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200224 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200226 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 if ($this->db_debug)
229 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200230 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 return FALSE;
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 return TRUE;
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 // --------------------------------------------------------------------
240
241 /**
242 * The name of the platform in use (mysql, mssql, etc...)
243 *
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return string
245 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500246 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 return $this->dbdriver;
249 }
250
251 // --------------------------------------------------------------------
252
253 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200254 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200256 * Returns a string containing the version of the database being used.
257 * Most drivers will override this method.
258 *
Barry Mienydd671972010-10-04 16:33:58 +0200259 * @return string
260 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200261 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200263 if (isset($this->data_cache['version']))
264 {
265 return $this->data_cache['version'];
266 }
267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 if (FALSE === ($sql = $this->_version()))
269 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200270 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
Derek Allard3683f772009-12-16 17:32:33 +0000272
Andrey Andreev08856b82012-03-03 03:19:28 +0200273 $query = $this->query($sql);
274 $query = $query->row();
275 return $this->data_cache['version'] = $query->ver;
276 }
Derek Allard3683f772009-12-16 17:32:33 +0000277
Andrey Andreev08856b82012-03-03 03:19:28 +0200278 // --------------------------------------------------------------------
279
280 /**
281 * Version number query string
282 *
283 * @return string
284 */
285 protected function _version()
286 {
287 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
291
292 /**
293 * Execute the query
294 *
295 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200296 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * upon successful execution of a "write" type query. Returns boolean
298 * FALSE upon failure, and if the $db_debug variable is set to TRUE
299 * will raise an error.
300 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * @param string An SQL query string
302 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200303 * @return mixed
304 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500305 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
307 if ($sql == '')
308 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200309 log_message('error', 'Invalid query: '.$sql);
310
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200311 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
313
314 // Verify table prefix and replace if necessary
Andrey Andreev6202a3c2012-03-28 14:46:24 +0300315 if ($this->dbprefix != '' && $this->swap_pre != '' && $this->dbprefix != $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600316 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200317 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
Derek Jonese7792202010-03-02 17:24:46 -0600319
Ryan Dialef7474c2012-03-01 16:11:36 -0500320 // Compile binds if needed
321 if ($binds !== FALSE)
322 {
323 $sql = $this->compile_binds($sql, $binds);
324 }
325
Andrey Andreev4c202602012-03-06 20:34:52 +0200326 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // we will load the caching class and return the previously
328 // cached query if it exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200329 if ($this->cache_on == TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200331 $this->load_rdriver();
332 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200334 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Jones37f4b9c2011-07-01 17:56:50 -0500338 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 if ($this->save_queries == TRUE)
340 {
341 $this->queries[] = $sql;
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200345 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // Run the Query
348 if (FALSE === ($this->result_id = $this->simple_query($sql)))
349 {
350 if ($this->save_queries == TRUE)
351 {
352 $this->query_times[] = 0;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // This will trigger a rollback if transactions are being used
356 $this->_trans_status = FALSE;
357
Andrey Andreev4be5de12012-03-02 15:45:41 +0200358 // Grab the error now, as we might run some additional queries before displaying the error
359 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200360
361 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200362 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 if ($this->db_debug)
365 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200367 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200368 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // transactions to remain in limbo.
370 $this->trans_complete();
371
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200372 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200373 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 return FALSE;
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200380 $time_end = microtime(TRUE);
381 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000382
383 if ($this->save_queries == TRUE)
384 {
dixyab3cab52012-04-21 00:58:00 +0200385 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // Increment the query counter
389 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // Was the query a "write" type?
392 // If so we'll simply return true
393 if ($this->is_write_type($sql) === TRUE)
394 {
395 // If caching is enabled we'll auto-cleanup any
396 // existing files related to this particular URI
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200397 if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $this->CACHE->delete();
400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 return TRUE;
403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // Return TRUE if we don't need to create a result object
406 // Currently only the Oracle driver uses this when stored
407 // procedures are used
408 if ($return_object !== TRUE)
409 {
410 return TRUE;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
413 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200414 $driver = $this->load_rdriver();
415 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200416
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200417 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // result object and save it to a cache file.
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200419 if ($this->cache_on == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 // We'll create a new instance of the result object
422 // only without the platform specific driver since
423 // we can't use it with cached data (the query result
424 // resource ID won't be any good once we've cached the
425 // result object, so we'll have to compile the data
426 // and save it)
427 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 $CR->result_object = $RES->result_object();
429 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200430 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // Reset these since cached objects can not utilize resource IDs.
433 $CR->conn_id = NULL;
434 $CR->result_id = NULL;
435
436 $this->CACHE->write($sql, $CR);
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 return $RES;
440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Load the result drivers
446 *
Barry Mienydd671972010-10-04 16:33:58 +0200447 * @return string the name of the result class
448 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500449 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 $driver = 'CI_DB_'.$this->dbdriver.'_result';
452
453 if ( ! class_exists($driver))
454 {
Greg Aker3a746652011-04-19 10:59:47 -0500455 include_once(BASEPATH.'database/DB_result.php');
456 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 return $driver;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
463
464 /**
465 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200466 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * we only use it when running transaction commands since they do
468 * not require all the features of the main query() function.
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200471 * @return mixed
472 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500473 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 if ( ! $this->conn_id)
476 {
477 $this->initialize();
478 }
479
480 return $this->_execute($sql);
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // --------------------------------------------------------------------
484
485 /**
486 * Disable Transactions
487 * This permits transactions to be disabled at run-time.
488 *
Barry Mienydd671972010-10-04 16:33:58 +0200489 * @return void
490 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500491 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 $this->trans_enabled = FALSE;
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
499 * Enable/disable Transaction Strict Mode
500 * When strict mode is enabled, if you are running multiple groups of
501 * transactions, if one group fails all groups will be rolled back.
502 * If strict mode is disabled, each group is treated autonomously, meaning
503 * a failure of one group will not affect any others
504 *
Barry Mienydd671972010-10-04 16:33:58 +0200505 * @return void
506 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500507 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Start Transaction
516 *
Barry Mienydd671972010-10-04 16:33:58 +0200517 * @return void
518 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500519 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200520 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 if ( ! $this->trans_enabled)
522 {
523 return FALSE;
524 }
525
526 // When transactions are nested we only begin/commit/rollback the outermost ones
527 if ($this->_trans_depth > 0)
528 {
529 $this->_trans_depth += 1;
530 return;
531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500534 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Complete Transaction
541 *
Barry Mienydd671972010-10-04 16:33:58 +0200542 * @return bool
543 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500544 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
546 if ( ! $this->trans_enabled)
547 {
548 return FALSE;
549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 // When transactions are nested we only begin/commit/rollback the outermost ones
552 if ($this->_trans_depth > 1)
553 {
554 $this->_trans_depth -= 1;
555 return TRUE;
556 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500557 else
558 {
559 $this->_trans_depth = 0;
560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 // The query() function will set this flag to FALSE in the event that a query failed
563 if ($this->_trans_status === FALSE)
564 {
565 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 // If we are NOT running in strict mode, we will reset
568 // the _trans_status flag so that subsequent groups of transactions
569 // will be permitted.
570 if ($this->trans_strict === FALSE)
571 {
572 $this->_trans_status = TRUE;
573 }
574
575 log_message('debug', 'DB Transaction Failure');
576 return FALSE;
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 $this->trans_commit();
580 return TRUE;
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Lets you retrieve the transaction flag to determine if it has failed
587 *
Barry Mienydd671972010-10-04 16:33:58 +0200588 * @return bool
589 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500590 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 return $this->_trans_status;
593 }
594
595 // --------------------------------------------------------------------
596
597 /**
598 * Compile Bindings
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @param string the sql statement
601 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200602 * @return string
603 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500604 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 if (strpos($sql, $this->bind_marker) === FALSE)
607 {
608 return $sql;
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 if ( ! is_array($binds))
612 {
613 $binds = array($binds);
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // Get the sql segments around the bind markers
617 $segments = explode($this->bind_marker, $sql);
618
619 // The count of bind should be 1 less then the count of segments
620 // If there are more bind arguments trim it down
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200621 if (count($binds) >= count($segments))
622 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 $binds = array_slice($binds, 0, count($segments)-1);
624 }
625
626 // Construct the binded query
627 $result = $segments[0];
628 $i = 0;
629 foreach ($binds as $bind)
630 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200631 $result .= $this->escape($bind).$segments[++$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
633
634 return $result;
635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
638
639 /**
640 * Determines if a query is a "write" type.
641 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200643 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200644 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200645 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300647 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 // --------------------------------------------------------------------
651
652 /**
653 * Calculate the aggregate query elapsed time
654 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200655 * @param int The number of decimal places
656 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200657 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500658 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
660 return number_format($this->benchmark, $decimals);
661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // --------------------------------------------------------------------
664
665 /**
666 * Returns the total number of queries
667 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200668 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200669 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500670 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 return $this->query_count;
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // --------------------------------------------------------------------
676
677 /**
678 * Returns the last query that was executed
679 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200680 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200681 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500682 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 return end($this->queries);
685 }
686
687 // --------------------------------------------------------------------
688
689 /**
690 * "Smart" Escape String
691 *
692 * Escapes data based on type
693 * Sets boolean and null types
694 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200696 * @return mixed
697 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500698 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200699 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500700 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200702 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000703 }
704 elseif (is_bool($str))
705 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200706 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000707 }
708 elseif (is_null($str))
709 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200710 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000711 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000712
713 return $str;
714 }
715
716 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600717
Derek Jonese4ed5832009-02-20 21:44:59 +0000718 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000719 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000720 *
721 * Calls the individual driver for platform
722 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200723 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000724 * @param string
725 * @return mixed
726 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500727 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200728 {
729 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000730 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000731
Derek Jonese4ed5832009-02-20 21:44:59 +0000732 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 /**
735 * Primary
736 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200737 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 * position is the primary key
739 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200741 * @return string
742 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500743 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200744 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200746 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 }
748
749 // --------------------------------------------------------------------
750
751 /**
752 * Returns an array of table names
753 *
Barry Mienydd671972010-10-04 16:33:58 +0200754 * @return array
755 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500756 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
758 // Is there a cached result?
759 if (isset($this->data_cache['table_names']))
760 {
761 return $this->data_cache['table_names'];
762 }
Barry Mienydd671972010-10-04 16:33:58 +0200763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
765 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200766 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
768
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200769 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200771
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200772 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200774 // Do we know from which column to get the table name?
775 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300777 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200778 {
779 $key = 'table_name';
780 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300781 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200782 {
783 $key = 'TABLE_NAME';
784 }
785 else
786 {
787 /* We have no other choice but to just get the first element's key.
788 * Due to array_shift() accepting it's argument by reference, if
789 * E_STRICT is on, this would trigger a warning. So we'll have to
790 * assign it first.
791 */
792 $key = array_keys($row);
793 $key = array_shift($key);
794 }
Taufan Aditya18209332012-02-09 16:07:27 +0700795 }
796
Andrey Andreev12567e82012-01-25 22:50:33 +0200797 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 }
799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 return $this->data_cache['table_names'];
801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 // --------------------------------------------------------------------
804
805 /**
806 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500807 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200808 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500810 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200811 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200812 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 // --------------------------------------------------------------------
816
817 /**
818 * Fetch MySQL Field Names
819 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200821 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500823 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 {
825 // Is there a cached result?
826 if (isset($this->data_cache['field_names'][$table]))
827 {
828 return $this->data_cache['field_names'][$table];
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 if ($table == '')
832 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200833 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Greg Aker1edde302010-01-26 00:17:01 +0000836 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200838 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200842 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200843
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500844 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200846 // Do we know from where to get the column's name?
847 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300849 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200850 {
851 $key = 'column_name';
852 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300853 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200854 {
855 $key = 'COLUMN_NAME';
856 }
857 else
858 {
859 /* We have no other choice but to just get the first element's key.
860 * Due to array_shift() accepting it's argument by reference, if
861 * E_STRICT is on, this would trigger a warning. So we'll have to
862 * assign it first.
863 */
864 $key = array_keys($row);
865 $key = array_shift($key);
866 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200868
869 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 return $this->data_cache['field_names'][$table];
873 }
874
875 // --------------------------------------------------------------------
876
877 /**
878 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500879 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 * @param string
881 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200882 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500884 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200885 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200886 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // --------------------------------------------------------------------
890
891 /**
892 * Returns an object with field data
893 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200895 * @return object
896 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500897 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 if ($table == '')
900 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200901 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
Barry Mienydd671972010-10-04 16:33:58 +0200903
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200904 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200906 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000907
908 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200909
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300911 * Escape the SQL Identifiers
912 *
913 * This function escapes column and table names
914 *
915 * @param string
916 * @return string
917 */
918 public function escape_identifiers($item)
919 {
920 if ($this->_escape_char == '')
921 {
922 return $item;
923 }
924
925 foreach ($this->_reserved_identifiers as $id)
926 {
927 if (strpos($item, '.'.$id) !== FALSE)
928 {
929 $item = str_replace('.', $this->_escape_char.'.', $item);
930
931 // remove duplicates if the user already included the escape
932 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
933 }
934 }
935
936 if (strpos($item, '.') !== FALSE)
937 {
938 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
939 }
940
941 // remove duplicates if the user already included the escape
942 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
943 }
944
945 // --------------------------------------------------------------------
946
947 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 * Generate an insert string
949 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 * @param string the table upon which the query will be performed
951 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200952 * @return string
953 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500954 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200956 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200957
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500958 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300960 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $values[] = $this->escape($val);
962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200964 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200965 }
966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // --------------------------------------------------------------------
968
969 /**
970 * Generate an update string
971 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 * @param string the table upon which the query will be performed
973 * @param array an associative array data of key/values
974 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200975 * @return string
976 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500977 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 {
979 if ($where == '')
980 {
Andrey Andreev4c202602012-03-06 20:34:52 +0200981 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500985 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200987 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
989
990 if ( ! is_array($where))
991 {
992 $dest = array($where);
993 }
994 else
995 {
996 $dest = array();
997 foreach ($where as $key => $val)
998 {
999 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001000 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 if ($val !== '')
1003 {
1004 if ( ! $this->_has_operator($key))
1005 {
1006 $key .= ' =';
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 $val = ' '.$this->escape($val);
1010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 $dest[] = $prefix.$key.$val;
1013 }
Barry Mienydd671972010-10-04 16:33:58 +02001014 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001015
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001016 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001017 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001018
1019 // --------------------------------------------------------------------
1020
1021 /**
1022 * Tests whether the string has an SQL operator
1023 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @param string
1025 * @return bool
1026 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001027 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001029 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
1031
1032 // --------------------------------------------------------------------
1033
1034 /**
1035 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string the function name
1038 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001039 * @return mixed
1040 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001041 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
1043 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001044
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 if (FALSE === strpos($driver, $function))
1046 {
1047 $function = $driver.$function;
1048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 if ( ! function_exists($function))
1051 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001052 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001054
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001055 return (func_num_args() > 1)
1056 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1057 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 }
1059
1060 // --------------------------------------------------------------------
1061
1062 /**
1063 * Set Cache Directory Path
1064 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * @param string the path to the cache directory
1066 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001067 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001068 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
1070 $this->cachedir = $path;
1071 }
1072
1073 // --------------------------------------------------------------------
1074
1075 /**
1076 * Enable Query Caching
1077 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001078 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001079 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001080 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001082 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 }
1084
1085 // --------------------------------------------------------------------
1086
1087 /**
1088 * Disable Query Caching
1089 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001090 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001091 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001092 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001094 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 }
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097
1098 // --------------------------------------------------------------------
1099
1100 /**
1101 * Delete the cache files associated with a particular URI
1102 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001103 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001104 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001105 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001107 return ($this->_cache_init())
1108 ? $this->CACHE->delete($segment_one, $segment_two)
1109 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 }
1111
1112 // --------------------------------------------------------------------
1113
1114 /**
1115 * Delete All cache files
1116 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001117 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001118 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001119 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001121 return ($this->_cache_init())
1122 ? $this->CACHE->delete_all()
1123 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 }
1125
1126 // --------------------------------------------------------------------
1127
1128 /**
1129 * Initialize the Cache Class
1130 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001131 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001132 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001133 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001135 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001137 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001138 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001139 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001140 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001142 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1143 {
1144 return $this->cache_off();
1145 }
Derek Allarde37ab382009-02-03 16:13:57 +00001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1148 return TRUE;
1149 }
1150
1151 // --------------------------------------------------------------------
1152
1153 /**
1154 * Close DB Connection
1155 *
Barry Mienydd671972010-10-04 16:33:58 +02001156 * @return void
1157 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001158 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001160 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
1162 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001163 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
Barry Mienydd671972010-10-04 16:33:58 +02001166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 // --------------------------------------------------------------------
1168
1169 /**
1170 * Display an error message
1171 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 * @param string the error message
1173 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001174 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001175 * @return string sends the application/error_db.php template
1176 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001177 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 {
Derek Jonese7792202010-03-02 17:24:46 -06001179 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 $LANG->load('db');
1181
1182 $heading = $LANG->line('db_error_heading');
1183
1184 if ($native == TRUE)
1185 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001186 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 }
1188 else
1189 {
1190 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Pascal Kriete60f8c392010-08-25 18:03:28 +02001193 // Find the most likely culprit of the error by going through
1194 // the backtrace until the source file is no longer in the
1195 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001196 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001197 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001198 {
1199 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1200 {
1201 // Found it - use a relative path for safety
1202 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1203 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001204 break;
1205 }
1206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Derek Jonese7792202010-03-02 17:24:46 -06001208 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 echo $error->show_error($heading, $message, 'error_db');
1210 exit;
1211 }
1212
1213 // --------------------------------------------------------------------
1214
1215 /**
1216 * Protect Identifiers
1217 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001218 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001219 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001221 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001222 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 *
1224 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1225 *
1226 * Or a query with aliasing:
1227 *
1228 * SELECT m.member_id, m.member_name FROM members AS m
1229 *
1230 * Since the column name can include up to four segments (host, DB, table, column)
1231 * or also have an alias prefix, we need to do a bit of work to figure this out and
1232 * insert the table prefix (if it exists) in the proper position, and escape only
1233 * the correct identifiers.
1234 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 * @param string
1236 * @param bool
1237 * @param mixed
1238 * @param bool
1239 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001240 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001241 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
1243 if ( ! is_bool($protect_identifiers))
1244 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001245 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 }
Derek Allarde37ab382009-02-03 16:13:57 +00001247
1248 if (is_array($item))
1249 {
1250 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001251 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001252 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001253 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001254 }
1255
1256 return $escaped_array;
1257 }
1258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001260 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001261
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 // If the item has an alias declaration we remove it and set it aside.
1263 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001265 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001266 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 $item = substr($item, 0, - strlen($alias));
1268 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001269 else
1270 {
1271 $alias = '';
1272 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001273
Derek Allard911d3e02008-12-15 14:08:35 +00001274 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001275 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001276 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001277 // way to deal with this, but I'm not thinking of it -- Rick
1278 if (strpos($item, '(') !== FALSE)
1279 {
1280 return $item.$alias;
1281 }
1282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 // Break the string apart if it contains periods, then insert the table prefix
1284 // in the correct location, assuming the period doesn't indicate that we're dealing
1285 // with an alias. While we're at it, we will escape the components
1286 if (strpos($item, '.') !== FALSE)
1287 {
1288 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001291 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001293 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001294 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 if ($protect_identifiers === TRUE)
1296 {
1297 foreach ($parts as $key => $val)
1298 {
1299 if ( ! in_array($val, $this->_reserved_identifiers))
1300 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001301 $parts[$key] = $this->escape_identifiers($val);
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 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001306 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001307
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 return $item.$alias;
1309 }
Barry Mienydd671972010-10-04 16:33:58 +02001310
Andrey Andreev4c202602012-03-06 20:34:52 +02001311 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 if ($this->dbprefix != '')
1313 {
1314 // We now add the table prefix based on some logic.
1315 // Do we have 4 segments (hostname.database.table.column)?
1316 // If so, we add the table prefix to the column name in the 3rd segment.
1317 if (isset($parts[3]))
1318 {
1319 $i = 2;
1320 }
1321 // Do we have 3 segments (database.table.column)?
1322 // If so, we add the table prefix to the column name in 2nd position
1323 elseif (isset($parts[2]))
1324 {
1325 $i = 1;
1326 }
1327 // Do we have 2 segments (table.column)?
1328 // If so, we add the table prefix to the column name in 1st segment
1329 else
1330 {
1331 $i = 0;
1332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 // This flag is set when the supplied $item does not contain a field name.
1335 // This can happen when this function is being called from a JOIN.
1336 if ($field_exists == FALSE)
1337 {
1338 $i++;
1339 }
Barry Mienydd671972010-10-04 16:33:58 +02001340
Derek Jones55acc8b2009-07-11 03:38:13 +00001341 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001342 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001343 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001344 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001345 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001347 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 {
1349 $parts[$i] = $this->dbprefix.$parts[$i];
1350 }
Barry Mienydd671972010-10-04 16:33:58 +02001351
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 // Put the parts back together
1353 $item = implode('.', $parts);
1354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 if ($protect_identifiers === TRUE)
1357 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001358 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 }
Barry Mienydd671972010-10-04 16:33:58 +02001360
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 return $item.$alias;
1362 }
1363
Andrey Andreev4c202602012-03-06 20:34:52 +02001364 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 if ($this->dbprefix != '')
1366 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001367 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001368 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001369 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001370 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001371 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001373 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 {
1375 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001376 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 }
1378
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001379 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001381 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 }
Barry Mienydd671972010-10-04 16:33:58 +02001383
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 return $item.$alias;
1385 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001386
Túbal Martín511f2252011-11-24 14:43:45 +01001387 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001388
Túbal Martín511f2252011-11-24 14:43:45 +01001389 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001390 * Dummy method that allows Query Builder class to be disabled
Túbal Martín511f2252011-11-24 14:43:45 +01001391 *
1392 * This function is used extensively by every db driver.
1393 *
Túbal Martín511f2252011-11-24 14:43:45 +01001394 * @return void
1395 */
1396 protected function _reset_select()
1397 {
Túbal Martín511f2252011-11-24 14:43:45 +01001398 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001399
1400}
1401
Derek Allard2067d1a2008-11-13 22:59:24 +00001402/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001403/* Location: ./system/database/DB_driver.php */