blob: 7b8d0870fb6f25172ef92a5a1fc4d358bcd0bff3 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
30/**
31 * Database Driver Class
32 *
33 * This is the platform-independent base DB implementation class.
34 * This class will not be called directly. Rather, the adapter
35 * class for the specific database will extend and instantiate it.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_driver {
44
Andrey Andreevd1add432012-03-06 20:26:01 +020045 public $username;
46 public $password;
47 public $hostname;
48 public $database;
49 public $dbdriver = 'mysql';
50 public $dbprefix = '';
51 public $char_set = 'utf8';
52 public $dbcollat = 'utf8_general_ci';
53 public $autoinit = TRUE; // Whether to automatically initialize the DB
54 public $swap_pre = '';
55 public $port = '';
56 public $pconnect = FALSE;
57 public $conn_id = FALSE;
58 public $result_id = FALSE;
59 public $db_debug = FALSE;
60 public $benchmark = 0;
61 public $query_count = 0;
62 public $bind_marker = '?';
63 public $save_queries = TRUE;
64 public $queries = array();
65 public $query_times = array();
66 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000067
Andrey Andreevd1add432012-03-06 20:26:01 +020068 public $trans_enabled = TRUE;
69 public $trans_strict = TRUE;
70 protected $_trans_depth = 0;
71 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000072
Andrey Andreevd1add432012-03-06 20:26:01 +020073 public $cache_on = FALSE;
74 public $cachedir = '';
75 public $cache_autodel = FALSE;
76 public $CACHE; // The cache class object
77
78 protected $_protect_identifiers = TRUE;
79 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
80
Timothy Warrene45518d2012-03-06 07:38:00 -050081 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
83 if (is_array($params))
84 {
85 foreach ($params as $key => $val)
86 {
87 $this->$key = $val;
88 }
89 }
90
91 log_message('debug', 'Database Driver Class Initialized');
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // --------------------------------------------------------------------
95
96 /**
97 * Initialize Database Settings
98 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +020099 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200100 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200101 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
103 // If an existing connection resource is available
104 // there is no need to connect and select the database
105 if (is_resource($this->conn_id) OR is_object($this->conn_id))
106 {
107 return TRUE;
108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // Connect to the database and set the connection ID
113 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
114
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200115 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if ( ! $this->conn_id)
117 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100118 // Check if there is a failover set
119 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100121 // Go over all the failovers
122 foreach ($this->failover as $failover)
123 {
124 // Replace the current settings with those of the failover
125 foreach ($failover as $key => $val)
126 {
127 $this->$key = $val;
128 }
129
130 // Try to connect
131 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
132
133 // If a connection is made break the foreach loop
134 if ($this->conn_id)
135 {
136 break;
137 }
138 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100140
141 // We still don't have a connection?
142 if ( ! $this->conn_id)
143 {
144 log_message('error', 'Unable to connect to the database');
145
146 if ($this->db_debug)
147 {
148 $this->display_error('db_unable_to_connect');
149 }
150 return FALSE;
151 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // ----------------------------------------------------------------
155
156 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200157 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200159 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200161 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200163 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200165 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 // Now we set the character set and that's all
169 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // --------------------------------------------------------------------
173
174 /**
175 * Set client character set
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param string
178 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200179 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 */
Andrey Andreev063f5962012-02-27 12:20:52 +0200181 public function db_set_charset($charset, $collation = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200183 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset, $collation))
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200185 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 if ($this->db_debug)
188 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200189 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 return FALSE;
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 return TRUE;
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // --------------------------------------------------------------------
199
200 /**
201 * The name of the platform in use (mysql, mssql, etc...)
202 *
203 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200204 * @return string
205 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500206 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.
218 *
Barry Mienydd671972010-10-04 16:33:58 +0200219 * @return string
220 */
Andrey Andreev08856b82012-03-03 03:19:28 +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 Andreev08856b82012-03-03 03:19:28 +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 *
261 * @access public
262 * @param string An SQL query string
263 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200264 * @return mixed
265 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500266 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 if ($sql == '')
269 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200270 log_message('error', 'Invalid query: '.$sql);
271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if ($this->db_debug)
273 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 return $this->display_error('db_invalid_query');
275 }
276 return FALSE;
277 }
278
279 // Verify table prefix and replace if necessary
280 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600281 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
283 }
Derek Jonese7792202010-03-02 17:24:46 -0600284
Ryan Dialef7474c2012-03-01 16:11:36 -0500285 // Compile binds if needed
286 if ($binds !== FALSE)
287 {
288 $sql = $this->compile_binds($sql, $binds);
289 }
290
Derek Jones37f4b9c2011-07-01 17:56:50 -0500291 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 // we will load the caching class and return the previously
293 // cached query if it exists
294 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
295 {
296 if ($this->_cache_init())
297 {
298 $this->load_rdriver();
299 if (FALSE !== ($cache = $this->CACHE->read($sql)))
300 {
301 return $cache;
302 }
303 }
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Jones37f4b9c2011-07-01 17:56:50 -0500306 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 if ($this->save_queries == TRUE)
308 {
309 $this->queries[] = $sql;
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Start the Query Timer
313 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // Run the Query
316 if (FALSE === ($this->result_id = $this->simple_query($sql)))
317 {
318 if ($this->save_queries == TRUE)
319 {
320 $this->query_times[] = 0;
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // This will trigger a rollback if transactions are being used
324 $this->_trans_status = FALSE;
325
Andrey Andreev4be5de12012-03-02 15:45:41 +0200326 // Grab the error now, as we might run some additional queries before displaying the error
327 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200328
329 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200330 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 if ($this->db_debug)
333 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200335 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200336 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // transactions to remain in limbo.
338 $this->trans_complete();
339
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200340 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 return $this->display_error(
Andrey Andreev4be5de12012-03-02 15:45:41 +0200342 array(
343 'Error Number: '.$error['code'],
344 $error['message'],
345 $sql
346 )
347 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 return FALSE;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // Stop and aggregate the query time results
354 $time_end = list($em, $es) = explode(' ', microtime());
355 $this->benchmark += ($em + $es) - ($sm + $ss);
356
357 if ($this->save_queries == TRUE)
358 {
359 $this->query_times[] = ($em + $es) - ($sm + $ss);
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Increment the query counter
363 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // Was the query a "write" type?
366 // If so we'll simply return true
367 if ($this->is_write_type($sql) === TRUE)
368 {
369 // If caching is enabled we'll auto-cleanup any
370 // existing files related to this particular URI
371 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
372 {
373 $this->CACHE->delete();
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 return TRUE;
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // Return TRUE if we don't need to create a result object
380 // Currently only the Oracle driver uses this when stored
381 // procedures are used
382 if ($return_object !== TRUE)
383 {
384 return TRUE;
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
387 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200388 $driver = $this->load_rdriver();
389 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Jones37f4b9c2011-07-01 17:56:50 -0500393 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // result object and save it to a cache file.
395 if ($this->cache_on == TRUE AND $this->_cache_init())
396 {
397 // We'll create a new instance of the result object
398 // only without the platform specific driver since
399 // we can't use it with cached data (the query result
400 // resource ID won't be any good once we've cached the
401 // result object, so we'll have to compile the data
402 // and save it)
403 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200404 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 $CR->result_object = $RES->result_object();
406 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // Reset these since cached objects can not utilize resource IDs.
409 $CR->conn_id = NULL;
410 $CR->result_id = NULL;
411
412 $this->CACHE->write($sql, $CR);
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 return $RES;
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Load the result drivers
422 *
Barry Mienydd671972010-10-04 16:33:58 +0200423 * @return string the name of the result class
424 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500425 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 $driver = 'CI_DB_'.$this->dbdriver.'_result';
428
429 if ( ! class_exists($driver))
430 {
Greg Aker3a746652011-04-19 10:59:47 -0500431 include_once(BASEPATH.'database/DB_result.php');
432 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 return $driver;
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // --------------------------------------------------------------------
439
440 /**
441 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500442 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * we only use it when running transaction commands since they do
444 * not require all the features of the main query() function.
445 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200447 * @return mixed
448 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500449 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 if ( ! $this->conn_id)
452 {
453 $this->initialize();
454 }
455
456 return $this->_execute($sql);
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // --------------------------------------------------------------------
460
461 /**
462 * Disable Transactions
463 * This permits transactions to be disabled at run-time.
464 *
Barry Mienydd671972010-10-04 16:33:58 +0200465 * @return void
466 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500467 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 $this->trans_enabled = FALSE;
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Enable/disable Transaction Strict Mode
476 * When strict mode is enabled, if you are running multiple groups of
477 * transactions, if one group fails all groups will be rolled back.
478 * If strict mode is disabled, each group is treated autonomously, meaning
479 * a failure of one group will not affect any others
480 *
Barry Mienydd671972010-10-04 16:33:58 +0200481 * @return void
482 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500483 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 // --------------------------------------------------------------------
489
490 /**
491 * Start Transaction
492 *
Barry Mienydd671972010-10-04 16:33:58 +0200493 * @return void
494 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500495 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200496 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if ( ! $this->trans_enabled)
498 {
499 return FALSE;
500 }
501
502 // When transactions are nested we only begin/commit/rollback the outermost ones
503 if ($this->_trans_depth > 0)
504 {
505 $this->_trans_depth += 1;
506 return;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500510 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Complete Transaction
517 *
Barry Mienydd671972010-10-04 16:33:58 +0200518 * @return bool
519 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500520 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
522 if ( ! $this->trans_enabled)
523 {
524 return FALSE;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // When transactions are nested we only begin/commit/rollback the outermost ones
528 if ($this->_trans_depth > 1)
529 {
530 $this->_trans_depth -= 1;
531 return TRUE;
532 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500533 else
534 {
535 $this->_trans_depth = 0;
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 // The query() function will set this flag to FALSE in the event that a query failed
539 if ($this->_trans_status === FALSE)
540 {
541 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 // If we are NOT running in strict mode, we will reset
544 // the _trans_status flag so that subsequent groups of transactions
545 // will be permitted.
546 if ($this->trans_strict === FALSE)
547 {
548 $this->_trans_status = TRUE;
549 }
550
551 log_message('debug', 'DB Transaction Failure');
552 return FALSE;
553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 $this->trans_commit();
556 return TRUE;
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Lets you retrieve the transaction flag to determine if it has failed
563 *
Barry Mienydd671972010-10-04 16:33:58 +0200564 * @return bool
565 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500566 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 return $this->_trans_status;
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Compile Bindings
575 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * @param string the sql statement
577 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200578 * @return string
579 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500580 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 if (strpos($sql, $this->bind_marker) === FALSE)
583 {
584 return $sql;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 if ( ! is_array($binds))
588 {
589 $binds = array($binds);
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // Get the sql segments around the bind markers
593 $segments = explode($this->bind_marker, $sql);
594
595 // The count of bind should be 1 less then the count of segments
596 // If there are more bind arguments trim it down
597 if (count($binds) >= count($segments)) {
598 $binds = array_slice($binds, 0, count($segments)-1);
599 }
600
601 // Construct the binded query
602 $result = $segments[0];
603 $i = 0;
604 foreach ($binds as $bind)
605 {
606 $result .= $this->escape($bind);
607 $result .= $segments[++$i];
608 }
609
610 return $result;
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
614
615 /**
616 * Determines if a query is a "write" type.
617 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200619 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200620 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200621 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200623 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 +0000624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // --------------------------------------------------------------------
627
628 /**
629 * Calculate the aggregate query elapsed time
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200632 * @return integer
633 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500634 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 return number_format($this->benchmark, $decimals);
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 // --------------------------------------------------------------------
640
641 /**
642 * Returns the total number of queries
643 *
Barry Mienydd671972010-10-04 16:33:58 +0200644 * @return integer
645 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500646 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
648 return $this->query_count;
649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // --------------------------------------------------------------------
652
653 /**
654 * Returns the last query that was executed
655 *
Barry Mienydd671972010-10-04 16:33:58 +0200656 * @return void
657 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500658 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
660 return end($this->queries);
661 }
662
663 // --------------------------------------------------------------------
664
665 /**
666 * "Smart" Escape String
667 *
668 * Escapes data based on type
669 * Sets boolean and null types
670 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200672 * @return mixed
673 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500674 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200675 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000676 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000678 $str = "'".$this->escape_str($str)."'";
679 }
680 elseif (is_bool($str))
681 {
682 $str = ($str === FALSE) ? 0 : 1;
683 }
684 elseif (is_null($str))
685 {
686 $str = 'NULL';
687 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000688
689 return $str;
690 }
691
692 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600693
Derek Jonese4ed5832009-02-20 21:44:59 +0000694 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000695 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000696 *
697 * Calls the individual driver for platform
698 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200699 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000700 * @param string
701 * @return mixed
702 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500703 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200704 {
705 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000706 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000707
Derek Jonese4ed5832009-02-20 21:44:59 +0000708 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 /**
711 * Primary
712 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500713 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 * position is the primary key
715 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200717 * @return string
718 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500719 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200720 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 if ( ! is_array($fields))
724 {
725 return FALSE;
726 }
727
728 return current($fields);
729 }
730
731 // --------------------------------------------------------------------
732
733 /**
734 * Returns an array of table names
735 *
Barry Mienydd671972010-10-04 16:33:58 +0200736 * @return array
737 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500738 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
740 // Is there a cached result?
741 if (isset($this->data_cache['table_names']))
742 {
743 return $this->data_cache['table_names'];
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
747 {
748 if ($this->db_debug)
749 {
750 return $this->display_error('db_unsupported_function');
751 }
752 return FALSE;
753 }
754
755 $retval = array();
756 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 if ($query->num_rows() > 0)
759 {
Taufan Aditya18209332012-02-09 16:07:27 +0700760 $table = FALSE;
761 $rows = $query->result_array();
762 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
763
764 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 {
Taufan Aditya18209332012-02-09 16:07:27 +0700766 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
767 }
768
769 foreach ($rows as $row)
770 {
771 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
773 }
774
775 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 return $this->data_cache['table_names'];
778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 // --------------------------------------------------------------------
781
782 /**
783 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500784 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * @return boolean
786 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500787 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200788 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200789 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 // --------------------------------------------------------------------
793
794 /**
795 * Fetch MySQL Field Names
796 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200798 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500800 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
802 // Is there a cached result?
803 if (isset($this->data_cache['field_names'][$table]))
804 {
805 return $this->data_cache['field_names'][$table];
806 }
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 if ($table == '')
809 {
810 if ($this->db_debug)
811 {
812 return $this->display_error('db_field_param_missing');
813 }
814 return FALSE;
815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Greg Aker1edde302010-01-26 00:17:01 +0000817 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 {
819 if ($this->db_debug)
820 {
821 return $this->display_error('db_unsupported_function');
822 }
823 return FALSE;
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500829 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
831 if (isset($row['COLUMN_NAME']))
832 {
833 $retval[] = $row['COLUMN_NAME'];
834 }
835 else
836 {
837 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200838 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 $this->data_cache['field_names'][$table] = $retval;
842 return $this->data_cache['field_names'][$table];
843 }
844
845 // --------------------------------------------------------------------
846
847 /**
848 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500849 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * @param string
851 * @param string
852 * @return boolean
853 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500854 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200855 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 // --------------------------------------------------------------------
860
861 /**
862 * Returns an object with field data
863 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200865 * @return object
866 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500867 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 {
869 if ($table == '')
870 {
871 if ($this->db_debug)
872 {
873 return $this->display_error('db_field_param_missing');
874 }
875 return FALSE;
876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200878 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000879
880 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200881 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000882
883 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 /**
886 * Generate an insert string
887 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * @param string the table upon which the query will be performed
889 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200890 * @return string
891 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500892 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
894 $fields = array();
895 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200896
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500897 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 $fields[] = $this->_escape_identifiers($key);
900 $values[] = $this->escape($val);
901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200903 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200904 }
905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // --------------------------------------------------------------------
907
908 /**
909 * Generate an update string
910 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 * @param string the table upon which the query will be performed
912 * @param array an associative array data of key/values
913 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200914 * @return string
915 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500916 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
918 if ($where == '')
919 {
920 return false;
921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500924 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200926 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
928
929 if ( ! is_array($where))
930 {
931 $dest = array($where);
932 }
933 else
934 {
935 $dest = array();
936 foreach ($where as $key => $val)
937 {
938 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200939 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 if ($val !== '')
942 {
943 if ( ! $this->_has_operator($key))
944 {
945 $key .= ' =';
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 $val = ' '.$this->escape($val);
949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 $dest[] = $prefix.$key.$val;
952 }
Barry Mienydd671972010-10-04 16:33:58 +0200953 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000954
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200955 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200956 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000957
958 // --------------------------------------------------------------------
959
960 /**
961 * Tests whether the string has an SQL operator
962 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 * @param string
964 * @return bool
965 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500966 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
968 $str = trim($str);
969 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
970 {
971 return FALSE;
972 }
973
974 return TRUE;
975 }
976
977 // --------------------------------------------------------------------
978
979 /**
980 * Enables a native PHP function to be run, using a platform agnostic wrapper.
981 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 * @param string the function name
983 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200984 * @return mixed
985 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500986 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 if (FALSE === strpos($driver, $function))
991 {
992 $function = $driver.$function;
993 }
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 if ( ! function_exists($function))
996 {
997 if ($this->db_debug)
998 {
999 return $this->display_error('db_unsupported_function');
1000 }
1001 return FALSE;
1002 }
1003 else
1004 {
1005 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1006
Repox71b78092011-12-01 09:19:43 +01001007 if (is_null($args))
1008 {
1009 return call_user_func($function);
1010 }
1011 else
1012 {
1013 return call_user_func_array($function, $args);
1014 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 }
1016 }
1017
1018 // --------------------------------------------------------------------
1019
1020 /**
1021 * Set Cache Directory Path
1022 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * @param string the path to the cache directory
1024 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001025 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001026 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
1028 $this->cachedir = $path;
1029 }
1030
1031 // --------------------------------------------------------------------
1032
1033 /**
1034 * Enable Query Caching
1035 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001037 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001038 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 {
1040 $this->cache_on = TRUE;
1041 return TRUE;
1042 }
1043
1044 // --------------------------------------------------------------------
1045
1046 /**
1047 * Disable Query Caching
1048 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001050 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001051 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 {
1053 $this->cache_on = FALSE;
1054 return FALSE;
1055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057
1058 // --------------------------------------------------------------------
1059
1060 /**
1061 * Delete the cache files associated with a particular URI
1062 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001064 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001065 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 {
1067 if ( ! $this->_cache_init())
1068 {
1069 return FALSE;
1070 }
1071 return $this->CACHE->delete($segment_one, $segment_two);
1072 }
1073
1074 // --------------------------------------------------------------------
1075
1076 /**
1077 * Delete All cache files
1078 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001080 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001081 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
1083 if ( ! $this->_cache_init())
1084 {
1085 return FALSE;
1086 }
1087
1088 return $this->CACHE->delete_all();
1089 }
1090
1091 // --------------------------------------------------------------------
1092
1093 /**
1094 * Initialize the Cache Class
1095 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001097 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001098 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
1100 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1101 {
1102 return TRUE;
1103 }
Derek Allarde37ab382009-02-03 16:13:57 +00001104
1105 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 {
Greg Aker3a746652011-04-19 10:59:47 -05001107 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001108 {
1109 return $this->cache_off();
1110 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 }
Derek Allarde37ab382009-02-03 16:13:57 +00001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1114 return TRUE;
1115 }
1116
1117 // --------------------------------------------------------------------
1118
1119 /**
1120 * Close DB Connection
1121 *
Barry Mienydd671972010-10-04 16:33:58 +02001122 * @return void
1123 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001124 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 {
1126 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1127 {
1128 $this->_close($this->conn_id);
1129 }
1130 $this->conn_id = FALSE;
1131 }
Barry Mienydd671972010-10-04 16:33:58 +02001132
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 // --------------------------------------------------------------------
1134
1135 /**
1136 * Display an error message
1137 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 * @param string the error message
1139 * @param string any "swap" values
1140 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001141 * @return string sends the application/error_db.php template
1142 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001143 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Derek Jonese7792202010-03-02 17:24:46 -06001145 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 $LANG->load('db');
1147
1148 $heading = $LANG->line('db_error_heading');
1149
1150 if ($native == TRUE)
1151 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001152 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
1154 else
1155 {
1156 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1157 }
Barry Mienydd671972010-10-04 16:33:58 +02001158
Pascal Kriete60f8c392010-08-25 18:03:28 +02001159 // Find the most likely culprit of the error by going through
1160 // the backtrace until the source file is no longer in the
1161 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001162
Pascal Kriete60f8c392010-08-25 18:03:28 +02001163 $trace = debug_backtrace();
1164
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001165 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001166 {
1167 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1168 {
1169 // Found it - use a relative path for safety
1170 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1171 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001172
Pascal Kriete60f8c392010-08-25 18:03:28 +02001173 break;
1174 }
1175 }
Barry Mienydd671972010-10-04 16:33:58 +02001176
Derek Jonese7792202010-03-02 17:24:46 -06001177 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 echo $error->show_error($heading, $message, 'error_db');
1179 exit;
1180 }
1181
1182 // --------------------------------------------------------------------
1183
1184 /**
1185 * Protect Identifiers
1186 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001188 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001190 * the table prefix onto it. Some logic is necessary in order to deal with
1191 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 *
1193 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1194 *
1195 * Or a query with aliasing:
1196 *
1197 * SELECT m.member_id, m.member_name FROM members AS m
1198 *
1199 * Since the column name can include up to four segments (host, DB, table, column)
1200 * or also have an alias prefix, we need to do a bit of work to figure this out and
1201 * insert the table prefix (if it exists) in the proper position, and escape only
1202 * the correct identifiers.
1203 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 * @param string
1205 * @param bool
1206 * @param mixed
1207 * @param bool
1208 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001209 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001210 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 {
1212 if ( ! is_bool($protect_identifiers))
1213 {
1214 $protect_identifiers = $this->_protect_identifiers;
1215 }
Derek Allarde37ab382009-02-03 16:13:57 +00001216
1217 if (is_array($item))
1218 {
1219 $escaped_array = array();
1220
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001221 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001222 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001223 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001224 }
1225
1226 return $escaped_array;
1227 }
1228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001230 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // If the item has an alias declaration we remove it and set it aside.
1233 // Basically we remove everything to the right of the first space
1234 $alias = '';
1235 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001236 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 $alias = strstr($item, " ");
1238 $item = substr($item, 0, - strlen($alias));
1239 }
1240
Derek Allard911d3e02008-12-15 14:08:35 +00001241 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001242 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001243 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001244 // way to deal with this, but I'm not thinking of it -- Rick
1245 if (strpos($item, '(') !== FALSE)
1246 {
1247 return $item.$alias;
1248 }
1249
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 // Break the string apart if it contains periods, then insert the table prefix
1251 // in the correct location, assuming the period doesn't indicate that we're dealing
1252 // with an alias. While we're at it, we will escape the components
1253 if (strpos($item, '.') !== FALSE)
1254 {
1255 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001256
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001258 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // we have nothing more to do other than escape the item
1260 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001261 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 if ($protect_identifiers === TRUE)
1263 {
1264 foreach ($parts as $key => $val)
1265 {
1266 if ( ! in_array($val, $this->_reserved_identifiers))
1267 {
1268 $parts[$key] = $this->_escape_identifiers($val);
1269 }
1270 }
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001273 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 return $item.$alias;
1275 }
Barry Mienydd671972010-10-04 16:33:58 +02001276
Derek Jones37f4b9c2011-07-01 17:56:50 -05001277 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 if ($this->dbprefix != '')
1279 {
1280 // We now add the table prefix based on some logic.
1281 // Do we have 4 segments (hostname.database.table.column)?
1282 // If so, we add the table prefix to the column name in the 3rd segment.
1283 if (isset($parts[3]))
1284 {
1285 $i = 2;
1286 }
1287 // Do we have 3 segments (database.table.column)?
1288 // If so, we add the table prefix to the column name in 2nd position
1289 elseif (isset($parts[2]))
1290 {
1291 $i = 1;
1292 }
1293 // Do we have 2 segments (table.column)?
1294 // If so, we add the table prefix to the column name in 1st segment
1295 else
1296 {
1297 $i = 0;
1298 }
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 // This flag is set when the supplied $item does not contain a field name.
1301 // This can happen when this function is being called from a JOIN.
1302 if ($field_exists == FALSE)
1303 {
1304 $i++;
1305 }
Barry Mienydd671972010-10-04 16:33:58 +02001306
Derek Jones55acc8b2009-07-11 03:38:13 +00001307 // Verify table prefix and replace if necessary
1308 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1309 {
1310 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 // We only add the table prefix if it does not already exist
1314 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1315 {
1316 $parts[$i] = $this->dbprefix.$parts[$i];
1317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 // Put the parts back together
1320 $item = implode('.', $parts);
1321 }
Barry Mienydd671972010-10-04 16:33:58 +02001322
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 if ($protect_identifiers === TRUE)
1324 {
1325 $item = $this->_escape_identifiers($item);
1326 }
Barry Mienydd671972010-10-04 16:33:58 +02001327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 return $item.$alias;
1329 }
1330
Derek Jones37f4b9c2011-07-01 17:56:50 -05001331 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 if ($this->dbprefix != '')
1333 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001334 // Verify table prefix and replace if necessary
1335 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1336 {
1337 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1338 }
1339
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 // Do we prefix an item with no segments?
1341 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1342 {
1343 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001344 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 }
1346
1347 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1348 {
1349 $item = $this->_escape_identifiers($item);
1350 }
Barry Mienydd671972010-10-04 16:33:58 +02001351
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 return $item.$alias;
1353 }
Túbal Martín511f2252011-11-24 14:43:45 +01001354
1355 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001356
Túbal Martín511f2252011-11-24 14:43:45 +01001357 /**
1358 * Dummy method that allows Active Record class to be disabled
1359 *
1360 * This function is used extensively by every db driver.
1361 *
Túbal Martín511f2252011-11-24 14:43:45 +01001362 * @return void
1363 */
1364 protected function _reset_select()
1365 {
1366
1367 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001368
1369}
1370
Derek Allard2067d1a2008-11-13 22:59:24 +00001371/* End of file DB_driver.php */
Timothy Warren6f531dc2011-10-10 10:10:46 -04001372/* Location: ./system/database/DB_driver.php */