blob: 4296815f8e7a05da9fd73d69ffae82de26ae033c [file] [log] [blame]
Andrey Andreev4c202602012-03-06 20:34:52 +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 Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +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 */
Timothy Warren7eeda532012-03-19 16:01:55 -040041abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreev738f5342012-03-06 20:43:40 +020043 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
Andrey Andreev75546112012-07-05 11:01:29 +030048 public $dbdriver = 'mysqli';
Andrey Andreevfbba54e2012-06-23 20:26:31 +030049 public $subdriver;
Andrey Andreevd1add432012-03-06 20:26:01 +020050 public $dbprefix = '';
51 public $char_set = 'utf8';
52 public $dbcollat = 'utf8_general_ci';
53 public $autoinit = TRUE; // Whether to automatically initialize the DB
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020054 public $compress = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020055 public $swap_pre = '';
56 public $port = '';
57 public $pconnect = FALSE;
58 public $conn_id = FALSE;
59 public $result_id = FALSE;
60 public $db_debug = FALSE;
61 public $benchmark = 0;
62 public $query_count = 0;
63 public $bind_marker = '?';
64 public $save_queries = TRUE;
65 public $queries = array();
66 public $query_times = array();
67 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000068
Andrey Andreevd1add432012-03-06 20:26:01 +020069 public $trans_enabled = TRUE;
70 public $trans_strict = TRUE;
71 protected $_trans_depth = 0;
72 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000073
Andrey Andreevd1add432012-03-06 20:26:01 +020074 public $cache_on = FALSE;
75 public $cachedir = '';
76 public $cache_autodel = FALSE;
77 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020078
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000079 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020080 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
81
Andrey Andreev75546112012-07-05 11:01:29 +030082 /**
Andrey Andreev77a5d942012-07-05 15:42:14 +030083 * The syntax to count rows is slightly different across different
84 * database engines, so this string appears in each driver and is
85 * used for the count_all() and count_all_results() functions.
86 */
87 protected $_count_string = 'SELECT COUNT(*) AS ';
88
89 /**
Andrey Andreev75546112012-07-05 11:01:29 +030090 * Constructor
91 *
92 * @param array
93 * @return void
94 */
Timothy Warrene45518d2012-03-06 07:38:00 -050095 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
97 if (is_array($params))
98 {
99 foreach ($params as $key => $val)
100 {
101 $this->$key = $val;
102 }
103 }
104
105 log_message('debug', 'Database Driver Class Initialized');
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Gints Murans89f77ee2012-05-23 00:23:50 +0300108 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 /**
111 * Initialize Database Settings
112 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200113 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200115 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200117 /* If an established connection is available, then there's
118 * no need to connect and select the database.
119 *
120 * Depending on the database driver, conn_id can be either
121 * boolean TRUE, a resource or an object.
122 */
123 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
125 return TRUE;
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100131 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000132
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200133 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 if ( ! $this->conn_id)
135 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100136 // Check if there is a failover set
137 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100139 // Go over all the failovers
140 foreach ($this->failover as $failover)
141 {
142 // Replace the current settings with those of the failover
143 foreach ($failover as $key => $val)
144 {
145 $this->$key = $val;
146 }
147
148 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100149 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100150
151 // If a connection is made break the foreach loop
152 if ($this->conn_id)
153 {
154 break;
155 }
156 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100158
159 // We still don't have a connection?
160 if ( ! $this->conn_id)
161 {
162 log_message('error', 'Unable to connect to the database');
163
164 if ($this->db_debug)
165 {
166 $this->display_error('db_unable_to_connect');
167 }
168 return FALSE;
169 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // ----------------------------------------------------------------
173
174 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200175 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200177 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200178
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200179 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200181 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200183 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200186 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200187 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // --------------------------------------------------------------------
191
192 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200193 * Reconnect
194 *
195 * Keep / reestablish the db connection if no queries have been
196 * sent for a length of time exceeding the server's idle timeout.
197 *
198 * This is just a dummy method to allow drivers without such
199 * functionality to not declare it, while others will override it.
200 *
201 * @return void
202 */
203 public function reconnect()
204 {
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300210 * Select database
211 *
212 * This is just a dummy method to allow drivers without such
213 * functionality to not declare it, while others will override it.
214 *
215 * @return bool
216 */
217 public function db_select()
218 {
219 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Set client character set
226 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200228 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200230 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200232 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200234 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 if ($this->db_debug)
237 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200238 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 return FALSE;
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 return TRUE;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // --------------------------------------------------------------------
248
249 /**
250 * The name of the platform in use (mysql, mssql, etc...)
251 *
Barry Mienydd671972010-10-04 16:33:58 +0200252 * @return string
253 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500254 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 return $this->dbdriver;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200262 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200264 * Returns a string containing the version of the database being used.
265 * Most drivers will override this method.
266 *
Barry Mienydd671972010-10-04 16:33:58 +0200267 * @return string
268 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200269 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200271 if (isset($this->data_cache['version']))
272 {
273 return $this->data_cache['version'];
274 }
275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 if (FALSE === ($sql = $this->_version()))
277 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200278 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
Derek Allard3683f772009-12-16 17:32:33 +0000280
Andrey Andreev08856b82012-03-03 03:19:28 +0200281 $query = $this->query($sql);
282 $query = $query->row();
283 return $this->data_cache['version'] = $query->ver;
284 }
Derek Allard3683f772009-12-16 17:32:33 +0000285
Andrey Andreev08856b82012-03-03 03:19:28 +0200286 // --------------------------------------------------------------------
287
288 /**
289 * Version number query string
290 *
291 * @return string
292 */
293 protected function _version()
294 {
295 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Execute the query
302 *
303 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200304 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * upon successful execution of a "write" type query. Returns boolean
306 * FALSE upon failure, and if the $db_debug variable is set to TRUE
307 * will raise an error.
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @param string An SQL query string
310 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200311 * @return mixed
312 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300313 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100315 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200317 log_message('error', 'Invalid query: '.$sql);
318
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200319 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300321 elseif ( ! is_bool($return_object))
322 {
323 $return_object = ! $this->is_write_type($sql);
324 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
326 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100327 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600328 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200329 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Derek Jonese7792202010-03-02 17:24:46 -0600331
Ryan Dialef7474c2012-03-01 16:11:36 -0500332 // Compile binds if needed
333 if ($binds !== FALSE)
334 {
335 $sql = $this->compile_binds($sql, $binds);
336 }
337
Andrey Andreev4c202602012-03-06 20:34:52 +0200338 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // we will load the caching class and return the previously
340 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300341 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200343 $this->load_rdriver();
344 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200346 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Andrey Andreevb66664b2012-06-27 14:22:10 +0300350 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100351 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 $this->queries[] = $sql;
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200357 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // Run the Query
360 if (FALSE === ($this->result_id = $this->simple_query($sql)))
361 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100362 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 $this->query_times[] = 0;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // This will trigger a rollback if transactions are being used
368 $this->_trans_status = FALSE;
369
Andrey Andreev4be5de12012-03-02 15:45:41 +0200370 // Grab the error now, as we might run some additional queries before displaying the error
371 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200372
373 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300374 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 if ($this->db_debug)
377 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200379 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200380 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // transactions to remain in limbo.
382 $this->trans_complete();
383
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200384 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200385 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 return FALSE;
389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200392 $time_end = microtime(TRUE);
393 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000394
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100395 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
dixyab3cab52012-04-21 00:58:00 +0200397 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // Increment the query counter
401 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200402
Andrey Andreevb66664b2012-06-27 14:22:10 +0300403 // Will we have a result object instantiated? If not - we'll simply return TRUE
404 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300406 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100407 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 $this->CACHE->delete();
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 return TRUE;
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 if ($return_object !== TRUE)
417 {
418 return TRUE;
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
421 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200422 $driver = $this->load_rdriver();
423 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200424
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200425 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100427 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 // We'll create a new instance of the result object
430 // only without the platform specific driver since
431 // we can't use it with cached data (the query result
432 // resource ID won't be any good once we've cached the
433 // result object, so we'll have to compile the data
434 // and save it)
435 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 $CR->result_object = $RES->result_object();
437 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200438 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // Reset these since cached objects can not utilize resource IDs.
441 $CR->conn_id = NULL;
442 $CR->result_id = NULL;
443
444 $this->CACHE->write($sql, $CR);
445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 return $RES;
448 }
449
450 // --------------------------------------------------------------------
451
452 /**
453 * Load the result drivers
454 *
Barry Mienydd671972010-10-04 16:33:58 +0200455 * @return string the name of the result class
456 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500457 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
459 $driver = 'CI_DB_'.$this->dbdriver.'_result';
460
461 if ( ! class_exists($driver))
462 {
Greg Aker3a746652011-04-19 10:59:47 -0500463 include_once(BASEPATH.'database/DB_result.php');
464 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 return $driver;
468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 // --------------------------------------------------------------------
471
472 /**
473 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200474 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * we only use it when running transaction commands since they do
476 * not require all the features of the main query() function.
477 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200479 * @return mixed
480 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500481 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 if ( ! $this->conn_id)
484 {
485 $this->initialize();
486 }
487
488 return $this->_execute($sql);
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * Disable Transactions
495 * This permits transactions to be disabled at run-time.
496 *
Barry Mienydd671972010-10-04 16:33:58 +0200497 * @return void
498 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500499 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 $this->trans_enabled = FALSE;
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Enable/disable Transaction Strict Mode
508 * When strict mode is enabled, if you are running multiple groups of
509 * transactions, if one group fails all groups will be rolled back.
510 * If strict mode is disabled, each group is treated autonomously, meaning
511 * a failure of one group will not affect any others
512 *
Barry Mienydd671972010-10-04 16:33:58 +0200513 * @return void
514 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500515 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Start Transaction
524 *
Barry Mienydd671972010-10-04 16:33:58 +0200525 * @return void
526 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500527 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200528 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 if ( ! $this->trans_enabled)
530 {
531 return FALSE;
532 }
533
534 // When transactions are nested we only begin/commit/rollback the outermost ones
535 if ($this->_trans_depth > 0)
536 {
537 $this->_trans_depth += 1;
538 return;
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500542 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Complete Transaction
549 *
Barry Mienydd671972010-10-04 16:33:58 +0200550 * @return bool
551 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500552 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
554 if ( ! $this->trans_enabled)
555 {
556 return FALSE;
557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // When transactions are nested we only begin/commit/rollback the outermost ones
560 if ($this->_trans_depth > 1)
561 {
562 $this->_trans_depth -= 1;
563 return TRUE;
564 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500565 else
566 {
567 $this->_trans_depth = 0;
568 }
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 // The query() function will set this flag to FALSE in the event that a query failed
571 if ($this->_trans_status === FALSE)
572 {
573 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // If we are NOT running in strict mode, we will reset
576 // the _trans_status flag so that subsequent groups of transactions
577 // will be permitted.
578 if ($this->trans_strict === FALSE)
579 {
580 $this->_trans_status = TRUE;
581 }
582
583 log_message('debug', 'DB Transaction Failure');
584 return FALSE;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 $this->trans_commit();
588 return TRUE;
589 }
590
591 // --------------------------------------------------------------------
592
593 /**
594 * Lets you retrieve the transaction flag to determine if it has failed
595 *
Barry Mienydd671972010-10-04 16:33:58 +0200596 * @return bool
597 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500598 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
600 return $this->_trans_status;
601 }
602
603 // --------------------------------------------------------------------
604
605 /**
606 * Compile Bindings
607 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 * @param string the sql statement
609 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200610 * @return string
611 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500612 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300614 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
616 return $sql;
617 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300618 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300620 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300621 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300623 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200624 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300625 // Make sure we're using numeric keys
626 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300627 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300630 // We'll need the marker length later
631 $ml = strlen($this->bind_marker);
632
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300633 // Make sure not to replace a chunk inside a string that happens to match the bind marker
634 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300636 $c = preg_match_all('/'.preg_quote($this->bind_marker).'/i',
637 str_replace($matches[0],
638 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
639 $sql, $c),
640 $matches, PREG_OFFSET_CAPTURE);
641
642 // Bind values' count must match the count of markers in the query
643 if ($bind_count !== $c)
644 {
645 return $sql;
646 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300647 }
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300648 elseif (($c = preg_match_all('/'.preg_quote($this->bind_marker).'/i', $sql, $matches, PREG_OFFSET_CAPTURE)) !== $bind_count)
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300649 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300650 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
652
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300653 do
654 {
655 $c--;
656 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
657 }
658 while ($c !== 0);
659
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300660 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // --------------------------------------------------------------------
664
665 /**
666 * Determines if a query is a "write" type.
667 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200669 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200670 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200671 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300673 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 // --------------------------------------------------------------------
677
678 /**
679 * Calculate the aggregate query elapsed time
680 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200681 * @param int The number of decimal places
682 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200683 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500684 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 return number_format($this->benchmark, $decimals);
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // --------------------------------------------------------------------
690
691 /**
692 * Returns the total number of queries
693 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200694 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200695 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500696 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
698 return $this->query_count;
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * Returns the last query that was executed
705 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200706 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200707 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500708 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
710 return end($this->queries);
711 }
712
713 // --------------------------------------------------------------------
714
715 /**
716 * "Smart" Escape String
717 *
718 * Escapes data based on type
719 * Sets boolean and null types
720 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200722 * @return mixed
723 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500724 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200725 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500726 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200728 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000729 }
730 elseif (is_bool($str))
731 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200732 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000733 }
734 elseif (is_null($str))
735 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200736 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000737 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000738
739 return $str;
740 }
741
742 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600743
Derek Jonese4ed5832009-02-20 21:44:59 +0000744 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000745 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000746 *
747 * Calls the individual driver for platform
748 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200749 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000750 * @param string
751 * @return mixed
752 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500753 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200754 {
755 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000756 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000757
Derek Jonese4ed5832009-02-20 21:44:59 +0000758 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 /**
761 * Primary
762 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200763 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 * position is the primary key
765 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200767 * @return string
768 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500769 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200770 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200772 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
774
775 // --------------------------------------------------------------------
776
777 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300778 * "Count All" query
779 *
780 * Generates a platform-specific query string that counts all records in
781 * the specified database
782 *
783 * @param string
784 * @return int
785 */
786 public function count_all($table = '')
787 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100788 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300789 {
790 return 0;
791 }
792
793 $query = $this->query($this->_count_string.$this->escape_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100794 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300795 {
796 return 0;
797 }
798
799 $query = $query->row();
800 $this->_reset_select();
801 return (int) $query->numrows;
802 }
803
804 // --------------------------------------------------------------------
805
806 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 * Returns an array of table names
808 *
Barry Mienydd671972010-10-04 16:33:58 +0200809 * @return array
810 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500811 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
813 // Is there a cached result?
814 if (isset($this->data_cache['table_names']))
815 {
816 return $this->data_cache['table_names'];
817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
820 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200821 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 }
823
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200824 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200826
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200827 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200829 // Do we know from which column to get the table name?
830 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300832 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200833 {
834 $key = 'table_name';
835 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300836 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200837 {
838 $key = 'TABLE_NAME';
839 }
840 else
841 {
842 /* We have no other choice but to just get the first element's key.
843 * Due to array_shift() accepting it's argument by reference, if
844 * E_STRICT is on, this would trigger a warning. So we'll have to
845 * assign it first.
846 */
847 $key = array_keys($row);
848 $key = array_shift($key);
849 }
Taufan Aditya18209332012-02-09 16:07:27 +0700850 }
851
Andrey Andreev12567e82012-01-25 22:50:33 +0200852 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 return $this->data_cache['table_names'];
856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 // --------------------------------------------------------------------
859
860 /**
861 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500862 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200863 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500865 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200866 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200867 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 // --------------------------------------------------------------------
871
872 /**
Andrey Andreev75546112012-07-05 11:01:29 +0300873 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200876 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500878 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
880 // Is there a cached result?
881 if (isset($this->data_cache['field_names'][$table]))
882 {
883 return $this->data_cache['field_names'][$table];
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100886 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200888 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 }
Barry Mienydd671972010-10-04 16:33:58 +0200890
Greg Aker1edde302010-01-26 00:17:01 +0000891 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200893 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200897 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200898
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500899 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200901 // Do we know from where to get the column's name?
902 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300904 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200905 {
906 $key = 'column_name';
907 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300908 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200909 {
910 $key = 'COLUMN_NAME';
911 }
912 else
913 {
914 /* We have no other choice but to just get the first element's key.
915 * Due to array_shift() accepting it's argument by reference, if
916 * E_STRICT is on, this would trigger a warning. So we'll have to
917 * assign it first.
918 */
919 $key = array_keys($row);
920 $key = array_shift($key);
921 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200923
924 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 return $this->data_cache['field_names'][$table];
928 }
929
930 // --------------------------------------------------------------------
931
932 /**
933 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500934 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @param string
936 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200937 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500939 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200940 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200941 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 // --------------------------------------------------------------------
945
946 /**
947 * Returns an object with field data
948 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200950 * @return object
951 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500952 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100954 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200956 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200959 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200961 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000962
963 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300966 * Escape the SQL Identifiers
967 *
968 * This function escapes column and table names
969 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300970 * @param mixed
971 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300972 */
973 public function escape_identifiers($item)
974 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300975 if ($this->_escape_char === '' OR empty($item))
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300976 {
977 return $item;
978 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300979 elseif (is_array($item))
980 {
981 foreach ($item as $key => $value)
982 {
983 $item[$key] = $this->escape_identifiers($value);
984 }
985
986 return $item;
987 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300988 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +0300989 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +0300990 {
991 return $item;
992 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300993
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300994 static $preg_ec = array();
995
996 if (empty($preg_ec))
997 {
998 if (is_array($this->_escape_char))
999 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001000 $preg_ec = array(
1001 preg_quote($this->_escape_char[0]), preg_quote($this->_escape_char[1]),
1002 $this->_escape_char[0], $this->_escape_char[1]
1003 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001004 }
1005 else
1006 {
1007 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001008 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001009 }
1010 }
1011
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001012 foreach ($this->_reserved_identifiers as $id)
1013 {
1014 if (strpos($item, '.'.$id) !== FALSE)
1015 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001016 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\./i', $preg_ec[2].'$1'.$preg_ec[3].'.', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001017 }
1018 }
1019
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001020 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?(\.)?/i', $preg_ec[2].'$1'.$preg_ec[3].'$2', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001021 }
1022
1023 // --------------------------------------------------------------------
1024
1025 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 * Generate an insert string
1027 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @param string the table upon which the query will be performed
1029 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001030 * @return string
1031 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001032 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001034 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001035
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001036 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001038 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 $values[] = $this->escape($val);
1040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001042 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001043 }
1044
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 // --------------------------------------------------------------------
1046
1047 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001048 * Insert statement
1049 *
1050 * Generates a platform-specific insert string from the supplied data
1051 *
1052 * @param string the table name
1053 * @param array the insert keys
1054 * @param array the insert values
1055 * @return string
1056 */
1057 protected function _insert($table, $keys, $values)
1058 {
1059 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1060 }
1061
1062 // --------------------------------------------------------------------
1063
1064 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * Generate an update string
1066 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 * @param string the table upon which the query will be performed
1068 * @param array an associative array data of key/values
1069 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001070 * @return string
1071 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001072 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001074 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001076 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 }
Barry Mienydd671972010-10-04 16:33:58 +02001078
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001080 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001082 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 }
1084
1085 if ( ! is_array($where))
1086 {
1087 $dest = array($where);
1088 }
1089 else
1090 {
1091 $dest = array();
1092 foreach ($where as $key => $val)
1093 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001094 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001095 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 if ($val !== '')
1098 {
1099 if ( ! $this->_has_operator($key))
1100 {
1101 $key .= ' =';
1102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 $val = ' '.$this->escape($val);
1105 }
Barry Mienydd671972010-10-04 16:33:58 +02001106
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 $dest[] = $prefix.$key.$val;
1108 }
Barry Mienydd671972010-10-04 16:33:58 +02001109 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001110
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001111 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001112 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001113
1114 // --------------------------------------------------------------------
1115
1116 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001117 * Update statement
1118 *
1119 * Generates a platform-specific update string from the supplied data
1120 *
1121 * @param string the table name
1122 * @param array the update data
1123 * @param array the where clause
1124 * @param array the orderby clause
1125 * @param array the limit clause
1126 * @param array the like clause
1127 * @return string
1128 */
1129 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
1130 {
1131 foreach ($values as $key => $val)
1132 {
1133 $valstr[] = $key.' = '.$val;
1134 }
1135
1136 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1137
1138 if ( ! empty($like))
1139 {
1140 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1141 }
1142
1143 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
1144 .$where
1145 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1146 .($limit ? ' LIMIT '.$limit : '');
1147 }
1148
1149 // --------------------------------------------------------------------
1150
1151 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * Tests whether the string has an SQL operator
1153 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 * @param string
1155 * @return bool
1156 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001157 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Andrey Andreevd454f0e2012-06-10 14:51:04 +03001159 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL|BETWEEN)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 }
1161
1162 // --------------------------------------------------------------------
1163
1164 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001165 * Returns the SQL string operator
1166 *
1167 * @param string
1168 * @return string
1169 */
1170 protected function _get_operator($str)
1171 {
1172 return preg_match('/(=|!|<|>| IS NULL| IS NOT NULL| BETWEEN)/i', $str, $match)
1173 ? $match[1] : FALSE;
1174 }
1175
1176 // --------------------------------------------------------------------
1177
1178 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1180 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 * @param string the function name
1182 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001183 * @return mixed
1184 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001185 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001187 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001188
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 if (FALSE === strpos($driver, $function))
1190 {
1191 $function = $driver.$function;
1192 }
Barry Mienydd671972010-10-04 16:33:58 +02001193
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 if ( ! function_exists($function))
1195 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001196 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001198
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001199 return (func_num_args() > 1)
1200 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1201 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 }
1203
1204 // --------------------------------------------------------------------
1205
1206 /**
1207 * Set Cache Directory Path
1208 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 * @param string the path to the cache directory
1210 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001211 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001212 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 {
1214 $this->cachedir = $path;
1215 }
1216
1217 // --------------------------------------------------------------------
1218
1219 /**
1220 * Enable Query Caching
1221 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001222 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001223 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001224 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001226 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 }
1228
1229 // --------------------------------------------------------------------
1230
1231 /**
1232 * Disable Query Caching
1233 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001234 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001235 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001236 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001238 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 // --------------------------------------------------------------------
1242
1243 /**
1244 * Delete the cache files associated with a particular URI
1245 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001246 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001247 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001248 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001250 return ($this->_cache_init())
1251 ? $this->CACHE->delete($segment_one, $segment_two)
1252 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 }
1254
1255 // --------------------------------------------------------------------
1256
1257 /**
1258 * Delete All cache files
1259 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001260 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001261 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001262 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001264 return ($this->_cache_init())
1265 ? $this->CACHE->delete_all()
1266 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 }
1268
1269 // --------------------------------------------------------------------
1270
1271 /**
1272 * Initialize the Cache Class
1273 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001274 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001275 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001276 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001278 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001280 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001281 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001282 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001283 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001285 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1286 {
1287 return $this->cache_off();
1288 }
Derek Allarde37ab382009-02-03 16:13:57 +00001289
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1291 return TRUE;
1292 }
1293
1294 // --------------------------------------------------------------------
1295
1296 /**
1297 * Close DB Connection
1298 *
Barry Mienydd671972010-10-04 16:33:58 +02001299 * @return void
1300 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001301 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001303 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001305 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001306 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 // --------------------------------------------------------------------
1311
1312 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001313 * Close DB Connection
1314 *
1315 * This method would be overriden by most of the drivers.
1316 *
1317 * @return void
1318 */
1319 protected function _close()
1320 {
1321 $this->conn_id = FALSE;
1322 }
1323
1324 // --------------------------------------------------------------------
1325
1326 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 * Display an error message
1328 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 * @param string the error message
1330 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001331 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001332 * @return string sends the application/error_db.php template
1333 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001334 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 {
Derek Jonese7792202010-03-02 17:24:46 -06001336 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 $LANG->load('db');
1338
1339 $heading = $LANG->line('db_error_heading');
1340
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001341 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001343 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 }
1345 else
1346 {
1347 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1348 }
Barry Mienydd671972010-10-04 16:33:58 +02001349
Pascal Kriete60f8c392010-08-25 18:03:28 +02001350 // Find the most likely culprit of the error by going through
1351 // the backtrace until the source file is no longer in the
1352 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001353 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001354 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001355 {
1356 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1357 {
1358 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001359 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001360 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001361 break;
1362 }
1363 }
Barry Mienydd671972010-10-04 16:33:58 +02001364
Derek Jonese7792202010-03-02 17:24:46 -06001365 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 echo $error->show_error($heading, $message, 'error_db');
1367 exit;
1368 }
1369
1370 // --------------------------------------------------------------------
1371
1372 /**
1373 * Protect Identifiers
1374 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001375 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001376 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001378 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001379 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 *
1381 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1382 *
1383 * Or a query with aliasing:
1384 *
1385 * SELECT m.member_id, m.member_name FROM members AS m
1386 *
1387 * Since the column name can include up to four segments (host, DB, table, column)
1388 * or also have an alias prefix, we need to do a bit of work to figure this out and
1389 * insert the table prefix (if it exists) in the proper position, and escape only
1390 * the correct identifiers.
1391 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 * @param string
1393 * @param bool
1394 * @param mixed
1395 * @param bool
1396 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001397 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001398 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 {
1400 if ( ! is_bool($protect_identifiers))
1401 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001402 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 }
Derek Allarde37ab382009-02-03 16:13:57 +00001404
1405 if (is_array($item))
1406 {
1407 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001408 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001409 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001410 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001411 }
1412
1413 return $escaped_array;
1414 }
1415
Derek Allard911d3e02008-12-15 14:08:35 +00001416 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001417 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001418 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001419 // way to deal with this, but I'm not thinking of it -- Rick
1420 if (strpos($item, '(') !== FALSE)
1421 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001422 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001423 }
1424
Andrey Andreevbf940582012-06-10 07:05:05 +03001425 // Convert tabs or multiple spaces into single spaces
1426 $item = preg_replace('/\s+/', ' ', $item);
1427
Andrey Andreevbf940582012-06-10 07:05:05 +03001428 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001429 // Note: strripos() is used in order to support spaces in table names
1430 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001431 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001432 $alias = ($protect_identifiers)
1433 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1434 : substr($item, $offset);
1435 $item = substr($item, 0, $offset);
1436 }
1437 elseif ($offset = strrpos($item, ' '))
1438 {
1439 $alias = ($protect_identifiers)
1440 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1441 : substr($item, $offset);
1442 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001443 }
1444 else
1445 {
1446 $alias = '';
1447 }
1448
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 // Break the string apart if it contains periods, then insert the table prefix
1450 // in the correct location, assuming the period doesn't indicate that we're dealing
1451 // with an alias. While we're at it, we will escape the components
1452 if (strpos($item, '.') !== FALSE)
1453 {
1454 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001455
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001457 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001459 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001460 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 if ($protect_identifiers === TRUE)
1462 {
1463 foreach ($parts as $key => $val)
1464 {
1465 if ( ! in_array($val, $this->_reserved_identifiers))
1466 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001467 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 }
1469 }
Barry Mienydd671972010-10-04 16:33:58 +02001470
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001472 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001473
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 return $item.$alias;
1475 }
Barry Mienydd671972010-10-04 16:33:58 +02001476
Andrey Andreev4c202602012-03-06 20:34:52 +02001477 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001478 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 {
1480 // We now add the table prefix based on some logic.
1481 // Do we have 4 segments (hostname.database.table.column)?
1482 // If so, we add the table prefix to the column name in the 3rd segment.
1483 if (isset($parts[3]))
1484 {
1485 $i = 2;
1486 }
1487 // Do we have 3 segments (database.table.column)?
1488 // If so, we add the table prefix to the column name in 2nd position
1489 elseif (isset($parts[2]))
1490 {
1491 $i = 1;
1492 }
1493 // Do we have 2 segments (table.column)?
1494 // If so, we add the table prefix to the column name in 1st segment
1495 else
1496 {
1497 $i = 0;
1498 }
Barry Mienydd671972010-10-04 16:33:58 +02001499
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 // This flag is set when the supplied $item does not contain a field name.
1501 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001502 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 {
1504 $i++;
1505 }
Barry Mienydd671972010-10-04 16:33:58 +02001506
Derek Jones55acc8b2009-07-11 03:38:13 +00001507 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001508 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001509 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001510 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001511 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001513 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 {
1515 $parts[$i] = $this->dbprefix.$parts[$i];
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 // Put the parts back together
1519 $item = implode('.', $parts);
1520 }
Barry Mienydd671972010-10-04 16:33:58 +02001521
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 if ($protect_identifiers === TRUE)
1523 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001524 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 }
Barry Mienydd671972010-10-04 16:33:58 +02001526
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 return $item.$alias;
1528 }
1529
Andrey Andreev4c202602012-03-06 20:34:52 +02001530 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001531 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001533 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001534 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001535 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001536 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001537 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001539 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 {
1541 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001542 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 }
1544
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001545 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001547 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 }
Barry Mienydd671972010-10-04 16:33:58 +02001549
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 return $item.$alias;
1551 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001552
Túbal Martín511f2252011-11-24 14:43:45 +01001553 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001554
Túbal Martín511f2252011-11-24 14:43:45 +01001555 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001556 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001557 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001558 *
Túbal Martín511f2252011-11-24 14:43:45 +01001559 * @return void
1560 */
1561 protected function _reset_select()
1562 {
Túbal Martín511f2252011-11-24 14:43:45 +01001563 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001564
1565}
1566
Derek Allard2067d1a2008-11-13 22:59:24 +00001567/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001568/* Location: ./system/database/DB_driver.php */