blob: 03a222f9b137ae0d4ef21f1293b2f523fc76f8a9 [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
Andrey Andreev4be5de12012-03-02 15:45:41 +0200333 // Grab the error now, as we might run some additional queries before displaying the error
334 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200335
336 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200337 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 if ($this->db_debug)
340 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200342 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200343 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // transactions to remain in limbo.
345 $this->trans_complete();
346
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200347 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 return $this->display_error(
Andrey Andreev4be5de12012-03-02 15:45:41 +0200349 array(
350 'Error Number: '.$error['code'],
351 $error['message'],
352 $sql
353 )
354 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 return FALSE;
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // Stop and aggregate the query time results
361 $time_end = list($em, $es) = explode(' ', microtime());
362 $this->benchmark += ($em + $es) - ($sm + $ss);
363
364 if ($this->save_queries == TRUE)
365 {
366 $this->query_times[] = ($em + $es) - ($sm + $ss);
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // Increment the query counter
370 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // Was the query a "write" type?
373 // If so we'll simply return true
374 if ($this->is_write_type($sql) === TRUE)
375 {
376 // If caching is enabled we'll auto-cleanup any
377 // existing files related to this particular URI
378 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
379 {
380 $this->CACHE->delete();
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 return TRUE;
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // Return TRUE if we don't need to create a result object
387 // Currently only the Oracle driver uses this when stored
388 // procedures are used
389 if ($return_object !== TRUE)
390 {
391 return TRUE;
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
394 // Load and instantiate the result driver
395
396 $driver = $this->load_rdriver();
397 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 $RES->conn_id = $this->conn_id;
399 $RES->result_id = $this->result_id;
400
401 if ($this->dbdriver == 'oci8')
402 {
403 $RES->stmt_id = $this->stmt_id;
404 $RES->curs_id = NULL;
405 $RES->limit_used = $this->limit_used;
406 $this->stmt_id = FALSE;
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // oci8 vars must be set before calling this
410 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Jones37f4b9c2011-07-01 17:56:50 -0500412 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // result object and save it to a cache file.
414 if ($this->cache_on == TRUE AND $this->_cache_init())
415 {
416 // We'll create a new instance of the result object
417 // only without the platform specific driver since
418 // we can't use it with cached data (the query result
419 // resource ID won't be any good once we've cached the
420 // result object, so we'll have to compile the data
421 // and save it)
422 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200423 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 $CR->result_object = $RES->result_object();
425 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // Reset these since cached objects can not utilize resource IDs.
428 $CR->conn_id = NULL;
429 $CR->result_id = NULL;
430
431 $this->CACHE->write($sql, $CR);
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 return $RES;
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Load the result drivers
441 *
442 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200443 * @return string the name of the result class
444 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 function load_rdriver()
446 {
447 $driver = 'CI_DB_'.$this->dbdriver.'_result';
448
449 if ( ! class_exists($driver))
450 {
Greg Aker3a746652011-04-19 10:59:47 -0500451 include_once(BASEPATH.'database/DB_result.php');
452 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 return $driver;
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // --------------------------------------------------------------------
459
460 /**
461 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500462 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * we only use it when running transaction commands since they do
464 * not require all the features of the main query() function.
465 *
466 * @access public
467 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200468 * @return mixed
469 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 function simple_query($sql)
471 {
472 if ( ! $this->conn_id)
473 {
474 $this->initialize();
475 }
476
477 return $this->_execute($sql);
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 /**
483 * Disable Transactions
484 * This permits transactions to be disabled at run-time.
485 *
486 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200487 * @return void
488 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 function trans_off()
490 {
491 $this->trans_enabled = FALSE;
492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * Enable/disable Transaction Strict Mode
498 * When strict mode is enabled, if you are running multiple groups of
499 * transactions, if one group fails all groups will be rolled back.
500 * If strict mode is disabled, each group is treated autonomously, meaning
501 * a failure of one group will not affect any others
502 *
503 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200504 * @return void
505 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 function trans_strict($mode = TRUE)
507 {
508 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 // --------------------------------------------------------------------
512
513 /**
514 * Start Transaction
515 *
516 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200517 * @return void
518 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200520 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 if ( ! $this->trans_enabled)
522 {
523 return FALSE;
524 }
525
526 // When transactions are nested we only begin/commit/rollback the outermost ones
527 if ($this->_trans_depth > 0)
528 {
529 $this->_trans_depth += 1;
530 return;
531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500534 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Complete Transaction
541 *
542 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200543 * @return bool
544 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 function trans_complete()
546 {
547 if ( ! $this->trans_enabled)
548 {
549 return FALSE;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // When transactions are nested we only begin/commit/rollback the outermost ones
553 if ($this->_trans_depth > 1)
554 {
555 $this->_trans_depth -= 1;
556 return TRUE;
557 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500558 else
559 {
560 $this->_trans_depth = 0;
561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // The query() function will set this flag to FALSE in the event that a query failed
564 if ($this->_trans_status === FALSE)
565 {
566 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 // If we are NOT running in strict mode, we will reset
569 // the _trans_status flag so that subsequent groups of transactions
570 // will be permitted.
571 if ($this->trans_strict === FALSE)
572 {
573 $this->_trans_status = TRUE;
574 }
575
576 log_message('debug', 'DB Transaction Failure');
577 return FALSE;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 $this->trans_commit();
581 return TRUE;
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
587 * Lets you retrieve the transaction flag to determine if it has failed
588 *
589 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200590 * @return bool
591 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 function trans_status()
593 {
594 return $this->_trans_status;
595 }
596
597 // --------------------------------------------------------------------
598
599 /**
600 * Compile Bindings
601 *
602 * @access public
603 * @param string the sql statement
604 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200605 * @return string
606 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 function compile_binds($sql, $binds)
608 {
609 if (strpos($sql, $this->bind_marker) === FALSE)
610 {
611 return $sql;
612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 if ( ! is_array($binds))
615 {
616 $binds = array($binds);
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // Get the sql segments around the bind markers
620 $segments = explode($this->bind_marker, $sql);
621
622 // The count of bind should be 1 less then the count of segments
623 // If there are more bind arguments trim it down
624 if (count($binds) >= count($segments)) {
625 $binds = array_slice($binds, 0, count($segments)-1);
626 }
627
628 // Construct the binded query
629 $result = $segments[0];
630 $i = 0;
631 foreach ($binds as $bind)
632 {
633 $result .= $this->escape($bind);
634 $result .= $segments[++$i];
635 }
636
637 return $result;
638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 // --------------------------------------------------------------------
641
642 /**
643 * Determines if a query is a "write" type.
644 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200646 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200647 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200648 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
Andrey Andreev67f71a42012-03-01 16:18:42 +0200650 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 +0000651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
656 * Calculate the aggregate query elapsed time
657 *
658 * @access public
659 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200660 * @return integer
661 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 function elapsed_time($decimals = 6)
663 {
664 return number_format($this->benchmark, $decimals);
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Returns the total number of queries
671 *
672 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200673 * @return integer
674 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 function total_queries()
676 {
677 return $this->query_count;
678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // --------------------------------------------------------------------
681
682 /**
683 * Returns the last query that was executed
684 *
685 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200686 * @return void
687 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 function last_query()
689 {
690 return end($this->queries);
691 }
692
693 // --------------------------------------------------------------------
694
695 /**
696 * "Smart" Escape String
697 *
698 * Escapes data based on type
699 * Sets boolean and null types
700 *
701 * @access public
702 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200703 * @return mixed
704 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200706 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000707 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000709 $str = "'".$this->escape_str($str)."'";
710 }
711 elseif (is_bool($str))
712 {
713 $str = ($str === FALSE) ? 0 : 1;
714 }
715 elseif (is_null($str))
716 {
717 $str = 'NULL';
718 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000719
720 return $str;
721 }
722
723 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600724
Derek Jonese4ed5832009-02-20 21:44:59 +0000725 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000726 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000727 *
728 * Calls the individual driver for platform
729 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200730 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000731 * @access public
732 * @param string
733 * @return mixed
734 */
Barry Mienydd671972010-10-04 16:33:58 +0200735 function escape_like_str($str)
736 {
737 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000738 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000739
Derek Jonese4ed5832009-02-20 21:44:59 +0000740 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 /**
743 * Primary
744 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500745 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 * position is the primary key
747 *
748 * @access public
749 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200750 * @return string
751 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200753 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 if ( ! is_array($fields))
757 {
758 return FALSE;
759 }
760
761 return current($fields);
762 }
763
764 // --------------------------------------------------------------------
765
766 /**
767 * Returns an array of table names
768 *
769 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200770 * @return array
771 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 function list_tables($constrain_by_prefix = FALSE)
773 {
774 // Is there a cached result?
775 if (isset($this->data_cache['table_names']))
776 {
777 return $this->data_cache['table_names'];
778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
781 {
782 if ($this->db_debug)
783 {
784 return $this->display_error('db_unsupported_function');
785 }
786 return FALSE;
787 }
788
789 $retval = array();
790 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 if ($query->num_rows() > 0)
793 {
Taufan Aditya18209332012-02-09 16:07:27 +0700794 $table = FALSE;
795 $rows = $query->result_array();
796 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
797
798 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Taufan Aditya18209332012-02-09 16:07:27 +0700800 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
801 }
802
803 foreach ($rows as $row)
804 {
805 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 }
807 }
808
809 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 return $this->data_cache['table_names'];
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 // --------------------------------------------------------------------
815
816 /**
817 * Determine if a particular table exists
818 * @access public
819 * @return boolean
820 */
821 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200822 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 // --------------------------------------------------------------------
827
828 /**
829 * Fetch MySQL Field Names
830 *
831 * @access public
832 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200833 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 */
835 function list_fields($table = '')
836 {
837 // Is there a cached result?
838 if (isset($this->data_cache['field_names'][$table]))
839 {
840 return $this->data_cache['field_names'][$table];
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 if ($table == '')
844 {
845 if ($this->db_debug)
846 {
847 return $this->display_error('db_field_param_missing');
848 }
849 return FALSE;
850 }
Barry Mienydd671972010-10-04 16:33:58 +0200851
Greg Aker1edde302010-01-26 00:17:01 +0000852 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 if ($this->db_debug)
855 {
856 return $this->display_error('db_unsupported_function');
857 }
858 return FALSE;
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500864 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
866 if (isset($row['COLUMN_NAME']))
867 {
868 $retval[] = $row['COLUMN_NAME'];
869 }
870 else
871 {
872 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200873 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 $this->data_cache['field_names'][$table] = $retval;
877 return $this->data_cache['field_names'][$table];
878 }
879
880 // --------------------------------------------------------------------
881
882 /**
883 * Determine if a particular field exists
884 * @access public
885 * @param string
886 * @param string
887 * @return boolean
888 */
889 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200890 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 // --------------------------------------------------------------------
895
896 /**
897 * Returns an object with field data
898 *
899 * @access public
900 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200901 * @return object
902 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 function field_data($table = '')
904 {
905 if ($table == '')
906 {
907 if ($this->db_debug)
908 {
909 return $this->display_error('db_field_param_missing');
910 }
911 return FALSE;
912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
915
916 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200917 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000918
919 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 /**
922 * Generate an insert string
923 *
924 * @access public
925 * @param string the table upon which the query will be performed
926 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200927 * @return string
928 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 function insert_string($table, $data)
930 {
931 $fields = array();
932 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200933
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500934 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
936 $fields[] = $this->_escape_identifiers($key);
937 $values[] = $this->escape($val);
938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200941 }
942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 // --------------------------------------------------------------------
944
945 /**
946 * Generate an update string
947 *
948 * @access public
949 * @param string the table upon which the query will be performed
950 * @param array an associative array data of key/values
951 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200952 * @return string
953 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 function update_string($table, $data, $where)
955 {
956 if ($where == '')
957 {
958 return false;
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500962 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
964 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
965 }
966
967 if ( ! is_array($where))
968 {
969 $dest = array($where);
970 }
971 else
972 {
973 $dest = array();
974 foreach ($where as $key => $val)
975 {
976 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300977 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200978
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 if ($val !== '')
980 {
981 if ( ! $this->_has_operator($key))
982 {
983 $key .= ' =';
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 $val = ' '.$this->escape($val);
987 }
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 $dest[] = $prefix.$key.$val;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000992
993 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200994 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000995
996 // --------------------------------------------------------------------
997
998 /**
999 * Tests whether the string has an SQL operator
1000 *
1001 * @access private
1002 * @param string
1003 * @return bool
1004 */
1005 function _has_operator($str)
1006 {
1007 $str = trim($str);
1008 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1009 {
1010 return FALSE;
1011 }
1012
1013 return TRUE;
1014 }
1015
1016 // --------------------------------------------------------------------
1017
1018 /**
1019 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1020 *
1021 * @access public
1022 * @param string the function name
1023 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001024 * @return mixed
1025 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 function call_function($function)
1027 {
1028 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 if (FALSE === strpos($driver, $function))
1031 {
1032 $function = $driver.$function;
1033 }
Barry Mienydd671972010-10-04 16:33:58 +02001034
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 if ( ! function_exists($function))
1036 {
1037 if ($this->db_debug)
1038 {
1039 return $this->display_error('db_unsupported_function');
1040 }
1041 return FALSE;
1042 }
1043 else
1044 {
1045 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1046
Repox71b78092011-12-01 09:19:43 +01001047 if (is_null($args))
1048 {
1049 return call_user_func($function);
1050 }
1051 else
1052 {
1053 return call_user_func_array($function, $args);
1054 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 }
1056 }
1057
1058 // --------------------------------------------------------------------
1059
1060 /**
1061 * Set Cache Directory Path
1062 *
1063 * @access public
1064 * @param string the path to the cache directory
1065 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001066 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 function cache_set_path($path = '')
1068 {
1069 $this->cachedir = $path;
1070 }
1071
1072 // --------------------------------------------------------------------
1073
1074 /**
1075 * Enable Query Caching
1076 *
1077 * @access public
1078 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001079 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 function cache_on()
1081 {
1082 $this->cache_on = TRUE;
1083 return TRUE;
1084 }
1085
1086 // --------------------------------------------------------------------
1087
1088 /**
1089 * Disable Query Caching
1090 *
1091 * @access public
1092 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001093 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 function cache_off()
1095 {
1096 $this->cache_on = FALSE;
1097 return FALSE;
1098 }
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100
1101 // --------------------------------------------------------------------
1102
1103 /**
1104 * Delete the cache files associated with a particular URI
1105 *
1106 * @access public
1107 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001108 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 function cache_delete($segment_one = '', $segment_two = '')
1110 {
1111 if ( ! $this->_cache_init())
1112 {
1113 return FALSE;
1114 }
1115 return $this->CACHE->delete($segment_one, $segment_two);
1116 }
1117
1118 // --------------------------------------------------------------------
1119
1120 /**
1121 * Delete All cache files
1122 *
1123 * @access public
1124 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001125 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 function cache_delete_all()
1127 {
1128 if ( ! $this->_cache_init())
1129 {
1130 return FALSE;
1131 }
1132
1133 return $this->CACHE->delete_all();
1134 }
1135
1136 // --------------------------------------------------------------------
1137
1138 /**
1139 * Initialize the Cache Class
1140 *
1141 * @access private
1142 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001143 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 function _cache_init()
1145 {
1146 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1147 {
1148 return TRUE;
1149 }
Derek Allarde37ab382009-02-03 16:13:57 +00001150
1151 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
Greg Aker3a746652011-04-19 10:59:47 -05001153 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001154 {
1155 return $this->cache_off();
1156 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 }
Derek Allarde37ab382009-02-03 16:13:57 +00001158
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1160 return TRUE;
1161 }
1162
1163 // --------------------------------------------------------------------
1164
1165 /**
1166 * Close DB Connection
1167 *
1168 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001169 * @return void
1170 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 function close()
1172 {
1173 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1174 {
1175 $this->_close($this->conn_id);
1176 }
1177 $this->conn_id = FALSE;
1178 }
Barry Mienydd671972010-10-04 16:33:58 +02001179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 // --------------------------------------------------------------------
1181
1182 /**
1183 * Display an error message
1184 *
1185 * @access public
1186 * @param string the error message
1187 * @param string any "swap" values
1188 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001189 * @return string sends the application/error_db.php template
1190 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 function display_error($error = '', $swap = '', $native = FALSE)
1192 {
Derek Jonese7792202010-03-02 17:24:46 -06001193 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 $LANG->load('db');
1195
1196 $heading = $LANG->line('db_error_heading');
1197
1198 if ($native == TRUE)
1199 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001200 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 }
1202 else
1203 {
1204 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Pascal Kriete60f8c392010-08-25 18:03:28 +02001207 // Find the most likely culprit of the error by going through
1208 // the backtrace until the source file is no longer in the
1209 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001210
Pascal Kriete60f8c392010-08-25 18:03:28 +02001211 $trace = debug_backtrace();
1212
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001213 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001214 {
1215 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1216 {
1217 // Found it - use a relative path for safety
1218 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1219 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001220
Pascal Kriete60f8c392010-08-25 18:03:28 +02001221 break;
1222 }
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Jonese7792202010-03-02 17:24:46 -06001225 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 echo $error->show_error($heading, $message, 'error_db');
1227 exit;
1228 }
1229
1230 // --------------------------------------------------------------------
1231
1232 /**
1233 * Protect Identifiers
1234 *
1235 * This function adds backticks if appropriate based on db type
1236 *
1237 * @access private
1238 * @param mixed the item to escape
1239 * @return mixed the item with backticks
1240 */
1241 function protect_identifiers($item, $prefix_single = FALSE)
1242 {
1243 return $this->_protect_identifiers($item, $prefix_single);
1244 }
1245
1246 // --------------------------------------------------------------------
1247
1248 /**
1249 * Protect Identifiers
1250 *
1251 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001252 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001254 * the table prefix onto it. Some logic is necessary in order to deal with
1255 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 *
1257 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1258 *
1259 * Or a query with aliasing:
1260 *
1261 * SELECT m.member_id, m.member_name FROM members AS m
1262 *
1263 * Since the column name can include up to four segments (host, DB, table, column)
1264 * or also have an alias prefix, we need to do a bit of work to figure this out and
1265 * insert the table prefix (if it exists) in the proper position, and escape only
1266 * the correct identifiers.
1267 *
1268 * @access private
1269 * @param string
1270 * @param bool
1271 * @param mixed
1272 * @param bool
1273 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001274 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
1276 {
1277 if ( ! is_bool($protect_identifiers))
1278 {
1279 $protect_identifiers = $this->_protect_identifiers;
1280 }
Derek Allarde37ab382009-02-03 16:13:57 +00001281
1282 if (is_array($item))
1283 {
1284 $escaped_array = array();
1285
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001286 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001287 {
1288 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1289 }
1290
1291 return $escaped_array;
1292 }
1293
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001295 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 // If the item has an alias declaration we remove it and set it aside.
1298 // Basically we remove everything to the right of the first space
1299 $alias = '';
1300 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001301 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 $alias = strstr($item, " ");
1303 $item = substr($item, 0, - strlen($alias));
1304 }
1305
Derek Allard911d3e02008-12-15 14:08:35 +00001306 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001307 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001308 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001309 // way to deal with this, but I'm not thinking of it -- Rick
1310 if (strpos($item, '(') !== FALSE)
1311 {
1312 return $item.$alias;
1313 }
1314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 // Break the string apart if it contains periods, then insert the table prefix
1316 // in the correct location, assuming the period doesn't indicate that we're dealing
1317 // with an alias. While we're at it, we will escape the components
1318 if (strpos($item, '.') !== FALSE)
1319 {
1320 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001321
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001323 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 // we have nothing more to do other than escape the item
1325 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001326 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 if ($protect_identifiers === TRUE)
1328 {
1329 foreach ($parts as $key => $val)
1330 {
1331 if ( ! in_array($val, $this->_reserved_identifiers))
1332 {
1333 $parts[$key] = $this->_escape_identifiers($val);
1334 }
1335 }
Barry Mienydd671972010-10-04 16:33:58 +02001336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001338 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 return $item.$alias;
1340 }
Barry Mienydd671972010-10-04 16:33:58 +02001341
Derek Jones37f4b9c2011-07-01 17:56:50 -05001342 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 if ($this->dbprefix != '')
1344 {
1345 // We now add the table prefix based on some logic.
1346 // Do we have 4 segments (hostname.database.table.column)?
1347 // If so, we add the table prefix to the column name in the 3rd segment.
1348 if (isset($parts[3]))
1349 {
1350 $i = 2;
1351 }
1352 // Do we have 3 segments (database.table.column)?
1353 // If so, we add the table prefix to the column name in 2nd position
1354 elseif (isset($parts[2]))
1355 {
1356 $i = 1;
1357 }
1358 // Do we have 2 segments (table.column)?
1359 // If so, we add the table prefix to the column name in 1st segment
1360 else
1361 {
1362 $i = 0;
1363 }
Barry Mienydd671972010-10-04 16:33:58 +02001364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 // This flag is set when the supplied $item does not contain a field name.
1366 // This can happen when this function is being called from a JOIN.
1367 if ($field_exists == FALSE)
1368 {
1369 $i++;
1370 }
Barry Mienydd671972010-10-04 16:33:58 +02001371
Derek Jones55acc8b2009-07-11 03:38:13 +00001372 // Verify table prefix and replace if necessary
1373 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1374 {
1375 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1376 }
Barry Mienydd671972010-10-04 16:33:58 +02001377
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 // We only add the table prefix if it does not already exist
1379 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1380 {
1381 $parts[$i] = $this->dbprefix.$parts[$i];
1382 }
Barry Mienydd671972010-10-04 16:33:58 +02001383
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 // Put the parts back together
1385 $item = implode('.', $parts);
1386 }
Barry Mienydd671972010-10-04 16:33:58 +02001387
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 if ($protect_identifiers === TRUE)
1389 {
1390 $item = $this->_escape_identifiers($item);
1391 }
Barry Mienydd671972010-10-04 16:33:58 +02001392
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 return $item.$alias;
1394 }
1395
Derek Jones37f4b9c2011-07-01 17:56:50 -05001396 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 if ($this->dbprefix != '')
1398 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001399 // Verify table prefix and replace if necessary
1400 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1401 {
1402 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1403 }
1404
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 // Do we prefix an item with no segments?
1406 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1407 {
1408 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001409 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 }
1411
1412 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1413 {
1414 $item = $this->_escape_identifiers($item);
1415 }
Barry Mienydd671972010-10-04 16:33:58 +02001416
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 return $item.$alias;
1418 }
Túbal Martín511f2252011-11-24 14:43:45 +01001419
1420 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001421
Túbal Martín511f2252011-11-24 14:43:45 +01001422 /**
1423 * Dummy method that allows Active Record class to be disabled
1424 *
1425 * This function is used extensively by every db driver.
1426 *
1427 * @access private
1428 * @return void
1429 */
1430 protected function _reset_select()
1431 {
1432
1433 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001434
1435}
1436
Derek Allard2067d1a2008-11-13 22:59:24 +00001437/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001438/* Location: ./system/database/DB_driver.php */