blob: f1e9e723964f1b1c0149dd6f95d639689a34dc4e [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
45 var $username;
46 var $password;
47 var $hostname;
48 var $database;
49 var $dbdriver = 'mysql';
50 var $dbprefix = '';
51 var $char_set = 'utf8';
52 var $dbcollat = 'utf8_general_ci';
53 var $autoinit = TRUE; // Whether to automatically initialize the DB
54 var $swap_pre = '';
55 var $port = '';
56 var $pconnect = FALSE;
57 var $conn_id = FALSE;
58 var $result_id = FALSE;
59 var $db_debug = FALSE;
60 var $benchmark = 0;
61 var $query_count = 0;
62 var $bind_marker = '?';
63 var $save_queries = TRUE;
64 var $queries = array();
65 var $query_times = array();
66 var $data_cache = array();
67 var $trans_enabled = TRUE;
68 var $trans_strict = TRUE;
69 var $_trans_depth = 0;
70 var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
71 var $cache_on = FALSE;
72 var $cachedir = '';
73 var $cache_autodel = FALSE;
74 var $CACHE; // The cache class object
75
76 // Private variables
77 var $_protect_identifiers = TRUE;
78 var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
80 // These are use with Oracle
81 var $stmt_id;
82 var $curs_id;
83 var $limit_used;
Taufan Aditya18209332012-02-09 16:07:27 +070084
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050087 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * connection settings.
89 *
90 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020091 */
Timothy Warrena2097a02011-10-10 10:10:46 -040092 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
94 if (is_array($params))
95 {
96 foreach ($params as $key => $val)
97 {
98 $this->$key = $val;
99 }
100 }
101
102 log_message('debug', 'Database Driver Class Initialized');
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
106
107 /**
108 * Initialize Database Settings
109 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200110 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200111 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200112 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
114 // If an existing connection resource is available
115 // there is no need to connect and select the database
116 if (is_resource($this->conn_id) OR is_object($this->conn_id))
117 {
118 return TRUE;
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // Connect to the database and set the connection ID
124 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
125
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200126 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if ( ! $this->conn_id)
128 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100129 // Check if there is a failover set
130 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100132 // Go over all the failovers
133 foreach ($this->failover as $failover)
134 {
135 // Replace the current settings with those of the failover
136 foreach ($failover as $key => $val)
137 {
138 $this->$key = $val;
139 }
140
141 // Try to connect
142 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
143
144 // If a connection is made break the foreach loop
145 if ($this->conn_id)
146 {
147 break;
148 }
149 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100151
152 // We still don't have a connection?
153 if ( ! $this->conn_id)
154 {
155 log_message('error', 'Unable to connect to the database');
156
157 if ($this->db_debug)
158 {
159 $this->display_error('db_unable_to_connect');
160 }
161 return FALSE;
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 // ----------------------------------------------------------------
166
167 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200170 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200171
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200172 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200174 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200176 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200179 // Now we set the character set and that's all
180 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Set client character set
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param string
189 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200190 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 */
Andrey Andreev063f5962012-02-27 12:20:52 +0200192 public function db_set_charset($charset, $collation = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200194 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset, $collation))
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200196 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 if ($this->db_debug)
199 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200200 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 return FALSE;
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 return TRUE;
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 // --------------------------------------------------------------------
210
211 /**
212 * The name of the platform in use (mysql, mssql, etc...)
213 *
214 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200215 * @return string
216 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 function platform()
218 {
219 return $this->dbdriver;
220 }
221
222 // --------------------------------------------------------------------
223
224 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500225 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * version of the database being used
227 *
228 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200229 * @return string
230 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 function version()
232 {
233 if (FALSE === ($sql = $this->_version()))
234 {
235 if ($this->db_debug)
236 {
237 return $this->display_error('db_unsupported_function');
238 }
239 return FALSE;
240 }
Derek Allard3683f772009-12-16 17:32:33 +0000241
242 // Some DBs have functions that return the version, and don't run special
243 // SQL queries per se. In these instances, just return the result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200244 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli');
Derek Allard3683f772009-12-16 17:32:33 +0000245
246 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 return $sql;
249 }
Derek Allard3683f772009-12-16 17:32:33 +0000250 else
251 {
252 $query = $this->query($sql);
253 return $query->row('ver');
254 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // --------------------------------------------------------------------
258
259 /**
260 * Execute the query
261 *
262 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500263 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * upon successful execution of a "write" type query. Returns boolean
265 * FALSE upon failure, and if the $db_debug variable is set to TRUE
266 * will raise an error.
267 *
268 * @access public
269 * @param string An SQL query string
270 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200271 * @return mixed
272 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 function query($sql, $binds = FALSE, $return_object = TRUE)
274 {
275 if ($sql == '')
276 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200277 log_message('error', 'Invalid query: '.$sql);
278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 if ($this->db_debug)
280 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 return $this->display_error('db_invalid_query');
282 }
283 return FALSE;
284 }
285
286 // Verify table prefix and replace if necessary
287 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600288 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
290 }
Derek Jonese7792202010-03-02 17:24:46 -0600291
Derek Jones37f4b9c2011-07-01 17:56:50 -0500292 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // we will load the caching class and return the previously
294 // cached query if it exists
295 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
296 {
297 if ($this->_cache_init())
298 {
299 $this->load_rdriver();
300 if (FALSE !== ($cache = $this->CACHE->read($sql)))
301 {
302 return $cache;
303 }
304 }
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // Compile binds if needed
308 if ($binds !== FALSE)
309 {
310 $sql = $this->compile_binds($sql, $binds);
311 }
312
Derek Jones37f4b9c2011-07-01 17:56:50 -0500313 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 if ($this->save_queries == TRUE)
315 {
316 $this->queries[] = $sql;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Start the Query Timer
320 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Run the Query
323 if (FALSE === ($this->result_id = $this->simple_query($sql)))
324 {
325 if ($this->save_queries == TRUE)
326 {
327 $this->query_times[] = 0;
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // This will trigger a rollback if transactions are being used
331 $this->_trans_status = FALSE;
332
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200333 // Grab the error number and message now, as we might run some
334 // additional queries before displaying the error
335 $error_no = $this->_error_number();
336 $error_msg = $this->_error_message();
337
338 // Log errors
339 log_message('error', 'Query error: '.$error_msg);
340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 if ($this->db_debug)
342 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500344 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200345 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // transactions to remain in limbo.
347 $this->trans_complete();
348
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200349 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 return $this->display_error(
351 array(
352 'Error Number: '.$error_no,
353 $error_msg,
354 $sql
355 )
356 );
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 return FALSE;
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Stop and aggregate the query time results
363 $time_end = list($em, $es) = explode(' ', microtime());
364 $this->benchmark += ($em + $es) - ($sm + $ss);
365
366 if ($this->save_queries == TRUE)
367 {
368 $this->query_times[] = ($em + $es) - ($sm + $ss);
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 // Increment the query counter
372 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // Was the query a "write" type?
375 // If so we'll simply return true
376 if ($this->is_write_type($sql) === TRUE)
377 {
378 // If caching is enabled we'll auto-cleanup any
379 // existing files related to this particular URI
380 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
381 {
382 $this->CACHE->delete();
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 return TRUE;
386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // Return TRUE if we don't need to create a result object
389 // Currently only the Oracle driver uses this when stored
390 // procedures are used
391 if ($return_object !== TRUE)
392 {
393 return TRUE;
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
396 // Load and instantiate the result driver
397
398 $driver = $this->load_rdriver();
399 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $RES->conn_id = $this->conn_id;
401 $RES->result_id = $this->result_id;
402
403 if ($this->dbdriver == 'oci8')
404 {
405 $RES->stmt_id = $this->stmt_id;
406 $RES->curs_id = NULL;
407 $RES->limit_used = $this->limit_used;
408 $this->stmt_id = FALSE;
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // oci8 vars must be set before calling this
412 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Jones37f4b9c2011-07-01 17:56:50 -0500414 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // result object and save it to a cache file.
416 if ($this->cache_on == TRUE AND $this->_cache_init())
417 {
418 // We'll create a new instance of the result object
419 // only without the platform specific driver since
420 // we can't use it with cached data (the query result
421 // resource ID won't be any good once we've cached the
422 // result object, so we'll have to compile the data
423 // and save it)
424 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200425 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 $CR->result_object = $RES->result_object();
427 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 // Reset these since cached objects can not utilize resource IDs.
430 $CR->conn_id = NULL;
431 $CR->result_id = NULL;
432
433 $this->CACHE->write($sql, $CR);
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return $RES;
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Load the result drivers
443 *
444 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200445 * @return string the name of the result class
446 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 function load_rdriver()
448 {
449 $driver = 'CI_DB_'.$this->dbdriver.'_result';
450
451 if ( ! class_exists($driver))
452 {
Greg Aker3a746652011-04-19 10:59:47 -0500453 include_once(BASEPATH.'database/DB_result.php');
454 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 return $driver;
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 // --------------------------------------------------------------------
461
462 /**
463 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500464 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 * we only use it when running transaction commands since they do
466 * not require all the features of the main query() function.
467 *
468 * @access public
469 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200470 * @return mixed
471 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 function simple_query($sql)
473 {
474 if ( ! $this->conn_id)
475 {
476 $this->initialize();
477 }
478
479 return $this->_execute($sql);
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Disable Transactions
486 * This permits transactions to be disabled at run-time.
487 *
488 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200489 * @return void
490 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 function trans_off()
492 {
493 $this->trans_enabled = FALSE;
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
499 * Enable/disable Transaction Strict Mode
500 * When strict mode is enabled, if you are running multiple groups of
501 * transactions, if one group fails all groups will be rolled back.
502 * If strict mode is disabled, each group is treated autonomously, meaning
503 * a failure of one group will not affect any others
504 *
505 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200506 * @return void
507 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 function trans_strict($mode = TRUE)
509 {
510 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 // --------------------------------------------------------------------
514
515 /**
516 * Start Transaction
517 *
518 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200519 * @return void
520 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200522 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 if ( ! $this->trans_enabled)
524 {
525 return FALSE;
526 }
527
528 // When transactions are nested we only begin/commit/rollback the outermost ones
529 if ($this->_trans_depth > 0)
530 {
531 $this->_trans_depth += 1;
532 return;
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500536 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Complete Transaction
543 *
544 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200545 * @return bool
546 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 function trans_complete()
548 {
549 if ( ! $this->trans_enabled)
550 {
551 return FALSE;
552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // When transactions are nested we only begin/commit/rollback the outermost ones
555 if ($this->_trans_depth > 1)
556 {
557 $this->_trans_depth -= 1;
558 return TRUE;
559 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500560 else
561 {
562 $this->_trans_depth = 0;
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 // The query() function will set this flag to FALSE in the event that a query failed
566 if ($this->_trans_status === FALSE)
567 {
568 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 // If we are NOT running in strict mode, we will reset
571 // the _trans_status flag so that subsequent groups of transactions
572 // will be permitted.
573 if ($this->trans_strict === FALSE)
574 {
575 $this->_trans_status = TRUE;
576 }
577
578 log_message('debug', 'DB Transaction Failure');
579 return FALSE;
580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 $this->trans_commit();
583 return TRUE;
584 }
585
586 // --------------------------------------------------------------------
587
588 /**
589 * Lets you retrieve the transaction flag to determine if it has failed
590 *
591 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200592 * @return bool
593 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 function trans_status()
595 {
596 return $this->_trans_status;
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Compile Bindings
603 *
604 * @access public
605 * @param string the sql statement
606 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200607 * @return string
608 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 function compile_binds($sql, $binds)
610 {
611 if (strpos($sql, $this->bind_marker) === FALSE)
612 {
613 return $sql;
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 if ( ! is_array($binds))
617 {
618 $binds = array($binds);
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // Get the sql segments around the bind markers
622 $segments = explode($this->bind_marker, $sql);
623
624 // The count of bind should be 1 less then the count of segments
625 // If there are more bind arguments trim it down
626 if (count($binds) >= count($segments)) {
627 $binds = array_slice($binds, 0, count($segments)-1);
628 }
629
630 // Construct the binded query
631 $result = $segments[0];
632 $i = 0;
633 foreach ($binds as $bind)
634 {
635 $result .= $this->escape($bind);
636 $result .= $segments[++$i];
637 }
638
639 return $result;
640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 // --------------------------------------------------------------------
643
644 /**
645 * Determines if a query is a "write" type.
646 *
647 * @access public
648 * @param string An SQL query string
Barry Mienydd671972010-10-04 16:33:58 +0200649 * @return boolean
650 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 function is_write_type($sql)
652 {
Derek Allarde37ab382009-02-03 16:13:57 +0000653 if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 return FALSE;
656 }
657 return TRUE;
658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 // --------------------------------------------------------------------
661
662 /**
663 * Calculate the aggregate query elapsed time
664 *
665 * @access public
666 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200667 * @return integer
668 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 function elapsed_time($decimals = 6)
670 {
671 return number_format($this->benchmark, $decimals);
672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 // --------------------------------------------------------------------
675
676 /**
677 * Returns the total number of queries
678 *
679 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200680 * @return integer
681 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 function total_queries()
683 {
684 return $this->query_count;
685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 // --------------------------------------------------------------------
688
689 /**
690 * Returns the last query that was executed
691 *
692 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200693 * @return void
694 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 function last_query()
696 {
697 return end($this->queries);
698 }
699
700 // --------------------------------------------------------------------
701
702 /**
703 * "Smart" Escape String
704 *
705 * Escapes data based on type
706 * Sets boolean and null types
707 *
708 * @access public
709 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200710 * @return mixed
711 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200713 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000714 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000716 $str = "'".$this->escape_str($str)."'";
717 }
718 elseif (is_bool($str))
719 {
720 $str = ($str === FALSE) ? 0 : 1;
721 }
722 elseif (is_null($str))
723 {
724 $str = 'NULL';
725 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000726
727 return $str;
728 }
729
730 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600731
Derek Jonese4ed5832009-02-20 21:44:59 +0000732 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000733 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000734 *
735 * Calls the individual driver for platform
736 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200737 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000738 * @access public
739 * @param string
740 * @return mixed
741 */
Barry Mienydd671972010-10-04 16:33:58 +0200742 function escape_like_str($str)
743 {
744 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000745 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000746
Derek Jonese4ed5832009-02-20 21:44:59 +0000747 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 /**
750 * Primary
751 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500752 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 * position is the primary key
754 *
755 * @access public
756 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200757 * @return string
758 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200760 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 if ( ! is_array($fields))
764 {
765 return FALSE;
766 }
767
768 return current($fields);
769 }
770
771 // --------------------------------------------------------------------
772
773 /**
774 * Returns an array of table names
775 *
776 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200777 * @return array
778 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 function list_tables($constrain_by_prefix = FALSE)
780 {
781 // Is there a cached result?
782 if (isset($this->data_cache['table_names']))
783 {
784 return $this->data_cache['table_names'];
785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
788 {
789 if ($this->db_debug)
790 {
791 return $this->display_error('db_unsupported_function');
792 }
793 return FALSE;
794 }
795
796 $retval = array();
797 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 if ($query->num_rows() > 0)
800 {
Taufan Aditya18209332012-02-09 16:07:27 +0700801 $table = FALSE;
802 $rows = $query->result_array();
803 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
804
805 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
Taufan Aditya18209332012-02-09 16:07:27 +0700807 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
808 }
809
810 foreach ($rows as $row)
811 {
812 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
814 }
815
816 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 return $this->data_cache['table_names'];
819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 // --------------------------------------------------------------------
822
823 /**
824 * Determine if a particular table exists
825 * @access public
826 * @return boolean
827 */
828 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200829 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 // --------------------------------------------------------------------
834
835 /**
836 * Fetch MySQL Field Names
837 *
838 * @access public
839 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200840 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 */
842 function list_fields($table = '')
843 {
844 // Is there a cached result?
845 if (isset($this->data_cache['field_names'][$table]))
846 {
847 return $this->data_cache['field_names'][$table];
848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 if ($table == '')
851 {
852 if ($this->db_debug)
853 {
854 return $this->display_error('db_field_param_missing');
855 }
856 return FALSE;
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Greg Aker1edde302010-01-26 00:17:01 +0000859 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
861 if ($this->db_debug)
862 {
863 return $this->display_error('db_unsupported_function');
864 }
865 return FALSE;
866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500871 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
873 if (isset($row['COLUMN_NAME']))
874 {
875 $retval[] = $row['COLUMN_NAME'];
876 }
877 else
878 {
879 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200880 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 $this->data_cache['field_names'][$table] = $retval;
884 return $this->data_cache['field_names'][$table];
885 }
886
887 // --------------------------------------------------------------------
888
889 /**
890 * Determine if a particular field exists
891 * @access public
892 * @param string
893 * @param string
894 * @return boolean
895 */
896 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200897 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // --------------------------------------------------------------------
902
903 /**
904 * Returns an object with field data
905 *
906 * @access public
907 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200908 * @return object
909 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 function field_data($table = '')
911 {
912 if ($table == '')
913 {
914 if ($this->db_debug)
915 {
916 return $this->display_error('db_field_param_missing');
917 }
918 return FALSE;
919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
922
923 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200924 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000925
926 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 /**
929 * Generate an insert string
930 *
931 * @access public
932 * @param string the table upon which the query will be performed
933 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200934 * @return string
935 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 function insert_string($table, $data)
937 {
938 $fields = array();
939 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200940
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500941 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 {
943 $fields[] = $this->_escape_identifiers($key);
944 $values[] = $this->escape($val);
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200948 }
949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 // --------------------------------------------------------------------
951
952 /**
953 * Generate an update string
954 *
955 * @access public
956 * @param string the table upon which the query will be performed
957 * @param array an associative array data of key/values
958 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200959 * @return string
960 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 function update_string($table, $data, $where)
962 {
963 if ($where == '')
964 {
965 return false;
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500969 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
971 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
972 }
973
974 if ( ! is_array($where))
975 {
976 $dest = array($where);
977 }
978 else
979 {
980 $dest = array();
981 foreach ($where as $key => $val)
982 {
983 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300984 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 if ($val !== '')
987 {
988 if ( ! $this->_has_operator($key))
989 {
990 $key .= ' =';
991 }
Barry Mienydd671972010-10-04 16:33:58 +0200992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 $val = ' '.$this->escape($val);
994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 $dest[] = $prefix.$key.$val;
997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000999
1000 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001001 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001002
1003 // --------------------------------------------------------------------
1004
1005 /**
1006 * Tests whether the string has an SQL operator
1007 *
1008 * @access private
1009 * @param string
1010 * @return bool
1011 */
1012 function _has_operator($str)
1013 {
1014 $str = trim($str);
1015 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1016 {
1017 return FALSE;
1018 }
1019
1020 return TRUE;
1021 }
1022
1023 // --------------------------------------------------------------------
1024
1025 /**
1026 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1027 *
1028 * @access public
1029 * @param string the function name
1030 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001031 * @return mixed
1032 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 function call_function($function)
1034 {
1035 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 if (FALSE === strpos($driver, $function))
1038 {
1039 $function = $driver.$function;
1040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 if ( ! function_exists($function))
1043 {
1044 if ($this->db_debug)
1045 {
1046 return $this->display_error('db_unsupported_function');
1047 }
1048 return FALSE;
1049 }
1050 else
1051 {
1052 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1053
Repox71b78092011-12-01 09:19:43 +01001054 if (is_null($args))
1055 {
1056 return call_user_func($function);
1057 }
1058 else
1059 {
1060 return call_user_func_array($function, $args);
1061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 }
1063 }
1064
1065 // --------------------------------------------------------------------
1066
1067 /**
1068 * Set Cache Directory Path
1069 *
1070 * @access public
1071 * @param string the path to the cache directory
1072 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001073 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 function cache_set_path($path = '')
1075 {
1076 $this->cachedir = $path;
1077 }
1078
1079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Enable Query Caching
1083 *
1084 * @access public
1085 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001086 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 function cache_on()
1088 {
1089 $this->cache_on = TRUE;
1090 return TRUE;
1091 }
1092
1093 // --------------------------------------------------------------------
1094
1095 /**
1096 * Disable Query Caching
1097 *
1098 * @access public
1099 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001100 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 function cache_off()
1102 {
1103 $this->cache_on = FALSE;
1104 return FALSE;
1105 }
Barry Mienydd671972010-10-04 16:33:58 +02001106
Derek Allard2067d1a2008-11-13 22:59:24 +00001107
1108 // --------------------------------------------------------------------
1109
1110 /**
1111 * Delete the cache files associated with a particular URI
1112 *
1113 * @access public
1114 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001115 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 function cache_delete($segment_one = '', $segment_two = '')
1117 {
1118 if ( ! $this->_cache_init())
1119 {
1120 return FALSE;
1121 }
1122 return $this->CACHE->delete($segment_one, $segment_two);
1123 }
1124
1125 // --------------------------------------------------------------------
1126
1127 /**
1128 * Delete All cache files
1129 *
1130 * @access public
1131 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001132 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 function cache_delete_all()
1134 {
1135 if ( ! $this->_cache_init())
1136 {
1137 return FALSE;
1138 }
1139
1140 return $this->CACHE->delete_all();
1141 }
1142
1143 // --------------------------------------------------------------------
1144
1145 /**
1146 * Initialize the Cache Class
1147 *
1148 * @access private
1149 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001150 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 function _cache_init()
1152 {
1153 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1154 {
1155 return TRUE;
1156 }
Derek Allarde37ab382009-02-03 16:13:57 +00001157
1158 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
Greg Aker3a746652011-04-19 10:59:47 -05001160 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001161 {
1162 return $this->cache_off();
1163 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
Derek Allarde37ab382009-02-03 16:13:57 +00001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1167 return TRUE;
1168 }
1169
1170 // --------------------------------------------------------------------
1171
1172 /**
1173 * Close DB Connection
1174 *
1175 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001176 * @return void
1177 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 function close()
1179 {
1180 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1181 {
1182 $this->_close($this->conn_id);
1183 }
1184 $this->conn_id = FALSE;
1185 }
Barry Mienydd671972010-10-04 16:33:58 +02001186
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 // --------------------------------------------------------------------
1188
1189 /**
1190 * Display an error message
1191 *
1192 * @access public
1193 * @param string the error message
1194 * @param string any "swap" values
1195 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001196 * @return string sends the application/error_db.php template
1197 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 function display_error($error = '', $swap = '', $native = FALSE)
1199 {
Derek Jonese7792202010-03-02 17:24:46 -06001200 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 $LANG->load('db');
1202
1203 $heading = $LANG->line('db_error_heading');
1204
1205 if ($native == TRUE)
1206 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001207 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 }
1209 else
1210 {
1211 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1212 }
Barry Mienydd671972010-10-04 16:33:58 +02001213
Pascal Kriete60f8c392010-08-25 18:03:28 +02001214 // Find the most likely culprit of the error by going through
1215 // the backtrace until the source file is no longer in the
1216 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001217
Pascal Kriete60f8c392010-08-25 18:03:28 +02001218 $trace = debug_backtrace();
1219
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001220 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001221 {
1222 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1223 {
1224 // Found it - use a relative path for safety
1225 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1226 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001227
Pascal Kriete60f8c392010-08-25 18:03:28 +02001228 break;
1229 }
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Jonese7792202010-03-02 17:24:46 -06001232 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 echo $error->show_error($heading, $message, 'error_db');
1234 exit;
1235 }
1236
1237 // --------------------------------------------------------------------
1238
1239 /**
1240 * Protect Identifiers
1241 *
1242 * This function adds backticks if appropriate based on db type
1243 *
1244 * @access private
1245 * @param mixed the item to escape
1246 * @return mixed the item with backticks
1247 */
1248 function protect_identifiers($item, $prefix_single = FALSE)
1249 {
1250 return $this->_protect_identifiers($item, $prefix_single);
1251 }
1252
1253 // --------------------------------------------------------------------
1254
1255 /**
1256 * Protect Identifiers
1257 *
1258 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001259 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001261 * the table prefix onto it. Some logic is necessary in order to deal with
1262 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 *
1264 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1265 *
1266 * Or a query with aliasing:
1267 *
1268 * SELECT m.member_id, m.member_name FROM members AS m
1269 *
1270 * Since the column name can include up to four segments (host, DB, table, column)
1271 * or also have an alias prefix, we need to do a bit of work to figure this out and
1272 * insert the table prefix (if it exists) in the proper position, and escape only
1273 * the correct identifiers.
1274 *
1275 * @access private
1276 * @param string
1277 * @param bool
1278 * @param mixed
1279 * @param bool
1280 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001281 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
1283 {
1284 if ( ! is_bool($protect_identifiers))
1285 {
1286 $protect_identifiers = $this->_protect_identifiers;
1287 }
Derek Allarde37ab382009-02-03 16:13:57 +00001288
1289 if (is_array($item))
1290 {
1291 $escaped_array = array();
1292
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001293 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001294 {
1295 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1296 }
1297
1298 return $escaped_array;
1299 }
1300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001302 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001303
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 // If the item has an alias declaration we remove it and set it aside.
1305 // Basically we remove everything to the right of the first space
1306 $alias = '';
1307 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001308 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 $alias = strstr($item, " ");
1310 $item = substr($item, 0, - strlen($alias));
1311 }
1312
Derek Allard911d3e02008-12-15 14:08:35 +00001313 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001314 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001315 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001316 // way to deal with this, but I'm not thinking of it -- Rick
1317 if (strpos($item, '(') !== FALSE)
1318 {
1319 return $item.$alias;
1320 }
1321
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 // Break the string apart if it contains periods, then insert the table prefix
1323 // in the correct location, assuming the period doesn't indicate that we're dealing
1324 // with an alias. While we're at it, we will escape the components
1325 if (strpos($item, '.') !== FALSE)
1326 {
1327 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001328
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001330 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 // we have nothing more to do other than escape the item
1332 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001333 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 if ($protect_identifiers === TRUE)
1335 {
1336 foreach ($parts as $key => $val)
1337 {
1338 if ( ! in_array($val, $this->_reserved_identifiers))
1339 {
1340 $parts[$key] = $this->_escape_identifiers($val);
1341 }
1342 }
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001345 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 return $item.$alias;
1347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Jones37f4b9c2011-07-01 17:56:50 -05001349 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 if ($this->dbprefix != '')
1351 {
1352 // We now add the table prefix based on some logic.
1353 // Do we have 4 segments (hostname.database.table.column)?
1354 // If so, we add the table prefix to the column name in the 3rd segment.
1355 if (isset($parts[3]))
1356 {
1357 $i = 2;
1358 }
1359 // Do we have 3 segments (database.table.column)?
1360 // If so, we add the table prefix to the column name in 2nd position
1361 elseif (isset($parts[2]))
1362 {
1363 $i = 1;
1364 }
1365 // Do we have 2 segments (table.column)?
1366 // If so, we add the table prefix to the column name in 1st segment
1367 else
1368 {
1369 $i = 0;
1370 }
Barry Mienydd671972010-10-04 16:33:58 +02001371
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 // This flag is set when the supplied $item does not contain a field name.
1373 // This can happen when this function is being called from a JOIN.
1374 if ($field_exists == FALSE)
1375 {
1376 $i++;
1377 }
Barry Mienydd671972010-10-04 16:33:58 +02001378
Derek Jones55acc8b2009-07-11 03:38:13 +00001379 // Verify table prefix and replace if necessary
1380 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1381 {
1382 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1383 }
Barry Mienydd671972010-10-04 16:33:58 +02001384
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 // We only add the table prefix if it does not already exist
1386 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1387 {
1388 $parts[$i] = $this->dbprefix.$parts[$i];
1389 }
Barry Mienydd671972010-10-04 16:33:58 +02001390
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 // Put the parts back together
1392 $item = implode('.', $parts);
1393 }
Barry Mienydd671972010-10-04 16:33:58 +02001394
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 if ($protect_identifiers === TRUE)
1396 {
1397 $item = $this->_escape_identifiers($item);
1398 }
Barry Mienydd671972010-10-04 16:33:58 +02001399
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 return $item.$alias;
1401 }
1402
Derek Jones37f4b9c2011-07-01 17:56:50 -05001403 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 if ($this->dbprefix != '')
1405 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001406 // Verify table prefix and replace if necessary
1407 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1408 {
1409 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1410 }
1411
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 // Do we prefix an item with no segments?
1413 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1414 {
1415 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001416 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 }
1418
1419 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1420 {
1421 $item = $this->_escape_identifiers($item);
1422 }
Barry Mienydd671972010-10-04 16:33:58 +02001423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 return $item.$alias;
1425 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001426}
1427
Derek Allard2067d1a2008-11-13 22:59:24 +00001428/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001429/* Location: ./system/database/DB_driver.php */