blob: 7309326e2c0e7ac555c647ba670e53b75a056337 [file] [log] [blame]
Andrey Andreev569091c2012-02-09 00:16:17 +02001<?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
Andrey Andreev569091c2012-02-09 00:16:17 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev569091c2012-02-09 00:16:17 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Andrey Andreev569091c2012-02-09 00:16:17 +020045 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
Derek Allard2067d1a2008-11-13 22:59:24 +000075
Andrey Andreev569091c2012-02-09 00:16:17 +020076 // Private variables
77 var $_protect_identifiers = TRUE;
78 var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 // These are use with Oracle
Andrey Andreev569091c2012-02-09 00:16:17 +020081 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 */
Andrey Andreev569091c2012-02-09 00:16:17 +020092 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
94 if (is_array($params))
95 {
96 foreach ($params as $key => $val)
97 {
98 $this->$key = $val;
99 }
100 }
101
102 log_message('debug', 'Database Driver Class Initialized');
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
106
107 /**
108 * Initialize Database Settings
109 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200110 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200111 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200112 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
114 // If an existing connection resource is available
115 // there is no need to connect and select the database
116 if (is_resource($this->conn_id) OR is_object($this->conn_id))
117 {
118 return TRUE;
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // Connect to the database and set the connection ID
124 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
125
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200126 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if ( ! $this->conn_id)
128 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100129 // Check if there is a failover set
130 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100132 // Go over all the failovers
133 foreach ($this->failover as $failover)
134 {
135 // Replace the current settings with those of the failover
136 foreach ($failover as $key => $val)
137 {
138 $this->$key = $val;
139 }
140
141 // Try to connect
142 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
143
144 // If a connection is made break the foreach loop
145 if ($this->conn_id)
146 {
147 break;
148 }
149 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100151
152 // We still don't have a connection?
153 if ( ! $this->conn_id)
154 {
155 log_message('error', 'Unable to connect to the database');
156
157 if ($this->db_debug)
158 {
159 $this->display_error('db_unable_to_connect');
160 }
161 return FALSE;
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 // ----------------------------------------------------------------
166
167 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200170 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200171
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200172 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200174 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200176 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200179 // Now we set the character set and that's all
180 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Set client character set
187 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200188 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string
190 * @param string
Andrey Andreev569091c2012-02-09 00:16:17 +0200191 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200193 function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
196 {
197 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 if ($this->db_debug)
200 {
201 $this->display_error('db_unable_to_set_charset', $this->char_set);
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 return FALSE;
205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 return TRUE;
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // --------------------------------------------------------------------
211
212 /**
213 * The name of the platform in use (mysql, mssql, etc...)
214 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200215 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return string
217 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200218 function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
220 return $this->dbdriver;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500226 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * version of the database being used
228 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200229 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200230 * @return string
231 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200232 function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 if (FALSE === ($sql = $this->_version()))
235 {
236 if ($this->db_debug)
237 {
238 return $this->display_error('db_unsupported_function');
239 }
240 return FALSE;
241 }
Derek Allard3683f772009-12-16 17:32:33 +0000242
243 // Some DBs have functions that return the version, and don't run special
244 // SQL queries per se. In these instances, just return the result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200245 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli');
Derek Allard3683f772009-12-16 17:32:33 +0000246
247 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 return $sql;
250 }
Derek Allard3683f772009-12-16 17:32:33 +0000251 else
252 {
253 $query = $this->query($sql);
254 return $query->row('ver');
255 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
261 * Execute the query
262 *
263 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500264 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 * upon successful execution of a "write" type query. Returns boolean
266 * FALSE upon failure, and if the $db_debug variable is set to TRUE
267 * will raise an error.
268 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200269 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param string An SQL query string
271 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200272 * @return mixed
273 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200274 function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 if ($sql == '')
277 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200278 log_message('error', 'Invalid query: '.$sql);
279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 if ($this->db_debug)
281 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 return $this->display_error('db_invalid_query');
283 }
284 return FALSE;
285 }
286
287 // Verify table prefix and replace if necessary
Andrey Andreev569091c2012-02-09 00:16:17 +0200288 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600289 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200290 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
Derek Jonese7792202010-03-02 17:24:46 -0600292
Derek Jones37f4b9c2011-07-01 17:56:50 -0500293 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // we will load the caching class and return the previously
295 // cached query if it exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200296 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 if ($this->_cache_init())
299 {
300 $this->load_rdriver();
301 if (FALSE !== ($cache = $this->CACHE->read($sql)))
302 {
303 return $cache;
304 }
305 }
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // Compile binds if needed
309 if ($binds !== FALSE)
310 {
311 $sql = $this->compile_binds($sql, $binds);
312 }
313
Derek Jones37f4b9c2011-07-01 17:56:50 -0500314 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 if ($this->save_queries == TRUE)
316 {
317 $this->queries[] = $sql;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Start the Query Timer
321 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Run the Query
324 if (FALSE === ($this->result_id = $this->simple_query($sql)))
325 {
326 if ($this->save_queries == TRUE)
327 {
328 $this->query_times[] = 0;
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // This will trigger a rollback if transactions are being used
332 $this->_trans_status = FALSE;
333
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200334 // Grab the error number and message now, as we might run some
335 // additional queries before displaying the error
336 $error_no = $this->_error_number();
337 $error_msg = $this->_error_message();
338
339 // Log errors
340 log_message('error', 'Query error: '.$error_msg);
341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 if ($this->db_debug)
343 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500345 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200346 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // transactions to remain in limbo.
348 $this->trans_complete();
349
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200350 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 return $this->display_error(
352 array(
353 'Error Number: '.$error_no,
354 $error_msg,
355 $sql
356 )
357 );
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 return FALSE;
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Stop and aggregate the query time results
364 $time_end = list($em, $es) = explode(' ', microtime());
365 $this->benchmark += ($em + $es) - ($sm + $ss);
366
367 if ($this->save_queries == TRUE)
368 {
369 $this->query_times[] = ($em + $es) - ($sm + $ss);
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // Increment the query counter
373 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // Was the query a "write" type?
376 // If so we'll simply return true
377 if ($this->is_write_type($sql) === TRUE)
378 {
379 // If caching is enabled we'll auto-cleanup any
380 // existing files related to this particular URI
Andrey Andreev569091c2012-02-09 00:16:17 +0200381 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 $this->CACHE->delete();
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 return TRUE;
387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // Return TRUE if we don't need to create a result object
390 // Currently only the Oracle driver uses this when stored
391 // procedures are used
392 if ($return_object !== TRUE)
393 {
394 return TRUE;
395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
397 // Load and instantiate the result driver
Barry Mienydd671972010-10-04 16:33:58 +0200398 $driver = $this->load_rdriver();
399 $RES = new $driver();
Andrey Andreev569091c2012-02-09 00:16:17 +0200400 $RES->conn_id = $this->conn_id;
401 $RES->result_id = $this->result_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402
Andrey Andreev569091c2012-02-09 00:16:17 +0200403 if ($this->dbdriver === 'oci8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 $RES->stmt_id = $this->stmt_id;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200406 $RES->curs_id = $this->curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 $RES->limit_used = $this->limit_used;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200408 // Passing the next one by reference to make sure it's updated, if needed:
409 $RES->commit_mode = &$this->_commit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 $this->stmt_id = FALSE;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Jones37f4b9c2011-07-01 17:56:50 -0500413 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // result object and save it to a cache file.
Andrey Andreev569091c2012-02-09 00:16:17 +0200415 if ($this->cache_on == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 // We'll create a new instance of the result object
418 // only without the platform specific driver since
419 // we can't use it with cached data (the query result
420 // resource ID won't be any good once we've cached the
421 // result object, so we'll have to compile the data
422 // and save it)
423 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 $CR->result_object = $RES->result_object();
425 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200426 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 // Reset these since cached objects can not utilize resource IDs.
429 $CR->conn_id = NULL;
430 $CR->result_id = NULL;
431
432 $this->CACHE->write($sql, $CR);
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 return $RES;
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Load the result drivers
442 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200443 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200444 * @return string the name of the result class
445 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200446 function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 $driver = 'CI_DB_'.$this->dbdriver.'_result';
449
450 if ( ! class_exists($driver))
451 {
Greg Aker3a746652011-04-19 10:59:47 -0500452 include_once(BASEPATH.'database/DB_result.php');
453 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 return $driver;
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // --------------------------------------------------------------------
460
461 /**
462 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500463 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * we only use it when running transaction commands since they do
465 * not require all the features of the main query() function.
466 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200467 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200469 * @return mixed
470 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200471 function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 if ( ! $this->conn_id)
474 {
475 $this->initialize();
476 }
477
478 return $this->_execute($sql);
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 // --------------------------------------------------------------------
482
483 /**
484 * Disable Transactions
485 * This permits transactions to be disabled at run-time.
486 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200487 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200488 * @return void
489 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200490 function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 $this->trans_enabled = FALSE;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Enable/disable Transaction Strict Mode
499 * When strict mode is enabled, if you are running multiple groups of
500 * transactions, if one group fails all groups will be rolled back.
501 * If strict mode is disabled, each group is treated autonomously, meaning
502 * a failure of one group will not affect any others
503 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200504 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200505 * @return void
506 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200507 function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Start Transaction
516 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200517 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200518 * @return void
519 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200520 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200521 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 if ( ! $this->trans_enabled)
523 {
524 return FALSE;
525 }
526
527 // When transactions are nested we only begin/commit/rollback the outermost ones
528 if ($this->_trans_depth > 0)
529 {
530 $this->_trans_depth += 1;
531 return;
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500535 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Complete Transaction
542 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200543 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200544 * @return bool
545 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200546 function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
548 if ( ! $this->trans_enabled)
549 {
550 return FALSE;
551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // When transactions are nested we only begin/commit/rollback the outermost ones
554 if ($this->_trans_depth > 1)
555 {
556 $this->_trans_depth -= 1;
557 return TRUE;
558 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500559 else
560 {
561 $this->_trans_depth = 0;
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 // The query() function will set this flag to FALSE in the event that a query failed
565 if ($this->_trans_status === FALSE)
566 {
567 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // If we are NOT running in strict mode, we will reset
570 // the _trans_status flag so that subsequent groups of transactions
571 // will be permitted.
572 if ($this->trans_strict === FALSE)
573 {
574 $this->_trans_status = TRUE;
575 }
576
577 log_message('debug', 'DB Transaction Failure');
578 return FALSE;
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 $this->trans_commit();
582 return TRUE;
583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Lets you retrieve the transaction flag to determine if it has failed
589 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200590 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200591 * @return bool
592 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200593 function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 return $this->_trans_status;
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Compile Bindings
602 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200603 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @param string the sql statement
605 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200606 * @return string
607 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200608 function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 if (strpos($sql, $this->bind_marker) === FALSE)
611 {
612 return $sql;
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 if ( ! is_array($binds))
616 {
617 $binds = array($binds);
618 }
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 // Get the sql segments around the bind markers
621 $segments = explode($this->bind_marker, $sql);
622
623 // The count of bind should be 1 less then the count of segments
624 // If there are more bind arguments trim it down
625 if (count($binds) >= count($segments)) {
626 $binds = array_slice($binds, 0, count($segments)-1);
627 }
628
629 // Construct the binded query
630 $result = $segments[0];
631 $i = 0;
632 foreach ($binds as $bind)
633 {
634 $result .= $this->escape($bind);
635 $result .= $segments[++$i];
636 }
637
638 return $result;
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // --------------------------------------------------------------------
642
643 /**
644 * Determines if a query is a "write" type.
645 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200646 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @param string An SQL query string
Andrey Andreev569091c2012-02-09 00:16:17 +0200648 * @return boolean
Barry Mienydd671972010-10-04 16:33:58 +0200649 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200650 function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200652 if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
653 {
654 return FALSE;
655 }
656 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // --------------------------------------------------------------------
660
661 /**
662 * Calculate the aggregate query elapsed time
663 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200664 * @access public
665 * @param integer The number of decimal places
666 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200667 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200668 function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
670 return number_format($this->benchmark, $decimals);
671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // --------------------------------------------------------------------
674
675 /**
676 * Returns the total number of queries
677 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200678 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200679 * @return integer
680 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200681 function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 return $this->query_count;
684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 // --------------------------------------------------------------------
687
688 /**
689 * Returns the last query that was executed
690 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200691 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200692 * @return void
693 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200694 function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
696 return end($this->queries);
697 }
698
699 // --------------------------------------------------------------------
700
701 /**
702 * "Smart" Escape String
703 *
704 * Escapes data based on type
705 * Sets boolean and null types
706 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200707 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200709 * @return mixed
710 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200711 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200712 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000713 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000715 $str = "'".$this->escape_str($str)."'";
716 }
717 elseif (is_bool($str))
718 {
719 $str = ($str === FALSE) ? 0 : 1;
720 }
721 elseif (is_null($str))
722 {
723 $str = 'NULL';
724 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000725
726 return $str;
727 }
728
729 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600730
Derek Jonese4ed5832009-02-20 21:44:59 +0000731 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000732 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000733 *
734 * Calls the individual driver for platform
735 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200736 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200737 * @access public
Derek Jonese4ed5832009-02-20 21:44:59 +0000738 * @param string
739 * @return mixed
740 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200741 function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200742 {
743 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000744 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000745
Derek Jonese4ed5832009-02-20 21:44:59 +0000746 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 /**
749 * Primary
750 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200751 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 * position is the primary key
753 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200754 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200756 * @return string
757 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200758 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200759 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 if ( ! is_array($fields))
763 {
764 return FALSE;
765 }
766
767 return current($fields);
768 }
769
770 // --------------------------------------------------------------------
771
772 /**
773 * Returns an array of table names
774 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200775 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200776 * @return array
777 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200778 function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 // Is there a cached result?
781 if (isset($this->data_cache['table_names']))
782 {
783 return $this->data_cache['table_names'];
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
787 {
788 if ($this->db_debug)
789 {
790 return $this->display_error('db_unsupported_function');
791 }
792 return FALSE;
793 }
794
795 $retval = array();
796 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 if ($query->num_rows() > 0)
799 {
Taufan Aditya18209332012-02-09 16:07:27 +0700800 $table = FALSE;
801 $rows = $query->result_array();
802 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
803
804 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
Taufan Aditya18209332012-02-09 16:07:27 +0700806 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
807 }
808
809 foreach ($rows as $row)
810 {
811 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813 }
814
815 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 return $this->data_cache['table_names'];
818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 // --------------------------------------------------------------------
821
822 /**
823 * Determine if a particular table exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200824 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 * @return boolean
826 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200827 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200828 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200829 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 // --------------------------------------------------------------------
833
834 /**
Andrey Andreev569091c2012-02-09 00:16:17 +0200835 * Fetch MySQL Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200837 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200839 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200841 function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
843 // Is there a cached result?
844 if (isset($this->data_cache['field_names'][$table]))
845 {
846 return $this->data_cache['field_names'][$table];
847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 if ($table == '')
850 {
851 if ($this->db_debug)
852 {
853 return $this->display_error('db_field_param_missing');
854 }
855 return FALSE;
856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Greg Aker1edde302010-01-26 00:17:01 +0000858 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
860 if ($this->db_debug)
861 {
862 return $this->display_error('db_unsupported_function');
863 }
864 return FALSE;
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500870 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
872 if (isset($row['COLUMN_NAME']))
873 {
874 $retval[] = $row['COLUMN_NAME'];
875 }
876 else
877 {
878 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200879 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 $this->data_cache['field_names'][$table] = $retval;
883 return $this->data_cache['field_names'][$table];
884 }
885
886 // --------------------------------------------------------------------
887
888 /**
889 * Determine if a particular field exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200890 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @param string
892 * @param string
893 * @return boolean
894 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200895 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200896 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200897 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // --------------------------------------------------------------------
901
902 /**
903 * Returns an object with field data
904 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200905 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200907 * @return object
908 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200909 function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
911 if ($table == '')
912 {
913 if ($this->db_debug)
914 {
915 return $this->display_error('db_field_param_missing');
916 }
917 return FALSE;
918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev569091c2012-02-09 00:16:17 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200923 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000924
925 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 /**
928 * Generate an insert string
929 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200930 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 * @param string the table upon which the query will be performed
932 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200933 * @return string
934 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200935 function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
937 $fields = array();
938 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200939
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500940 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
942 $fields[] = $this->_escape_identifiers($key);
943 $values[] = $this->escape($val);
944 }
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200947 }
948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 // --------------------------------------------------------------------
950
951 /**
952 * Generate an update string
953 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200954 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 * @param string the table upon which the query will be performed
956 * @param array an associative array data of key/values
957 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200958 * @return string
959 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200960 function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
962 if ($where == '')
963 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200964 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500968 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
970 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
971 }
972
973 if ( ! is_array($where))
974 {
975 $dest = array($where);
976 }
977 else
978 {
979 $dest = array();
980 foreach ($where as $key => $val)
981 {
982 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300983 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 if ($val !== '')
986 {
987 if ( ! $this->_has_operator($key))
988 {
989 $key .= ' =';
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 $val = ' '.$this->escape($val);
993 }
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 $dest[] = $prefix.$key.$val;
996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000998
999 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001000 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001001
1002 // --------------------------------------------------------------------
1003
1004 /**
1005 * Tests whether the string has an SQL operator
1006 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001007 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 * @param string
1009 * @return bool
1010 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001011 function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001013 $str = trim($str);
1014 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1015 {
1016 return FALSE;
1017 }
1018
1019 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 }
1021
1022 // --------------------------------------------------------------------
1023
1024 /**
1025 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1026 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001027 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @param string the function name
1029 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001030 * @return mixed
1031 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001032 function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
1034 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 if (FALSE === strpos($driver, $function))
1037 {
1038 $function = $driver.$function;
1039 }
Barry Mienydd671972010-10-04 16:33:58 +02001040
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 if ( ! function_exists($function))
1042 {
1043 if ($this->db_debug)
1044 {
1045 return $this->display_error('db_unsupported_function');
1046 }
1047 return FALSE;
1048 }
1049 else
1050 {
1051 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1052
Repox71b78092011-12-01 09:19:43 +01001053 if (is_null($args))
1054 {
1055 return call_user_func($function);
1056 }
1057 else
1058 {
1059 return call_user_func_array($function, $args);
1060 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 }
1062 }
1063
1064 // --------------------------------------------------------------------
1065
1066 /**
1067 * Set Cache Directory Path
1068 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001069 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 * @param string the path to the cache directory
1071 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001072 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001073 function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
1075 $this->cachedir = $path;
1076 }
1077
1078 // --------------------------------------------------------------------
1079
1080 /**
1081 * Enable Query Caching
1082 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001083 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001085 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001086 function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001088 $this->cache_on = TRUE;
1089 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 }
1091
1092 // --------------------------------------------------------------------
1093
1094 /**
1095 * Disable Query Caching
1096 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001097 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001099 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001100 function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001102 $this->cache_on = FALSE;
1103 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 }
Barry Mienydd671972010-10-04 16:33:58 +02001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
1107 // --------------------------------------------------------------------
1108
1109 /**
1110 * Delete the cache files associated with a particular URI
1111 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001112 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001114 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001115 function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 {
1117 if ( ! $this->_cache_init())
1118 {
1119 return FALSE;
1120 }
1121 return $this->CACHE->delete($segment_one, $segment_two);
1122 }
1123
1124 // --------------------------------------------------------------------
1125
1126 /**
1127 * Delete All cache files
1128 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001129 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001131 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001132 function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 {
1134 if ( ! $this->_cache_init())
1135 {
1136 return FALSE;
1137 }
1138
1139 return $this->CACHE->delete_all();
1140 }
1141
1142 // --------------------------------------------------------------------
1143
1144 /**
1145 * Initialize the Cache Class
1146 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001147 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001149 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001150 function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001152 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
1154 return TRUE;
1155 }
Derek Allarde37ab382009-02-03 16:13:57 +00001156
Andrey Andreev569091c2012-02-09 00:16:17 +02001157 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001159 if ( ! @include(BASEPATH.'database/DB_cache.php'))
1160 {
1161 return $this->cache_off();
1162 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 }
Derek Allarde37ab382009-02-03 16:13:57 +00001164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1166 return TRUE;
1167 }
1168
1169 // --------------------------------------------------------------------
1170
1171 /**
1172 * Close DB Connection
1173 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001174 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001175 * @return void
1176 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001177 function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 {
1179 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1180 {
1181 $this->_close($this->conn_id);
1182 }
1183 $this->conn_id = FALSE;
1184 }
Barry Mienydd671972010-10-04 16:33:58 +02001185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 // --------------------------------------------------------------------
1187
1188 /**
1189 * Display an error message
1190 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001191 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 * @param string the error message
1193 * @param string any "swap" values
1194 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001195 * @return string sends the application/error_db.php template
1196 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001197 function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 {
Derek Jonese7792202010-03-02 17:24:46 -06001199 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 $LANG->load('db');
1201
1202 $heading = $LANG->line('db_error_heading');
1203
1204 if ($native == TRUE)
1205 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001206 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 }
1208 else
1209 {
1210 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Pascal Kriete60f8c392010-08-25 18:03:28 +02001213 // Find the most likely culprit of the error by going through
1214 // the backtrace until the source file is no longer in the
1215 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001216
Pascal Kriete60f8c392010-08-25 18:03:28 +02001217 $trace = debug_backtrace();
1218
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001219 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001220 {
1221 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1222 {
1223 // Found it - use a relative path for safety
1224 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1225 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001226
Pascal Kriete60f8c392010-08-25 18:03:28 +02001227 break;
1228 }
1229 }
Barry Mienydd671972010-10-04 16:33:58 +02001230
Derek Jonese7792202010-03-02 17:24:46 -06001231 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 echo $error->show_error($heading, $message, 'error_db');
1233 exit;
1234 }
1235
1236 // --------------------------------------------------------------------
1237
1238 /**
1239 * Protect Identifiers
1240 *
1241 * This function adds backticks if appropriate based on db type
1242 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001243 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 * @param mixed the item to escape
1245 * @return mixed the item with backticks
1246 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001247 function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 {
1249 return $this->_protect_identifiers($item, $prefix_single);
1250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Protect Identifiers
1256 *
1257 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001258 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001260 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev569091c2012-02-09 00:16:17 +02001261 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 *
1263 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1264 *
1265 * Or a query with aliasing:
1266 *
1267 * SELECT m.member_id, m.member_name FROM members AS m
1268 *
1269 * Since the column name can include up to four segments (host, DB, table, column)
1270 * or also have an alias prefix, we need to do a bit of work to figure this out and
1271 * insert the table prefix (if it exists) in the proper position, and escape only
1272 * the correct identifiers.
1273 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001274 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 * @param string
1276 * @param bool
1277 * @param mixed
1278 * @param bool
1279 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001280 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001281 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 {
1283 if ( ! is_bool($protect_identifiers))
1284 {
1285 $protect_identifiers = $this->_protect_identifiers;
1286 }
Derek Allarde37ab382009-02-03 16:13:57 +00001287
1288 if (is_array($item))
1289 {
1290 $escaped_array = array();
1291
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001292 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001293 {
1294 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1295 }
1296
1297 return $escaped_array;
1298 }
1299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001301 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001302
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 // If the item has an alias declaration we remove it and set it aside.
1304 // Basically we remove everything to the right of the first space
1305 $alias = '';
1306 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001307 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 $alias = strstr($item, " ");
1309 $item = substr($item, 0, - strlen($alias));
1310 }
1311
Derek Allard911d3e02008-12-15 14:08:35 +00001312 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001313 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001314 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001315 // way to deal with this, but I'm not thinking of it -- Rick
1316 if (strpos($item, '(') !== FALSE)
1317 {
1318 return $item.$alias;
1319 }
1320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 // Break the string apart if it contains periods, then insert the table prefix
1322 // in the correct location, assuming the period doesn't indicate that we're dealing
1323 // with an alias. While we're at it, we will escape the components
1324 if (strpos($item, '.') !== FALSE)
1325 {
1326 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 // Does the first segment of the exploded item match
Andrey Andreev569091c2012-02-09 00:16:17 +02001329 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 // we have nothing more to do other than escape the item
1331 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001332 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 if ($protect_identifiers === TRUE)
1334 {
1335 foreach ($parts as $key => $val)
1336 {
1337 if ( ! in_array($val, $this->_reserved_identifiers))
1338 {
1339 $parts[$key] = $this->_escape_identifiers($val);
1340 }
1341 }
Barry Mienydd671972010-10-04 16:33:58 +02001342
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001344 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 return $item.$alias;
1346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Andrey Andreev569091c2012-02-09 00:16:17 +02001348 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 if ($this->dbprefix != '')
1350 {
1351 // We now add the table prefix based on some logic.
1352 // Do we have 4 segments (hostname.database.table.column)?
1353 // If so, we add the table prefix to the column name in the 3rd segment.
1354 if (isset($parts[3]))
1355 {
1356 $i = 2;
1357 }
1358 // Do we have 3 segments (database.table.column)?
1359 // If so, we add the table prefix to the column name in 2nd position
1360 elseif (isset($parts[2]))
1361 {
1362 $i = 1;
1363 }
1364 // Do we have 2 segments (table.column)?
1365 // If so, we add the table prefix to the column name in 1st segment
1366 else
1367 {
1368 $i = 0;
1369 }
Barry Mienydd671972010-10-04 16:33:58 +02001370
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 // This flag is set when the supplied $item does not contain a field name.
1372 // This can happen when this function is being called from a JOIN.
1373 if ($field_exists == FALSE)
1374 {
1375 $i++;
1376 }
Barry Mienydd671972010-10-04 16:33:58 +02001377
Derek Jones55acc8b2009-07-11 03:38:13 +00001378 // Verify table prefix and replace if necessary
1379 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1380 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001381 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001382 }
Barry Mienydd671972010-10-04 16:33:58 +02001383
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 // We only add the table prefix if it does not already exist
1385 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1386 {
1387 $parts[$i] = $this->dbprefix.$parts[$i];
1388 }
Barry Mienydd671972010-10-04 16:33:58 +02001389
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 // Put the parts back together
1391 $item = implode('.', $parts);
1392 }
Barry Mienydd671972010-10-04 16:33:58 +02001393
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 if ($protect_identifiers === TRUE)
1395 {
1396 $item = $this->_escape_identifiers($item);
1397 }
Barry Mienydd671972010-10-04 16:33:58 +02001398
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 return $item.$alias;
1400 }
1401
Andrey Andreev569091c2012-02-09 00:16:17 +02001402 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 if ($this->dbprefix != '')
1404 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001405 // Verify table prefix and replace if necessary
1406 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1407 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001408 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001409 }
1410
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 // Do we prefix an item with no segments?
Andrey Andreev569091c2012-02-09 00:16:17 +02001412 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 {
1414 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001415 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 }
1417
Andrey Andreev569091c2012-02-09 00:16:17 +02001418 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 {
1420 $item = $this->_escape_identifiers($item);
1421 }
Barry Mienydd671972010-10-04 16:33:58 +02001422
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 return $item.$alias;
1424 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001425}
1426
Derek Allard2067d1a2008-11-13 22:59:24 +00001427/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001428/* Location: ./system/database/DB_driver.php */