blob: 1fce76cb5bc879738655a2158a7ac9f365d35912 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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();
Derek Allard2067d1a2008-11-13 22:59:24 +000066
Andrey Andreevaf5d5582012-01-25 21:34:47 +020067 public $trans_enabled = TRUE;
68 public $trans_strict = TRUE;
69 protected $_trans_depth = 0;
70 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000071
Andrey Andreevaf5d5582012-01-25 21:34:47 +020072 public $cache_on = FALSE;
73 public $cachedir = '';
74 public $cache_autodel = FALSE;
75 public $CACHE; // The cache class object
Derek Allard2067d1a2008-11-13 22:59:24 +000076
Andrey Andreev043f2602012-03-06 20:16:42 +020077 protected $_protect_identifiers = TRUE;
Andrey Andreevaf5d5582012-01-25 21:34:47 +020078 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000079
Andrey Andreevaf5d5582012-01-25 21:34:47 +020080 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if (is_array($params))
83 {
84 foreach ($params as $key => $val)
85 {
86 $this->$key = $val;
87 }
88 }
89
90 log_message('debug', 'Database Driver Class Initialized');
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
96 * Initialize Database Settings
97 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +020098 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200100 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200102 /* If an established connection is available, then there's
103 * no need to connect and select the database.
104 *
105 * Depending on the database driver, conn_id can be either
106 * boolean TRUE, a resource or an object.
107 */
108 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
110 return TRUE;
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // Connect to the database and set the connection ID
116 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
117
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200118 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if ( ! $this->conn_id)
120 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100121 // Check if there is a failover set
122 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100124 // Go over all the failovers
125 foreach ($this->failover as $failover)
126 {
127 // Replace the current settings with those of the failover
128 foreach ($failover as $key => $val)
129 {
130 $this->$key = $val;
131 }
132
133 // Try to connect
134 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
135
136 // If a connection is made break the foreach loop
137 if ($this->conn_id)
138 {
139 break;
140 }
141 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100143
144 // We still don't have a connection?
145 if ( ! $this->conn_id)
146 {
147 log_message('error', 'Unable to connect to the database');
148
149 if ($this->db_debug)
150 {
151 $this->display_error('db_unable_to_connect');
152 }
153 return FALSE;
154 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // ----------------------------------------------------------------
158
159 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200160 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200163
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200166 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200171 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200172 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 // --------------------------------------------------------------------
176
177 /**
178 * Set client character set
179 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @param string
181 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200182 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200184 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200186 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200188 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 if ($this->db_debug)
191 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200192 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 return FALSE;
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 return TRUE;
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // --------------------------------------------------------------------
202
203 /**
204 * The name of the platform in use (mysql, mssql, etc...)
205 *
Barry Mienydd671972010-10-04 16:33:58 +0200206 * @return string
207 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200208 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 return $this->dbdriver;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200216 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200218 * Returns a string containing the version of the database being used.
219 * Most drivers will override this method.
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 *
Barry Mienydd671972010-10-04 16:33:58 +0200221 * @return string
222 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200223 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200225 if (isset($this->data_cache['version']))
226 {
227 return $this->data_cache['version'];
228 }
229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 if (FALSE === ($sql = $this->_version()))
231 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200232 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Derek Allard3683f772009-12-16 17:32:33 +0000234
Andrey Andreev08856b82012-03-03 03:19:28 +0200235 $query = $this->query($sql);
236 $query = $query->row();
237 return $this->data_cache['version'] = $query->ver;
238 }
Derek Allard3683f772009-12-16 17:32:33 +0000239
Andrey Andreev08856b82012-03-03 03:19:28 +0200240 // --------------------------------------------------------------------
241
242 /**
243 * Version number query string
244 *
245 * @return string
246 */
247 protected function _version()
248 {
249 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // --------------------------------------------------------------------
253
254 /**
255 * Execute the query
256 *
257 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200258 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * upon successful execution of a "write" type query. Returns boolean
260 * FALSE upon failure, and if the $db_debug variable is set to TRUE
261 * will raise an error.
262 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @param string An SQL query string
264 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200265 * @return mixed
266 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200267 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 if ($sql == '')
270 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200271 log_message('error', 'Invalid query: '.$sql);
272
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200273 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275
276 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200277 if ( ($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600278 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200279 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Derek Jonese7792202010-03-02 17:24:46 -0600281
Ryan Dialef7474c2012-03-01 16:11:36 -0500282 // Compile binds if needed
283 if ($binds !== FALSE)
284 {
285 $sql = $this->compile_binds($sql, $binds);
286 }
287
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200288 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // we will load the caching class and return the previously
290 // cached query if it exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200291 if ($this->cache_on == TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200293 $this->load_rdriver();
294 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200296 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Jones37f4b9c2011-07-01 17:56:50 -0500300 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 if ($this->save_queries == TRUE)
302 {
303 $this->queries[] = $sql;
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // Start the Query Timer
307 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // Run the Query
310 if (FALSE === ($this->result_id = $this->simple_query($sql)))
311 {
312 if ($this->save_queries == TRUE)
313 {
314 $this->query_times[] = 0;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // This will trigger a rollback if transactions are being used
318 $this->_trans_status = FALSE;
319
Andrey Andreev4be5de12012-03-02 15:45:41 +0200320 // Grab the error now, as we might run some additional queries before displaying the error
321 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200322
323 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200324 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 if ($this->db_debug)
327 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // We call this function in order to roll-back queries
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200329 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200330 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // transactions to remain in limbo.
332 $this->trans_complete();
333
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200334 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200335 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 return FALSE;
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // Stop and aggregate the query time results
342 $time_end = list($em, $es) = explode(' ', microtime());
343 $this->benchmark += ($em + $es) - ($sm + $ss);
344
345 if ($this->save_queries == TRUE)
346 {
347 $this->query_times[] = ($em + $es) - ($sm + $ss);
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // Increment the query counter
351 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // Was the query a "write" type?
354 // If so we'll simply return true
355 if ($this->is_write_type($sql) === TRUE)
356 {
357 // If caching is enabled we'll auto-cleanup any
358 // existing files related to this particular URI
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200359 if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
361 $this->CACHE->delete();
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 return TRUE;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // Return TRUE if we don't need to create a result object
368 // Currently only the Oracle driver uses this when stored
369 // procedures are used
370 if ($return_object !== TRUE)
371 {
372 return TRUE;
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
375 // Load and instantiate the result driver
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200376 $driver = $this->load_rdriver();
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200377 $RES = new $driver($this);
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200380
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200381 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // result object and save it to a cache file.
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200383 if ($this->cache_on == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 // We'll create a new instance of the result object
386 // only without the platform specific driver since
387 // we can't use it with cached data (the query result
388 // resource ID won't be any good once we've cached the
389 // result object, so we'll have to compile the data
390 // and save it)
391 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $CR->result_object = $RES->result_object();
393 $CR->result_array = $RES->result_array();
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200394 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // Reset these since cached objects can not utilize resource IDs.
397 $CR->conn_id = NULL;
398 $CR->result_id = NULL;
399
400 $this->CACHE->write($sql, $CR);
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $RES;
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Load the result drivers
410 *
Barry Mienydd671972010-10-04 16:33:58 +0200411 * @return string the name of the result class
412 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200413 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 $driver = 'CI_DB_'.$this->dbdriver.'_result';
416
417 if ( ! class_exists($driver))
418 {
Greg Aker3a746652011-04-19 10:59:47 -0500419 include_once(BASEPATH.'database/DB_result.php');
420 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 return $driver;
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // --------------------------------------------------------------------
427
428 /**
429 * Simple Query
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200430 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * we only use it when running transaction commands since they do
432 * not require all the features of the main query() function.
433 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200435 * @return mixed
436 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200437 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 if ( ! $this->conn_id)
440 {
441 $this->initialize();
442 }
443
444 return $this->_execute($sql);
445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // --------------------------------------------------------------------
448
449 /**
450 * Disable Transactions
451 * This permits transactions to be disabled at run-time.
452 *
Barry Mienydd671972010-10-04 16:33:58 +0200453 * @return void
454 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200455 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 $this->trans_enabled = FALSE;
458 }
459
460 // --------------------------------------------------------------------
461
462 /**
463 * Enable/disable Transaction Strict Mode
464 * When strict mode is enabled, if you are running multiple groups of
465 * transactions, if one group fails all groups will be rolled back.
466 * If strict mode is disabled, each group is treated autonomously, meaning
467 * a failure of one group will not affect any others
468 *
Barry Mienydd671972010-10-04 16:33:58 +0200469 * @return void
470 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200471 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Start Transaction
480 *
Barry Mienydd671972010-10-04 16:33:58 +0200481 * @return void
482 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200483 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200484 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 if ( ! $this->trans_enabled)
486 {
487 return FALSE;
488 }
489
490 // When transactions are nested we only begin/commit/rollback the outermost ones
491 if ($this->_trans_depth > 0)
492 {
493 $this->_trans_depth += 1;
494 return;
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500498 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500
501 // --------------------------------------------------------------------
502
503 /**
504 * Complete Transaction
505 *
Barry Mienydd671972010-10-04 16:33:58 +0200506 * @return bool
507 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200508 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 if ( ! $this->trans_enabled)
511 {
512 return FALSE;
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 // When transactions are nested we only begin/commit/rollback the outermost ones
516 if ($this->_trans_depth > 1)
517 {
518 $this->_trans_depth -= 1;
519 return TRUE;
520 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500521 else
522 {
523 $this->_trans_depth = 0;
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // The query() function will set this flag to FALSE in the event that a query failed
527 if ($this->_trans_status === FALSE)
528 {
529 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 // If we are NOT running in strict mode, we will reset
532 // the _trans_status flag so that subsequent groups of transactions
533 // will be permitted.
534 if ($this->trans_strict === FALSE)
535 {
536 $this->_trans_status = TRUE;
537 }
538
539 log_message('debug', 'DB Transaction Failure');
540 return FALSE;
541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 $this->trans_commit();
544 return TRUE;
545 }
546
547 // --------------------------------------------------------------------
548
549 /**
550 * Lets you retrieve the transaction flag to determine if it has failed
551 *
Barry Mienydd671972010-10-04 16:33:58 +0200552 * @return bool
553 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200554 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
556 return $this->_trans_status;
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Compile Bindings
563 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @param string the sql statement
565 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200566 * @return string
567 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200568 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
570 if (strpos($sql, $this->bind_marker) === FALSE)
571 {
572 return $sql;
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 if ( ! is_array($binds))
576 {
577 $binds = array($binds);
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // Get the sql segments around the bind markers
581 $segments = explode($this->bind_marker, $sql);
582
583 // The count of bind should be 1 less then the count of segments
584 // If there are more bind arguments trim it down
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200585 if (count($binds) >= count($segments))
586 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 $binds = array_slice($binds, 0, count($segments)-1);
588 }
589
590 // Construct the binded query
591 $result = $segments[0];
592 $i = 0;
593 foreach ($binds as $bind)
594 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200595 $result .= $this->escape($bind).$segments[++$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
597
598 return $result;
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // --------------------------------------------------------------------
602
603 /**
604 * Determines if a query is a "write" type.
605 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * @param string An SQL query string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200607 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200608 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200609 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200611 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|OPTIMIZE|REINDEX)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 // --------------------------------------------------------------------
615
616 /**
617 * Calculate the aggregate query elapsed time
618 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200619 * @param int The number of decimal places
620 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200621 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200622 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 return number_format($this->benchmark, $decimals);
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 // --------------------------------------------------------------------
628
629 /**
630 * Returns the total number of queries
631 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200632 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200633 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200634 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 return $this->query_count;
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 // --------------------------------------------------------------------
640
641 /**
642 * Returns the last query that was executed
643 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200644 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200646 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
648 return end($this->queries);
649 }
650
651 // --------------------------------------------------------------------
652
653 /**
654 * "Smart" Escape String
655 *
656 * Escapes data based on type
657 * Sets boolean and null types
658 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200660 * @return mixed
661 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200662 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200663 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500664 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200666 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000667 }
668 elseif (is_bool($str))
669 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200670 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000671 }
672 elseif (is_null($str))
673 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200674 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000675 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000676
677 return $str;
678 }
679
680 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600681
Derek Jonese4ed5832009-02-20 21:44:59 +0000682 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000683 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000684 *
685 * Calls the individual driver for platform
686 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200687 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000688 * @param string
689 * @return mixed
690 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200691 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200692 {
693 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000694 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000695
Derek Jonese4ed5832009-02-20 21:44:59 +0000696 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 /**
699 * Primary
700 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200701 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 * position is the primary key
703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200705 * @return string
706 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200707 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200708 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200710 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 }
712
713 // --------------------------------------------------------------------
714
715 /**
716 * Returns an array of table names
717 *
Barry Mienydd671972010-10-04 16:33:58 +0200718 * @return array
719 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200720 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 {
722 // Is there a cached result?
723 if (isset($this->data_cache['table_names']))
724 {
725 return $this->data_cache['table_names'];
726 }
Barry Mienydd671972010-10-04 16:33:58 +0200727
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
729 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200730 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 }
732
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200733 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200735
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200736 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200738 // Do we know from which column to get the table name?
739 if ( ! isset($key))
740 {
741 if (array_key_exists('table_name', $row))
742 {
743 $key = 'table_name';
744 }
745 elseif (array_key_exists('TABLE_NAME', $row))
746 {
747 $key = 'TABLE_NAME';
748 }
749 else
750 {
751 /* We have no other choice but to just get the first element's key.
752 * Due to array_shift() accepting it's argument by reference, if
753 * E_STRICT is on, this would trigger a warning. So we'll have to
754 * assign it first.
755 */
756 $key = array_keys($row);
757 $key = array_shift($key);
758 }
759 }
760
761 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 return $this->data_cache['table_names'];
765 }
Barry Mienydd671972010-10-04 16:33:58 +0200766
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 // --------------------------------------------------------------------
768
769 /**
770 * Determine if a particular table exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200771 *
772 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200774 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200775 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200776 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 // --------------------------------------------------------------------
780
781 /**
782 * Fetch MySQL Field Names
783 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200785 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200787 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
789 // Is there a cached result?
790 if (isset($this->data_cache['field_names'][$table]))
791 {
792 return $this->data_cache['field_names'][$table];
793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 if ($table == '')
796 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200797 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 }
Barry Mienydd671972010-10-04 16:33:58 +0200799
Greg Aker1edde302010-01-26 00:17:01 +0000800 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200802 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200806 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200807
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500808 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200810 // Do we know from where to get the column's name?
811 if ( ! isset($key))
812 {
813 if (array_key_exists('column_name', $row))
814 {
815 $key = 'column_name';
816 }
817 elseif (array_key_exists('COLUMN_NAME', $row))
818 {
819 $key = 'COLUMN_NAME';
820 }
821 else
822 {
823 /* We have no other choice but to just get the first element's key.
824 * Due to array_shift() accepting it's argument by reference, if
825 * E_STRICT is on, this would trigger a warning. So we'll have to
826 * assign it first.
827 */
828 $key = array_keys($row);
829 $key = array_shift($key);
830 }
831 }
832
833 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 return $this->data_cache['field_names'][$table];
837 }
838
839 // --------------------------------------------------------------------
840
841 /**
842 * Determine if a particular field exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200843 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 * @param string
845 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200846 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200848 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200849 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200850 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 // --------------------------------------------------------------------
854
855 /**
856 * Returns an object with field data
857 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 * @param string the table name
Andrey Andreev5d93e132012-03-06 20:36:21 +0200859 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200860 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200861 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
863 if ($table == '')
864 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200865 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200868 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200870 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000871
872 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 /**
875 * Generate an insert string
876 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 * @param string the table upon which the query will be performed
878 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200879 * @return string
880 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200881 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200883 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200884
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500885 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
887 $fields[] = $this->_escape_identifiers($key);
888 $values[] = $this->escape($val);
889 }
Barry Mienydd671972010-10-04 16:33:58 +0200890
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200891 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200892 }
893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 // --------------------------------------------------------------------
895
896 /**
897 * Generate an update string
898 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 * @param string the table upon which the query will be performed
900 * @param array an associative array data of key/values
901 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200902 * @return string
903 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200904 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
906 if ($where == '')
907 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200908 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500912 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200914 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
916
917 if ( ! is_array($where))
918 {
919 $dest = array($where);
920 }
921 else
922 {
923 $dest = array();
924 foreach ($where as $key => $val)
925 {
926 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200927 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 if ($val !== '')
930 {
931 if ( ! $this->_has_operator($key))
932 {
933 $key .= ' =';
934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 $val = ' '.$this->escape($val);
937 }
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 $dest[] = $prefix.$key.$val;
940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000942
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200943 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200944 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000945
946 // --------------------------------------------------------------------
947
948 /**
949 * Tests whether the string has an SQL operator
950 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 * @param string
952 * @return bool
953 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200954 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreev043f2602012-03-06 20:16:42 +0200956 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
958
959 // --------------------------------------------------------------------
960
961 /**
962 * Enables a native PHP function to be run, using a platform agnostic wrapper.
963 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 * @param string the function name
965 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200966 * @return mixed
967 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200968 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
970 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 if (FALSE === strpos($driver, $function))
973 {
974 $function = $driver.$function;
975 }
Barry Mienydd671972010-10-04 16:33:58 +0200976
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 if ( ! function_exists($function))
978 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200979 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000981
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200982 return (func_num_args() > 1)
983 ? call_user_func_array($function, array_splice(func_get_args(), 1))
984 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 }
986
987 // --------------------------------------------------------------------
988
989 /**
990 * Set Cache Directory Path
991 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 * @param string the path to the cache directory
993 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200994 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200995 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
997 $this->cachedir = $path;
998 }
999
1000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Enable Query Caching
1004 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001005 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001006 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001007 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001009 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
1011
1012 // --------------------------------------------------------------------
1013
1014 /**
1015 * Disable 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_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001021 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024
1025 // --------------------------------------------------------------------
1026
1027 /**
1028 * Delete the cache files associated with a particular URI
1029 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001030 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001031 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001032 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001034 return ($this->_cache_init())
1035 ? $this->CACHE->delete($segment_one, $segment_two)
1036 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 }
1038
1039 // --------------------------------------------------------------------
1040
1041 /**
1042 * Delete All cache files
1043 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001044 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001045 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001046 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001048 return ($this->_cache_init())
1049 ? $this->CACHE->delete_all()
1050 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 }
1052
1053 // --------------------------------------------------------------------
1054
1055 /**
1056 * Initialize the Cache Class
1057 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001058 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001059 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001060 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001062 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001064 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001065 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001066 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001067 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001069 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1070 {
1071 return $this->cache_off();
1072 }
Derek Allarde37ab382009-02-03 16:13:57 +00001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1075 return TRUE;
1076 }
1077
1078 // --------------------------------------------------------------------
1079
1080 /**
1081 * Close DB Connection
1082 *
Barry Mienydd671972010-10-04 16:33:58 +02001083 * @return void
1084 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001085 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001087 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 {
1089 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001090 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 }
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 // --------------------------------------------------------------------
1095
1096 /**
1097 * Display an error message
1098 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 * @param string the error message
1100 * @param string any "swap" values
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001101 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001102 * @return string sends the application/error_db.php template
1103 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001104 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 {
Derek Jonese7792202010-03-02 17:24:46 -06001106 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 $LANG->load('db');
1108
1109 $heading = $LANG->line('db_error_heading');
1110
1111 if ($native == TRUE)
1112 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001113 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 }
1115 else
1116 {
1117 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Pascal Kriete60f8c392010-08-25 18:03:28 +02001120 // Find the most likely culprit of the error by going through
1121 // the backtrace until the source file is no longer in the
1122 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001123 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001124 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001125 {
1126 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1127 {
1128 // Found it - use a relative path for safety
1129 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1130 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001131 break;
1132 }
1133 }
Barry Mienydd671972010-10-04 16:33:58 +02001134
Derek Jonese7792202010-03-02 17:24:46 -06001135 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 echo $error->show_error($heading, $message, 'error_db');
1137 exit;
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Protect Identifiers
1144 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001146 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001148 * the table prefix onto it. Some logic is necessary in order to deal with
1149 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 *
1151 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1152 *
1153 * Or a query with aliasing:
1154 *
1155 * SELECT m.member_id, m.member_name FROM members AS m
1156 *
1157 * Since the column name can include up to four segments (host, DB, table, column)
1158 * or also have an alias prefix, we need to do a bit of work to figure this out and
1159 * insert the table prefix (if it exists) in the proper position, and escape only
1160 * the correct identifiers.
1161 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 * @param string
1163 * @param bool
1164 * @param mixed
1165 * @param bool
1166 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001167 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001168 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
1170 if ( ! is_bool($protect_identifiers))
1171 {
1172 $protect_identifiers = $this->_protect_identifiers;
1173 }
Derek Allarde37ab382009-02-03 16:13:57 +00001174
1175 if (is_array($item))
1176 {
1177 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001178 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001179 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001180 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001181 }
1182
1183 return $escaped_array;
1184 }
1185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001187 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001188
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 // If the item has an alias declaration we remove it and set it aside.
1190 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001192 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001193 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 $item = substr($item, 0, - strlen($alias));
1195 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001196 else
1197 {
1198 $alias = '';
1199 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001200
Derek Allard911d3e02008-12-15 14:08:35 +00001201 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001202 // If a parenthesis is found we know that we do not need to
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001203 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001204 // way to deal with this, but I'm not thinking of it -- Rick
1205 if (strpos($item, '(') !== FALSE)
1206 {
1207 return $item.$alias;
1208 }
1209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 // Break the string apart if it contains periods, then insert the table prefix
1211 // in the correct location, assuming the period doesn't indicate that we're dealing
1212 // with an alias. While we're at it, we will escape the components
1213 if (strpos($item, '.') !== FALSE)
1214 {
1215 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001216
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 // Does the first segment of the exploded item match
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001218 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 // we have nothing more to do other than escape the item
1220 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001221 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 if ($protect_identifiers === TRUE)
1223 {
1224 foreach ($parts as $key => $val)
1225 {
1226 if ( ! in_array($val, $this->_reserved_identifiers))
1227 {
1228 $parts[$key] = $this->_escape_identifiers($val);
1229 }
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001233 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001234
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 return $item.$alias;
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001238 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 if ($this->dbprefix != '')
1240 {
1241 // We now add the table prefix based on some logic.
1242 // Do we have 4 segments (hostname.database.table.column)?
1243 // If so, we add the table prefix to the column name in the 3rd segment.
1244 if (isset($parts[3]))
1245 {
1246 $i = 2;
1247 }
1248 // Do we have 3 segments (database.table.column)?
1249 // If so, we add the table prefix to the column name in 2nd position
1250 elseif (isset($parts[2]))
1251 {
1252 $i = 1;
1253 }
1254 // Do we have 2 segments (table.column)?
1255 // If so, we add the table prefix to the column name in 1st segment
1256 else
1257 {
1258 $i = 0;
1259 }
Barry Mienydd671972010-10-04 16:33:58 +02001260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // This flag is set when the supplied $item does not contain a field name.
1262 // This can happen when this function is being called from a JOIN.
1263 if ($field_exists == FALSE)
1264 {
1265 $i++;
1266 }
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Jones55acc8b2009-07-11 03:38:13 +00001268 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001269 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001270 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001271 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001272 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001274 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
1276 $parts[$i] = $this->dbprefix.$parts[$i];
1277 }
Barry Mienydd671972010-10-04 16:33:58 +02001278
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 // Put the parts back together
1280 $item = implode('.', $parts);
1281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 if ($protect_identifiers === TRUE)
1284 {
1285 $item = $this->_escape_identifiers($item);
1286 }
Barry Mienydd671972010-10-04 16:33:58 +02001287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 return $item.$alias;
1289 }
1290
Andrey Andreev4c202602012-03-06 20:34:52 +02001291 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 if ($this->dbprefix != '')
1293 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001294 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001295 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001296 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001297 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001298 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001300 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 {
1302 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001303 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 }
1305
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001306 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 {
1308 $item = $this->_escape_identifiers($item);
1309 }
Barry Mienydd671972010-10-04 16:33:58 +02001310
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 return $item.$alias;
1312 }
Andrey Andreev4c598362012-03-06 14:58:37 +02001313
Túbal Martín511f2252011-11-24 14:43:45 +01001314 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001315
Túbal Martín511f2252011-11-24 14:43:45 +01001316 /**
1317 * Dummy method that allows Active Record class to be disabled
1318 *
1319 * This function is used extensively by every db driver.
1320 *
Túbal Martín511f2252011-11-24 14:43:45 +01001321 * @return void
1322 */
1323 protected function _reset_select()
1324 {
Túbal Martín511f2252011-11-24 14:43:45 +01001325 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001326
1327}
1328
Derek Allard2067d1a2008-11-13 22:59:24 +00001329/* End of file DB_driver.php */
Andrey Andreev35bbb1a2012-03-20 15:33:55 +02001330/* Location: ./system/database/DB_driver.php */