blob: 8d297caf90d323724d7d829487fc117730cec423 [file] [log] [blame]
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevaf5d5582012-01-25 21:34:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevaf5d5582012-01-25 21:34:47 +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 */
41class CI_DB_driver {
42
Andrey Andreev45ba4f72012-02-13 01:24:39 +020043 public $dsn;
Andrey Andreevaf5d5582012-01-25 21:34:47 +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();
66 public $trans_enabled = TRUE;
67 public $trans_strict = TRUE;
68 protected $_trans_depth = 0;
69 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
70 public $cache_on = FALSE;
71 public $cachedir = '';
72 public $cache_autodel = FALSE;
73 public $CACHE; // The cache class object
Derek Allard2067d1a2008-11-13 22:59:24 +000074
Andrey Andreevaf5d5582012-01-25 21:34:47 +020075 protected $_protect_identifiers = TRUE;
76 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000077
Andrey Andreevaf5d5582012-01-25 21:34:47 +020078 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
80 if (is_array($params))
81 {
82 foreach ($params as $key => $val)
83 {
84 $this->$key = $val;
85 }
86 }
87
88 log_message('debug', 'Database Driver Class Initialized');
89 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
94 * Initialize Database Settings
95 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +020096 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020097 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +020098 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200100 /* If an established connection is available, then there's
101 * no need to connect and select the database.
102 *
103 * Depending on the database driver, conn_id can be either
104 * boolean TRUE, a resource or an object.
105 */
106 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
108 return TRUE;
109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // Connect to the database and set the connection ID
114 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
115
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200116 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 if ( ! $this->conn_id)
118 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100119 // Check if there is a failover set
120 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100122 // Go over all the failovers
123 foreach ($this->failover as $failover)
124 {
125 // Replace the current settings with those of the failover
126 foreach ($failover as $key => $val)
127 {
128 $this->$key = $val;
129 }
130
131 // Try to connect
132 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
133
134 // If a connection is made break the foreach loop
135 if ($this->conn_id)
136 {
137 break;
138 }
139 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100141
142 // We still don't have a connection?
143 if ( ! $this->conn_id)
144 {
145 log_message('error', 'Unable to connect to the database');
146
147 if ($this->db_debug)
148 {
149 $this->display_error('db_unable_to_connect');
150 }
151 return FALSE;
152 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154
155 // ----------------------------------------------------------------
156
157 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200158 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200160 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200161
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200166 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200169 // Now we set the character set and that's all
170 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // --------------------------------------------------------------------
174
175 /**
176 * Set client character set
177 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @param string
179 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200180 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 */
Andrey Andreev063f5962012-02-27 12:20:52 +0200182 public function db_set_charset($charset, $collation = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200184 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset, $collation))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200186 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 if ($this->db_debug)
189 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200190 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 return FALSE;
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 return TRUE;
197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 // --------------------------------------------------------------------
200
201 /**
202 * The name of the platform in use (mysql, mssql, etc...)
203 *
Barry Mienydd671972010-10-04 16:33:58 +0200204 * @return string
205 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200206 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 return $this->dbdriver;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200214 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200216 * Returns a string containing the version of the database being used.
217 * Most drivers will override this method.
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 *
Barry Mienydd671972010-10-04 16:33:58 +0200219 * @return string
220 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200221 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200223 if (isset($this->data_cache['version']))
224 {
225 return $this->data_cache['version'];
226 }
227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 if (FALSE === ($sql = $this->_version()))
229 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200230 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Derek Allard3683f772009-12-16 17:32:33 +0000232
Andrey Andreev08856b82012-03-03 03:19:28 +0200233 $query = $this->query($sql);
234 $query = $query->row();
235 return $this->data_cache['version'] = $query->ver;
236 }
Derek Allard3683f772009-12-16 17:32:33 +0000237
Andrey Andreev08856b82012-03-03 03:19:28 +0200238 // --------------------------------------------------------------------
239
240 /**
241 * Version number query string
242 *
243 * @return string
244 */
245 protected function _version()
246 {
247 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // --------------------------------------------------------------------
251
252 /**
253 * Execute the query
254 *
255 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500256 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * upon successful execution of a "write" type query. Returns boolean
258 * FALSE upon failure, and if the $db_debug variable is set to TRUE
259 * will raise an error.
260 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * @param string An SQL query string
262 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200263 * @return mixed
264 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200265 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 if ($sql == '')
268 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200269 log_message('error', 'Invalid query: '.$sql);
270
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200271 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273
274 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200275 if ( ($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600276 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200277 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
Derek Jonese7792202010-03-02 17:24:46 -0600279
Ryan Dialef7474c2012-03-01 16:11:36 -0500280 // Compile binds if needed
281 if ($binds !== FALSE)
282 {
283 $sql = $this->compile_binds($sql, $binds);
284 }
285
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200286 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // we will load the caching class and return the previously
288 // cached query if it exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200289 if ($this->cache_on == TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200291 $this->load_rdriver();
292 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200294 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jones37f4b9c2011-07-01 17:56:50 -0500298 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 if ($this->save_queries == TRUE)
300 {
301 $this->queries[] = $sql;
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // Start the Query Timer
305 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // Run the Query
308 if (FALSE === ($this->result_id = $this->simple_query($sql)))
309 {
310 if ($this->save_queries == TRUE)
311 {
312 $this->query_times[] = 0;
313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // This will trigger a rollback if transactions are being used
316 $this->_trans_status = FALSE;
317
Andrey Andreev4be5de12012-03-02 15:45:41 +0200318 // Grab the error now, as we might run some additional queries before displaying the error
319 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200320
321 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200322 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 if ($this->db_debug)
325 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // We call this function in order to roll-back queries
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200327 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200328 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // transactions to remain in limbo.
330 $this->trans_complete();
331
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200332 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200333 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 return FALSE;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // Stop and aggregate the query time results
340 $time_end = list($em, $es) = explode(' ', microtime());
341 $this->benchmark += ($em + $es) - ($sm + $ss);
342
343 if ($this->save_queries == TRUE)
344 {
345 $this->query_times[] = ($em + $es) - ($sm + $ss);
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Increment the query counter
349 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Was the query a "write" type?
352 // If so we'll simply return true
353 if ($this->is_write_type($sql) === TRUE)
354 {
355 // If caching is enabled we'll auto-cleanup any
356 // existing files related to this particular URI
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200357 if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 $this->CACHE->delete();
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 return TRUE;
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // Return TRUE if we don't need to create a result object
366 // Currently only the Oracle driver uses this when stored
367 // procedures are used
368 if ($return_object !== TRUE)
369 {
370 return TRUE;
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
373 // Load and instantiate the result driver
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200374 $driver = $this->load_rdriver();
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200375 $RES = new $driver($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200378
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200379 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // result object and save it to a cache file.
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200381 if ($this->cache_on == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 // We'll create a new instance of the result object
384 // only without the platform specific driver since
385 // we can't use it with cached data (the query result
386 // resource ID won't be any good once we've cached the
387 // result object, so we'll have to compile the data
388 // and save it)
389 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 $CR->result_object = $RES->result_object();
391 $CR->result_array = $RES->result_array();
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200392 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // Reset these since cached objects can not utilize resource IDs.
395 $CR->conn_id = NULL;
396 $CR->result_id = NULL;
397
398 $this->CACHE->write($sql, $CR);
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 return $RES;
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Load the result drivers
408 *
Barry Mienydd671972010-10-04 16:33:58 +0200409 * @return string the name of the result class
410 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200411 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 $driver = 'CI_DB_'.$this->dbdriver.'_result';
414
415 if ( ! class_exists($driver))
416 {
Greg Aker3a746652011-04-19 10:59:47 -0500417 include_once(BASEPATH.'database/DB_result.php');
418 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 return $driver;
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
425
426 /**
427 * Simple Query
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200428 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * we only use it when running transaction commands since they do
430 * not require all the features of the main query() function.
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200433 * @return mixed
434 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200435 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
437 if ( ! $this->conn_id)
438 {
439 $this->initialize();
440 }
441
442 return $this->_execute($sql);
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // --------------------------------------------------------------------
446
447 /**
448 * Disable Transactions
449 * This permits transactions to be disabled at run-time.
450 *
Barry Mienydd671972010-10-04 16:33:58 +0200451 * @return void
452 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200453 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 $this->trans_enabled = FALSE;
456 }
457
458 // --------------------------------------------------------------------
459
460 /**
461 * Enable/disable Transaction Strict Mode
462 * When strict mode is enabled, if you are running multiple groups of
463 * transactions, if one group fails all groups will be rolled back.
464 * If strict mode is disabled, each group is treated autonomously, meaning
465 * a failure of one group will not affect any others
466 *
Barry Mienydd671972010-10-04 16:33:58 +0200467 * @return void
468 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200469 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
475
476 /**
477 * Start Transaction
478 *
Barry Mienydd671972010-10-04 16:33:58 +0200479 * @return void
480 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200481 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200482 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 if ( ! $this->trans_enabled)
484 {
485 return FALSE;
486 }
487
488 // When transactions are nested we only begin/commit/rollback the outermost ones
489 if ($this->_trans_depth > 0)
490 {
491 $this->_trans_depth += 1;
492 return;
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500496 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498
499 // --------------------------------------------------------------------
500
501 /**
502 * Complete Transaction
503 *
Barry Mienydd671972010-10-04 16:33:58 +0200504 * @return bool
505 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200506 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 if ( ! $this->trans_enabled)
509 {
510 return FALSE;
511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 // When transactions are nested we only begin/commit/rollback the outermost ones
514 if ($this->_trans_depth > 1)
515 {
516 $this->_trans_depth -= 1;
517 return TRUE;
518 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500519 else
520 {
521 $this->_trans_depth = 0;
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 // The query() function will set this flag to FALSE in the event that a query failed
525 if ($this->_trans_status === FALSE)
526 {
527 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 // If we are NOT running in strict mode, we will reset
530 // the _trans_status flag so that subsequent groups of transactions
531 // will be permitted.
532 if ($this->trans_strict === FALSE)
533 {
534 $this->_trans_status = TRUE;
535 }
536
537 log_message('debug', 'DB Transaction Failure');
538 return FALSE;
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 $this->trans_commit();
542 return TRUE;
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Lets you retrieve the transaction flag to determine if it has failed
549 *
Barry Mienydd671972010-10-04 16:33:58 +0200550 * @return bool
551 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200552 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
554 return $this->_trans_status;
555 }
556
557 // --------------------------------------------------------------------
558
559 /**
560 * Compile Bindings
561 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @param string the sql statement
563 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200564 * @return string
565 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200566 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 if (strpos($sql, $this->bind_marker) === FALSE)
569 {
570 return $sql;
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 if ( ! is_array($binds))
574 {
575 $binds = array($binds);
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // Get the sql segments around the bind markers
579 $segments = explode($this->bind_marker, $sql);
580
581 // The count of bind should be 1 less then the count of segments
582 // If there are more bind arguments trim it down
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200583 if (count($binds) >= count($segments))
584 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 $binds = array_slice($binds, 0, count($segments)-1);
586 }
587
588 // Construct the binded query
589 $result = $segments[0];
590 $i = 0;
591 foreach ($binds as $bind)
592 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200593 $result .= $this->escape($bind).$segments[++$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
595
596 return $result;
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // --------------------------------------------------------------------
600
601 /**
602 * Determines if a query is a "write" type.
603 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @param string An SQL query string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200605 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200606 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200607 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200609 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|OPTIMIZE|REINDEX)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // --------------------------------------------------------------------
613
614 /**
615 * Calculate the aggregate query elapsed time
616 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200617 * @param int The number of decimal places
618 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200619 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200620 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
622 return number_format($this->benchmark, $decimals);
623 }
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 // --------------------------------------------------------------------
626
627 /**
628 * Returns the total number of queries
629 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200630 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200631 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200632 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
634 return $this->query_count;
635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
638
639 /**
640 * Returns the last query that was executed
641 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200642 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200643 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200644 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
646 return end($this->queries);
647 }
648
649 // --------------------------------------------------------------------
650
651 /**
652 * "Smart" Escape String
653 *
654 * Escapes data based on type
655 * Sets boolean and null types
656 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200658 * @return mixed
659 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200660 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200661 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000662 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200664 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000665 }
666 elseif (is_bool($str))
667 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200668 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000669 }
670 elseif (is_null($str))
671 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200672 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000673 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000674
675 return $str;
676 }
677
678 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600679
Derek Jonese4ed5832009-02-20 21:44:59 +0000680 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000681 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000682 *
683 * Calls the individual driver for platform
684 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200685 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000686 * @param string
687 * @return mixed
688 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200689 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200690 {
691 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000692 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000693
Derek Jonese4ed5832009-02-20 21:44:59 +0000694 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 /**
697 * Primary
698 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200699 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 * position is the primary key
701 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200703 * @return string
704 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200705 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200706 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200708 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 }
710
711 // --------------------------------------------------------------------
712
713 /**
714 * Returns an array of table names
715 *
Barry Mienydd671972010-10-04 16:33:58 +0200716 * @return array
717 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200718 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
720 // Is there a cached result?
721 if (isset($this->data_cache['table_names']))
722 {
723 return $this->data_cache['table_names'];
724 }
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
727 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200728 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
730
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200731 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200733
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200734 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200736 // Do we know from which column to get the table name?
737 if ( ! isset($key))
738 {
739 if (array_key_exists('table_name', $row))
740 {
741 $key = 'table_name';
742 }
743 elseif (array_key_exists('TABLE_NAME', $row))
744 {
745 $key = 'TABLE_NAME';
746 }
747 else
748 {
749 /* We have no other choice but to just get the first element's key.
750 * Due to array_shift() accepting it's argument by reference, if
751 * E_STRICT is on, this would trigger a warning. So we'll have to
752 * assign it first.
753 */
754 $key = array_keys($row);
755 $key = array_shift($key);
756 }
757 }
758
759 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 }
761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 return $this->data_cache['table_names'];
763 }
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 // --------------------------------------------------------------------
766
767 /**
768 * Determine if a particular table exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200769 *
770 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200772 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200773 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200774 return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * Fetch MySQL Field Names
781 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200783 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200785 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
787 // Is there a cached result?
788 if (isset($this->data_cache['field_names'][$table]))
789 {
790 return $this->data_cache['field_names'][$table];
791 }
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 if ($table == '')
794 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200795 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 }
Barry Mienydd671972010-10-04 16:33:58 +0200797
Greg Aker1edde302010-01-26 00:17:01 +0000798 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200800 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200804 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200805
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500806 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200808 // Do we know from where to get the column's name?
809 if ( ! isset($key))
810 {
811 if (array_key_exists('column_name', $row))
812 {
813 $key = 'column_name';
814 }
815 elseif (array_key_exists('COLUMN_NAME', $row))
816 {
817 $key = 'COLUMN_NAME';
818 }
819 else
820 {
821 /* We have no other choice but to just get the first element's key.
822 * Due to array_shift() accepting it's argument by reference, if
823 * E_STRICT is on, this would trigger a warning. So we'll have to
824 * assign it first.
825 */
826 $key = array_keys($row);
827 $key = array_shift($key);
828 }
829 }
830
831 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 return $this->data_cache['field_names'][$table];
835 }
836
837 // --------------------------------------------------------------------
838
839 /**
840 * Determine if a particular field exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200841 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 * @param string
843 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200844 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200846 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200847 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200848 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // --------------------------------------------------------------------
852
853 /**
854 * Returns an object with field data
855 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 * @param string the table name
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200857 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200858 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200859 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
861 if ($table == '')
862 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200863 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200868 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000869
870 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 /**
873 * Generate an insert string
874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @param string the table upon which the query will be performed
876 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200877 * @return string
878 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200879 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200881 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200882
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500883 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 {
885 $fields[] = $this->_escape_identifiers($key);
886 $values[] = $this->escape($val);
887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200890 }
891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 // --------------------------------------------------------------------
893
894 /**
895 * Generate an update string
896 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 * @param string the table upon which the query will be performed
898 * @param array an associative array data of key/values
899 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200900 * @return string
901 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200902 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
904 if ($where == '')
905 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200906 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 }
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500910 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
912 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
913 }
914
915 if ( ! is_array($where))
916 {
917 $dest = array($where);
918 }
919 else
920 {
921 $dest = array();
922 foreach ($where as $key => $val)
923 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200924 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300925 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 if ($val !== '')
928 {
929 if ( ! $this->_has_operator($key))
930 {
931 $key .= ' =';
932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 $val = ' '.$this->escape($val);
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 $dest[] = $prefix.$key.$val;
938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000940
941 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200942 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000943
944 // --------------------------------------------------------------------
945
946 /**
947 * Tests whether the string has an SQL operator
948 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 * @param string
950 * @return bool
951 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200952 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200954 return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
956
957 // --------------------------------------------------------------------
958
959 /**
960 * Enables a native PHP function to be run, using a platform agnostic wrapper.
961 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 * @param string the function name
963 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200964 * @return mixed
965 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200966 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
968 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200969
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 if (FALSE === strpos($driver, $function))
971 {
972 $function = $driver.$function;
973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 if ( ! function_exists($function))
976 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200977 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000979
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200980 return (func_num_args() > 1)
981 ? call_user_func_array($function, array_splice(func_get_args(), 1))
982 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
984
985 // --------------------------------------------------------------------
986
987 /**
988 * Set Cache Directory Path
989 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @param string the path to the cache directory
991 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200992 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200993 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
995 $this->cachedir = $path;
996 }
997
998 // --------------------------------------------------------------------
999
1000 /**
1001 * Enable Query Caching
1002 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001003 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001004 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001005 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001007 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 }
1009
1010 // --------------------------------------------------------------------
1011
1012 /**
1013 * Disable Query Caching
1014 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001015 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001016 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001017 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001019 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 }
Barry Mienydd671972010-10-04 16:33:58 +02001021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022
1023 // --------------------------------------------------------------------
1024
1025 /**
1026 * Delete the cache files associated with a particular URI
1027 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001028 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001029 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001030 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
1032 if ( ! $this->_cache_init())
1033 {
1034 return FALSE;
1035 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 return $this->CACHE->delete($segment_one, $segment_two);
1038 }
1039
1040 // --------------------------------------------------------------------
1041
1042 /**
1043 * Delete All cache files
1044 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001045 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001046 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001047 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
1049 if ( ! $this->_cache_init())
1050 {
1051 return FALSE;
1052 }
1053
1054 return $this->CACHE->delete_all();
1055 }
1056
1057 // --------------------------------------------------------------------
1058
1059 /**
1060 * Initialize the Cache Class
1061 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001063 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001064 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001066 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001068 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001069 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001070 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001071 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001073 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1074 {
1075 return $this->cache_off();
1076 }
Derek Allarde37ab382009-02-03 16:13:57 +00001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1079 return TRUE;
1080 }
1081
1082 // --------------------------------------------------------------------
1083
1084 /**
1085 * Close DB Connection
1086 *
Barry Mienydd671972010-10-04 16:33:58 +02001087 * @return void
1088 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001089 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001091 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 {
1093 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001094 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 }
Barry Mienydd671972010-10-04 16:33:58 +02001097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 // --------------------------------------------------------------------
1099
1100 /**
1101 * Display an error message
1102 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 * @param string the error message
1104 * @param string any "swap" values
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001105 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001106 * @return string sends the application/error_db.php template
1107 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001108 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 {
Derek Jonese7792202010-03-02 17:24:46 -06001110 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 $LANG->load('db');
1112
1113 $heading = $LANG->line('db_error_heading');
1114
1115 if ($native == TRUE)
1116 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001117 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 }
1119 else
1120 {
1121 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1122 }
Barry Mienydd671972010-10-04 16:33:58 +02001123
Pascal Kriete60f8c392010-08-25 18:03:28 +02001124 // Find the most likely culprit of the error by going through
1125 // the backtrace until the source file is no longer in the
1126 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001127 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001128 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001129 {
1130 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1131 {
1132 // Found it - use a relative path for safety
1133 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1134 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001135 break;
1136 }
1137 }
Barry Mienydd671972010-10-04 16:33:58 +02001138
Derek Jonese7792202010-03-02 17:24:46 -06001139 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 echo $error->show_error($heading, $message, 'error_db');
1141 exit;
1142 }
1143
1144 // --------------------------------------------------------------------
1145
1146 /**
1147 * Protect Identifiers
1148 *
1149 * This function adds backticks if appropriate based on db type
1150 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 * @param mixed the item to escape
1152 * @return mixed the item with backticks
1153 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001154 public function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
1156 return $this->_protect_identifiers($item, $prefix_single);
1157 }
1158
1159 // --------------------------------------------------------------------
1160
1161 /**
1162 * Protect Identifiers
1163 *
1164 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001165 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001167 * the table prefix onto it. Some logic is necessary in order to deal with
1168 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 *
1170 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1171 *
1172 * Or a query with aliasing:
1173 *
1174 * SELECT m.member_id, m.member_name FROM members AS m
1175 *
1176 * Since the column name can include up to four segments (host, DB, table, column)
1177 * or also have an alias prefix, we need to do a bit of work to figure this out and
1178 * insert the table prefix (if it exists) in the proper position, and escape only
1179 * the correct identifiers.
1180 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001181 * NOTE: This is used by DB_forge drivers and therefore needs to be public.
1182 * (until a better solution is implemented)
1183 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 * @param string
1185 * @param bool
1186 * @param mixed
1187 * @param bool
1188 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001189 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001190 public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 {
1192 if ( ! is_bool($protect_identifiers))
1193 {
1194 $protect_identifiers = $this->_protect_identifiers;
1195 }
Derek Allarde37ab382009-02-03 16:13:57 +00001196
1197 if (is_array($item))
1198 {
1199 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001200 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001201 {
1202 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1203 }
1204
1205 return $escaped_array;
1206 }
1207
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001209 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001210
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 // If the item has an alias declaration we remove it and set it aside.
1212 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001214 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001215 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 $item = substr($item, 0, - strlen($alias));
1217 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001218 else
1219 {
1220 $alias = '';
1221 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001222
Derek Allard911d3e02008-12-15 14:08:35 +00001223 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001224 // If a parenthesis is found we know that we do not need to
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001225 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001226 // way to deal with this, but I'm not thinking of it -- Rick
1227 if (strpos($item, '(') !== FALSE)
1228 {
1229 return $item.$alias;
1230 }
1231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // Break the string apart if it contains periods, then insert the table prefix
1233 // in the correct location, assuming the period doesn't indicate that we're dealing
1234 // with an alias. While we're at it, we will escape the components
1235 if (strpos($item, '.') !== FALSE)
1236 {
1237 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 // Does the first segment of the exploded item match
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001240 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 // we have nothing more to do other than escape the item
1242 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001243 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 if ($protect_identifiers === TRUE)
1245 {
1246 foreach ($parts as $key => $val)
1247 {
1248 if ( ! in_array($val, $this->_reserved_identifiers))
1249 {
1250 $parts[$key] = $this->_escape_identifiers($val);
1251 }
1252 }
Barry Mienydd671972010-10-04 16:33:58 +02001253
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001255 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001256
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 return $item.$alias;
1258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001260 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 if ($this->dbprefix != '')
1262 {
1263 // We now add the table prefix based on some logic.
1264 // Do we have 4 segments (hostname.database.table.column)?
1265 // If so, we add the table prefix to the column name in the 3rd segment.
1266 if (isset($parts[3]))
1267 {
1268 $i = 2;
1269 }
1270 // Do we have 3 segments (database.table.column)?
1271 // If so, we add the table prefix to the column name in 2nd position
1272 elseif (isset($parts[2]))
1273 {
1274 $i = 1;
1275 }
1276 // Do we have 2 segments (table.column)?
1277 // If so, we add the table prefix to the column name in 1st segment
1278 else
1279 {
1280 $i = 0;
1281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 // This flag is set when the supplied $item does not contain a field name.
1284 // This can happen when this function is being called from a JOIN.
1285 if ($field_exists == FALSE)
1286 {
1287 $i++;
1288 }
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Jones55acc8b2009-07-11 03:38:13 +00001290 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001291 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001292 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001293 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001294 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001296 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 {
1298 $parts[$i] = $this->dbprefix.$parts[$i];
1299 }
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 // Put the parts back together
1302 $item = implode('.', $parts);
1303 }
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 if ($protect_identifiers === TRUE)
1306 {
1307 $item = $this->_escape_identifiers($item);
1308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 return $item.$alias;
1311 }
1312
Derek Jones37f4b9c2011-07-01 17:56:50 -05001313 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 if ($this->dbprefix != '')
1315 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001316 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001317 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001318 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001319 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001320 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001322 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
1324 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001325 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 }
1327
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001328 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
1330 $item = $this->_escape_identifiers($item);
1331 }
Barry Mienydd671972010-10-04 16:33:58 +02001332
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 return $item.$alias;
1334 }
Andrey Andreev4c598362012-03-06 14:58:37 +02001335
Túbal Martín511f2252011-11-24 14:43:45 +01001336 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001337
Túbal Martín511f2252011-11-24 14:43:45 +01001338 /**
1339 * Dummy method that allows Active Record class to be disabled
1340 *
1341 * This function is used extensively by every db driver.
1342 *
Túbal Martín511f2252011-11-24 14:43:45 +01001343 * @return void
1344 */
1345 protected function _reset_select()
1346 {
Túbal Martín511f2252011-11-24 14:43:45 +01001347 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001348
1349}
1350
Derek Allard2067d1a2008-11-13 22:59:24 +00001351/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001352/* Location: ./system/database/DB_driver.php */