blob: 39eb1cc8a6f0bee08fb4b2583ec06ae82c50ce1b [file] [log] [blame]
Andrey Andreevaf5d5582012-01-25 21:34:47 +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 Andreevaf5d5582012-01-25 21:34:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevaf5d5582012-01-25 21:34:47 +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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_driver {
42
Andrey Andreevaf5d5582012-01-25 21:34:47 +020043 public $username;
44 public $password;
45 public $hostname;
46 public $database;
47 public $dbdriver = 'mysql';
48 public $dbprefix = '';
49 public $char_set = 'utf8';
50 public $dbcollat = 'utf8_general_ci';
51 public $autoinit = TRUE; // Whether to automatically initialize the DB
52 public $swap_pre = '';
53 public $port = '';
54 public $pconnect = FALSE;
55 public $conn_id = FALSE;
56 public $result_id = FALSE;
57 public $db_debug = FALSE;
58 public $benchmark = 0;
59 public $query_count = 0;
60 public $bind_marker = '?';
61 public $save_queries = TRUE;
62 public $queries = array();
63 public $query_times = array();
64 public $data_cache = array();
65 public $trans_enabled = TRUE;
66 public $trans_strict = TRUE;
67 protected $_trans_depth = 0;
68 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
69 public $cache_on = FALSE;
70 public $cachedir = '';
71 public $cache_autodel = FALSE;
72 public $CACHE; // The cache class object
Derek Allard2067d1a2008-11-13 22:59:24 +000073
Andrey Andreevaf5d5582012-01-25 21:34:47 +020074 protected $_protect_identifiers = TRUE;
75 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000076
Andrey Andreevf32110a2012-01-26 11:04:11 +020077 // These are used with Oracle
Andrey Andreevaf5d5582012-01-25 21:34:47 +020078 public $stmt_id;
79 public $curs_id;
80 public $limit_used;
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 /**
Andrey Andreevaf5d5582012-01-25 21:34:47 +020083 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * connection settings.
85 *
86 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020087 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +020088 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
90 if (is_array($params))
91 {
92 foreach ($params as $key => $val)
93 {
94 $this->$key = $val;
95 }
96 }
97
98 log_message('debug', 'Database Driver Class Initialized');
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 // --------------------------------------------------------------------
102
103 /**
104 * Initialize Database Settings
105 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200106 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200107 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200108 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200110 /* If an established connection is available, then there's
111 * no need to connect and select the database.
112 *
113 * Depending on the database driver, conn_id can be either
114 * boolean TRUE, a resource or an object.
115 */
116 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
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 Andreevaf5d5582012-01-25 21:34:47 +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
168 if ($this->database != '')
169 {
170 if ( ! $this->db_select())
171 {
172 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 if ($this->db_debug)
175 {
176 $this->display_error('db_unable_to_select', $this->database);
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180 else
181 {
182 // We've selected the DB. Now we set the character set
Andrey Andreeva7c06562012-01-27 21:18:48 +0200183 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185 }
186
187 return TRUE;
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // --------------------------------------------------------------------
191
192 /**
193 * Set client character set
194 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @param string
196 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200197 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200199 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
202 {
203 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if ($this->db_debug)
206 {
207 $this->display_error('db_unable_to_set_charset', $this->char_set);
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 return FALSE;
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 return TRUE;
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // --------------------------------------------------------------------
217
218 /**
219 * The name of the platform in use (mysql, mssql, etc...)
220 *
Barry Mienydd671972010-10-04 16:33:58 +0200221 * @return string
222 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200223 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 return $this->dbdriver;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200231 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * version of the database being used
233 *
Barry Mienydd671972010-10-04 16:33:58 +0200234 * @return string
235 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200236 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if (FALSE === ($sql = $this->_version()))
239 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200240 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
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 Andreeva7c06562012-01-27 21:18:48 +0200245 if (in_array($this->dbdriver, array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 return $sql;
248 }
Derek Allard3683f772009-12-16 17:32:33 +0000249 else
250 {
251 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200252 $query = $query->row();
253 return $query->ver;
Derek Allard3683f772009-12-16 17:32:33 +0000254 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // --------------------------------------------------------------------
258
259 /**
260 * Execute the query
261 *
262 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500263 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * upon successful execution of a "write" type query. Returns boolean
265 * FALSE upon failure, and if the $db_debug variable is set to TRUE
266 * will raise an error.
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string An SQL query string
269 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200270 * @return mixed
271 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200272 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 if ($sql == '')
275 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200276 log_message('error', 'Invalid query: '.$sql);
277
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200278 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200282 if ( ($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600283 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200284 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
Derek Jonese7792202010-03-02 17:24:46 -0600286
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200287 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // we will load the caching class and return the previously
289 // cached query if it exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200290 if ($this->cache_on == TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200292 $this->load_rdriver();
293 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200295 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // Compile binds if needed
300 if ($binds !== FALSE)
301 {
302 $sql = $this->compile_binds($sql, $binds);
303 }
304
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200305 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 if ($this->save_queries == TRUE)
307 {
308 $this->queries[] = $sql;
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // Start the Query Timer
312 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // Run the Query
315 if (FALSE === ($this->result_id = $this->simple_query($sql)))
316 {
317 if ($this->save_queries == TRUE)
318 {
319 $this->query_times[] = 0;
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // This will trigger a rollback if transactions are being used
323 $this->_trans_status = FALSE;
324
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200325 // Grab the error number and message now, as we might run some
326 // additional queries before displaying the error
327 $error_no = $this->_error_number();
328 $error_msg = $this->_error_message();
329
330 // Log errors
331 log_message('error', 'Query error: '.$error_msg);
332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 if ($this->db_debug)
334 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // We call this function in order to roll-back queries
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200336 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200337 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // transactions to remain in limbo.
339 $this->trans_complete();
340
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200341 // Display errors
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200342 return $this->display_error(array('Error Number: '.$error_no, $error_msg, $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 return FALSE;
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Stop and aggregate the query time results
349 $time_end = list($em, $es) = explode(' ', microtime());
350 $this->benchmark += ($em + $es) - ($sm + $ss);
351
352 if ($this->save_queries == TRUE)
353 {
354 $this->query_times[] = ($em + $es) - ($sm + $ss);
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // Increment the query counter
358 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // Was the query a "write" type?
361 // If so we'll simply return true
362 if ($this->is_write_type($sql) === TRUE)
363 {
364 // If caching is enabled we'll auto-cleanup any
365 // existing files related to this particular URI
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200366 if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 $this->CACHE->delete();
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 return TRUE;
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // Return TRUE if we don't need to create a result object
375 // Currently only the Oracle driver uses this when stored
376 // procedures are used
377 if ($return_object !== TRUE)
378 {
379 return TRUE;
380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
382 // Load and instantiate the result driver
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200383 $driver = $this->load_rdriver();
384 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 $RES->conn_id = $this->conn_id;
386 $RES->result_id = $this->result_id;
387
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200388 if ($this->dbdriver === 'oci8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $RES->stmt_id = $this->stmt_id;
Andrey Andreeva5f2f692012-01-25 21:44:56 +0200391 $RES->curs_id = $this->curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $RES->limit_used = $this->limit_used;
393 $this->stmt_id = FALSE;
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // oci8 vars must be set before calling this
397 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200398
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200399 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // result object and save it to a cache file.
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200401 if ($this->cache_on == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 // We'll create a new instance of the result object
404 // only without the platform specific driver since
405 // we can't use it with cached data (the query result
406 // resource ID won't be any good once we've cached the
407 // result object, so we'll have to compile the data
408 // and save it)
409 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 $CR->result_object = $RES->result_object();
411 $CR->result_array = $RES->result_array();
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200412 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // Reset these since cached objects can not utilize resource IDs.
415 $CR->conn_id = NULL;
416 $CR->result_id = NULL;
417
418 $this->CACHE->write($sql, $CR);
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 return $RES;
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Load the result drivers
428 *
Barry Mienydd671972010-10-04 16:33:58 +0200429 * @return string the name of the result class
430 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200431 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 $driver = 'CI_DB_'.$this->dbdriver.'_result';
434
435 if ( ! class_exists($driver))
436 {
Greg Aker3a746652011-04-19 10:59:47 -0500437 include_once(BASEPATH.'database/DB_result.php');
438 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 return $driver;
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // --------------------------------------------------------------------
445
446 /**
447 * Simple Query
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200448 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 * we only use it when running transaction commands since they do
450 * not require all the features of the main query() function.
451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200453 * @return mixed
454 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200455 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 if ( ! $this->conn_id)
458 {
459 $this->initialize();
460 }
461
462 return $this->_execute($sql);
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // --------------------------------------------------------------------
466
467 /**
468 * Disable Transactions
469 * This permits transactions to be disabled at run-time.
470 *
Barry Mienydd671972010-10-04 16:33:58 +0200471 * @return void
472 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200473 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $this->trans_enabled = FALSE;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Enable/disable Transaction Strict Mode
482 * When strict mode is enabled, if you are running multiple groups of
483 * transactions, if one group fails all groups will be rolled back.
484 * If strict mode is disabled, each group is treated autonomously, meaning
485 * a failure of one group will not affect any others
486 *
Barry Mienydd671972010-10-04 16:33:58 +0200487 * @return void
488 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200489 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 // --------------------------------------------------------------------
495
496 /**
497 * Start Transaction
498 *
Barry Mienydd671972010-10-04 16:33:58 +0200499 * @return void
500 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200501 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200502 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if ( ! $this->trans_enabled)
504 {
505 return FALSE;
506 }
507
508 // When transactions are nested we only begin/commit/rollback the outermost ones
509 if ($this->_trans_depth > 0)
510 {
511 $this->_trans_depth += 1;
512 return;
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500516 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518
519 // --------------------------------------------------------------------
520
521 /**
522 * Complete Transaction
523 *
Barry Mienydd671972010-10-04 16:33:58 +0200524 * @return bool
525 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200526 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 if ( ! $this->trans_enabled)
529 {
530 return FALSE;
531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // When transactions are nested we only begin/commit/rollback the outermost ones
534 if ($this->_trans_depth > 1)
535 {
536 $this->_trans_depth -= 1;
537 return TRUE;
538 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500539 else
540 {
541 $this->_trans_depth = 0;
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // The query() function will set this flag to FALSE in the event that a query failed
545 if ($this->_trans_status === FALSE)
546 {
547 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 // If we are NOT running in strict mode, we will reset
550 // the _trans_status flag so that subsequent groups of transactions
551 // will be permitted.
552 if ($this->trans_strict === FALSE)
553 {
554 $this->_trans_status = TRUE;
555 }
556
557 log_message('debug', 'DB Transaction Failure');
558 return FALSE;
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 $this->trans_commit();
562 return TRUE;
563 }
564
565 // --------------------------------------------------------------------
566
567 /**
568 * Lets you retrieve the transaction flag to determine if it has failed
569 *
Barry Mienydd671972010-10-04 16:33:58 +0200570 * @return bool
571 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200572 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
574 return $this->_trans_status;
575 }
576
577 // --------------------------------------------------------------------
578
579 /**
580 * Compile Bindings
581 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @param string the sql statement
583 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200584 * @return string
585 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200586 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 if (strpos($sql, $this->bind_marker) === FALSE)
589 {
590 return $sql;
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 if ( ! is_array($binds))
594 {
595 $binds = array($binds);
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // Get the sql segments around the bind markers
599 $segments = explode($this->bind_marker, $sql);
600
601 // The count of bind should be 1 less then the count of segments
602 // If there are more bind arguments trim it down
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200603 if (count($binds) >= count($segments))
604 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 $binds = array_slice($binds, 0, count($segments)-1);
606 }
607
608 // Construct the binded query
609 $result = $segments[0];
610 $i = 0;
611 foreach ($binds as $bind)
612 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200613 $result .= $this->escape($bind).$segments[++$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
615
616 return $result;
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // --------------------------------------------------------------------
620
621 /**
622 * Determines if a query is a "write" type.
623 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @param string An SQL query string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200625 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200626 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200627 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200629 return (bool) 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 +0000630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 // --------------------------------------------------------------------
633
634 /**
635 * Calculate the aggregate query elapsed time
636 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200637 * @param int The number of decimal places
638 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200639 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200640 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 return number_format($this->benchmark, $decimals);
643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 // --------------------------------------------------------------------
646
647 /**
648 * Returns the total number of queries
649 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200650 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200651 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200652 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
654 return $this->query_count;
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 // --------------------------------------------------------------------
658
659 /**
660 * Returns the last query that was executed
661 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200662 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200663 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200664 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
666 return end($this->queries);
667 }
668
669 // --------------------------------------------------------------------
670
671 /**
672 * "Smart" Escape String
673 *
674 * Escapes data based on type
675 * Sets boolean and null types
676 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200678 * @return mixed
679 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200680 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200681 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000682 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200684 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000685 }
686 elseif (is_bool($str))
687 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200688 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000689 }
690 elseif (is_null($str))
691 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200692 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000693 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000694
695 return $str;
696 }
697
698 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600699
Derek Jonese4ed5832009-02-20 21:44:59 +0000700 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000701 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000702 *
703 * Calls the individual driver for platform
704 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200705 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000706 * @param string
707 * @return mixed
708 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200709 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200710 {
711 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000712 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000713
Derek Jonese4ed5832009-02-20 21:44:59 +0000714 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 /**
717 * Primary
718 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200719 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 * position is the primary key
721 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200723 * @return string
724 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200725 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200726 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200728 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
730
731 // --------------------------------------------------------------------
732
733 /**
734 * Returns an array of table names
735 *
Barry Mienydd671972010-10-04 16:33:58 +0200736 * @return array
737 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200738 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
740 // Is there a cached result?
741 if (isset($this->data_cache['table_names']))
742 {
743 return $this->data_cache['table_names'];
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
747 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200748 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 }
750
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200751 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200753
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200754 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200756 // Do we know from which column to get the table name?
757 if ( ! isset($key))
758 {
759 if (array_key_exists('table_name', $row))
760 {
761 $key = 'table_name';
762 }
763 elseif (array_key_exists('TABLE_NAME', $row))
764 {
765 $key = 'TABLE_NAME';
766 }
767 else
768 {
769 /* We have no other choice but to just get the first element's key.
770 * Due to array_shift() accepting it's argument by reference, if
771 * E_STRICT is on, this would trigger a warning. So we'll have to
772 * assign it first.
773 */
774 $key = array_keys($row);
775 $key = array_shift($key);
776 }
777 }
778
779 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 return $this->data_cache['table_names'];
783 }
Barry Mienydd671972010-10-04 16:33:58 +0200784
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 // --------------------------------------------------------------------
786
787 /**
788 * Determine if a particular table exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200789 *
790 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200792 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200793 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200794 return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 // --------------------------------------------------------------------
798
799 /**
800 * Fetch MySQL Field Names
801 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200803 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200805 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
807 // Is there a cached result?
808 if (isset($this->data_cache['field_names'][$table]))
809 {
810 return $this->data_cache['field_names'][$table];
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 if ($table == '')
814 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200815 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Greg Aker1edde302010-01-26 00:17:01 +0000818 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200820 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200824 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200825
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500826 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200828 // Do we know from where to get the column's name?
829 if ( ! isset($key))
830 {
831 if (array_key_exists('column_name', $row))
832 {
833 $key = 'column_name';
834 }
835 elseif (array_key_exists('COLUMN_NAME', $row))
836 {
837 $key = 'COLUMN_NAME';
838 }
839 else
840 {
841 /* We have no other choice but to just get the first element's key.
842 * Due to array_shift() accepting it's argument by reference, if
843 * E_STRICT is on, this would trigger a warning. So we'll have to
844 * assign it first.
845 */
846 $key = array_keys($row);
847 $key = array_shift($key);
848 }
849 }
850
851 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 return $this->data_cache['field_names'][$table];
855 }
856
857 // --------------------------------------------------------------------
858
859 /**
860 * Determine if a particular field exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200861 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 * @param string
863 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200864 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200866 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200867 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200868 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 // --------------------------------------------------------------------
872
873 /**
874 * Returns an object with field data
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @param string the table name
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200877 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200878 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200879 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
881 if ($table == '')
882 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200883 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200888 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000889
890 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 /**
893 * Generate an insert string
894 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 * @param string the table upon which the query will be performed
896 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200897 * @return string
898 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200899 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200901 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200902
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500903 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
905 $fields[] = $this->_escape_identifiers($key);
906 $values[] = $this->escape($val);
907 }
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200910 }
911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
913
914 /**
915 * Generate an update string
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @param string the table upon which the query will be performed
918 * @param array an associative array data of key/values
919 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200920 * @return string
921 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200922 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
924 if ($where == '')
925 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200926 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500930 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
933 }
934
935 if ( ! is_array($where))
936 {
937 $dest = array($where);
938 }
939 else
940 {
941 $dest = array();
942 foreach ($where as $key => $val)
943 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200944 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300945 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 if ($val !== '')
948 {
949 if ( ! $this->_has_operator($key))
950 {
951 $key .= ' =';
952 }
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 $val = ' '.$this->escape($val);
955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 $dest[] = $prefix.$key.$val;
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
961 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200962 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000963
964 // --------------------------------------------------------------------
965
966 /**
967 * Tests whether the string has an SQL operator
968 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 * @param string
970 * @return bool
971 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200972 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200974 return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 }
976
977 // --------------------------------------------------------------------
978
979 /**
980 * Enables a native PHP function to be run, using a platform agnostic wrapper.
981 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 * @param string the function name
983 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200984 * @return mixed
985 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200986 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 if (FALSE === strpos($driver, $function))
991 {
992 $function = $driver.$function;
993 }
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 if ( ! function_exists($function))
996 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200997 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000999
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001000 return (func_num_args() > 1)
1001 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1002 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
1004
1005 // --------------------------------------------------------------------
1006
1007 /**
1008 * Set Cache Directory Path
1009 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 * @param string the path to the cache directory
1011 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001012 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001013 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
1015 $this->cachedir = $path;
1016 }
1017
1018 // --------------------------------------------------------------------
1019
1020 /**
1021 * Enable Query Caching
1022 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001023 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001024 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001025 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001027 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 }
1029
1030 // --------------------------------------------------------------------
1031
1032 /**
1033 * Disable Query Caching
1034 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001035 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001036 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001037 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001039 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
1043 // --------------------------------------------------------------------
1044
1045 /**
1046 * Delete the cache files associated with a particular URI
1047 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001048 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001049 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001050 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
1052 if ( ! $this->_cache_init())
1053 {
1054 return FALSE;
1055 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 return $this->CACHE->delete($segment_one, $segment_two);
1058 }
1059
1060 // --------------------------------------------------------------------
1061
1062 /**
1063 * Delete All cache files
1064 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001065 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001066 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001067 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
1069 if ( ! $this->_cache_init())
1070 {
1071 return FALSE;
1072 }
1073
1074 return $this->CACHE->delete_all();
1075 }
1076
1077 // --------------------------------------------------------------------
1078
1079 /**
1080 * Initialize the Cache Class
1081 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001083 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001084 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001086 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001088 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001089 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001090 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001091 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001093 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1094 {
1095 return $this->cache_off();
1096 }
Derek Allarde37ab382009-02-03 16:13:57 +00001097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1099 return TRUE;
1100 }
1101
1102 // --------------------------------------------------------------------
1103
1104 /**
1105 * Close DB Connection
1106 *
Barry Mienydd671972010-10-04 16:33:58 +02001107 * @return void
1108 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001109 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001111 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 {
1113 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001114 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 // --------------------------------------------------------------------
1119
1120 /**
1121 * Display an error message
1122 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 * @param string the error message
1124 * @param string any "swap" values
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001125 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001126 * @return string sends the application/error_db.php template
1127 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001128 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 {
Derek Jonese7792202010-03-02 17:24:46 -06001130 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 $LANG->load('db');
1132
1133 $heading = $LANG->line('db_error_heading');
1134
1135 if ($native == TRUE)
1136 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001137 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
1139 else
1140 {
1141 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1142 }
Barry Mienydd671972010-10-04 16:33:58 +02001143
Pascal Kriete60f8c392010-08-25 18:03:28 +02001144 // Find the most likely culprit of the error by going through
1145 // the backtrace until the source file is no longer in the
1146 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001147 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001148 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001149 {
1150 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1151 {
1152 // Found it - use a relative path for safety
1153 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1154 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001155 break;
1156 }
1157 }
Barry Mienydd671972010-10-04 16:33:58 +02001158
Derek Jonese7792202010-03-02 17:24:46 -06001159 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 echo $error->show_error($heading, $message, 'error_db');
1161 exit;
1162 }
1163
1164 // --------------------------------------------------------------------
1165
1166 /**
1167 * Protect Identifiers
1168 *
1169 * This function adds backticks if appropriate based on db type
1170 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 * @param mixed the item to escape
1172 * @return mixed the item with backticks
1173 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001174 public function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
1176 return $this->_protect_identifiers($item, $prefix_single);
1177 }
1178
1179 // --------------------------------------------------------------------
1180
1181 /**
1182 * Protect Identifiers
1183 *
1184 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001185 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001187 * the table prefix onto it. Some logic is necessary in order to deal with
1188 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 *
1190 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1191 *
1192 * Or a query with aliasing:
1193 *
1194 * SELECT m.member_id, m.member_name FROM members AS m
1195 *
1196 * Since the column name can include up to four segments (host, DB, table, column)
1197 * or also have an alias prefix, we need to do a bit of work to figure this out and
1198 * insert the table prefix (if it exists) in the proper position, and escape only
1199 * the correct identifiers.
1200 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001201 * NOTE: This is used by DB_forge drivers and therefore needs to be public.
1202 * (until a better solution is implemented)
1203 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 * @param string
1205 * @param bool
1206 * @param mixed
1207 * @param bool
1208 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001209 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001210 public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 {
1212 if ( ! is_bool($protect_identifiers))
1213 {
1214 $protect_identifiers = $this->_protect_identifiers;
1215 }
Derek Allarde37ab382009-02-03 16:13:57 +00001216
1217 if (is_array($item))
1218 {
1219 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001220 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001221 {
1222 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1223 }
1224
1225 return $escaped_array;
1226 }
1227
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001229 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001230
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 // If the item has an alias declaration we remove it and set it aside.
1232 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001234 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001235 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 $item = substr($item, 0, - strlen($alias));
1237 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001238 else
1239 {
1240 $alias = '';
1241 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001242
Derek Allard911d3e02008-12-15 14:08:35 +00001243 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001244 // If a parenthesis is found we know that we do not need to
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001245 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001246 // way to deal with this, but I'm not thinking of it -- Rick
1247 if (strpos($item, '(') !== FALSE)
1248 {
1249 return $item.$alias;
1250 }
1251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 // Break the string apart if it contains periods, then insert the table prefix
1253 // in the correct location, assuming the period doesn't indicate that we're dealing
1254 // with an alias. While we're at it, we will escape the components
1255 if (strpos($item, '.') !== FALSE)
1256 {
1257 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // Does the first segment of the exploded item match
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001260 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // we have nothing more to do other than escape the item
1262 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001263 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 if ($protect_identifiers === TRUE)
1265 {
1266 foreach ($parts as $key => $val)
1267 {
1268 if ( ! in_array($val, $this->_reserved_identifiers))
1269 {
1270 $parts[$key] = $this->_escape_identifiers($val);
1271 }
1272 }
Barry Mienydd671972010-10-04 16:33:58 +02001273
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001275 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 return $item.$alias;
1278 }
Barry Mienydd671972010-10-04 16:33:58 +02001279
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001280 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 if ($this->dbprefix != '')
1282 {
1283 // We now add the table prefix based on some logic.
1284 // Do we have 4 segments (hostname.database.table.column)?
1285 // If so, we add the table prefix to the column name in the 3rd segment.
1286 if (isset($parts[3]))
1287 {
1288 $i = 2;
1289 }
1290 // Do we have 3 segments (database.table.column)?
1291 // If so, we add the table prefix to the column name in 2nd position
1292 elseif (isset($parts[2]))
1293 {
1294 $i = 1;
1295 }
1296 // Do we have 2 segments (table.column)?
1297 // If so, we add the table prefix to the column name in 1st segment
1298 else
1299 {
1300 $i = 0;
1301 }
Barry Mienydd671972010-10-04 16:33:58 +02001302
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 // This flag is set when the supplied $item does not contain a field name.
1304 // This can happen when this function is being called from a JOIN.
1305 if ($field_exists == FALSE)
1306 {
1307 $i++;
1308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Jones55acc8b2009-07-11 03:38:13 +00001310 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001311 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001312 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001313 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001314 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001316 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 {
1318 $parts[$i] = $this->dbprefix.$parts[$i];
1319 }
Barry Mienydd671972010-10-04 16:33:58 +02001320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 // Put the parts back together
1322 $item = implode('.', $parts);
1323 }
Barry Mienydd671972010-10-04 16:33:58 +02001324
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 if ($protect_identifiers === TRUE)
1326 {
1327 $item = $this->_escape_identifiers($item);
1328 }
Barry Mienydd671972010-10-04 16:33:58 +02001329
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 return $item.$alias;
1331 }
1332
Derek Jones37f4b9c2011-07-01 17:56:50 -05001333 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 if ($this->dbprefix != '')
1335 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001336 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001337 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001338 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001339 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001340 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001342 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 {
1344 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001345 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 }
1347
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001348 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 {
1350 $item = $this->_escape_identifiers($item);
1351 }
Barry Mienydd671972010-10-04 16:33:58 +02001352
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 return $item.$alias;
1354 }
1355
1356
1357}
1358
Derek Allard2067d1a2008-11-13 22:59:24 +00001359/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001360/* Location: ./system/database/DB_driver.php */