blob: 744bb3cd713f49be1a6c5b1c3e4382e24377e4c3 [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 Andreev45ba4f72012-02-13 01:24:39 +020043 public $dsn;
Andrey Andreevaf5d5582012-01-25 21:34:47 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
48 public $dbdriver = 'mysql';
49 public $dbprefix = '';
50 public $char_set = 'utf8';
51 public $dbcollat = 'utf8_general_ci';
52 public $autoinit = TRUE; // Whether to automatically initialize the DB
53 public $swap_pre = '';
54 public $port = '';
55 public $pconnect = FALSE;
56 public $conn_id = FALSE;
57 public $result_id = FALSE;
58 public $db_debug = FALSE;
59 public $benchmark = 0;
60 public $query_count = 0;
61 public $bind_marker = '?';
62 public $save_queries = TRUE;
63 public $queries = array();
64 public $query_times = array();
65 public $data_cache = array();
66 public $trans_enabled = TRUE;
67 public $trans_strict = TRUE;
68 protected $_trans_depth = 0;
69 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
70 public $cache_on = FALSE;
71 public $cachedir = '';
72 public $cache_autodel = FALSE;
73 public $CACHE; // The cache class object
Derek Allard2067d1a2008-11-13 22:59:24 +000074
Andrey Andreevaf5d5582012-01-25 21:34:47 +020075 protected $_protect_identifiers = TRUE;
76 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000077
Andrey Andreevf32110a2012-01-26 11:04:11 +020078 // These are used with Oracle
Andrey Andreevaf5d5582012-01-25 21:34:47 +020079 public $stmt_id;
80 public $curs_id;
81 public $limit_used;
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 /**
Andrey Andreevaf5d5582012-01-25 21:34:47 +020084 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * connection settings.
86 *
87 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +020089 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
91 if (is_array($params))
92 {
93 foreach ($params as $key => $val)
94 {
95 $this->$key = $val;
96 }
97 }
98
99 log_message('debug', 'Database Driver Class Initialized');
100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // --------------------------------------------------------------------
103
104 /**
105 * Initialize Database Settings
106 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200107 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200108 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200109 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200111 /* If an established connection is available, then there's
112 * no need to connect and select the database.
113 *
114 * Depending on the database driver, conn_id can be either
115 * boolean TRUE, a resource or an object.
116 */
117 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 return TRUE;
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // Connect to the database and set the connection ID
125 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
126
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200127 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 if ( ! $this->conn_id)
129 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100130 // Check if there is a failover set
131 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100133 // Go over all the failovers
134 foreach ($this->failover as $failover)
135 {
136 // Replace the current settings with those of the failover
137 foreach ($failover as $key => $val)
138 {
139 $this->$key = $val;
140 }
141
142 // Try to connect
143 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
144
145 // If a connection is made break the foreach loop
146 if ($this->conn_id)
147 {
148 break;
149 }
150 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100152
153 // We still don't have a connection?
154 if ( ! $this->conn_id)
155 {
156 log_message('error', 'Unable to connect to the database');
157
158 if ($this->db_debug)
159 {
160 $this->display_error('db_unable_to_connect');
161 }
162 return FALSE;
163 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165
166 // ----------------------------------------------------------------
167
168 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200169 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200171 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200172
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200173 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200175 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200177 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200180 // Now we set the character set and that's all
181 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Set client character set
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string
190 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200191 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200193 public 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 *
Barry Mienydd671972010-10-04 16:33:58 +0200215 * @return string
216 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200217 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
219 return $this->dbdriver;
220 }
221
222 // --------------------------------------------------------------------
223
224 /**
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200225 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * version of the database being used
227 *
Barry Mienydd671972010-10-04 16:33:58 +0200228 * @return string
229 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200230 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
232 if (FALSE === ($sql = $this->_version()))
233 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200234 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
Derek Allard3683f772009-12-16 17:32:33 +0000236
237 // Some DBs have functions that return the version, and don't run special
238 // SQL queries per se. In these instances, just return the result.
Andrey Andreeva7c06562012-01-27 21:18:48 +0200239 if (in_array($this->dbdriver, array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 return $sql;
242 }
Derek Allard3683f772009-12-16 17:32:33 +0000243 else
244 {
245 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200246 $query = $query->row();
247 return $query->ver;
Derek Allard3683f772009-12-16 17:32:33 +0000248 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 // --------------------------------------------------------------------
252
253 /**
254 * Execute the query
255 *
256 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500257 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * upon successful execution of a "write" type query. Returns boolean
259 * FALSE upon failure, and if the $db_debug variable is set to TRUE
260 * will raise an error.
261 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 * @param string An SQL query string
263 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200264 * @return mixed
265 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200266 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 if ($sql == '')
269 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200270 log_message('error', 'Invalid query: '.$sql);
271
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200272 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274
275 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200276 if ( ($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600277 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200278 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
Derek Jonese7792202010-03-02 17:24:46 -0600280
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200281 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // we will load the caching class and return the previously
283 // cached query if it exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200284 if ($this->cache_on == TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200286 $this->load_rdriver();
287 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200289 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // Compile binds if needed
294 if ($binds !== FALSE)
295 {
296 $sql = $this->compile_binds($sql, $binds);
297 }
298
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200299 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 if ($this->save_queries == TRUE)
301 {
302 $this->queries[] = $sql;
303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // Start the Query Timer
306 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // Run the Query
309 if (FALSE === ($this->result_id = $this->simple_query($sql)))
310 {
311 if ($this->save_queries == TRUE)
312 {
313 $this->query_times[] = 0;
314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // This will trigger a rollback if transactions are being used
317 $this->_trans_status = FALSE;
318
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200319 // Grab the error number and message now, as we might run some
320 // additional queries before displaying the error
321 $error_no = $this->_error_number();
322 $error_msg = $this->_error_message();
323
324 // Log errors
325 log_message('error', 'Query error: '.$error_msg);
326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 if ($this->db_debug)
328 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // We call this function in order to roll-back queries
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200330 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200331 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // transactions to remain in limbo.
333 $this->trans_complete();
334
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200335 // Display errors
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200336 return $this->display_error(array('Error Number: '.$error_no, $error_msg, $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 return FALSE;
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // Stop and aggregate the query time results
343 $time_end = list($em, $es) = explode(' ', microtime());
344 $this->benchmark += ($em + $es) - ($sm + $ss);
345
346 if ($this->save_queries == TRUE)
347 {
348 $this->query_times[] = ($em + $es) - ($sm + $ss);
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Increment the query counter
352 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // Was the query a "write" type?
355 // If so we'll simply return true
356 if ($this->is_write_type($sql) === TRUE)
357 {
358 // If caching is enabled we'll auto-cleanup any
359 // existing files related to this particular URI
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200360 if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 $this->CACHE->delete();
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 return TRUE;
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 // Return TRUE if we don't need to create a result object
369 // Currently only the Oracle driver uses this when stored
370 // procedures are used
371 if ($return_object !== TRUE)
372 {
373 return TRUE;
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
376 // Load and instantiate the result driver
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200377 $driver = $this->load_rdriver();
378 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $RES->conn_id = $this->conn_id;
380 $RES->result_id = $this->result_id;
381
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200382 if ($this->dbdriver === 'oci8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $RES->stmt_id = $this->stmt_id;
Andrey Andreeva5f2f692012-01-25 21:44:56 +0200385 $RES->curs_id = $this->curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 $RES->limit_used = $this->limit_used;
387 $this->stmt_id = FALSE;
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // oci8 vars must be set before calling this
391 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200392
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200393 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // result object and save it to a cache file.
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200395 if ($this->cache_on == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 // We'll create a new instance of the result object
398 // only without the platform specific driver since
399 // we can't use it with cached data (the query result
400 // resource ID won't be any good once we've cached the
401 // result object, so we'll have to compile the data
402 // and save it)
403 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 $CR->result_object = $RES->result_object();
405 $CR->result_array = $RES->result_array();
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200406 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // Reset these since cached objects can not utilize resource IDs.
409 $CR->conn_id = NULL;
410 $CR->result_id = NULL;
411
412 $this->CACHE->write($sql, $CR);
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 return $RES;
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Load the result drivers
422 *
Barry Mienydd671972010-10-04 16:33:58 +0200423 * @return string the name of the result class
424 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200425 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 $driver = 'CI_DB_'.$this->dbdriver.'_result';
428
429 if ( ! class_exists($driver))
430 {
Greg Aker3a746652011-04-19 10:59:47 -0500431 include_once(BASEPATH.'database/DB_result.php');
432 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 return $driver;
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // --------------------------------------------------------------------
439
440 /**
441 * Simple Query
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200442 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * we only use it when running transaction commands since they do
444 * not require all the features of the main query() function.
445 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200447 * @return mixed
448 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200449 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 if ( ! $this->conn_id)
452 {
453 $this->initialize();
454 }
455
456 return $this->_execute($sql);
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // --------------------------------------------------------------------
460
461 /**
462 * Disable Transactions
463 * This permits transactions to be disabled at run-time.
464 *
Barry Mienydd671972010-10-04 16:33:58 +0200465 * @return void
466 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200467 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 $this->trans_enabled = FALSE;
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Enable/disable Transaction Strict Mode
476 * When strict mode is enabled, if you are running multiple groups of
477 * transactions, if one group fails all groups will be rolled back.
478 * If strict mode is disabled, each group is treated autonomously, meaning
479 * a failure of one group will not affect any others
480 *
Barry Mienydd671972010-10-04 16:33:58 +0200481 * @return void
482 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200483 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 // --------------------------------------------------------------------
489
490 /**
491 * Start Transaction
492 *
Barry Mienydd671972010-10-04 16:33:58 +0200493 * @return void
494 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200495 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200496 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if ( ! $this->trans_enabled)
498 {
499 return FALSE;
500 }
501
502 // When transactions are nested we only begin/commit/rollback the outermost ones
503 if ($this->_trans_depth > 0)
504 {
505 $this->_trans_depth += 1;
506 return;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500510 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Complete Transaction
517 *
Barry Mienydd671972010-10-04 16:33:58 +0200518 * @return bool
519 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200520 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
522 if ( ! $this->trans_enabled)
523 {
524 return FALSE;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // When transactions are nested we only begin/commit/rollback the outermost ones
528 if ($this->_trans_depth > 1)
529 {
530 $this->_trans_depth -= 1;
531 return TRUE;
532 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500533 else
534 {
535 $this->_trans_depth = 0;
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 // The query() function will set this flag to FALSE in the event that a query failed
539 if ($this->_trans_status === FALSE)
540 {
541 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 // If we are NOT running in strict mode, we will reset
544 // the _trans_status flag so that subsequent groups of transactions
545 // will be permitted.
546 if ($this->trans_strict === FALSE)
547 {
548 $this->_trans_status = TRUE;
549 }
550
551 log_message('debug', 'DB Transaction Failure');
552 return FALSE;
553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 $this->trans_commit();
556 return TRUE;
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Lets you retrieve the transaction flag to determine if it has failed
563 *
Barry Mienydd671972010-10-04 16:33:58 +0200564 * @return bool
565 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200566 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 return $this->_trans_status;
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Compile Bindings
575 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * @param string the sql statement
577 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200578 * @return string
579 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200580 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 if (strpos($sql, $this->bind_marker) === FALSE)
583 {
584 return $sql;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 if ( ! is_array($binds))
588 {
589 $binds = array($binds);
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // Get the sql segments around the bind markers
593 $segments = explode($this->bind_marker, $sql);
594
595 // The count of bind should be 1 less then the count of segments
596 // If there are more bind arguments trim it down
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200597 if (count($binds) >= count($segments))
598 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 $binds = array_slice($binds, 0, count($segments)-1);
600 }
601
602 // Construct the binded query
603 $result = $segments[0];
604 $i = 0;
605 foreach ($binds as $bind)
606 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200607 $result .= $this->escape($bind).$segments[++$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
609
610 return $result;
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
614
615 /**
616 * Determines if a query is a "write" type.
617 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * @param string An SQL query string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200619 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200620 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200621 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
Andrey Andreev8ad2ec72012-02-13 12:25:42 +0200623 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // --------------------------------------------------------------------
627
628 /**
629 * Calculate the aggregate query elapsed time
630 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200631 * @param int The number of decimal places
632 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200633 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200634 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 return number_format($this->benchmark, $decimals);
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 // --------------------------------------------------------------------
640
641 /**
642 * Returns the total number of queries
643 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200644 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200646 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
648 return $this->query_count;
649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // --------------------------------------------------------------------
652
653 /**
654 * Returns the last query that was executed
655 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200656 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200657 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200658 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
660 return end($this->queries);
661 }
662
663 // --------------------------------------------------------------------
664
665 /**
666 * "Smart" Escape String
667 *
668 * Escapes data based on type
669 * Sets boolean and null types
670 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200672 * @return mixed
673 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200674 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200675 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000676 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200678 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000679 }
680 elseif (is_bool($str))
681 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200682 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000683 }
684 elseif (is_null($str))
685 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200686 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000687 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000688
689 return $str;
690 }
691
692 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600693
Derek Jonese4ed5832009-02-20 21:44:59 +0000694 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000695 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000696 *
697 * Calls the individual driver for platform
698 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200699 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000700 * @param string
701 * @return mixed
702 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200703 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200704 {
705 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000706 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000707
Derek Jonese4ed5832009-02-20 21:44:59 +0000708 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 /**
711 * Primary
712 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200713 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 * position is the primary key
715 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200717 * @return string
718 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200719 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200720 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200722 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
724
725 // --------------------------------------------------------------------
726
727 /**
728 * Returns an array of table names
729 *
Barry Mienydd671972010-10-04 16:33:58 +0200730 * @return array
731 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200732 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
734 // Is there a cached result?
735 if (isset($this->data_cache['table_names']))
736 {
737 return $this->data_cache['table_names'];
738 }
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
741 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200742 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
744
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200745 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200747
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200748 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200750 // Do we know from which column to get the table name?
751 if ( ! isset($key))
752 {
753 if (array_key_exists('table_name', $row))
754 {
755 $key = 'table_name';
756 }
757 elseif (array_key_exists('TABLE_NAME', $row))
758 {
759 $key = 'TABLE_NAME';
760 }
761 else
762 {
763 /* We have no other choice but to just get the first element's key.
764 * Due to array_shift() accepting it's argument by reference, if
765 * E_STRICT is on, this would trigger a warning. So we'll have to
766 * assign it first.
767 */
768 $key = array_keys($row);
769 $key = array_shift($key);
770 }
771 }
772
773 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 }
775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 return $this->data_cache['table_names'];
777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 // --------------------------------------------------------------------
780
781 /**
782 * Determine if a particular table exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200783 *
784 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200786 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200787 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200788 return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 // --------------------------------------------------------------------
792
793 /**
794 * Fetch MySQL Field Names
795 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200797 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200799 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
801 // Is there a cached result?
802 if (isset($this->data_cache['field_names'][$table]))
803 {
804 return $this->data_cache['field_names'][$table];
805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 if ($table == '')
808 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200809 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Greg Aker1edde302010-01-26 00:17:01 +0000812 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200814 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200818 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200819
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500820 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200822 // Do we know from where to get the column's name?
823 if ( ! isset($key))
824 {
825 if (array_key_exists('column_name', $row))
826 {
827 $key = 'column_name';
828 }
829 elseif (array_key_exists('COLUMN_NAME', $row))
830 {
831 $key = 'COLUMN_NAME';
832 }
833 else
834 {
835 /* We have no other choice but to just get the first element's key.
836 * Due to array_shift() accepting it's argument by reference, if
837 * E_STRICT is on, this would trigger a warning. So we'll have to
838 * assign it first.
839 */
840 $key = array_keys($row);
841 $key = array_shift($key);
842 }
843 }
844
845 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 return $this->data_cache['field_names'][$table];
849 }
850
851 // --------------------------------------------------------------------
852
853 /**
854 * Determine if a particular field exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200855 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 * @param string
857 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200858 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200860 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200861 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200862 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 // --------------------------------------------------------------------
866
867 /**
868 * Returns an object with field data
869 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @param string the table name
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200871 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200872 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200873 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
875 if ($table == '')
876 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200877 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 }
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200882 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000883
884 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 /**
887 * Generate an insert string
888 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 * @param string the table upon which the query will be performed
890 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200891 * @return string
892 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200893 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200895 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200896
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500897 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 $fields[] = $this->_escape_identifiers($key);
900 $values[] = $this->escape($val);
901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200904 }
905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // --------------------------------------------------------------------
907
908 /**
909 * Generate an update string
910 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 * @param string the table upon which the query will be performed
912 * @param array an associative array data of key/values
913 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200914 * @return string
915 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200916 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
918 if ($where == '')
919 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200920 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500924 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 {
926 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
927 }
928
929 if ( ! is_array($where))
930 {
931 $dest = array($where);
932 }
933 else
934 {
935 $dest = array();
936 foreach ($where as $key => $val)
937 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200938 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300939 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 if ($val !== '')
942 {
943 if ( ! $this->_has_operator($key))
944 {
945 $key .= ' =';
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 $val = ' '.$this->escape($val);
949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 $dest[] = $prefix.$key.$val;
952 }
Barry Mienydd671972010-10-04 16:33:58 +0200953 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000954
955 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200956 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000957
958 // --------------------------------------------------------------------
959
960 /**
961 * Tests whether the string has an SQL operator
962 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 * @param string
964 * @return bool
965 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200966 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200968 return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
970
971 // --------------------------------------------------------------------
972
973 /**
974 * Enables a native PHP function to be run, using a platform agnostic wrapper.
975 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 * @param string the function name
977 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200978 * @return mixed
979 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200980 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 if (FALSE === strpos($driver, $function))
985 {
986 $function = $driver.$function;
987 }
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 if ( ! function_exists($function))
990 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200991 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000993
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200994 return (func_num_args() > 1)
995 ? call_user_func_array($function, array_splice(func_get_args(), 1))
996 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
998
999 // --------------------------------------------------------------------
1000
1001 /**
1002 * Set Cache Directory Path
1003 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 * @param string the path to the cache directory
1005 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001006 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001007 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 {
1009 $this->cachedir = $path;
1010 }
1011
1012 // --------------------------------------------------------------------
1013
1014 /**
1015 * Enable Query Caching
1016 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001017 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001018 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001019 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001021 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 }
1023
1024 // --------------------------------------------------------------------
1025
1026 /**
1027 * Disable Query Caching
1028 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001029 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001030 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001031 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001033 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036
1037 // --------------------------------------------------------------------
1038
1039 /**
1040 * Delete the cache files associated with a particular URI
1041 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001042 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001043 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001044 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
1046 if ( ! $this->_cache_init())
1047 {
1048 return FALSE;
1049 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 return $this->CACHE->delete($segment_one, $segment_two);
1052 }
1053
1054 // --------------------------------------------------------------------
1055
1056 /**
1057 * Delete All cache files
1058 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001059 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001060 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001061 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
1063 if ( ! $this->_cache_init())
1064 {
1065 return FALSE;
1066 }
1067
1068 return $this->CACHE->delete_all();
1069 }
1070
1071 // --------------------------------------------------------------------
1072
1073 /**
1074 * Initialize the Cache Class
1075 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001077 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001078 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001080 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001082 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001083 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001084 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001085 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001087 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1088 {
1089 return $this->cache_off();
1090 }
Derek Allarde37ab382009-02-03 16:13:57 +00001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1093 return TRUE;
1094 }
1095
1096 // --------------------------------------------------------------------
1097
1098 /**
1099 * Close DB Connection
1100 *
Barry Mienydd671972010-10-04 16:33:58 +02001101 * @return void
1102 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001103 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001105 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 {
1107 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001108 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 }
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 // --------------------------------------------------------------------
1113
1114 /**
1115 * Display an error message
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string the error message
1118 * @param string any "swap" values
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001119 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001120 * @return string sends the application/error_db.php template
1121 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001122 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Derek Jonese7792202010-03-02 17:24:46 -06001124 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 $LANG->load('db');
1126
1127 $heading = $LANG->line('db_error_heading');
1128
1129 if ($native == TRUE)
1130 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001131 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 }
1133 else
1134 {
1135 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1136 }
Barry Mienydd671972010-10-04 16:33:58 +02001137
Pascal Kriete60f8c392010-08-25 18:03:28 +02001138 // Find the most likely culprit of the error by going through
1139 // the backtrace until the source file is no longer in the
1140 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001141 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001142 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001143 {
1144 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1145 {
1146 // Found it - use a relative path for safety
1147 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1148 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001149 break;
1150 }
1151 }
Barry Mienydd671972010-10-04 16:33:58 +02001152
Derek Jonese7792202010-03-02 17:24:46 -06001153 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 echo $error->show_error($heading, $message, 'error_db');
1155 exit;
1156 }
1157
1158 // --------------------------------------------------------------------
1159
1160 /**
1161 * Protect Identifiers
1162 *
1163 * This function adds backticks if appropriate based on db type
1164 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 * @param mixed the item to escape
1166 * @return mixed the item with backticks
1167 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001168 public function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
1170 return $this->_protect_identifiers($item, $prefix_single);
1171 }
1172
1173 // --------------------------------------------------------------------
1174
1175 /**
1176 * Protect Identifiers
1177 *
1178 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001179 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001181 * the table prefix onto it. Some logic is necessary in order to deal with
1182 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 *
1184 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1185 *
1186 * Or a query with aliasing:
1187 *
1188 * SELECT m.member_id, m.member_name FROM members AS m
1189 *
1190 * Since the column name can include up to four segments (host, DB, table, column)
1191 * or also have an alias prefix, we need to do a bit of work to figure this out and
1192 * insert the table prefix (if it exists) in the proper position, and escape only
1193 * the correct identifiers.
1194 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001195 * NOTE: This is used by DB_forge drivers and therefore needs to be public.
1196 * (until a better solution is implemented)
1197 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 * @param string
1199 * @param bool
1200 * @param mixed
1201 * @param bool
1202 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001203 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001204 public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
1206 if ( ! is_bool($protect_identifiers))
1207 {
1208 $protect_identifiers = $this->_protect_identifiers;
1209 }
Derek Allarde37ab382009-02-03 16:13:57 +00001210
1211 if (is_array($item))
1212 {
1213 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001214 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001215 {
1216 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1217 }
1218
1219 return $escaped_array;
1220 }
1221
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001223 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // If the item has an alias declaration we remove it and set it aside.
1226 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001228 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001229 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 $item = substr($item, 0, - strlen($alias));
1231 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001232 else
1233 {
1234 $alias = '';
1235 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001236
Derek Allard911d3e02008-12-15 14:08:35 +00001237 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001238 // If a parenthesis is found we know that we do not need to
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001239 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001240 // way to deal with this, but I'm not thinking of it -- Rick
1241 if (strpos($item, '(') !== FALSE)
1242 {
1243 return $item.$alias;
1244 }
1245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 // Break the string apart if it contains periods, then insert the table prefix
1247 // in the correct location, assuming the period doesn't indicate that we're dealing
1248 // with an alias. While we're at it, we will escape the components
1249 if (strpos($item, '.') !== FALSE)
1250 {
1251 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 // Does the first segment of the exploded item match
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001254 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 // we have nothing more to do other than escape the item
1256 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001257 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 if ($protect_identifiers === TRUE)
1259 {
1260 foreach ($parts as $key => $val)
1261 {
1262 if ( ! in_array($val, $this->_reserved_identifiers))
1263 {
1264 $parts[$key] = $this->_escape_identifiers($val);
1265 }
1266 }
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001269 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001270
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 return $item.$alias;
1272 }
Barry Mienydd671972010-10-04 16:33:58 +02001273
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001274 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 if ($this->dbprefix != '')
1276 {
1277 // We now add the table prefix based on some logic.
1278 // Do we have 4 segments (hostname.database.table.column)?
1279 // If so, we add the table prefix to the column name in the 3rd segment.
1280 if (isset($parts[3]))
1281 {
1282 $i = 2;
1283 }
1284 // Do we have 3 segments (database.table.column)?
1285 // If so, we add the table prefix to the column name in 2nd position
1286 elseif (isset($parts[2]))
1287 {
1288 $i = 1;
1289 }
1290 // Do we have 2 segments (table.column)?
1291 // If so, we add the table prefix to the column name in 1st segment
1292 else
1293 {
1294 $i = 0;
1295 }
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 // This flag is set when the supplied $item does not contain a field name.
1298 // This can happen when this function is being called from a JOIN.
1299 if ($field_exists == FALSE)
1300 {
1301 $i++;
1302 }
Barry Mienydd671972010-10-04 16:33:58 +02001303
Derek Jones55acc8b2009-07-11 03:38:13 +00001304 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001305 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001306 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001307 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001308 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001310 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 {
1312 $parts[$i] = $this->dbprefix.$parts[$i];
1313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 // Put the parts back together
1316 $item = implode('.', $parts);
1317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 if ($protect_identifiers === TRUE)
1320 {
1321 $item = $this->_escape_identifiers($item);
1322 }
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 return $item.$alias;
1325 }
1326
Derek Jones37f4b9c2011-07-01 17:56:50 -05001327 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 if ($this->dbprefix != '')
1329 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001330 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001331 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001332 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001333 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001334 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001336 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
1338 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001339 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 }
1341
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001342 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 {
1344 $item = $this->_escape_identifiers($item);
1345 }
Barry Mienydd671972010-10-04 16:33:58 +02001346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 return $item.$alias;
1348 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001349}
1350
Derek Allard2067d1a2008-11-13 22:59:24 +00001351/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001352/* Location: ./system/database/DB_driver.php */