blob: 6a20c001a7d897f51837b0369870c0370a65e007 [file] [log] [blame]
Andrey Andreev4c202602012-03-06 20:34:52 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
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 Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_driver {
42
Andrey Andreev738f5342012-03-06 20:43:40 +020043 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
48 public $dbdriver = 'mysql';
49 public $dbprefix = '';
50 public $char_set = 'utf8';
51 public $dbcollat = 'utf8_general_ci';
52 public $autoinit = TRUE; // Whether to automatically initialize the DB
53 public $swap_pre = '';
54 public $port = '';
55 public $pconnect = FALSE;
56 public $conn_id = FALSE;
57 public $result_id = FALSE;
58 public $db_debug = FALSE;
59 public $benchmark = 0;
60 public $query_count = 0;
61 public $bind_marker = '?';
62 public $save_queries = TRUE;
63 public $queries = array();
64 public $query_times = array();
65 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000066
Andrey Andreevd1add432012-03-06 20:26:01 +020067 public $trans_enabled = TRUE;
68 public $trans_strict = TRUE;
69 protected $_trans_depth = 0;
70 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000071
Andrey Andreevd1add432012-03-06 20:26:01 +020072 public $cache_on = FALSE;
73 public $cachedir = '';
74 public $cache_autodel = FALSE;
75 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020076
Andrey Andreevd1add432012-03-06 20:26:01 +020077 protected $_protect_identifiers = TRUE;
78 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
Timothy Warrene45518d2012-03-06 07:38:00 -050080 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if (is_array($params))
83 {
84 foreach ($params as $key => $val)
85 {
86 $this->$key = $val;
87 }
88 }
89
90 log_message('debug', 'Database Driver Class Initialized');
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
96 * Initialize Database Settings
97 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +020098 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200100 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
102 // If an existing connection resource is available
103 // there is no need to connect and select the database
104 if (is_resource($this->conn_id) OR is_object($this->conn_id))
105 {
106 return TRUE;
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // Connect to the database and set the connection ID
112 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
113
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200114 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if ( ! $this->conn_id)
116 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100117 // Check if there is a failover set
118 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100120 // Go over all the failovers
121 foreach ($this->failover as $failover)
122 {
123 // Replace the current settings with those of the failover
124 foreach ($failover as $key => $val)
125 {
126 $this->$key = $val;
127 }
128
129 // Try to connect
130 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
131
132 // If a connection is made break the foreach loop
133 if ($this->conn_id)
134 {
135 break;
136 }
137 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100139
140 // We still don't have a connection?
141 if ( ! $this->conn_id)
142 {
143 log_message('error', 'Unable to connect to the database');
144
145 if ($this->db_debug)
146 {
147 $this->display_error('db_unable_to_connect');
148 }
149 return FALSE;
150 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // ----------------------------------------------------------------
154
155 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200156 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200158 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200159
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200160 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200167 // Now we set the character set and that's all
168 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
172
173 /**
174 * Set client character set
175 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @param string
177 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200178 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 */
Andrey Andreev063f5962012-02-27 12:20:52 +0200180 public function db_set_charset($charset, $collation = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200182 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset, $collation))
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200184 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if ($this->db_debug)
187 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200188 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 return FALSE;
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 return TRUE;
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
200 * The name of the platform in use (mysql, mssql, etc...)
201 *
Barry Mienydd671972010-10-04 16:33:58 +0200202 * @return string
203 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500204 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 return $this->dbdriver;
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200212 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200214 * Returns a string containing the version of the database being used.
215 * Most drivers will override this method.
216 *
Barry Mienydd671972010-10-04 16:33:58 +0200217 * @return string
218 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200219 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200221 if (isset($this->data_cache['version']))
222 {
223 return $this->data_cache['version'];
224 }
225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 if (FALSE === ($sql = $this->_version()))
227 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200228 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Derek Allard3683f772009-12-16 17:32:33 +0000230
Andrey Andreev08856b82012-03-03 03:19:28 +0200231 $query = $this->query($sql);
232 $query = $query->row();
233 return $this->data_cache['version'] = $query->ver;
234 }
Derek Allard3683f772009-12-16 17:32:33 +0000235
Andrey Andreev08856b82012-03-03 03:19:28 +0200236 // --------------------------------------------------------------------
237
238 /**
239 * Version number query string
240 *
241 * @return string
242 */
243 protected function _version()
244 {
245 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // --------------------------------------------------------------------
249
250 /**
251 * Execute the query
252 *
253 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200254 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * upon successful execution of a "write" type query. Returns boolean
256 * FALSE upon failure, and if the $db_debug variable is set to TRUE
257 * will raise an error.
258 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * @param string An SQL query string
260 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200261 * @return mixed
262 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500263 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 if ($sql == '')
266 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200267 log_message('error', 'Invalid query: '.$sql);
268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 if ($this->db_debug)
270 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 return $this->display_error('db_invalid_query');
272 }
273 return FALSE;
274 }
275
276 // Verify table prefix and replace if necessary
277 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600278 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
280 }
Derek Jonese7792202010-03-02 17:24:46 -0600281
Ryan Dialef7474c2012-03-01 16:11:36 -0500282 // Compile binds if needed
283 if ($binds !== FALSE)
284 {
285 $sql = $this->compile_binds($sql, $binds);
286 }
287
Andrey Andreev4c202602012-03-06 20:34:52 +0200288 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // we will load the caching class and return the previously
290 // cached query if it exists
291 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
292 {
293 if ($this->_cache_init())
294 {
295 $this->load_rdriver();
296 if (FALSE !== ($cache = $this->CACHE->read($sql)))
297 {
298 return $cache;
299 }
300 }
301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jones37f4b9c2011-07-01 17:56:50 -0500303 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 if ($this->save_queries == TRUE)
305 {
306 $this->queries[] = $sql;
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // Start the Query Timer
310 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Run the Query
313 if (FALSE === ($this->result_id = $this->simple_query($sql)))
314 {
315 if ($this->save_queries == TRUE)
316 {
317 $this->query_times[] = 0;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // This will trigger a rollback if transactions are being used
321 $this->_trans_status = FALSE;
322
Andrey Andreev4be5de12012-03-02 15:45:41 +0200323 // Grab the error now, as we might run some additional queries before displaying the error
324 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200325
326 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200327 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 if ($this->db_debug)
330 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200332 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200333 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // transactions to remain in limbo.
335 $this->trans_complete();
336
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200337 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 return $this->display_error(
Andrey Andreev4be5de12012-03-02 15:45:41 +0200339 array(
340 'Error Number: '.$error['code'],
341 $error['message'],
342 $sql
343 )
344 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 return FALSE;
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // Stop and aggregate the query time results
351 $time_end = list($em, $es) = explode(' ', microtime());
352 $this->benchmark += ($em + $es) - ($sm + $ss);
353
354 if ($this->save_queries == TRUE)
355 {
356 $this->query_times[] = ($em + $es) - ($sm + $ss);
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // Increment the query counter
360 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Was the query a "write" type?
363 // If so we'll simply return true
364 if ($this->is_write_type($sql) === TRUE)
365 {
366 // If caching is enabled we'll auto-cleanup any
367 // existing files related to this particular URI
368 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
369 {
370 $this->CACHE->delete();
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 return TRUE;
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // Return TRUE if we don't need to create a result object
377 // Currently only the Oracle driver uses this when stored
378 // procedures are used
379 if ($return_object !== TRUE)
380 {
381 return TRUE;
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
384 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200385 $driver = $this->load_rdriver();
386 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Jones37f4b9c2011-07-01 17:56:50 -0500390 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // result object and save it to a cache file.
392 if ($this->cache_on == TRUE AND $this->_cache_init())
393 {
394 // We'll create a new instance of the result object
395 // only without the platform specific driver since
396 // we can't use it with cached data (the query result
397 // resource ID won't be any good once we've cached the
398 // result object, so we'll have to compile the data
399 // and save it)
400 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200401 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 $CR->result_object = $RES->result_object();
403 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // Reset these since cached objects can not utilize resource IDs.
406 $CR->conn_id = NULL;
407 $CR->result_id = NULL;
408
409 $this->CACHE->write($sql, $CR);
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 return $RES;
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Load the result drivers
419 *
Barry Mienydd671972010-10-04 16:33:58 +0200420 * @return string the name of the result class
421 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500422 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 $driver = 'CI_DB_'.$this->dbdriver.'_result';
425
426 if ( ! class_exists($driver))
427 {
Greg Aker3a746652011-04-19 10:59:47 -0500428 include_once(BASEPATH.'database/DB_result.php');
429 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 return $driver;
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // --------------------------------------------------------------------
436
437 /**
438 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200439 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * we only use it when running transaction commands since they do
441 * not require all the features of the main query() function.
442 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200444 * @return mixed
445 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500446 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 if ( ! $this->conn_id)
449 {
450 $this->initialize();
451 }
452
453 return $this->_execute($sql);
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // --------------------------------------------------------------------
457
458 /**
459 * Disable Transactions
460 * This permits transactions to be disabled at run-time.
461 *
Barry Mienydd671972010-10-04 16:33:58 +0200462 * @return void
463 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500464 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
466 $this->trans_enabled = FALSE;
467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Enable/disable Transaction Strict Mode
473 * When strict mode is enabled, if you are running multiple groups of
474 * transactions, if one group fails all groups will be rolled back.
475 * If strict mode is disabled, each group is treated autonomously, meaning
476 * a failure of one group will not affect any others
477 *
Barry Mienydd671972010-10-04 16:33:58 +0200478 * @return void
479 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500480 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // --------------------------------------------------------------------
486
487 /**
488 * Start Transaction
489 *
Barry Mienydd671972010-10-04 16:33:58 +0200490 * @return void
491 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500492 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200493 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 if ( ! $this->trans_enabled)
495 {
496 return FALSE;
497 }
498
499 // When transactions are nested we only begin/commit/rollback the outermost ones
500 if ($this->_trans_depth > 0)
501 {
502 $this->_trans_depth += 1;
503 return;
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500507 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509
510 // --------------------------------------------------------------------
511
512 /**
513 * Complete Transaction
514 *
Barry Mienydd671972010-10-04 16:33:58 +0200515 * @return bool
516 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500517 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 if ( ! $this->trans_enabled)
520 {
521 return FALSE;
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 // When transactions are nested we only begin/commit/rollback the outermost ones
525 if ($this->_trans_depth > 1)
526 {
527 $this->_trans_depth -= 1;
528 return TRUE;
529 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500530 else
531 {
532 $this->_trans_depth = 0;
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 // The query() function will set this flag to FALSE in the event that a query failed
536 if ($this->_trans_status === FALSE)
537 {
538 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 // If we are NOT running in strict mode, we will reset
541 // the _trans_status flag so that subsequent groups of transactions
542 // will be permitted.
543 if ($this->trans_strict === FALSE)
544 {
545 $this->_trans_status = TRUE;
546 }
547
548 log_message('debug', 'DB Transaction Failure');
549 return FALSE;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 $this->trans_commit();
553 return TRUE;
554 }
555
556 // --------------------------------------------------------------------
557
558 /**
559 * Lets you retrieve the transaction flag to determine if it has failed
560 *
Barry Mienydd671972010-10-04 16:33:58 +0200561 * @return bool
562 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500563 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
565 return $this->_trans_status;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Compile Bindings
572 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * @param string the sql statement
574 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200575 * @return string
576 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500577 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 if (strpos($sql, $this->bind_marker) === FALSE)
580 {
581 return $sql;
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 if ( ! is_array($binds))
585 {
586 $binds = array($binds);
587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // Get the sql segments around the bind markers
590 $segments = explode($this->bind_marker, $sql);
591
592 // The count of bind should be 1 less then the count of segments
593 // If there are more bind arguments trim it down
594 if (count($binds) >= count($segments)) {
595 $binds = array_slice($binds, 0, count($segments)-1);
596 }
597
598 // Construct the binded query
599 $result = $segments[0];
600 $i = 0;
601 foreach ($binds as $bind)
602 {
603 $result .= $this->escape($bind);
604 $result .= $segments[++$i];
605 }
606
607 return $result;
608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 // --------------------------------------------------------------------
611
612 /**
613 * Determines if a query is a "write" type.
614 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200616 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200617 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200618 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200620 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 +0000621 }
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // --------------------------------------------------------------------
624
625 /**
626 * Calculate the aggregate query elapsed time
627 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200628 * @param int The number of decimal places
629 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200630 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500631 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 return number_format($this->benchmark, $decimals);
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // --------------------------------------------------------------------
637
638 /**
639 * Returns the total number of queries
640 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200641 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200642 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500643 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 return $this->query_count;
646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 // --------------------------------------------------------------------
649
650 /**
651 * Returns the last query that was executed
652 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200653 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200654 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500655 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 {
657 return end($this->queries);
658 }
659
660 // --------------------------------------------------------------------
661
662 /**
663 * "Smart" Escape String
664 *
665 * Escapes data based on type
666 * Sets boolean and null types
667 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200669 * @return mixed
670 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500671 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200672 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000673 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000675 $str = "'".$this->escape_str($str)."'";
676 }
677 elseif (is_bool($str))
678 {
679 $str = ($str === FALSE) ? 0 : 1;
680 }
681 elseif (is_null($str))
682 {
683 $str = 'NULL';
684 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000685
686 return $str;
687 }
688
689 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600690
Derek Jonese4ed5832009-02-20 21:44:59 +0000691 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000692 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000693 *
694 * Calls the individual driver for platform
695 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200696 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000697 * @param string
698 * @return mixed
699 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500700 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200701 {
702 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000703 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000704
Derek Jonese4ed5832009-02-20 21:44:59 +0000705 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 /**
708 * Primary
709 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200710 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 * position is the primary key
712 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200714 * @return string
715 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500716 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200717 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 if ( ! is_array($fields))
721 {
722 return FALSE;
723 }
724
725 return current($fields);
726 }
727
728 // --------------------------------------------------------------------
729
730 /**
731 * Returns an array of table names
732 *
Barry Mienydd671972010-10-04 16:33:58 +0200733 * @return array
734 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500735 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
737 // Is there a cached result?
738 if (isset($this->data_cache['table_names']))
739 {
740 return $this->data_cache['table_names'];
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
744 {
745 if ($this->db_debug)
746 {
747 return $this->display_error('db_unsupported_function');
748 }
749 return FALSE;
750 }
751
752 $retval = array();
753 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200754
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 if ($query->num_rows() > 0)
756 {
Taufan Aditya18209332012-02-09 16:07:27 +0700757 $table = FALSE;
758 $rows = $query->result_array();
759 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
760
761 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 {
Taufan Aditya18209332012-02-09 16:07:27 +0700763 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
764 }
765
766 foreach ($rows as $row)
767 {
768 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
770 }
771
772 $this->data_cache['table_names'] = $retval;
Andrey Andreev4c202602012-03-06 20:34:52 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 return $this->data_cache['table_names'];
775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500781 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200782 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500784 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200785 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200786 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 // --------------------------------------------------------------------
790
791 /**
792 * Fetch MySQL Field Names
793 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200795 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500797 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 {
799 // Is there a cached result?
800 if (isset($this->data_cache['field_names'][$table]))
801 {
802 return $this->data_cache['field_names'][$table];
803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 if ($table == '')
806 {
807 if ($this->db_debug)
808 {
809 return $this->display_error('db_field_param_missing');
810 }
811 return FALSE;
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Greg Aker1edde302010-01-26 00:17:01 +0000814 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
816 if ($this->db_debug)
817 {
818 return $this->display_error('db_unsupported_function');
819 }
820 return FALSE;
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500826 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 {
828 if (isset($row['COLUMN_NAME']))
829 {
830 $retval[] = $row['COLUMN_NAME'];
831 }
832 else
833 {
834 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200835 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 }
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 $this->data_cache['field_names'][$table] = $retval;
839 return $this->data_cache['field_names'][$table];
840 }
841
842 // --------------------------------------------------------------------
843
844 /**
845 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500846 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 * @param string
848 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200849 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500851 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200852 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // --------------------------------------------------------------------
857
858 /**
859 * Returns an object with field data
860 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200862 * @return object
863 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500864 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
866 if ($table == '')
867 {
868 if ($this->db_debug)
869 {
870 return $this->display_error('db_field_param_missing');
871 }
872 return FALSE;
873 }
Barry Mienydd671972010-10-04 16:33:58 +0200874
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200875 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000876
877 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200878 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000879
880 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 /**
883 * Generate an insert string
884 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @param string the table upon which the query will be performed
886 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200887 * @return string
888 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500889 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
891 $fields = array();
892 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200893
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500894 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
896 $fields[] = $this->_escape_identifiers($key);
897 $values[] = $this->escape($val);
898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200900 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200901 }
902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 // --------------------------------------------------------------------
904
905 /**
906 * Generate an update string
907 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 * @param string the table upon which the query will be performed
909 * @param array an associative array data of key/values
910 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200911 * @return string
912 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500913 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
915 if ($where == '')
916 {
Andrey Andreev4c202602012-03-06 20:34:52 +0200917 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500921 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200923 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
925
926 if ( ! is_array($where))
927 {
928 $dest = array($where);
929 }
930 else
931 {
932 $dest = array();
933 foreach ($where as $key => $val)
934 {
935 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200936 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 if ($val !== '')
939 {
940 if ( ! $this->_has_operator($key))
941 {
942 $key .= ' =';
943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 $val = ' '.$this->escape($val);
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 $dest[] = $prefix.$key.$val;
949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000951
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200952 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200953 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000954
955 // --------------------------------------------------------------------
956
957 /**
958 * Tests whether the string has an SQL operator
959 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 * @param string
961 * @return bool
962 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500963 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
965 $str = trim($str);
966 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
967 {
968 return FALSE;
969 }
970
971 return TRUE;
972 }
973
974 // --------------------------------------------------------------------
975
976 /**
977 * Enables a native PHP function to be run, using a platform agnostic wrapper.
978 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 * @param string the function name
980 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200981 * @return mixed
982 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500983 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 if (FALSE === strpos($driver, $function))
988 {
989 $function = $driver.$function;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 if ( ! function_exists($function))
993 {
994 if ($this->db_debug)
995 {
996 return $this->display_error('db_unsupported_function');
997 }
998 return FALSE;
999 }
1000 else
1001 {
1002 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1003
Repox71b78092011-12-01 09:19:43 +01001004 if (is_null($args))
1005 {
1006 return call_user_func($function);
1007 }
1008 else
1009 {
1010 return call_user_func_array($function, $args);
1011 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 }
1013 }
1014
1015 // --------------------------------------------------------------------
1016
1017 /**
1018 * Set Cache Directory Path
1019 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * @param string the path to the cache directory
1021 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001022 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001023 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
1025 $this->cachedir = $path;
1026 }
1027
1028 // --------------------------------------------------------------------
1029
1030 /**
1031 * Enable Query Caching
1032 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001033 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001034 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001035 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
1037 $this->cache_on = TRUE;
1038 return TRUE;
1039 }
1040
1041 // --------------------------------------------------------------------
1042
1043 /**
1044 * Disable Query Caching
1045 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001046 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001047 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001048 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 {
1050 $this->cache_on = FALSE;
1051 return FALSE;
1052 }
Barry Mienydd671972010-10-04 16:33:58 +02001053
Derek Allard2067d1a2008-11-13 22:59:24 +00001054
1055 // --------------------------------------------------------------------
1056
1057 /**
1058 * Delete the cache files associated with a particular URI
1059 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001060 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001061 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001062 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 {
1064 if ( ! $this->_cache_init())
1065 {
1066 return FALSE;
1067 }
1068 return $this->CACHE->delete($segment_one, $segment_two);
1069 }
1070
1071 // --------------------------------------------------------------------
1072
1073 /**
1074 * Delete All cache files
1075 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001076 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001077 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001078 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 {
1080 if ( ! $this->_cache_init())
1081 {
1082 return FALSE;
1083 }
1084
1085 return $this->CACHE->delete_all();
1086 }
1087
1088 // --------------------------------------------------------------------
1089
1090 /**
1091 * Initialize the Cache Class
1092 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001093 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001094 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001095 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
1097 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1098 {
1099 return TRUE;
1100 }
Derek Allarde37ab382009-02-03 16:13:57 +00001101
1102 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 {
Greg Aker3a746652011-04-19 10:59:47 -05001104 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001105 {
1106 return $this->cache_off();
1107 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 }
Derek Allarde37ab382009-02-03 16:13:57 +00001109
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1111 return TRUE;
1112 }
1113
1114 // --------------------------------------------------------------------
1115
1116 /**
1117 * Close DB Connection
1118 *
Barry Mienydd671972010-10-04 16:33:58 +02001119 * @return void
1120 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001121 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
1123 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1124 {
1125 $this->_close($this->conn_id);
1126 }
1127 $this->conn_id = FALSE;
1128 }
Barry Mienydd671972010-10-04 16:33:58 +02001129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 // --------------------------------------------------------------------
1131
1132 /**
1133 * Display an error message
1134 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 * @param string the error message
1136 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001137 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001138 * @return string sends the application/error_db.php template
1139 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001140 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
Derek Jonese7792202010-03-02 17:24:46 -06001142 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 $LANG->load('db');
1144
1145 $heading = $LANG->line('db_error_heading');
1146
1147 if ($native == TRUE)
1148 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001149 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
1151 else
1152 {
1153 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1154 }
Barry Mienydd671972010-10-04 16:33:58 +02001155
Pascal Kriete60f8c392010-08-25 18:03:28 +02001156 // Find the most likely culprit of the error by going through
1157 // the backtrace until the source file is no longer in the
1158 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001159
Pascal Kriete60f8c392010-08-25 18:03:28 +02001160 $trace = debug_backtrace();
1161
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001162 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001163 {
1164 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1165 {
1166 // Found it - use a relative path for safety
1167 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1168 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001169
Pascal Kriete60f8c392010-08-25 18:03:28 +02001170 break;
1171 }
1172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173
Derek Jonese7792202010-03-02 17:24:46 -06001174 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 echo $error->show_error($heading, $message, 'error_db');
1176 exit;
1177 }
1178
1179 // --------------------------------------------------------------------
1180
1181 /**
1182 * Protect Identifiers
1183 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001184 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001185 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001187 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001188 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 *
1190 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1191 *
1192 * Or a query with aliasing:
1193 *
1194 * SELECT m.member_id, m.member_name FROM members AS m
1195 *
1196 * Since the column name can include up to four segments (host, DB, table, column)
1197 * or also have an alias prefix, we need to do a bit of work to figure this out and
1198 * insert the table prefix (if it exists) in the proper position, and escape only
1199 * the correct identifiers.
1200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 * @param string
1202 * @param bool
1203 * @param mixed
1204 * @param bool
1205 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001206 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001207 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 {
1209 if ( ! is_bool($protect_identifiers))
1210 {
1211 $protect_identifiers = $this->_protect_identifiers;
1212 }
Derek Allarde37ab382009-02-03 16:13:57 +00001213
1214 if (is_array($item))
1215 {
1216 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001217 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001218 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001219 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001220 }
1221
1222 return $escaped_array;
1223 }
1224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001226 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001227
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 // If the item has an alias declaration we remove it and set it aside.
1229 // Basically we remove everything to the right of the first space
1230 $alias = '';
1231 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001232 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 $alias = strstr($item, " ");
1234 $item = substr($item, 0, - strlen($alias));
1235 }
1236
Derek Allard911d3e02008-12-15 14:08:35 +00001237 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001238 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001239 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001240 // way to deal with this, but I'm not thinking of it -- Rick
1241 if (strpos($item, '(') !== FALSE)
1242 {
1243 return $item.$alias;
1244 }
1245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 // Break the string apart if it contains periods, then insert the table prefix
1247 // in the correct location, assuming the period doesn't indicate that we're dealing
1248 // with an alias. While we're at it, we will escape the components
1249 if (strpos($item, '.') !== FALSE)
1250 {
1251 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001254 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001256 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001257 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 if ($protect_identifiers === TRUE)
1259 {
1260 foreach ($parts as $key => $val)
1261 {
1262 if ( ! in_array($val, $this->_reserved_identifiers))
1263 {
1264 $parts[$key] = $this->_escape_identifiers($val);
1265 }
1266 }
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001269 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 return $item.$alias;
1271 }
Barry Mienydd671972010-10-04 16:33:58 +02001272
Andrey Andreev4c202602012-03-06 20:34:52 +02001273 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 if ($this->dbprefix != '')
1275 {
1276 // We now add the table prefix based on some logic.
1277 // Do we have 4 segments (hostname.database.table.column)?
1278 // If so, we add the table prefix to the column name in the 3rd segment.
1279 if (isset($parts[3]))
1280 {
1281 $i = 2;
1282 }
1283 // Do we have 3 segments (database.table.column)?
1284 // If so, we add the table prefix to the column name in 2nd position
1285 elseif (isset($parts[2]))
1286 {
1287 $i = 1;
1288 }
1289 // Do we have 2 segments (table.column)?
1290 // If so, we add the table prefix to the column name in 1st segment
1291 else
1292 {
1293 $i = 0;
1294 }
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 // This flag is set when the supplied $item does not contain a field name.
1297 // This can happen when this function is being called from a JOIN.
1298 if ($field_exists == FALSE)
1299 {
1300 $i++;
1301 }
Barry Mienydd671972010-10-04 16:33:58 +02001302
Derek Jones55acc8b2009-07-11 03:38:13 +00001303 // Verify table prefix and replace if necessary
1304 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1305 {
1306 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1307 }
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 // We only add the table prefix if it does not already exist
1310 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1311 {
1312 $parts[$i] = $this->dbprefix.$parts[$i];
1313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 // Put the parts back together
1316 $item = implode('.', $parts);
1317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 if ($protect_identifiers === TRUE)
1320 {
1321 $item = $this->_escape_identifiers($item);
1322 }
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 return $item.$alias;
1325 }
1326
Andrey Andreev4c202602012-03-06 20:34:52 +02001327 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 if ($this->dbprefix != '')
1329 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001330 // Verify table prefix and replace if necessary
1331 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1332 {
1333 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1334 }
1335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 // Do we prefix an item with no segments?
1337 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1338 {
1339 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001340 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 }
1342
1343 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1344 {
1345 $item = $this->_escape_identifiers($item);
1346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 return $item.$alias;
1349 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001350
Túbal Martín511f2252011-11-24 14:43:45 +01001351 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001352
Túbal Martín511f2252011-11-24 14:43:45 +01001353 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001354 * Dummy method that allows Query Builder class to be disabled
Túbal Martín511f2252011-11-24 14:43:45 +01001355 *
1356 * This function is used extensively by every db driver.
1357 *
Túbal Martín511f2252011-11-24 14:43:45 +01001358 * @return void
1359 */
1360 protected function _reset_select()
1361 {
Túbal Martín511f2252011-11-24 14:43:45 +01001362 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001363
1364}
1365
Derek Allard2067d1a2008-11-13 22:59:24 +00001366/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001367/* Location: ./system/database/DB_driver.php */