blob: c2d57a8337a92919f9c53cd7f0ae04c7242717df [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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
1067 return call_user_func_array($function, $args);
1068 }
1069 }
1070
1071 // --------------------------------------------------------------------
1072
1073 /**
1074 * Set Cache Directory Path
1075 *
1076 * @access public
1077 * @param string the path to the cache directory
1078 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001079 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 function cache_set_path($path = '')
1081 {
1082 $this->cachedir = $path;
1083 }
1084
1085 // --------------------------------------------------------------------
1086
1087 /**
1088 * Enable Query Caching
1089 *
1090 * @access public
1091 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001092 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 function cache_on()
1094 {
1095 $this->cache_on = TRUE;
1096 return TRUE;
1097 }
1098
1099 // --------------------------------------------------------------------
1100
1101 /**
1102 * Disable Query Caching
1103 *
1104 * @access public
1105 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001106 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 function cache_off()
1108 {
1109 $this->cache_on = FALSE;
1110 return FALSE;
1111 }
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113
1114 // --------------------------------------------------------------------
1115
1116 /**
1117 * Delete the cache files associated with a particular URI
1118 *
1119 * @access public
1120 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001121 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 function cache_delete($segment_one = '', $segment_two = '')
1123 {
1124 if ( ! $this->_cache_init())
1125 {
1126 return FALSE;
1127 }
1128 return $this->CACHE->delete($segment_one, $segment_two);
1129 }
1130
1131 // --------------------------------------------------------------------
1132
1133 /**
1134 * Delete All cache files
1135 *
1136 * @access public
1137 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001138 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 function cache_delete_all()
1140 {
1141 if ( ! $this->_cache_init())
1142 {
1143 return FALSE;
1144 }
1145
1146 return $this->CACHE->delete_all();
1147 }
1148
1149 // --------------------------------------------------------------------
1150
1151 /**
1152 * Initialize the Cache Class
1153 *
1154 * @access private
1155 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001156 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 function _cache_init()
1158 {
1159 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1160 {
1161 return TRUE;
1162 }
Derek Allarde37ab382009-02-03 16:13:57 +00001163
1164 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
Greg Aker3a746652011-04-19 10:59:47 -05001166 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001167 {
1168 return $this->cache_off();
1169 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 }
Derek Allarde37ab382009-02-03 16:13:57 +00001171
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1173 return TRUE;
1174 }
1175
1176 // --------------------------------------------------------------------
1177
1178 /**
1179 * Close DB Connection
1180 *
1181 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001182 * @return void
1183 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 function close()
1185 {
1186 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1187 {
1188 $this->_close($this->conn_id);
1189 }
1190 $this->conn_id = FALSE;
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 // --------------------------------------------------------------------
1194
1195 /**
1196 * Display an error message
1197 *
1198 * @access public
1199 * @param string the error message
1200 * @param string any "swap" values
1201 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001202 * @return string sends the application/error_db.php template
1203 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 function display_error($error = '', $swap = '', $native = FALSE)
1205 {
Derek Jonese7792202010-03-02 17:24:46 -06001206 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 $LANG->load('db');
1208
1209 $heading = $LANG->line('db_error_heading');
1210
1211 if ($native == TRUE)
1212 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001213 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 }
1215 else
1216 {
1217 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1218 }
Barry Mienydd671972010-10-04 16:33:58 +02001219
Pascal Kriete60f8c392010-08-25 18:03:28 +02001220 // Find the most likely culprit of the error by going through
1221 // the backtrace until the source file is no longer in the
1222 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001223
Pascal Kriete60f8c392010-08-25 18:03:28 +02001224 $trace = debug_backtrace();
1225
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001226 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001227 {
1228 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1229 {
1230 // Found it - use a relative path for safety
1231 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1232 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001233
Pascal Kriete60f8c392010-08-25 18:03:28 +02001234 break;
1235 }
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Jonese7792202010-03-02 17:24:46 -06001238 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 echo $error->show_error($heading, $message, 'error_db');
1240 exit;
1241 }
1242
1243 // --------------------------------------------------------------------
1244
1245 /**
1246 * Protect Identifiers
1247 *
1248 * This function adds backticks if appropriate based on db type
1249 *
1250 * @access private
1251 * @param mixed the item to escape
1252 * @return mixed the item with backticks
1253 */
1254 function protect_identifiers($item, $prefix_single = FALSE)
1255 {
1256 return $this->_protect_identifiers($item, $prefix_single);
1257 }
1258
1259 // --------------------------------------------------------------------
1260
1261 /**
1262 * Protect Identifiers
1263 *
1264 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001265 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001267 * the table prefix onto it. Some logic is necessary in order to deal with
1268 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 *
1270 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1271 *
1272 * Or a query with aliasing:
1273 *
1274 * SELECT m.member_id, m.member_name FROM members AS m
1275 *
1276 * Since the column name can include up to four segments (host, DB, table, column)
1277 * or also have an alias prefix, we need to do a bit of work to figure this out and
1278 * insert the table prefix (if it exists) in the proper position, and escape only
1279 * the correct identifiers.
1280 *
1281 * @access private
1282 * @param string
1283 * @param bool
1284 * @param mixed
1285 * @param bool
1286 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001287 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
1289 {
1290 if ( ! is_bool($protect_identifiers))
1291 {
1292 $protect_identifiers = $this->_protect_identifiers;
1293 }
Derek Allarde37ab382009-02-03 16:13:57 +00001294
1295 if (is_array($item))
1296 {
1297 $escaped_array = array();
1298
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001299 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001300 {
1301 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1302 }
1303
1304 return $escaped_array;
1305 }
1306
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001308 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 // If the item has an alias declaration we remove it and set it aside.
1311 // Basically we remove everything to the right of the first space
1312 $alias = '';
1313 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001314 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 $alias = strstr($item, " ");
1316 $item = substr($item, 0, - strlen($alias));
1317 }
1318
Derek Allard911d3e02008-12-15 14:08:35 +00001319 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001320 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001321 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001322 // way to deal with this, but I'm not thinking of it -- Rick
1323 if (strpos($item, '(') !== FALSE)
1324 {
1325 return $item.$alias;
1326 }
1327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 // Break the string apart if it contains periods, then insert the table prefix
1329 // in the correct location, assuming the period doesn't indicate that we're dealing
1330 // with an alias. While we're at it, we will escape the components
1331 if (strpos($item, '.') !== FALSE)
1332 {
1333 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001334
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001336 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 // we have nothing more to do other than escape the item
1338 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001339 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 if ($protect_identifiers === TRUE)
1341 {
1342 foreach ($parts as $key => $val)
1343 {
1344 if ( ! in_array($val, $this->_reserved_identifiers))
1345 {
1346 $parts[$key] = $this->_escape_identifiers($val);
1347 }
1348 }
Barry Mienydd671972010-10-04 16:33:58 +02001349
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001351 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 return $item.$alias;
1353 }
Barry Mienydd671972010-10-04 16:33:58 +02001354
Derek Jones37f4b9c2011-07-01 17:56:50 -05001355 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 if ($this->dbprefix != '')
1357 {
1358 // We now add the table prefix based on some logic.
1359 // Do we have 4 segments (hostname.database.table.column)?
1360 // If so, we add the table prefix to the column name in the 3rd segment.
1361 if (isset($parts[3]))
1362 {
1363 $i = 2;
1364 }
1365 // Do we have 3 segments (database.table.column)?
1366 // If so, we add the table prefix to the column name in 2nd position
1367 elseif (isset($parts[2]))
1368 {
1369 $i = 1;
1370 }
1371 // Do we have 2 segments (table.column)?
1372 // If so, we add the table prefix to the column name in 1st segment
1373 else
1374 {
1375 $i = 0;
1376 }
Barry Mienydd671972010-10-04 16:33:58 +02001377
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 // This flag is set when the supplied $item does not contain a field name.
1379 // This can happen when this function is being called from a JOIN.
1380 if ($field_exists == FALSE)
1381 {
1382 $i++;
1383 }
Barry Mienydd671972010-10-04 16:33:58 +02001384
Derek Jones55acc8b2009-07-11 03:38:13 +00001385 // Verify table prefix and replace if necessary
1386 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1387 {
1388 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1389 }
Barry Mienydd671972010-10-04 16:33:58 +02001390
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 // We only add the table prefix if it does not already exist
1392 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1393 {
1394 $parts[$i] = $this->dbprefix.$parts[$i];
1395 }
Barry Mienydd671972010-10-04 16:33:58 +02001396
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 // Put the parts back together
1398 $item = implode('.', $parts);
1399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 if ($protect_identifiers === TRUE)
1402 {
1403 $item = $this->_escape_identifiers($item);
1404 }
Barry Mienydd671972010-10-04 16:33:58 +02001405
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 return $item.$alias;
1407 }
1408
Derek Jones37f4b9c2011-07-01 17:56:50 -05001409 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 if ($this->dbprefix != '')
1411 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001412 // Verify table prefix and replace if necessary
1413 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1414 {
1415 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1416 }
1417
Derek Allard2067d1a2008-11-13 22:59:24 +00001418 // Do we prefix an item with no segments?
1419 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1420 {
1421 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001422 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 }
1424
1425 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1426 {
1427 $item = $this->_escape_identifiers($item);
1428 }
Barry Mienydd671972010-10-04 16:33:58 +02001429
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 return $item.$alias;
1431 }
1432
1433
1434}
1435
1436
1437/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001438/* Location: ./system/database/DB_driver.php */