blob: 1dc80a104477bf91cfde05b85a1e6a8ecd0e0cb9 [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 Andreev043f2602012-03-06 20:16:42 +020075 protected $_protect_identifiers = TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +020076 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 Andreev032e7ea2012-03-06 19:48:35 +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
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200866 $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
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200889 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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200912 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
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 {
924 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200925 $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
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200941 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 Andreev043f2602012-03-06 20:16:42 +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 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001032 return ($this->_cache_init())
1033 ? $this->CACHE->delete($segment_one, $segment_two)
1034 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 }
1036
1037 // --------------------------------------------------------------------
1038
1039 /**
1040 * Delete All cache files
1041 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001042 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001043 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001044 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001046 return ($this->_cache_init())
1047 ? $this->CACHE->delete_all()
1048 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 }
1050
1051 // --------------------------------------------------------------------
1052
1053 /**
1054 * Initialize the Cache Class
1055 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001057 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001058 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001060 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001062 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001063 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001064 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001065 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001067 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1068 {
1069 return $this->cache_off();
1070 }
Derek Allarde37ab382009-02-03 16:13:57 +00001071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1073 return TRUE;
1074 }
1075
1076 // --------------------------------------------------------------------
1077
1078 /**
1079 * Close DB Connection
1080 *
Barry Mienydd671972010-10-04 16:33:58 +02001081 * @return void
1082 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001083 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001085 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 {
1087 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001088 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 }
Barry Mienydd671972010-10-04 16:33:58 +02001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 // --------------------------------------------------------------------
1093
1094 /**
1095 * Display an error message
1096 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 * @param string the error message
1098 * @param string any "swap" values
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001099 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001100 * @return string sends the application/error_db.php template
1101 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001102 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 {
Derek Jonese7792202010-03-02 17:24:46 -06001104 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 $LANG->load('db');
1106
1107 $heading = $LANG->line('db_error_heading');
1108
1109 if ($native == TRUE)
1110 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001111 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 }
1113 else
1114 {
1115 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Pascal Kriete60f8c392010-08-25 18:03:28 +02001118 // Find the most likely culprit of the error by going through
1119 // the backtrace until the source file is no longer in the
1120 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001121 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001122 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001123 {
1124 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1125 {
1126 // Found it - use a relative path for safety
1127 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1128 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001129 break;
1130 }
1131 }
Barry Mienydd671972010-10-04 16:33:58 +02001132
Derek Jonese7792202010-03-02 17:24:46 -06001133 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 echo $error->show_error($heading, $message, 'error_db');
1135 exit;
1136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
1141 * Protect Identifiers
1142 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001144 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001146 * the table prefix onto it. Some logic is necessary in order to deal with
1147 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 *
1149 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1150 *
1151 * Or a query with aliasing:
1152 *
1153 * SELECT m.member_id, m.member_name FROM members AS m
1154 *
1155 * Since the column name can include up to four segments (host, DB, table, column)
1156 * or also have an alias prefix, we need to do a bit of work to figure this out and
1157 * insert the table prefix (if it exists) in the proper position, and escape only
1158 * the correct identifiers.
1159 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 * @param string
1161 * @param bool
1162 * @param mixed
1163 * @param bool
1164 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001165 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001166 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
1168 if ( ! is_bool($protect_identifiers))
1169 {
1170 $protect_identifiers = $this->_protect_identifiers;
1171 }
Derek Allarde37ab382009-02-03 16:13:57 +00001172
1173 if (is_array($item))
1174 {
1175 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001176 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001177 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001178 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001179 }
1180
1181 return $escaped_array;
1182 }
1183
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001185 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001186
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 // If the item has an alias declaration we remove it and set it aside.
1188 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001190 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001191 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 $item = substr($item, 0, - strlen($alias));
1193 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001194 else
1195 {
1196 $alias = '';
1197 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001198
Derek Allard911d3e02008-12-15 14:08:35 +00001199 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001200 // If a parenthesis is found we know that we do not need to
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001201 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001202 // way to deal with this, but I'm not thinking of it -- Rick
1203 if (strpos($item, '(') !== FALSE)
1204 {
1205 return $item.$alias;
1206 }
1207
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 // Break the string apart if it contains periods, then insert the table prefix
1209 // in the correct location, assuming the period doesn't indicate that we're dealing
1210 // with an alias. While we're at it, we will escape the components
1211 if (strpos($item, '.') !== FALSE)
1212 {
1213 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 // Does the first segment of the exploded item match
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001216 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 // we have nothing more to do other than escape the item
1218 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001219 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 if ($protect_identifiers === TRUE)
1221 {
1222 foreach ($parts as $key => $val)
1223 {
1224 if ( ! in_array($val, $this->_reserved_identifiers))
1225 {
1226 $parts[$key] = $this->_escape_identifiers($val);
1227 }
1228 }
Barry Mienydd671972010-10-04 16:33:58 +02001229
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001231 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001232
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 return $item.$alias;
1234 }
Barry Mienydd671972010-10-04 16:33:58 +02001235
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001236 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 if ($this->dbprefix != '')
1238 {
1239 // We now add the table prefix based on some logic.
1240 // Do we have 4 segments (hostname.database.table.column)?
1241 // If so, we add the table prefix to the column name in the 3rd segment.
1242 if (isset($parts[3]))
1243 {
1244 $i = 2;
1245 }
1246 // Do we have 3 segments (database.table.column)?
1247 // If so, we add the table prefix to the column name in 2nd position
1248 elseif (isset($parts[2]))
1249 {
1250 $i = 1;
1251 }
1252 // Do we have 2 segments (table.column)?
1253 // If so, we add the table prefix to the column name in 1st segment
1254 else
1255 {
1256 $i = 0;
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // This flag is set when the supplied $item does not contain a field name.
1260 // This can happen when this function is being called from a JOIN.
1261 if ($field_exists == FALSE)
1262 {
1263 $i++;
1264 }
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Jones55acc8b2009-07-11 03:38:13 +00001266 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001267 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001268 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001269 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001270 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001272 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
1274 $parts[$i] = $this->dbprefix.$parts[$i];
1275 }
Barry Mienydd671972010-10-04 16:33:58 +02001276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 // Put the parts back together
1278 $item = implode('.', $parts);
1279 }
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 if ($protect_identifiers === TRUE)
1282 {
1283 $item = $this->_escape_identifiers($item);
1284 }
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 return $item.$alias;
1287 }
1288
Derek Jones37f4b9c2011-07-01 17:56:50 -05001289 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 if ($this->dbprefix != '')
1291 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001292 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001293 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001294 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001295 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001296 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001298 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 {
1300 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001301 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 }
1303
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001304 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
1306 $item = $this->_escape_identifiers($item);
1307 }
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 return $item.$alias;
1310 }
Andrey Andreev4c598362012-03-06 14:58:37 +02001311
Túbal Martín511f2252011-11-24 14:43:45 +01001312 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001313
Túbal Martín511f2252011-11-24 14:43:45 +01001314 /**
1315 * Dummy method that allows Active Record class to be disabled
1316 *
1317 * This function is used extensively by every db driver.
1318 *
Túbal Martín511f2252011-11-24 14:43:45 +01001319 * @return void
1320 */
1321 protected function _reset_select()
1322 {
Túbal Martín511f2252011-11-24 14:43:45 +01001323 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001324
1325}
1326
Derek Allard2067d1a2008-11-13 22:59:24 +00001327/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001328/* Location: ./system/database/DB_driver.php */