blob: 661b42ced81061092103d30ebbac5396566d5cbb [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;
84
85
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050088 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * connection settings.
90 *
91 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020092 */
Timothy Warrena2097a02011-10-10 10:10:46 -040093 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 if (is_array($params))
96 {
97 foreach ($params as $key => $val)
98 {
99 $this->$key = $val;
100 }
101 }
102
103 log_message('debug', 'Database Driver Class Initialized');
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
109 * Initialize Database Settings
110 *
111 * @access private Called by the constructor
112 * @param mixed
113 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 function initialize()
116 {
117 // If an existing connection resource is available
118 // there is no need to connect and select the database
119 if (is_resource($this->conn_id) OR is_object($this->conn_id))
120 {
121 return TRUE;
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // Connect to the database and set the connection ID
127 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
128
Felix Balfoort5d581b62011-11-29 15:53:01 +0100129 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if ( ! $this->conn_id)
131 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100132 // Check if there is a failover set
133 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100135 // Go over all the failovers
136 foreach ($this->failover as $failover)
137 {
138 // Replace the current settings with those of the failover
139 foreach ($failover as $key => $val)
140 {
141 $this->$key = $val;
142 }
143
144 // Try to connect
145 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
146
147 // If a connection is made break the foreach loop
148 if ($this->conn_id)
149 {
150 break;
151 }
152 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100154
155 // We still don't have a connection?
156 if ( ! $this->conn_id)
157 {
158 log_message('error', 'Unable to connect to the database');
159
160 if ($this->db_debug)
161 {
162 $this->display_error('db_unable_to_connect');
163 }
164 return FALSE;
165 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
168 // ----------------------------------------------------------------
169
170 // Select the DB... assuming a database name is specified in the config file
171 if ($this->database != '')
172 {
173 if ( ! $this->db_select())
174 {
175 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if ($this->db_debug)
178 {
179 $this->display_error('db_unable_to_select', $this->database);
180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183 else
184 {
185 // We've selected the DB. Now we set the character set
186 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
187 {
188 return FALSE;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 return TRUE;
192 }
193 }
194
195 return TRUE;
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // --------------------------------------------------------------------
199
200 /**
201 * Set client character set
202 *
203 * @access public
204 * @param string
205 * @param string
206 * @return resource
207 */
208 function db_set_charset($charset, $collation)
209 {
210 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
211 {
212 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 if ($this->db_debug)
215 {
216 $this->display_error('db_unable_to_set_charset', $this->char_set);
217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 return FALSE;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 return TRUE;
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * The name of the platform in use (mysql, mssql, etc...)
229 *
230 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200231 * @return string
232 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 function platform()
234 {
235 return $this->dbdriver;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500241 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * version of the database being used
243 *
244 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200245 * @return string
246 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 function version()
248 {
249 if (FALSE === ($sql = $this->_version()))
250 {
251 if ($this->db_debug)
252 {
253 return $this->display_error('db_unsupported_function');
254 }
255 return FALSE;
256 }
Derek Allard3683f772009-12-16 17:32:33 +0000257
258 // Some DBs have functions that return the version, and don't run special
259 // SQL queries per se. In these instances, just return the result.
Timothy Warren36fb8de2011-08-24 08:29:05 -0400260 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo');
Derek Allard3683f772009-12-16 17:32:33 +0000261
262 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 return $sql;
265 }
Derek Allard3683f772009-12-16 17:32:33 +0000266 else
267 {
268 $query = $this->query($sql);
269 return $query->row('ver');
270 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * Execute the query
277 *
278 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500279 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * upon successful execution of a "write" type query. Returns boolean
281 * FALSE upon failure, and if the $db_debug variable is set to TRUE
282 * will raise an error.
283 *
284 * @access public
285 * @param string An SQL query string
286 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200287 * @return mixed
288 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 function query($sql, $binds = FALSE, $return_object = TRUE)
290 {
291 if ($sql == '')
292 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200293 log_message('error', 'Invalid query: '.$sql);
294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 if ($this->db_debug)
296 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 return $this->display_error('db_invalid_query');
298 }
299 return FALSE;
300 }
301
302 // Verify table prefix and replace if necessary
303 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600304 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
306 }
Derek Jonese7792202010-03-02 17:24:46 -0600307
Derek Jones37f4b9c2011-07-01 17:56:50 -0500308 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // we will load the caching class and return the previously
310 // cached query if it exists
311 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
312 {
313 if ($this->_cache_init())
314 {
315 $this->load_rdriver();
316 if (FALSE !== ($cache = $this->CACHE->read($sql)))
317 {
318 return $cache;
319 }
320 }
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Compile binds if needed
324 if ($binds !== FALSE)
325 {
326 $sql = $this->compile_binds($sql, $binds);
327 }
328
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 if ($this->save_queries == TRUE)
331 {
332 $this->queries[] = $sql;
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // Start the Query Timer
336 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Run the Query
339 if (FALSE === ($this->result_id = $this->simple_query($sql)))
340 {
341 if ($this->save_queries == TRUE)
342 {
343 $this->query_times[] = 0;
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // This will trigger a rollback if transactions are being used
347 $this->_trans_status = FALSE;
348
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200349 // Grab the error number and message now, as we might run some
350 // additional queries before displaying the error
351 $error_no = $this->_error_number();
352 $error_msg = $this->_error_message();
353
354 // Log errors
355 log_message('error', 'Query error: '.$error_msg);
356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 if ($this->db_debug)
358 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500360 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200361 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // transactions to remain in limbo.
363 $this->trans_complete();
364
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200365 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 return $this->display_error(
367 array(
368 'Error Number: '.$error_no,
369 $error_msg,
370 $sql
371 )
372 );
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 return FALSE;
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // Stop and aggregate the query time results
379 $time_end = list($em, $es) = explode(' ', microtime());
380 $this->benchmark += ($em + $es) - ($sm + $ss);
381
382 if ($this->save_queries == TRUE)
383 {
384 $this->query_times[] = ($em + $es) - ($sm + $ss);
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // Increment the query counter
388 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // Was the query a "write" type?
391 // If so we'll simply return true
392 if ($this->is_write_type($sql) === TRUE)
393 {
394 // If caching is enabled we'll auto-cleanup any
395 // existing files related to this particular URI
396 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
397 {
398 $this->CACHE->delete();
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 return TRUE;
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // Return TRUE if we don't need to create a result object
405 // Currently only the Oracle driver uses this when stored
406 // procedures are used
407 if ($return_object !== TRUE)
408 {
409 return TRUE;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
412 // Load and instantiate the result driver
413
414 $driver = $this->load_rdriver();
415 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 $RES->conn_id = $this->conn_id;
417 $RES->result_id = $this->result_id;
418
419 if ($this->dbdriver == 'oci8')
420 {
421 $RES->stmt_id = $this->stmt_id;
422 $RES->curs_id = NULL;
423 $RES->limit_used = $this->limit_used;
424 $this->stmt_id = FALSE;
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // oci8 vars must be set before calling this
428 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Jones37f4b9c2011-07-01 17:56:50 -0500430 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // result object and save it to a cache file.
432 if ($this->cache_on == TRUE AND $this->_cache_init())
433 {
434 // We'll create a new instance of the result object
435 // only without the platform specific driver since
436 // we can't use it with cached data (the query result
437 // resource ID won't be any good once we've cached the
438 // result object, so we'll have to compile the data
439 // and save it)
440 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200441 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $CR->result_object = $RES->result_object();
443 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // Reset these since cached objects can not utilize resource IDs.
446 $CR->conn_id = NULL;
447 $CR->result_id = NULL;
448
449 $this->CACHE->write($sql, $CR);
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 return $RES;
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Load the result drivers
459 *
460 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200461 * @return string the name of the result class
462 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 function load_rdriver()
464 {
465 $driver = 'CI_DB_'.$this->dbdriver.'_result';
466
467 if ( ! class_exists($driver))
468 {
Greg Aker3a746652011-04-19 10:59:47 -0500469 include_once(BASEPATH.'database/DB_result.php');
470 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 return $driver;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500480 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * we only use it when running transaction commands since they do
482 * not require all the features of the main query() function.
483 *
484 * @access public
485 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200486 * @return mixed
487 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 function simple_query($sql)
489 {
490 if ( ! $this->conn_id)
491 {
492 $this->initialize();
493 }
494
495 return $this->_execute($sql);
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // --------------------------------------------------------------------
499
500 /**
501 * Disable Transactions
502 * This permits transactions to be disabled at run-time.
503 *
504 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200505 * @return void
506 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 function trans_off()
508 {
509 $this->trans_enabled = FALSE;
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
515 * Enable/disable Transaction Strict Mode
516 * When strict mode is enabled, if you are running multiple groups of
517 * transactions, if one group fails all groups will be rolled back.
518 * If strict mode is disabled, each group is treated autonomously, meaning
519 * a failure of one group will not affect any others
520 *
521 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200522 * @return void
523 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 function trans_strict($mode = TRUE)
525 {
526 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 // --------------------------------------------------------------------
530
531 /**
532 * Start Transaction
533 *
534 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200535 * @return void
536 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200538 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 if ( ! $this->trans_enabled)
540 {
541 return FALSE;
542 }
543
544 // When transactions are nested we only begin/commit/rollback the outermost ones
545 if ($this->_trans_depth > 0)
546 {
547 $this->_trans_depth += 1;
548 return;
549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500552 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
554
555 // --------------------------------------------------------------------
556
557 /**
558 * Complete Transaction
559 *
560 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200561 * @return bool
562 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 function trans_complete()
564 {
565 if ( ! $this->trans_enabled)
566 {
567 return FALSE;
568 }
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 // When transactions are nested we only begin/commit/rollback the outermost ones
571 if ($this->_trans_depth > 1)
572 {
573 $this->_trans_depth -= 1;
574 return TRUE;
575 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500576 else
577 {
578 $this->_trans_depth = 0;
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // The query() function will set this flag to FALSE in the event that a query failed
582 if ($this->_trans_status === FALSE)
583 {
584 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 // If we are NOT running in strict mode, we will reset
587 // the _trans_status flag so that subsequent groups of transactions
588 // will be permitted.
589 if ($this->trans_strict === FALSE)
590 {
591 $this->_trans_status = TRUE;
592 }
593
594 log_message('debug', 'DB Transaction Failure');
595 return FALSE;
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 $this->trans_commit();
599 return TRUE;
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Lets you retrieve the transaction flag to determine if it has failed
606 *
607 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200608 * @return bool
609 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 function trans_status()
611 {
612 return $this->_trans_status;
613 }
614
615 // --------------------------------------------------------------------
616
617 /**
618 * Compile Bindings
619 *
620 * @access public
621 * @param string the sql statement
622 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200623 * @return string
624 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 function compile_binds($sql, $binds)
626 {
627 if (strpos($sql, $this->bind_marker) === FALSE)
628 {
629 return $sql;
630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 if ( ! is_array($binds))
633 {
634 $binds = array($binds);
635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // Get the sql segments around the bind markers
638 $segments = explode($this->bind_marker, $sql);
639
640 // The count of bind should be 1 less then the count of segments
641 // If there are more bind arguments trim it down
642 if (count($binds) >= count($segments)) {
643 $binds = array_slice($binds, 0, count($segments)-1);
644 }
645
646 // Construct the binded query
647 $result = $segments[0];
648 $i = 0;
649 foreach ($binds as $bind)
650 {
651 $result .= $this->escape($bind);
652 $result .= $segments[++$i];
653 }
654
655 return $result;
656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 // --------------------------------------------------------------------
659
660 /**
661 * Determines if a query is a "write" type.
662 *
663 * @access public
664 * @param string An SQL query string
Barry Mienydd671972010-10-04 16:33:58 +0200665 * @return boolean
666 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 function is_write_type($sql)
668 {
Derek Allarde37ab382009-02-03 16:13:57 +0000669 if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
671 return FALSE;
672 }
673 return TRUE;
674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 // --------------------------------------------------------------------
677
678 /**
679 * Calculate the aggregate query elapsed time
680 *
681 * @access public
682 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200683 * @return integer
684 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 function elapsed_time($decimals = 6)
686 {
687 return number_format($this->benchmark, $decimals);
688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // --------------------------------------------------------------------
691
692 /**
693 * Returns the total number of queries
694 *
695 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200696 * @return integer
697 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 function total_queries()
699 {
700 return $this->query_count;
701 }
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 // --------------------------------------------------------------------
704
705 /**
706 * Returns the last query that was executed
707 *
708 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200709 * @return void
710 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 function last_query()
712 {
713 return end($this->queries);
714 }
715
716 // --------------------------------------------------------------------
717
718 /**
719 * "Smart" Escape String
720 *
721 * Escapes data based on type
722 * Sets boolean and null types
723 *
724 * @access public
725 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200726 * @return mixed
727 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200729 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000730 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000732 $str = "'".$this->escape_str($str)."'";
733 }
734 elseif (is_bool($str))
735 {
736 $str = ($str === FALSE) ? 0 : 1;
737 }
738 elseif (is_null($str))
739 {
740 $str = 'NULL';
741 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000742
743 return $str;
744 }
745
746 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600747
Derek Jonese4ed5832009-02-20 21:44:59 +0000748 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000749 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000750 *
751 * Calls the individual driver for platform
752 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200753 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000754 * @access public
755 * @param string
756 * @return mixed
757 */
Barry Mienydd671972010-10-04 16:33:58 +0200758 function escape_like_str($str)
759 {
760 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000761 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000762
Derek Jonese4ed5832009-02-20 21:44:59 +0000763 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 /**
766 * Primary
767 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500768 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 * position is the primary key
770 *
771 * @access public
772 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200773 * @return string
774 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200776 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 if ( ! is_array($fields))
780 {
781 return FALSE;
782 }
783
784 return current($fields);
785 }
786
787 // --------------------------------------------------------------------
788
789 /**
790 * Returns an array of table names
791 *
792 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200793 * @return array
794 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 function list_tables($constrain_by_prefix = FALSE)
796 {
797 // Is there a cached result?
798 if (isset($this->data_cache['table_names']))
799 {
800 return $this->data_cache['table_names'];
801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
804 {
805 if ($this->db_debug)
806 {
807 return $this->display_error('db_unsupported_function');
808 }
809 return FALSE;
810 }
811
812 $retval = array();
813 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 if ($query->num_rows() > 0)
816 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500817 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 {
819 if (isset($row['TABLE_NAME']))
820 {
821 $retval[] = $row['TABLE_NAME'];
822 }
823 else
824 {
825 $retval[] = array_shift($row);
826 }
827 }
828 }
829
830 $this->data_cache['table_names'] = $retval;
831 return $this->data_cache['table_names'];
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 // --------------------------------------------------------------------
835
836 /**
837 * Determine if a particular table exists
838 * @access public
839 * @return boolean
840 */
841 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200842 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 // --------------------------------------------------------------------
847
848 /**
849 * Fetch MySQL Field Names
850 *
851 * @access public
852 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200853 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 */
855 function list_fields($table = '')
856 {
857 // Is there a cached result?
858 if (isset($this->data_cache['field_names'][$table]))
859 {
860 return $this->data_cache['field_names'][$table];
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 if ($table == '')
864 {
865 if ($this->db_debug)
866 {
867 return $this->display_error('db_field_param_missing');
868 }
869 return FALSE;
870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Greg Aker1edde302010-01-26 00:17:01 +0000872 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
874 if ($this->db_debug)
875 {
876 return $this->display_error('db_unsupported_function');
877 }
878 return FALSE;
879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500884 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
886 if (isset($row['COLUMN_NAME']))
887 {
888 $retval[] = $row['COLUMN_NAME'];
889 }
890 else
891 {
892 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200893 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 $this->data_cache['field_names'][$table] = $retval;
897 return $this->data_cache['field_names'][$table];
898 }
899
900 // --------------------------------------------------------------------
901
902 /**
903 * Determine if a particular field exists
904 * @access public
905 * @param string
906 * @param string
907 * @return boolean
908 */
909 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200910 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 // --------------------------------------------------------------------
915
916 /**
917 * Returns an object with field data
918 *
919 * @access public
920 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200921 * @return object
922 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 function field_data($table = '')
924 {
925 if ($table == '')
926 {
927 if ($this->db_debug)
928 {
929 return $this->display_error('db_field_param_missing');
930 }
931 return FALSE;
932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
935
936 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200937 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000938
939 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 /**
942 * Generate an insert string
943 *
944 * @access public
945 * @param string the table upon which the query will be performed
946 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200947 * @return string
948 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 function insert_string($table, $data)
950 {
951 $fields = array();
952 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200953
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500954 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
956 $fields[] = $this->_escape_identifiers($key);
957 $values[] = $this->escape($val);
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200961 }
962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 // --------------------------------------------------------------------
964
965 /**
966 * Generate an update string
967 *
968 * @access public
969 * @param string the table upon which the query will be performed
970 * @param array an associative array data of key/values
971 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200972 * @return string
973 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 function update_string($table, $data, $where)
975 {
976 if ($where == '')
977 {
978 return false;
979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500982 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
985 }
986
987 if ( ! is_array($where))
988 {
989 $dest = array($where);
990 }
991 else
992 {
993 $dest = array();
994 foreach ($where as $key => $val)
995 {
996 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300997 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 if ($val !== '')
1000 {
1001 if ( ! $this->_has_operator($key))
1002 {
1003 $key .= ' =';
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 $val = ' '.$this->escape($val);
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 $dest[] = $prefix.$key.$val;
1010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
1013 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001014 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001015
1016 // --------------------------------------------------------------------
1017
1018 /**
1019 * Tests whether the string has an SQL operator
1020 *
1021 * @access private
1022 * @param string
1023 * @return bool
1024 */
1025 function _has_operator($str)
1026 {
1027 $str = trim($str);
1028 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1029 {
1030 return FALSE;
1031 }
1032
1033 return TRUE;
1034 }
1035
1036 // --------------------------------------------------------------------
1037
1038 /**
1039 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1040 *
1041 * @access public
1042 * @param string the function name
1043 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001044 * @return mixed
1045 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 function call_function($function)
1047 {
1048 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 if (FALSE === strpos($driver, $function))
1051 {
1052 $function = $driver.$function;
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 if ( ! function_exists($function))
1056 {
1057 if ($this->db_debug)
1058 {
1059 return $this->display_error('db_unsupported_function');
1060 }
1061 return FALSE;
1062 }
1063 else
1064 {
1065 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1066
Repox71b78092011-12-01 09:19:43 +01001067 if (is_null($args))
1068 {
1069 return call_user_func($function);
1070 }
1071 else
1072 {
1073 return call_user_func_array($function, $args);
1074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
1076 }
1077
1078 // --------------------------------------------------------------------
1079
1080 /**
1081 * Set Cache Directory Path
1082 *
1083 * @access public
1084 * @param string the path to the cache directory
1085 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001086 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 function cache_set_path($path = '')
1088 {
1089 $this->cachedir = $path;
1090 }
1091
1092 // --------------------------------------------------------------------
1093
1094 /**
1095 * Enable Query Caching
1096 *
1097 * @access public
1098 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001099 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 function cache_on()
1101 {
1102 $this->cache_on = TRUE;
1103 return TRUE;
1104 }
1105
1106 // --------------------------------------------------------------------
1107
1108 /**
1109 * Disable Query Caching
1110 *
1111 * @access public
1112 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001113 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 function cache_off()
1115 {
1116 $this->cache_on = FALSE;
1117 return FALSE;
1118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120
1121 // --------------------------------------------------------------------
1122
1123 /**
1124 * Delete the cache files associated with a particular URI
1125 *
1126 * @access public
1127 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001128 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 function cache_delete($segment_one = '', $segment_two = '')
1130 {
1131 if ( ! $this->_cache_init())
1132 {
1133 return FALSE;
1134 }
1135 return $this->CACHE->delete($segment_one, $segment_two);
1136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
1141 * Delete All cache files
1142 *
1143 * @access public
1144 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001145 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 function cache_delete_all()
1147 {
1148 if ( ! $this->_cache_init())
1149 {
1150 return FALSE;
1151 }
1152
1153 return $this->CACHE->delete_all();
1154 }
1155
1156 // --------------------------------------------------------------------
1157
1158 /**
1159 * Initialize the Cache Class
1160 *
1161 * @access private
1162 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001163 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 function _cache_init()
1165 {
1166 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1167 {
1168 return TRUE;
1169 }
Derek Allarde37ab382009-02-03 16:13:57 +00001170
1171 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
Greg Aker3a746652011-04-19 10:59:47 -05001173 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001174 {
1175 return $this->cache_off();
1176 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 }
Derek Allarde37ab382009-02-03 16:13:57 +00001178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1180 return TRUE;
1181 }
1182
1183 // --------------------------------------------------------------------
1184
1185 /**
1186 * Close DB Connection
1187 *
1188 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001189 * @return void
1190 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 function close()
1192 {
1193 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1194 {
1195 $this->_close($this->conn_id);
1196 }
1197 $this->conn_id = FALSE;
1198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 // --------------------------------------------------------------------
1201
1202 /**
1203 * Display an error message
1204 *
1205 * @access public
1206 * @param string the error message
1207 * @param string any "swap" values
1208 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001209 * @return string sends the application/error_db.php template
1210 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 function display_error($error = '', $swap = '', $native = FALSE)
1212 {
Derek Jonese7792202010-03-02 17:24:46 -06001213 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 $LANG->load('db');
1215
1216 $heading = $LANG->line('db_error_heading');
1217
1218 if ($native == TRUE)
1219 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001220 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 }
1222 else
1223 {
1224 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1225 }
Barry Mienydd671972010-10-04 16:33:58 +02001226
Pascal Kriete60f8c392010-08-25 18:03:28 +02001227 // Find the most likely culprit of the error by going through
1228 // the backtrace until the source file is no longer in the
1229 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001230
Pascal Kriete60f8c392010-08-25 18:03:28 +02001231 $trace = debug_backtrace();
1232
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001233 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001234 {
1235 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1236 {
1237 // Found it - use a relative path for safety
1238 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1239 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001240
Pascal Kriete60f8c392010-08-25 18:03:28 +02001241 break;
1242 }
1243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Jonese7792202010-03-02 17:24:46 -06001245 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 echo $error->show_error($heading, $message, 'error_db');
1247 exit;
1248 }
1249
1250 // --------------------------------------------------------------------
1251
1252 /**
1253 * Protect Identifiers
1254 *
1255 * This function adds backticks if appropriate based on db type
1256 *
1257 * @access private
1258 * @param mixed the item to escape
1259 * @return mixed the item with backticks
1260 */
1261 function protect_identifiers($item, $prefix_single = FALSE)
1262 {
1263 return $this->_protect_identifiers($item, $prefix_single);
1264 }
1265
1266 // --------------------------------------------------------------------
1267
1268 /**
1269 * Protect Identifiers
1270 *
1271 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001272 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001274 * the table prefix onto it. Some logic is necessary in order to deal with
1275 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 *
1277 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1278 *
1279 * Or a query with aliasing:
1280 *
1281 * SELECT m.member_id, m.member_name FROM members AS m
1282 *
1283 * Since the column name can include up to four segments (host, DB, table, column)
1284 * or also have an alias prefix, we need to do a bit of work to figure this out and
1285 * insert the table prefix (if it exists) in the proper position, and escape only
1286 * the correct identifiers.
1287 *
1288 * @access private
1289 * @param string
1290 * @param bool
1291 * @param mixed
1292 * @param bool
1293 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001294 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
1296 {
1297 if ( ! is_bool($protect_identifiers))
1298 {
1299 $protect_identifiers = $this->_protect_identifiers;
1300 }
Derek Allarde37ab382009-02-03 16:13:57 +00001301
1302 if (is_array($item))
1303 {
1304 $escaped_array = array();
1305
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001306 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001307 {
1308 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1309 }
1310
1311 return $escaped_array;
1312 }
1313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001315 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001316
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 // If the item has an alias declaration we remove it and set it aside.
1318 // Basically we remove everything to the right of the first space
1319 $alias = '';
1320 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001321 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 $alias = strstr($item, " ");
1323 $item = substr($item, 0, - strlen($alias));
1324 }
1325
Derek Allard911d3e02008-12-15 14:08:35 +00001326 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001327 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001328 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001329 // way to deal with this, but I'm not thinking of it -- Rick
1330 if (strpos($item, '(') !== FALSE)
1331 {
1332 return $item.$alias;
1333 }
1334
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // Break the string apart if it contains periods, then insert the table prefix
1336 // in the correct location, assuming the period doesn't indicate that we're dealing
1337 // with an alias. While we're at it, we will escape the components
1338 if (strpos($item, '.') !== FALSE)
1339 {
1340 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001341
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001343 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 // we have nothing more to do other than escape the item
1345 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001346 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 if ($protect_identifiers === TRUE)
1348 {
1349 foreach ($parts as $key => $val)
1350 {
1351 if ( ! in_array($val, $this->_reserved_identifiers))
1352 {
1353 $parts[$key] = $this->_escape_identifiers($val);
1354 }
1355 }
Barry Mienydd671972010-10-04 16:33:58 +02001356
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001358 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 return $item.$alias;
1360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Derek Jones37f4b9c2011-07-01 17:56:50 -05001362 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 if ($this->dbprefix != '')
1364 {
1365 // We now add the table prefix based on some logic.
1366 // Do we have 4 segments (hostname.database.table.column)?
1367 // If so, we add the table prefix to the column name in the 3rd segment.
1368 if (isset($parts[3]))
1369 {
1370 $i = 2;
1371 }
1372 // Do we have 3 segments (database.table.column)?
1373 // If so, we add the table prefix to the column name in 2nd position
1374 elseif (isset($parts[2]))
1375 {
1376 $i = 1;
1377 }
1378 // Do we have 2 segments (table.column)?
1379 // If so, we add the table prefix to the column name in 1st segment
1380 else
1381 {
1382 $i = 0;
1383 }
Barry Mienydd671972010-10-04 16:33:58 +02001384
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 // This flag is set when the supplied $item does not contain a field name.
1386 // This can happen when this function is being called from a JOIN.
1387 if ($field_exists == FALSE)
1388 {
1389 $i++;
1390 }
Barry Mienydd671972010-10-04 16:33:58 +02001391
Derek Jones55acc8b2009-07-11 03:38:13 +00001392 // Verify table prefix and replace if necessary
1393 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1394 {
1395 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1396 }
Barry Mienydd671972010-10-04 16:33:58 +02001397
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 // We only add the table prefix if it does not already exist
1399 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1400 {
1401 $parts[$i] = $this->dbprefix.$parts[$i];
1402 }
Barry Mienydd671972010-10-04 16:33:58 +02001403
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 // Put the parts back together
1405 $item = implode('.', $parts);
1406 }
Barry Mienydd671972010-10-04 16:33:58 +02001407
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 if ($protect_identifiers === TRUE)
1409 {
1410 $item = $this->_escape_identifiers($item);
1411 }
Barry Mienydd671972010-10-04 16:33:58 +02001412
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 return $item.$alias;
1414 }
1415
Derek Jones37f4b9c2011-07-01 17:56:50 -05001416 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 if ($this->dbprefix != '')
1418 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001419 // Verify table prefix and replace if necessary
1420 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1421 {
1422 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1423 }
1424
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 // Do we prefix an item with no segments?
1426 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1427 {
1428 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001429 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 }
1431
1432 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1433 {
1434 $item = $this->_escape_identifiers($item);
1435 }
Barry Mienydd671972010-10-04 16:33:58 +02001436
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 return $item.$alias;
1438 }
1439
1440
1441}
1442
1443
1444/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001445/* Location: ./system/database/DB_driver.php */