blob: 73ec4e3edd095f31d125b6095171df275f12d987 [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 Andreevdd2c41f2012-01-27 21:26:14 +0200244 $driver_version_exceptions = array('oci8', 'sqlite', 'sqlite3', '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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200648 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200649 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200650 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Andrey Andreev67f71a42012-03-01 16:18:42 +0200652 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|OPTIMIZE)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // --------------------------------------------------------------------
656
657 /**
658 * Calculate the aggregate query elapsed time
659 *
660 * @access public
661 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200662 * @return integer
663 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 function elapsed_time($decimals = 6)
665 {
666 return number_format($this->benchmark, $decimals);
667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 // --------------------------------------------------------------------
670
671 /**
672 * Returns the total number of queries
673 *
674 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200675 * @return integer
676 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 function total_queries()
678 {
679 return $this->query_count;
680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // --------------------------------------------------------------------
683
684 /**
685 * Returns the last query that was executed
686 *
687 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200688 * @return void
689 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 function last_query()
691 {
692 return end($this->queries);
693 }
694
695 // --------------------------------------------------------------------
696
697 /**
698 * "Smart" Escape String
699 *
700 * Escapes data based on type
701 * Sets boolean and null types
702 *
703 * @access public
704 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200705 * @return mixed
706 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200708 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000709 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000711 $str = "'".$this->escape_str($str)."'";
712 }
713 elseif (is_bool($str))
714 {
715 $str = ($str === FALSE) ? 0 : 1;
716 }
717 elseif (is_null($str))
718 {
719 $str = 'NULL';
720 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000721
722 return $str;
723 }
724
725 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600726
Derek Jonese4ed5832009-02-20 21:44:59 +0000727 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000728 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000729 *
730 * Calls the individual driver for platform
731 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200732 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000733 * @access public
734 * @param string
735 * @return mixed
736 */
Barry Mienydd671972010-10-04 16:33:58 +0200737 function escape_like_str($str)
738 {
739 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000740 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000741
Derek Jonese4ed5832009-02-20 21:44:59 +0000742 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 /**
745 * Primary
746 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500747 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 * position is the primary key
749 *
750 * @access public
751 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200752 * @return string
753 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200755 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 if ( ! is_array($fields))
759 {
760 return FALSE;
761 }
762
763 return current($fields);
764 }
765
766 // --------------------------------------------------------------------
767
768 /**
769 * Returns an array of table names
770 *
771 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200772 * @return array
773 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 function list_tables($constrain_by_prefix = FALSE)
775 {
776 // Is there a cached result?
777 if (isset($this->data_cache['table_names']))
778 {
779 return $this->data_cache['table_names'];
780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
783 {
784 if ($this->db_debug)
785 {
786 return $this->display_error('db_unsupported_function');
787 }
788 return FALSE;
789 }
790
791 $retval = array();
792 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 if ($query->num_rows() > 0)
795 {
Taufan Aditya18209332012-02-09 16:07:27 +0700796 $table = FALSE;
797 $rows = $query->result_array();
798 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
799
800 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Taufan Aditya18209332012-02-09 16:07:27 +0700802 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
803 }
804
805 foreach ($rows as $row)
806 {
807 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
809 }
810
811 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 return $this->data_cache['table_names'];
814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 // --------------------------------------------------------------------
817
818 /**
819 * Determine if a particular table exists
820 * @access public
821 * @return boolean
822 */
823 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200824 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 // --------------------------------------------------------------------
829
830 /**
831 * Fetch MySQL Field Names
832 *
833 * @access public
834 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200835 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 */
837 function list_fields($table = '')
838 {
839 // Is there a cached result?
840 if (isset($this->data_cache['field_names'][$table]))
841 {
842 return $this->data_cache['field_names'][$table];
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 if ($table == '')
846 {
847 if ($this->db_debug)
848 {
849 return $this->display_error('db_field_param_missing');
850 }
851 return FALSE;
852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Greg Aker1edde302010-01-26 00:17:01 +0000854 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 {
856 if ($this->db_debug)
857 {
858 return $this->display_error('db_unsupported_function');
859 }
860 return FALSE;
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500866 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
868 if (isset($row['COLUMN_NAME']))
869 {
870 $retval[] = $row['COLUMN_NAME'];
871 }
872 else
873 {
874 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200875 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 $this->data_cache['field_names'][$table] = $retval;
879 return $this->data_cache['field_names'][$table];
880 }
881
882 // --------------------------------------------------------------------
883
884 /**
885 * Determine if a particular field exists
886 * @access public
887 * @param string
888 * @param string
889 * @return boolean
890 */
891 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200892 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 // --------------------------------------------------------------------
897
898 /**
899 * Returns an object with field data
900 *
901 * @access public
902 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200903 * @return object
904 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 function field_data($table = '')
906 {
907 if ($table == '')
908 {
909 if ($this->db_debug)
910 {
911 return $this->display_error('db_field_param_missing');
912 }
913 return FALSE;
914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
917
918 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200919 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000920
921 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 /**
924 * Generate an insert string
925 *
926 * @access public
927 * @param string the table upon which the query will be performed
928 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200929 * @return string
930 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 function insert_string($table, $data)
932 {
933 $fields = array();
934 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200935
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500936 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
938 $fields[] = $this->_escape_identifiers($key);
939 $values[] = $this->escape($val);
940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200943 }
944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 // --------------------------------------------------------------------
946
947 /**
948 * Generate an update string
949 *
950 * @access public
951 * @param string the table upon which the query will be performed
952 * @param array an associative array data of key/values
953 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200954 * @return string
955 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 function update_string($table, $data, $where)
957 {
958 if ($where == '')
959 {
960 return false;
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500964 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
966 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
967 }
968
969 if ( ! is_array($where))
970 {
971 $dest = array($where);
972 }
973 else
974 {
975 $dest = array();
976 foreach ($where as $key => $val)
977 {
978 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300979 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 if ($val !== '')
982 {
983 if ( ! $this->_has_operator($key))
984 {
985 $key .= ' =';
986 }
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 $val = ' '.$this->escape($val);
989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 $dest[] = $prefix.$key.$val;
992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000994
995 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200996 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000997
998 // --------------------------------------------------------------------
999
1000 /**
1001 * Tests whether the string has an SQL operator
1002 *
1003 * @access private
1004 * @param string
1005 * @return bool
1006 */
1007 function _has_operator($str)
1008 {
1009 $str = trim($str);
1010 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1011 {
1012 return FALSE;
1013 }
1014
1015 return TRUE;
1016 }
1017
1018 // --------------------------------------------------------------------
1019
1020 /**
1021 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1022 *
1023 * @access public
1024 * @param string the function name
1025 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001026 * @return mixed
1027 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 function call_function($function)
1029 {
1030 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 if (FALSE === strpos($driver, $function))
1033 {
1034 $function = $driver.$function;
1035 }
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 if ( ! function_exists($function))
1038 {
1039 if ($this->db_debug)
1040 {
1041 return $this->display_error('db_unsupported_function');
1042 }
1043 return FALSE;
1044 }
1045 else
1046 {
1047 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1048
Repox71b78092011-12-01 09:19:43 +01001049 if (is_null($args))
1050 {
1051 return call_user_func($function);
1052 }
1053 else
1054 {
1055 return call_user_func_array($function, $args);
1056 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 }
1058 }
1059
1060 // --------------------------------------------------------------------
1061
1062 /**
1063 * Set Cache Directory Path
1064 *
1065 * @access public
1066 * @param string the path to the cache directory
1067 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001068 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 function cache_set_path($path = '')
1070 {
1071 $this->cachedir = $path;
1072 }
1073
1074 // --------------------------------------------------------------------
1075
1076 /**
1077 * Enable Query Caching
1078 *
1079 * @access public
1080 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001081 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 function cache_on()
1083 {
1084 $this->cache_on = TRUE;
1085 return TRUE;
1086 }
1087
1088 // --------------------------------------------------------------------
1089
1090 /**
1091 * Disable Query Caching
1092 *
1093 * @access public
1094 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001095 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 function cache_off()
1097 {
1098 $this->cache_on = FALSE;
1099 return FALSE;
1100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102
1103 // --------------------------------------------------------------------
1104
1105 /**
1106 * Delete the cache files associated with a particular URI
1107 *
1108 * @access public
1109 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001110 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 function cache_delete($segment_one = '', $segment_two = '')
1112 {
1113 if ( ! $this->_cache_init())
1114 {
1115 return FALSE;
1116 }
1117 return $this->CACHE->delete($segment_one, $segment_two);
1118 }
1119
1120 // --------------------------------------------------------------------
1121
1122 /**
1123 * Delete All cache files
1124 *
1125 * @access public
1126 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001127 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 function cache_delete_all()
1129 {
1130 if ( ! $this->_cache_init())
1131 {
1132 return FALSE;
1133 }
1134
1135 return $this->CACHE->delete_all();
1136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
1141 * Initialize the Cache Class
1142 *
1143 * @access private
1144 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001145 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 function _cache_init()
1147 {
1148 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1149 {
1150 return TRUE;
1151 }
Derek Allarde37ab382009-02-03 16:13:57 +00001152
1153 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Greg Aker3a746652011-04-19 10:59:47 -05001155 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001156 {
1157 return $this->cache_off();
1158 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 }
Derek Allarde37ab382009-02-03 16:13:57 +00001160
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1162 return TRUE;
1163 }
1164
1165 // --------------------------------------------------------------------
1166
1167 /**
1168 * Close DB Connection
1169 *
1170 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001171 * @return void
1172 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 function close()
1174 {
1175 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1176 {
1177 $this->_close($this->conn_id);
1178 }
1179 $this->conn_id = FALSE;
1180 }
Barry Mienydd671972010-10-04 16:33:58 +02001181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 // --------------------------------------------------------------------
1183
1184 /**
1185 * Display an error message
1186 *
1187 * @access public
1188 * @param string the error message
1189 * @param string any "swap" values
1190 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001191 * @return string sends the application/error_db.php template
1192 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 function display_error($error = '', $swap = '', $native = FALSE)
1194 {
Derek Jonese7792202010-03-02 17:24:46 -06001195 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 $LANG->load('db');
1197
1198 $heading = $LANG->line('db_error_heading');
1199
1200 if ($native == TRUE)
1201 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001202 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 }
1204 else
1205 {
1206 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Pascal Kriete60f8c392010-08-25 18:03:28 +02001209 // Find the most likely culprit of the error by going through
1210 // the backtrace until the source file is no longer in the
1211 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001212
Pascal Kriete60f8c392010-08-25 18:03:28 +02001213 $trace = debug_backtrace();
1214
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001215 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001216 {
1217 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1218 {
1219 // Found it - use a relative path for safety
1220 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1221 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001222
Pascal Kriete60f8c392010-08-25 18:03:28 +02001223 break;
1224 }
1225 }
Barry Mienydd671972010-10-04 16:33:58 +02001226
Derek Jonese7792202010-03-02 17:24:46 -06001227 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 echo $error->show_error($heading, $message, 'error_db');
1229 exit;
1230 }
1231
1232 // --------------------------------------------------------------------
1233
1234 /**
1235 * Protect Identifiers
1236 *
1237 * This function adds backticks if appropriate based on db type
1238 *
1239 * @access private
1240 * @param mixed the item to escape
1241 * @return mixed the item with backticks
1242 */
1243 function protect_identifiers($item, $prefix_single = FALSE)
1244 {
1245 return $this->_protect_identifiers($item, $prefix_single);
1246 }
1247
1248 // --------------------------------------------------------------------
1249
1250 /**
1251 * Protect Identifiers
1252 *
1253 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001254 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001256 * the table prefix onto it. Some logic is necessary in order to deal with
1257 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 *
1259 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1260 *
1261 * Or a query with aliasing:
1262 *
1263 * SELECT m.member_id, m.member_name FROM members AS m
1264 *
1265 * Since the column name can include up to four segments (host, DB, table, column)
1266 * or also have an alias prefix, we need to do a bit of work to figure this out and
1267 * insert the table prefix (if it exists) in the proper position, and escape only
1268 * the correct identifiers.
1269 *
1270 * @access private
1271 * @param string
1272 * @param bool
1273 * @param mixed
1274 * @param bool
1275 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001276 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
1278 {
1279 if ( ! is_bool($protect_identifiers))
1280 {
1281 $protect_identifiers = $this->_protect_identifiers;
1282 }
Derek Allarde37ab382009-02-03 16:13:57 +00001283
1284 if (is_array($item))
1285 {
1286 $escaped_array = array();
1287
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001288 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001289 {
1290 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1291 }
1292
1293 return $escaped_array;
1294 }
1295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001297 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 // If the item has an alias declaration we remove it and set it aside.
1300 // Basically we remove everything to the right of the first space
1301 $alias = '';
1302 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001303 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 $alias = strstr($item, " ");
1305 $item = substr($item, 0, - strlen($alias));
1306 }
1307
Derek Allard911d3e02008-12-15 14:08:35 +00001308 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001309 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001310 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001311 // way to deal with this, but I'm not thinking of it -- Rick
1312 if (strpos($item, '(') !== FALSE)
1313 {
1314 return $item.$alias;
1315 }
1316
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 // Break the string apart if it contains periods, then insert the table prefix
1318 // in the correct location, assuming the period doesn't indicate that we're dealing
1319 // with an alias. While we're at it, we will escape the components
1320 if (strpos($item, '.') !== FALSE)
1321 {
1322 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001325 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 // we have nothing more to do other than escape the item
1327 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001328 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 if ($protect_identifiers === TRUE)
1330 {
1331 foreach ($parts as $key => $val)
1332 {
1333 if ( ! in_array($val, $this->_reserved_identifiers))
1334 {
1335 $parts[$key] = $this->_escape_identifiers($val);
1336 }
1337 }
Barry Mienydd671972010-10-04 16:33:58 +02001338
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001340 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 return $item.$alias;
1342 }
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Jones37f4b9c2011-07-01 17:56:50 -05001344 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 if ($this->dbprefix != '')
1346 {
1347 // We now add the table prefix based on some logic.
1348 // Do we have 4 segments (hostname.database.table.column)?
1349 // If so, we add the table prefix to the column name in the 3rd segment.
1350 if (isset($parts[3]))
1351 {
1352 $i = 2;
1353 }
1354 // Do we have 3 segments (database.table.column)?
1355 // If so, we add the table prefix to the column name in 2nd position
1356 elseif (isset($parts[2]))
1357 {
1358 $i = 1;
1359 }
1360 // Do we have 2 segments (table.column)?
1361 // If so, we add the table prefix to the column name in 1st segment
1362 else
1363 {
1364 $i = 0;
1365 }
Barry Mienydd671972010-10-04 16:33:58 +02001366
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 // This flag is set when the supplied $item does not contain a field name.
1368 // This can happen when this function is being called from a JOIN.
1369 if ($field_exists == FALSE)
1370 {
1371 $i++;
1372 }
Barry Mienydd671972010-10-04 16:33:58 +02001373
Derek Jones55acc8b2009-07-11 03:38:13 +00001374 // Verify table prefix and replace if necessary
1375 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1376 {
1377 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1378 }
Barry Mienydd671972010-10-04 16:33:58 +02001379
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 // We only add the table prefix if it does not already exist
1381 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1382 {
1383 $parts[$i] = $this->dbprefix.$parts[$i];
1384 }
Barry Mienydd671972010-10-04 16:33:58 +02001385
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 // Put the parts back together
1387 $item = implode('.', $parts);
1388 }
Barry Mienydd671972010-10-04 16:33:58 +02001389
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 if ($protect_identifiers === TRUE)
1391 {
1392 $item = $this->_escape_identifiers($item);
1393 }
Barry Mienydd671972010-10-04 16:33:58 +02001394
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 return $item.$alias;
1396 }
1397
Derek Jones37f4b9c2011-07-01 17:56:50 -05001398 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 if ($this->dbprefix != '')
1400 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001401 // Verify table prefix and replace if necessary
1402 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1403 {
1404 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1405 }
1406
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 // Do we prefix an item with no segments?
1408 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1409 {
1410 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001411 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 }
1413
1414 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1415 {
1416 $item = $this->_escape_identifiers($item);
1417 }
Barry Mienydd671972010-10-04 16:33:58 +02001418
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 return $item.$alias;
1420 }
Túbal Martín511f2252011-11-24 14:43:45 +01001421
1422 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001423
Túbal Martín511f2252011-11-24 14:43:45 +01001424 /**
1425 * Dummy method that allows Active Record class to be disabled
1426 *
1427 * This function is used extensively by every db driver.
1428 *
1429 * @access private
1430 * @return void
1431 */
1432 protected function _reset_select()
1433 {
1434
1435 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001436
1437}
1438
Derek Allard2067d1a2008-11-13 22:59:24 +00001439/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001440/* Location: ./system/database/DB_driver.php */