blob: 339226cbbe26e88edd26a7a964ce01676d4f170d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Database Driver Class
32 *
33 * This is the platform-independent base DB implementation class.
34 * This class will not be called directly. Rather, the adapter
35 * class for the specific database will extend and instantiate it.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_driver {
44
45 var $username;
46 var $password;
47 var $hostname;
48 var $database;
49 var $dbdriver = 'mysql';
50 var $dbprefix = '';
51 var $char_set = 'utf8';
52 var $dbcollat = 'utf8_general_ci';
53 var $autoinit = TRUE; // Whether to automatically initialize the DB
54 var $swap_pre = '';
55 var $port = '';
56 var $pconnect = FALSE;
57 var $conn_id = FALSE;
58 var $result_id = FALSE;
59 var $db_debug = FALSE;
60 var $benchmark = 0;
61 var $query_count = 0;
62 var $bind_marker = '?';
63 var $save_queries = TRUE;
64 var $queries = array();
65 var $query_times = array();
66 var $data_cache = array();
67 var $trans_enabled = TRUE;
68 var $trans_strict = TRUE;
69 var $_trans_depth = 0;
70 var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
71 var $cache_on = FALSE;
72 var $cachedir = '';
73 var $cache_autodel = FALSE;
74 var $CACHE; // The cache class object
75
76 // Private variables
77 var $_protect_identifiers = TRUE;
78 var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
80 // These are use with Oracle
81 var $stmt_id;
82 var $curs_id;
83 var $limit_used;
Taufan Aditya18209332012-02-09 16:07:27 +070084
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050087 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * connection settings.
89 *
90 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020091 */
Timothy Warrena2097a02011-10-10 10:10:46 -040092 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
94 if (is_array($params))
95 {
96 foreach ($params as $key => $val)
97 {
98 $this->$key = $val;
99 }
100 }
101
102 log_message('debug', 'Database Driver Class Initialized');
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
106
107 /**
108 * Initialize Database Settings
109 *
110 * @access private Called by the constructor
111 * @param mixed
112 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200113 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 function initialize()
115 {
116 // If an existing connection resource is available
117 // there is no need to connect and select the database
118 if (is_resource($this->conn_id) OR is_object($this->conn_id))
119 {
120 return TRUE;
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // Connect to the database and set the connection ID
126 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
127
Felix Balfoort5d581b62011-11-29 15:53:01 +0100128 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if ( ! $this->conn_id)
130 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100131 // Check if there is a failover set
132 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100134 // Go over all the failovers
135 foreach ($this->failover as $failover)
136 {
137 // Replace the current settings with those of the failover
138 foreach ($failover as $key => $val)
139 {
140 $this->$key = $val;
141 }
142
143 // Try to connect
144 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
145
146 // If a connection is made break the foreach loop
147 if ($this->conn_id)
148 {
149 break;
150 }
151 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100153
154 // We still don't have a connection?
155 if ( ! $this->conn_id)
156 {
157 log_message('error', 'Unable to connect to the database');
158
159 if ($this->db_debug)
160 {
161 $this->display_error('db_unable_to_connect');
162 }
163 return FALSE;
164 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
167 // ----------------------------------------------------------------
168
169 // Select the DB... assuming a database name is specified in the config file
170 if ($this->database != '')
171 {
172 if ( ! $this->db_select())
173 {
174 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 if ($this->db_debug)
177 {
178 $this->display_error('db_unable_to_select', $this->database);
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182 else
183 {
184 // We've selected the DB. Now we set the character set
185 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
186 {
187 return FALSE;
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 return TRUE;
191 }
192 }
193
194 return TRUE;
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
200 * Set client character set
201 *
202 * @access public
203 * @param string
204 * @param string
205 * @return resource
206 */
207 function db_set_charset($charset, $collation)
208 {
209 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
210 {
211 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 if ($this->db_debug)
214 {
215 $this->display_error('db_unable_to_set_charset', $this->char_set);
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 return FALSE;
219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 return TRUE;
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * The name of the platform in use (mysql, mssql, etc...)
228 *
229 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200230 * @return string
231 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 function platform()
233 {
234 return $this->dbdriver;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * version of the database being used
242 *
243 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return string
245 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 function version()
247 {
248 if (FALSE === ($sql = $this->_version()))
249 {
250 if ($this->db_debug)
251 {
252 return $this->display_error('db_unsupported_function');
253 }
254 return FALSE;
255 }
Derek Allard3683f772009-12-16 17:32:33 +0000256
257 // Some DBs have functions that return the version, and don't run special
258 // SQL queries per se. In these instances, just return the result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200259 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli');
Derek Allard3683f772009-12-16 17:32:33 +0000260
261 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 return $sql;
264 }
Derek Allard3683f772009-12-16 17:32:33 +0000265 else
266 {
267 $query = $this->query($sql);
268 return $query->row('ver');
269 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
275 * Execute the query
276 *
277 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500278 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * upon successful execution of a "write" type query. Returns boolean
280 * FALSE upon failure, and if the $db_debug variable is set to TRUE
281 * will raise an error.
282 *
283 * @access public
284 * @param string An SQL query string
285 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200286 * @return mixed
287 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 function query($sql, $binds = FALSE, $return_object = TRUE)
289 {
290 if ($sql == '')
291 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200292 log_message('error', 'Invalid query: '.$sql);
293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 if ($this->db_debug)
295 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 return $this->display_error('db_invalid_query');
297 }
298 return FALSE;
299 }
300
301 // Verify table prefix and replace if necessary
302 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600303 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
305 }
Derek Jonese7792202010-03-02 17:24:46 -0600306
Derek Jones37f4b9c2011-07-01 17:56:50 -0500307 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // we will load the caching class and return the previously
309 // cached query if it exists
310 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
311 {
312 if ($this->_cache_init())
313 {
314 $this->load_rdriver();
315 if (FALSE !== ($cache = $this->CACHE->read($sql)))
316 {
317 return $cache;
318 }
319 }
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Compile binds if needed
323 if ($binds !== FALSE)
324 {
325 $sql = $this->compile_binds($sql, $binds);
326 }
327
Derek Jones37f4b9c2011-07-01 17:56:50 -0500328 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 if ($this->save_queries == TRUE)
330 {
331 $this->queries[] = $sql;
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // Start the Query Timer
335 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // Run the Query
338 if (FALSE === ($this->result_id = $this->simple_query($sql)))
339 {
340 if ($this->save_queries == TRUE)
341 {
342 $this->query_times[] = 0;
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // This will trigger a rollback if transactions are being used
346 $this->_trans_status = FALSE;
347
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200348 // Grab the error number and message now, as we might run some
349 // additional queries before displaying the error
350 $error_no = $this->_error_number();
351 $error_msg = $this->_error_message();
352
353 // Log errors
354 log_message('error', 'Query error: '.$error_msg);
355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 if ($this->db_debug)
357 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500359 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200360 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // transactions to remain in limbo.
362 $this->trans_complete();
363
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200364 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 return $this->display_error(
366 array(
367 'Error Number: '.$error_no,
368 $error_msg,
369 $sql
370 )
371 );
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 return FALSE;
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // Stop and aggregate the query time results
378 $time_end = list($em, $es) = explode(' ', microtime());
379 $this->benchmark += ($em + $es) - ($sm + $ss);
380
381 if ($this->save_queries == TRUE)
382 {
383 $this->query_times[] = ($em + $es) - ($sm + $ss);
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // Increment the query counter
387 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // Was the query a "write" type?
390 // If so we'll simply return true
391 if ($this->is_write_type($sql) === TRUE)
392 {
393 // If caching is enabled we'll auto-cleanup any
394 // existing files related to this particular URI
395 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
396 {
397 $this->CACHE->delete();
398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 return TRUE;
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // Return TRUE if we don't need to create a result object
404 // Currently only the Oracle driver uses this when stored
405 // procedures are used
406 if ($return_object !== TRUE)
407 {
408 return TRUE;
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
411 // Load and instantiate the result driver
412
413 $driver = $this->load_rdriver();
414 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 $RES->conn_id = $this->conn_id;
416 $RES->result_id = $this->result_id;
417
418 if ($this->dbdriver == 'oci8')
419 {
420 $RES->stmt_id = $this->stmt_id;
421 $RES->curs_id = NULL;
422 $RES->limit_used = $this->limit_used;
423 $this->stmt_id = FALSE;
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // oci8 vars must be set before calling this
427 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Jones37f4b9c2011-07-01 17:56:50 -0500429 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 // result object and save it to a cache file.
431 if ($this->cache_on == TRUE AND $this->_cache_init())
432 {
433 // We'll create a new instance of the result object
434 // only without the platform specific driver since
435 // we can't use it with cached data (the query result
436 // resource ID won't be any good once we've cached the
437 // result object, so we'll have to compile the data
438 // and save it)
439 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200440 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 $CR->result_object = $RES->result_object();
442 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // Reset these since cached objects can not utilize resource IDs.
445 $CR->conn_id = NULL;
446 $CR->result_id = NULL;
447
448 $this->CACHE->write($sql, $CR);
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 return $RES;
452 }
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Load the result drivers
458 *
459 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200460 * @return string the name of the result class
461 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 function load_rdriver()
463 {
464 $driver = 'CI_DB_'.$this->dbdriver.'_result';
465
466 if ( ! class_exists($driver))
467 {
Greg Aker3a746652011-04-19 10:59:47 -0500468 include_once(BASEPATH.'database/DB_result.php');
469 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 return $driver;
473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
478 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500479 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * we only use it when running transaction commands since they do
481 * not require all the features of the main query() function.
482 *
483 * @access public
484 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200485 * @return mixed
486 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 function simple_query($sql)
488 {
489 if ( ! $this->conn_id)
490 {
491 $this->initialize();
492 }
493
494 return $this->_execute($sql);
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
498
499 /**
500 * Disable Transactions
501 * This permits transactions to be disabled at run-time.
502 *
503 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200504 * @return void
505 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 function trans_off()
507 {
508 $this->trans_enabled = FALSE;
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Enable/disable Transaction Strict Mode
515 * When strict mode is enabled, if you are running multiple groups of
516 * transactions, if one group fails all groups will be rolled back.
517 * If strict mode is disabled, each group is treated autonomously, meaning
518 * a failure of one group will not affect any others
519 *
520 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200521 * @return void
522 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 function trans_strict($mode = TRUE)
524 {
525 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // --------------------------------------------------------------------
529
530 /**
531 * Start Transaction
532 *
533 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200534 * @return void
535 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200537 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 if ( ! $this->trans_enabled)
539 {
540 return FALSE;
541 }
542
543 // When transactions are nested we only begin/commit/rollback the outermost ones
544 if ($this->_trans_depth > 0)
545 {
546 $this->_trans_depth += 1;
547 return;
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500551 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Complete Transaction
558 *
559 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200560 * @return bool
561 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 function trans_complete()
563 {
564 if ( ! $this->trans_enabled)
565 {
566 return FALSE;
567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // When transactions are nested we only begin/commit/rollback the outermost ones
570 if ($this->_trans_depth > 1)
571 {
572 $this->_trans_depth -= 1;
573 return TRUE;
574 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500575 else
576 {
577 $this->_trans_depth = 0;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // The query() function will set this flag to FALSE in the event that a query failed
581 if ($this->_trans_status === FALSE)
582 {
583 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // If we are NOT running in strict mode, we will reset
586 // the _trans_status flag so that subsequent groups of transactions
587 // will be permitted.
588 if ($this->trans_strict === FALSE)
589 {
590 $this->_trans_status = TRUE;
591 }
592
593 log_message('debug', 'DB Transaction Failure');
594 return FALSE;
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 $this->trans_commit();
598 return TRUE;
599 }
600
601 // --------------------------------------------------------------------
602
603 /**
604 * Lets you retrieve the transaction flag to determine if it has failed
605 *
606 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200607 * @return bool
608 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 function trans_status()
610 {
611 return $this->_trans_status;
612 }
613
614 // --------------------------------------------------------------------
615
616 /**
617 * Compile Bindings
618 *
619 * @access public
620 * @param string the sql statement
621 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200622 * @return string
623 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 function compile_binds($sql, $binds)
625 {
626 if (strpos($sql, $this->bind_marker) === FALSE)
627 {
628 return $sql;
629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 if ( ! is_array($binds))
632 {
633 $binds = array($binds);
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // Get the sql segments around the bind markers
637 $segments = explode($this->bind_marker, $sql);
638
639 // The count of bind should be 1 less then the count of segments
640 // If there are more bind arguments trim it down
641 if (count($binds) >= count($segments)) {
642 $binds = array_slice($binds, 0, count($segments)-1);
643 }
644
645 // Construct the binded query
646 $result = $segments[0];
647 $i = 0;
648 foreach ($binds as $bind)
649 {
650 $result .= $this->escape($bind);
651 $result .= $segments[++$i];
652 }
653
654 return $result;
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 // --------------------------------------------------------------------
658
659 /**
660 * Determines if a query is a "write" type.
661 *
662 * @access public
663 * @param string An SQL query string
Barry Mienydd671972010-10-04 16:33:58 +0200664 * @return boolean
665 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 function is_write_type($sql)
667 {
Derek Allarde37ab382009-02-03 16:13:57 +0000668 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 +0000669 {
670 return FALSE;
671 }
672 return TRUE;
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // --------------------------------------------------------------------
676
677 /**
678 * Calculate the aggregate query elapsed time
679 *
680 * @access public
681 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200682 * @return integer
683 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 function elapsed_time($decimals = 6)
685 {
686 return number_format($this->benchmark, $decimals);
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // --------------------------------------------------------------------
690
691 /**
692 * Returns the total number of queries
693 *
694 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200695 * @return integer
696 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 function total_queries()
698 {
699 return $this->query_count;
700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 // --------------------------------------------------------------------
703
704 /**
705 * Returns the last query that was executed
706 *
707 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200708 * @return void
709 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 function last_query()
711 {
712 return end($this->queries);
713 }
714
715 // --------------------------------------------------------------------
716
717 /**
718 * "Smart" Escape String
719 *
720 * Escapes data based on type
721 * Sets boolean and null types
722 *
723 * @access public
724 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200725 * @return mixed
726 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200728 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000729 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000731 $str = "'".$this->escape_str($str)."'";
732 }
733 elseif (is_bool($str))
734 {
735 $str = ($str === FALSE) ? 0 : 1;
736 }
737 elseif (is_null($str))
738 {
739 $str = 'NULL';
740 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000741
742 return $str;
743 }
744
745 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600746
Derek Jonese4ed5832009-02-20 21:44:59 +0000747 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000748 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000749 *
750 * Calls the individual driver for platform
751 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200752 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000753 * @access public
754 * @param string
755 * @return mixed
756 */
Barry Mienydd671972010-10-04 16:33:58 +0200757 function escape_like_str($str)
758 {
759 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000760 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000761
Derek Jonese4ed5832009-02-20 21:44:59 +0000762 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 /**
765 * Primary
766 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500767 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 * position is the primary key
769 *
770 * @access public
771 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200772 * @return string
773 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200775 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 if ( ! is_array($fields))
779 {
780 return FALSE;
781 }
782
783 return current($fields);
784 }
785
786 // --------------------------------------------------------------------
787
788 /**
789 * Returns an array of table names
790 *
791 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200792 * @return array
793 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 function list_tables($constrain_by_prefix = FALSE)
795 {
796 // Is there a cached result?
797 if (isset($this->data_cache['table_names']))
798 {
799 return $this->data_cache['table_names'];
800 }
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
803 {
804 if ($this->db_debug)
805 {
806 return $this->display_error('db_unsupported_function');
807 }
808 return FALSE;
809 }
810
811 $retval = array();
812 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 if ($query->num_rows() > 0)
815 {
Taufan Aditya18209332012-02-09 16:07:27 +0700816 $table = FALSE;
817 $rows = $query->result_array();
818 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
819
820 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 {
Taufan Aditya18209332012-02-09 16:07:27 +0700822 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
823 }
824
825 foreach ($rows as $row)
826 {
827 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 }
829 }
830
831 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 return $this->data_cache['table_names'];
834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // --------------------------------------------------------------------
837
838 /**
839 * Determine if a particular table exists
840 * @access public
841 * @return boolean
842 */
843 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200844 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 // --------------------------------------------------------------------
849
850 /**
851 * Fetch MySQL Field Names
852 *
853 * @access public
854 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200855 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 */
857 function list_fields($table = '')
858 {
859 // Is there a cached result?
860 if (isset($this->data_cache['field_names'][$table]))
861 {
862 return $this->data_cache['field_names'][$table];
863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 if ($table == '')
866 {
867 if ($this->db_debug)
868 {
869 return $this->display_error('db_field_param_missing');
870 }
871 return FALSE;
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Greg Aker1edde302010-01-26 00:17:01 +0000874 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
876 if ($this->db_debug)
877 {
878 return $this->display_error('db_unsupported_function');
879 }
880 return FALSE;
881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500886 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 if (isset($row['COLUMN_NAME']))
889 {
890 $retval[] = $row['COLUMN_NAME'];
891 }
892 else
893 {
894 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200895 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 $this->data_cache['field_names'][$table] = $retval;
899 return $this->data_cache['field_names'][$table];
900 }
901
902 // --------------------------------------------------------------------
903
904 /**
905 * Determine if a particular field exists
906 * @access public
907 * @param string
908 * @param string
909 * @return boolean
910 */
911 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200912 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 // --------------------------------------------------------------------
917
918 /**
919 * Returns an object with field data
920 *
921 * @access public
922 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200923 * @return object
924 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 function field_data($table = '')
926 {
927 if ($table == '')
928 {
929 if ($this->db_debug)
930 {
931 return $this->display_error('db_field_param_missing');
932 }
933 return FALSE;
934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
937
938 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200939 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000940
941 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 /**
944 * Generate an insert string
945 *
946 * @access public
947 * @param string the table upon which the query will be performed
948 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200949 * @return string
950 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 function insert_string($table, $data)
952 {
953 $fields = array();
954 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200955
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500956 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
958 $fields[] = $this->_escape_identifiers($key);
959 $values[] = $this->escape($val);
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200963 }
964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 // --------------------------------------------------------------------
966
967 /**
968 * Generate an update string
969 *
970 * @access public
971 * @param string the table upon which the query will be performed
972 * @param array an associative array data of key/values
973 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200974 * @return string
975 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 function update_string($table, $data, $where)
977 {
978 if ($where == '')
979 {
980 return false;
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500984 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
987 }
988
989 if ( ! is_array($where))
990 {
991 $dest = array($where);
992 }
993 else
994 {
995 $dest = array();
996 foreach ($where as $key => $val)
997 {
998 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300999 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 if ($val !== '')
1002 {
1003 if ( ! $this->_has_operator($key))
1004 {
1005 $key .= ' =';
1006 }
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 $val = ' '.$this->escape($val);
1009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 $dest[] = $prefix.$key.$val;
1012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001014
1015 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001016 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001017
1018 // --------------------------------------------------------------------
1019
1020 /**
1021 * Tests whether the string has an SQL operator
1022 *
1023 * @access private
1024 * @param string
1025 * @return bool
1026 */
1027 function _has_operator($str)
1028 {
1029 $str = trim($str);
1030 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1031 {
1032 return FALSE;
1033 }
1034
1035 return TRUE;
1036 }
1037
1038 // --------------------------------------------------------------------
1039
1040 /**
1041 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1042 *
1043 * @access public
1044 * @param string the function name
1045 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001046 * @return mixed
1047 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 function call_function($function)
1049 {
1050 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 if (FALSE === strpos($driver, $function))
1053 {
1054 $function = $driver.$function;
1055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 if ( ! function_exists($function))
1058 {
1059 if ($this->db_debug)
1060 {
1061 return $this->display_error('db_unsupported_function');
1062 }
1063 return FALSE;
1064 }
1065 else
1066 {
1067 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1068
Repox71b78092011-12-01 09:19:43 +01001069 if (is_null($args))
1070 {
1071 return call_user_func($function);
1072 }
1073 else
1074 {
1075 return call_user_func_array($function, $args);
1076 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 }
1078 }
1079
1080 // --------------------------------------------------------------------
1081
1082 /**
1083 * Set Cache Directory Path
1084 *
1085 * @access public
1086 * @param string the path to the cache directory
1087 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001088 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 function cache_set_path($path = '')
1090 {
1091 $this->cachedir = $path;
1092 }
1093
1094 // --------------------------------------------------------------------
1095
1096 /**
1097 * Enable Query Caching
1098 *
1099 * @access public
1100 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001101 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 function cache_on()
1103 {
1104 $this->cache_on = TRUE;
1105 return TRUE;
1106 }
1107
1108 // --------------------------------------------------------------------
1109
1110 /**
1111 * Disable Query Caching
1112 *
1113 * @access public
1114 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001115 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 function cache_off()
1117 {
1118 $this->cache_on = FALSE;
1119 return FALSE;
1120 }
Barry Mienydd671972010-10-04 16:33:58 +02001121
Derek Allard2067d1a2008-11-13 22:59:24 +00001122
1123 // --------------------------------------------------------------------
1124
1125 /**
1126 * Delete the cache files associated with a particular URI
1127 *
1128 * @access public
1129 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001130 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 function cache_delete($segment_one = '', $segment_two = '')
1132 {
1133 if ( ! $this->_cache_init())
1134 {
1135 return FALSE;
1136 }
1137 return $this->CACHE->delete($segment_one, $segment_two);
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Delete All cache files
1144 *
1145 * @access public
1146 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001147 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 function cache_delete_all()
1149 {
1150 if ( ! $this->_cache_init())
1151 {
1152 return FALSE;
1153 }
1154
1155 return $this->CACHE->delete_all();
1156 }
1157
1158 // --------------------------------------------------------------------
1159
1160 /**
1161 * Initialize the Cache Class
1162 *
1163 * @access private
1164 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001165 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 function _cache_init()
1167 {
1168 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1169 {
1170 return TRUE;
1171 }
Derek Allarde37ab382009-02-03 16:13:57 +00001172
1173 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Greg Aker3a746652011-04-19 10:59:47 -05001175 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001176 {
1177 return $this->cache_off();
1178 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
Derek Allarde37ab382009-02-03 16:13:57 +00001180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1182 return TRUE;
1183 }
1184
1185 // --------------------------------------------------------------------
1186
1187 /**
1188 * Close DB Connection
1189 *
1190 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001191 * @return void
1192 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 function close()
1194 {
1195 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1196 {
1197 $this->_close($this->conn_id);
1198 }
1199 $this->conn_id = FALSE;
1200 }
Barry Mienydd671972010-10-04 16:33:58 +02001201
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 // --------------------------------------------------------------------
1203
1204 /**
1205 * Display an error message
1206 *
1207 * @access public
1208 * @param string the error message
1209 * @param string any "swap" values
1210 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001211 * @return string sends the application/error_db.php template
1212 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 function display_error($error = '', $swap = '', $native = FALSE)
1214 {
Derek Jonese7792202010-03-02 17:24:46 -06001215 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 $LANG->load('db');
1217
1218 $heading = $LANG->line('db_error_heading');
1219
1220 if ($native == TRUE)
1221 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001222 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 }
1224 else
1225 {
1226 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1227 }
Barry Mienydd671972010-10-04 16:33:58 +02001228
Pascal Kriete60f8c392010-08-25 18:03:28 +02001229 // Find the most likely culprit of the error by going through
1230 // the backtrace until the source file is no longer in the
1231 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001232
Pascal Kriete60f8c392010-08-25 18:03:28 +02001233 $trace = debug_backtrace();
1234
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001235 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001236 {
1237 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1238 {
1239 // Found it - use a relative path for safety
1240 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1241 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001242
Pascal Kriete60f8c392010-08-25 18:03:28 +02001243 break;
1244 }
1245 }
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Jonese7792202010-03-02 17:24:46 -06001247 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 echo $error->show_error($heading, $message, 'error_db');
1249 exit;
1250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Protect Identifiers
1256 *
1257 * This function adds backticks if appropriate based on db type
1258 *
1259 * @access private
1260 * @param mixed the item to escape
1261 * @return mixed the item with backticks
1262 */
1263 function protect_identifiers($item, $prefix_single = FALSE)
1264 {
1265 return $this->_protect_identifiers($item, $prefix_single);
1266 }
1267
1268 // --------------------------------------------------------------------
1269
1270 /**
1271 * Protect Identifiers
1272 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001273 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001274 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001276 * the table prefix onto it. Some logic is necessary in order to deal with
1277 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 *
1279 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1280 *
1281 * Or a query with aliasing:
1282 *
1283 * SELECT m.member_id, m.member_name FROM members AS m
1284 *
1285 * Since the column name can include up to four segments (host, DB, table, column)
1286 * or also have an alias prefix, we need to do a bit of work to figure this out and
1287 * insert the table prefix (if it exists) in the proper position, and escape only
1288 * the correct identifiers.
1289 *
1290 * @access private
1291 * @param string
1292 * @param bool
1293 * @param mixed
1294 * @param bool
1295 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001296 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
1298 {
1299 if ( ! is_bool($protect_identifiers))
1300 {
1301 $protect_identifiers = $this->_protect_identifiers;
1302 }
Derek Allarde37ab382009-02-03 16:13:57 +00001303
1304 if (is_array($item))
1305 {
1306 $escaped_array = array();
1307
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001308 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001309 {
1310 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1311 }
1312
1313 return $escaped_array;
1314 }
1315
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001317 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 // If the item has an alias declaration we remove it and set it aside.
1320 // Basically we remove everything to the right of the first space
1321 $alias = '';
1322 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001323 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 $alias = strstr($item, " ");
1325 $item = substr($item, 0, - strlen($alias));
1326 }
1327
Derek Allard911d3e02008-12-15 14:08:35 +00001328 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001329 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001330 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001331 // way to deal with this, but I'm not thinking of it -- Rick
1332 if (strpos($item, '(') !== FALSE)
1333 {
1334 return $item.$alias;
1335 }
1336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 // Break the string apart if it contains periods, then insert the table prefix
1338 // in the correct location, assuming the period doesn't indicate that we're dealing
1339 // with an alias. While we're at it, we will escape the components
1340 if (strpos($item, '.') !== FALSE)
1341 {
1342 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001345 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001347 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001348 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 if ($protect_identifiers === TRUE)
1350 {
1351 foreach ($parts as $key => $val)
1352 {
1353 if ( ! in_array($val, $this->_reserved_identifiers))
1354 {
1355 $parts[$key] = $this->_escape_identifiers($val);
1356 }
1357 }
Barry Mienydd671972010-10-04 16:33:58 +02001358
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001360 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 return $item.$alias;
1362 }
Barry Mienydd671972010-10-04 16:33:58 +02001363
Derek Jones37f4b9c2011-07-01 17:56:50 -05001364 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 if ($this->dbprefix != '')
1366 {
1367 // We now add the table prefix based on some logic.
1368 // Do we have 4 segments (hostname.database.table.column)?
1369 // If so, we add the table prefix to the column name in the 3rd segment.
1370 if (isset($parts[3]))
1371 {
1372 $i = 2;
1373 }
1374 // Do we have 3 segments (database.table.column)?
1375 // If so, we add the table prefix to the column name in 2nd position
1376 elseif (isset($parts[2]))
1377 {
1378 $i = 1;
1379 }
1380 // Do we have 2 segments (table.column)?
1381 // If so, we add the table prefix to the column name in 1st segment
1382 else
1383 {
1384 $i = 0;
1385 }
Barry Mienydd671972010-10-04 16:33:58 +02001386
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 // This flag is set when the supplied $item does not contain a field name.
1388 // This can happen when this function is being called from a JOIN.
1389 if ($field_exists == FALSE)
1390 {
1391 $i++;
1392 }
Barry Mienydd671972010-10-04 16:33:58 +02001393
Derek Jones55acc8b2009-07-11 03:38:13 +00001394 // Verify table prefix and replace if necessary
1395 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1396 {
1397 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1398 }
Barry Mienydd671972010-10-04 16:33:58 +02001399
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 // We only add the table prefix if it does not already exist
1401 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1402 {
1403 $parts[$i] = $this->dbprefix.$parts[$i];
1404 }
Barry Mienydd671972010-10-04 16:33:58 +02001405
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 // Put the parts back together
1407 $item = implode('.', $parts);
1408 }
Barry Mienydd671972010-10-04 16:33:58 +02001409
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 if ($protect_identifiers === TRUE)
1411 {
1412 $item = $this->_escape_identifiers($item);
1413 }
Barry Mienydd671972010-10-04 16:33:58 +02001414
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 return $item.$alias;
1416 }
1417
Derek Jones37f4b9c2011-07-01 17:56:50 -05001418 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 if ($this->dbprefix != '')
1420 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001421 // Verify table prefix and replace if necessary
1422 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1423 {
1424 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1425 }
1426
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 // Do we prefix an item with no segments?
1428 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1429 {
1430 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001431 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 }
1433
1434 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1435 {
1436 $item = $this->_escape_identifiers($item);
1437 }
Barry Mienydd671972010-10-04 16:33:58 +02001438
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 return $item.$alias;
1440 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001441}
1442
Derek Allard2067d1a2008-11-13 22:59:24 +00001443/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001444/* Location: ./system/database/DB_driver.php */