blob: 1060ecc6cffb89c6797a64fa0e5b0ec9efc658c0 [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 Andreevd1add432012-03-06 20:26:01 +020049 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 Andreevd1add432012-03-06 20:26:01 +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 Andreevd1add432012-03-06 20:26:01 +020072 public $cache_on = FALSE;
73 public $cachedir = '';
74 public $cache_autodel = FALSE;
75 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020076
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000077 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020078 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
Andrey Andreev75546112012-07-05 11:01:29 +030080 /**
Andrey Andreev77a5d942012-07-05 15:42:14 +030081 * The syntax to count rows is slightly different across different
82 * database engines, so this string appears in each driver and is
83 * used for the count_all() and count_all_results() functions.
84 */
85 protected $_count_string = 'SELECT COUNT(*) AS ';
86
87 /**
Andrey Andreev75546112012-07-05 11:01:29 +030088 * Constructor
89 *
90 * @param array
91 * @return void
92 */
Timothy Warrene45518d2012-03-06 07:38:00 -050093 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 if (is_array($params))
96 {
97 foreach ($params as $key => $val)
98 {
99 $this->$key = $val;
100 }
101 }
102
103 log_message('debug', 'Database Driver Class Initialized');
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Gints Murans89f77ee2012-05-23 00:23:50 +0300106 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 /**
109 * Initialize Database Settings
110 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200111 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200112 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200113 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200115 /* If an established connection is available, then there's
116 * no need to connect and select the database.
117 *
118 * Depending on the database driver, conn_id can be either
119 * boolean TRUE, a resource or an object.
120 */
121 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
123 return TRUE;
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100129 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000130
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200131 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 if ( ! $this->conn_id)
133 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100134 // Check if there is a failover set
135 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100137 // Go over all the failovers
138 foreach ($this->failover as $failover)
139 {
140 // Replace the current settings with those of the failover
141 foreach ($failover as $key => $val)
142 {
143 $this->$key = $val;
144 }
145
146 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100147 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100148
149 // If a connection is made break the foreach loop
150 if ($this->conn_id)
151 {
152 break;
153 }
154 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100156
157 // We still don't have a connection?
158 if ( ! $this->conn_id)
159 {
160 log_message('error', 'Unable to connect to the database');
161
162 if ($this->db_debug)
163 {
164 $this->display_error('db_unable_to_connect');
165 }
166 return FALSE;
167 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // ----------------------------------------------------------------
171
172 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200173 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200175 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200176
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200177 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200179 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200181 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200184 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200185 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // --------------------------------------------------------------------
189
190 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200191 * Reconnect
192 *
193 * Keep / reestablish the db connection if no queries have been
194 * sent for a length of time exceeding the server's idle timeout.
195 *
196 * This is just a dummy method to allow drivers without such
197 * functionality to not declare it, while others will override it.
198 *
199 * @return void
200 */
201 public function reconnect()
202 {
203 }
204
205 // --------------------------------------------------------------------
206
207 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300208 * Select database
209 *
210 * This is just a dummy method to allow drivers without such
211 * functionality to not declare it, while others will override it.
212 *
213 * @return bool
214 */
215 public function db_select()
216 {
217 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Set client character set
224 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200226 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200228 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200230 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200232 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 if ($this->db_debug)
235 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200236 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 return FALSE;
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 return TRUE;
243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // --------------------------------------------------------------------
246
247 /**
248 * The name of the platform in use (mysql, mssql, etc...)
249 *
Barry Mienydd671972010-10-04 16:33:58 +0200250 * @return string
251 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500252 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 return $this->dbdriver;
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200260 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200262 * Returns a string containing the version of the database being used.
263 * Most drivers will override this method.
264 *
Barry Mienydd671972010-10-04 16:33:58 +0200265 * @return string
266 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200267 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200269 if (isset($this->data_cache['version']))
270 {
271 return $this->data_cache['version'];
272 }
273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 if (FALSE === ($sql = $this->_version()))
275 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200276 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
Derek Allard3683f772009-12-16 17:32:33 +0000278
Andrey Andreev08856b82012-03-03 03:19:28 +0200279 $query = $this->query($sql);
280 $query = $query->row();
281 return $this->data_cache['version'] = $query->ver;
282 }
Derek Allard3683f772009-12-16 17:32:33 +0000283
Andrey Andreev08856b82012-03-03 03:19:28 +0200284 // --------------------------------------------------------------------
285
286 /**
287 * Version number query string
288 *
289 * @return string
290 */
291 protected function _version()
292 {
293 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // --------------------------------------------------------------------
297
298 /**
299 * Execute the query
300 *
301 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200302 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * upon successful execution of a "write" type query. Returns boolean
304 * FALSE upon failure, and if the $db_debug variable is set to TRUE
305 * will raise an error.
306 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @param string An SQL query string
308 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200309 * @return mixed
310 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300311 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100313 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200315 log_message('error', 'Invalid query: '.$sql);
316
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200317 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300319 elseif ( ! is_bool($return_object))
320 {
321 $return_object = ! $this->is_write_type($sql);
322 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000323
324 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100325 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600326 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200327 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
Derek Jonese7792202010-03-02 17:24:46 -0600329
Ryan Dialef7474c2012-03-01 16:11:36 -0500330 // Compile binds if needed
331 if ($binds !== FALSE)
332 {
333 $sql = $this->compile_binds($sql, $binds);
334 }
335
Andrey Andreev4c202602012-03-06 20:34:52 +0200336 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // we will load the caching class and return the previously
338 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300339 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200341 $this->load_rdriver();
342 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200344 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Andrey Andreevb66664b2012-06-27 14:22:10 +0300348 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100349 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $this->queries[] = $sql;
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200355 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // Run the Query
358 if (FALSE === ($this->result_id = $this->simple_query($sql)))
359 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100360 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 $this->query_times[] = 0;
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // This will trigger a rollback if transactions are being used
366 $this->_trans_status = FALSE;
367
Andrey Andreev4be5de12012-03-02 15:45:41 +0200368 // Grab the error now, as we might run some additional queries before displaying the error
369 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200370
371 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300372 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 if ($this->db_debug)
375 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200377 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200378 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // transactions to remain in limbo.
380 $this->trans_complete();
381
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200382 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200383 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 return FALSE;
387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200390 $time_end = microtime(TRUE);
391 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100393 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
dixyab3cab52012-04-21 00:58:00 +0200395 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // Increment the query counter
399 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200400
Andrey Andreevb66664b2012-06-27 14:22:10 +0300401 // Will we have a result object instantiated? If not - we'll simply return TRUE
402 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300404 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100405 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 $this->CACHE->delete();
408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return TRUE;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 if ($return_object !== TRUE)
415 {
416 return TRUE;
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
419 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200420 $driver = $this->load_rdriver();
421 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200422
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200423 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100425 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 // We'll create a new instance of the result object
428 // only without the platform specific driver since
429 // we can't use it with cached data (the query result
430 // resource ID won't be any good once we've cached the
431 // result object, so we'll have to compile the data
432 // and save it)
433 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 $CR->result_object = $RES->result_object();
435 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200436 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // Reset these since cached objects can not utilize resource IDs.
439 $CR->conn_id = NULL;
440 $CR->result_id = NULL;
441
442 $this->CACHE->write($sql, $CR);
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return $RES;
446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * Load the result drivers
452 *
Barry Mienydd671972010-10-04 16:33:58 +0200453 * @return string the name of the result class
454 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500455 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 $driver = 'CI_DB_'.$this->dbdriver.'_result';
458
459 if ( ! class_exists($driver))
460 {
Greg Aker3a746652011-04-19 10:59:47 -0500461 include_once(BASEPATH.'database/DB_result.php');
462 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 return $driver;
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
469
470 /**
471 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200472 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * we only use it when running transaction commands since they do
474 * not require all the features of the main query() function.
475 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200477 * @return mixed
478 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500479 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
481 if ( ! $this->conn_id)
482 {
483 $this->initialize();
484 }
485
486 return $this->_execute($sql);
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 /**
492 * Disable Transactions
493 * This permits transactions to be disabled at run-time.
494 *
Barry Mienydd671972010-10-04 16:33:58 +0200495 * @return void
496 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500497 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 $this->trans_enabled = FALSE;
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * Enable/disable Transaction Strict Mode
506 * When strict mode is enabled, if you are running multiple groups of
507 * transactions, if one group fails all groups will be rolled back.
508 * If strict mode is disabled, each group is treated autonomously, meaning
509 * a failure of one group will not affect any others
510 *
Barry Mienydd671972010-10-04 16:33:58 +0200511 * @return void
512 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500513 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // --------------------------------------------------------------------
519
520 /**
521 * Start Transaction
522 *
Barry Mienydd671972010-10-04 16:33:58 +0200523 * @return void
524 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500525 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200526 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 if ( ! $this->trans_enabled)
528 {
529 return FALSE;
530 }
531
532 // When transactions are nested we only begin/commit/rollback the outermost ones
533 if ($this->_trans_depth > 0)
534 {
535 $this->_trans_depth += 1;
536 return;
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500540 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542
543 // --------------------------------------------------------------------
544
545 /**
546 * Complete Transaction
547 *
Barry Mienydd671972010-10-04 16:33:58 +0200548 * @return bool
549 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500550 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
552 if ( ! $this->trans_enabled)
553 {
554 return FALSE;
555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 // When transactions are nested we only begin/commit/rollback the outermost ones
558 if ($this->_trans_depth > 1)
559 {
560 $this->_trans_depth -= 1;
561 return TRUE;
562 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500563 else
564 {
565 $this->_trans_depth = 0;
566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 // The query() function will set this flag to FALSE in the event that a query failed
569 if ($this->_trans_status === FALSE)
570 {
571 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 // If we are NOT running in strict mode, we will reset
574 // the _trans_status flag so that subsequent groups of transactions
575 // will be permitted.
576 if ($this->trans_strict === FALSE)
577 {
578 $this->_trans_status = TRUE;
579 }
580
581 log_message('debug', 'DB Transaction Failure');
582 return FALSE;
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 $this->trans_commit();
586 return TRUE;
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
592 * Lets you retrieve the transaction flag to determine if it has failed
593 *
Barry Mienydd671972010-10-04 16:33:58 +0200594 * @return bool
595 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500596 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 return $this->_trans_status;
599 }
600
601 // --------------------------------------------------------------------
602
603 /**
604 * Compile Bindings
605 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * @param string the sql statement
607 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200608 * @return string
609 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500610 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300612 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 return $sql;
615 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300616 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300618 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300619 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300621 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200622 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300623 // Make sure we're using numeric keys
624 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300625 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 }
627
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300628 // We'll need the marker length later
629 $ml = strlen($this->bind_marker);
630
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300631 // Make sure not to replace a chunk inside a string that happens to match the bind marker
632 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300634 $c = preg_match_all('/'.preg_quote($this->bind_marker).'/i',
635 str_replace($matches[0],
636 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
637 $sql, $c),
638 $matches, PREG_OFFSET_CAPTURE);
639
640 // Bind values' count must match the count of markers in the query
641 if ($bind_count !== $c)
642 {
643 return $sql;
644 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300645 }
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300646 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 +0300647 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300648 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
650
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300651 do
652 {
653 $c--;
654 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
655 }
656 while ($c !== 0);
657
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300658 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
662
663 /**
664 * Determines if a query is a "write" type.
665 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200667 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200668 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200669 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300671 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 +0000672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 // --------------------------------------------------------------------
675
676 /**
677 * Calculate the aggregate query elapsed time
678 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200679 * @param int The number of decimal places
680 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200681 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500682 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 return number_format($this->benchmark, $decimals);
685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 // --------------------------------------------------------------------
688
689 /**
690 * Returns the total number of queries
691 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200692 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200693 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500694 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
696 return $this->query_count;
697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 // --------------------------------------------------------------------
700
701 /**
702 * Returns the last query that was executed
703 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200704 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200705 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500706 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
708 return end($this->queries);
709 }
710
711 // --------------------------------------------------------------------
712
713 /**
714 * "Smart" Escape String
715 *
716 * Escapes data based on type
717 * Sets boolean and null types
718 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200720 * @return mixed
721 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500722 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200723 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500724 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200726 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000727 }
728 elseif (is_bool($str))
729 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200730 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000731 }
732 elseif (is_null($str))
733 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200734 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000735 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000736
737 return $str;
738 }
739
740 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600741
Derek Jonese4ed5832009-02-20 21:44:59 +0000742 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000743 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000744 *
745 * Calls the individual driver for platform
746 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200747 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000748 * @param string
749 * @return mixed
750 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500751 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200752 {
753 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000754 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000755
Derek Jonese4ed5832009-02-20 21:44:59 +0000756 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 /**
759 * Primary
760 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200761 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 * position is the primary key
763 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200765 * @return string
766 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500767 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200768 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200770 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
772
773 // --------------------------------------------------------------------
774
775 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300776 * "Count All" query
777 *
778 * Generates a platform-specific query string that counts all records in
779 * the specified database
780 *
781 * @param string
782 * @return int
783 */
784 public function count_all($table = '')
785 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100786 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300787 {
788 return 0;
789 }
790
791 $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 +0100792 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300793 {
794 return 0;
795 }
796
797 $query = $query->row();
798 $this->_reset_select();
799 return (int) $query->numrows;
800 }
801
802 // --------------------------------------------------------------------
803
804 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 * Returns an array of table names
806 *
Barry Mienydd671972010-10-04 16:33:58 +0200807 * @return array
808 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500809 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
811 // Is there a cached result?
812 if (isset($this->data_cache['table_names']))
813 {
814 return $this->data_cache['table_names'];
815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
818 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200819 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 }
821
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200822 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200824
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200825 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200827 // Do we know from which column to get the table name?
828 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300830 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200831 {
832 $key = 'table_name';
833 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300834 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200835 {
836 $key = 'TABLE_NAME';
837 }
838 else
839 {
840 /* We have no other choice but to just get the first element's key.
841 * Due to array_shift() accepting it's argument by reference, if
842 * E_STRICT is on, this would trigger a warning. So we'll have to
843 * assign it first.
844 */
845 $key = array_keys($row);
846 $key = array_shift($key);
847 }
Taufan Aditya18209332012-02-09 16:07:27 +0700848 }
849
Andrey Andreev12567e82012-01-25 22:50:33 +0200850 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 return $this->data_cache['table_names'];
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // --------------------------------------------------------------------
857
858 /**
859 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500860 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200861 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500863 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200864 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200865 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 // --------------------------------------------------------------------
869
870 /**
Andrey Andreev75546112012-07-05 11:01:29 +0300871 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200874 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500876 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
878 // Is there a cached result?
879 if (isset($this->data_cache['field_names'][$table]))
880 {
881 return $this->data_cache['field_names'][$table];
882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100884 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200886 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Greg Aker1edde302010-01-26 00:17:01 +0000889 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200891 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200895 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200896
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500897 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200899 // Do we know from where to get the column's name?
900 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300902 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200903 {
904 $key = 'column_name';
905 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300906 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200907 {
908 $key = 'COLUMN_NAME';
909 }
910 else
911 {
912 /* We have no other choice but to just get the first element's key.
913 * Due to array_shift() accepting it's argument by reference, if
914 * E_STRICT is on, this would trigger a warning. So we'll have to
915 * assign it first.
916 */
917 $key = array_keys($row);
918 $key = array_shift($key);
919 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200921
922 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 return $this->data_cache['field_names'][$table];
926 }
927
928 // --------------------------------------------------------------------
929
930 /**
931 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500932 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @param string
934 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200935 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500937 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200938 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200939 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 // --------------------------------------------------------------------
943
944 /**
945 * Returns an object with field data
946 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200948 * @return object
949 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500950 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100952 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200954 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200957 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200959 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
961 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300964 * Escape the SQL Identifiers
965 *
966 * This function escapes column and table names
967 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300968 * @param mixed
969 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300970 */
971 public function escape_identifiers($item)
972 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300973 if ($this->_escape_char === '' OR empty($item))
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300974 {
975 return $item;
976 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300977 elseif (is_array($item))
978 {
979 foreach ($item as $key => $value)
980 {
981 $item[$key] = $this->escape_identifiers($value);
982 }
983
984 return $item;
985 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300986 // Avoid breaking functions and literal values inside queries
987 elseif (ctype_digit($item) OR $item[0] === "'" OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +0300988 {
989 return $item;
990 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300991
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300992 static $preg_ec = array();
993
994 if (empty($preg_ec))
995 {
996 if (is_array($this->_escape_char))
997 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +0300998 $preg_ec = array(
999 preg_quote($this->_escape_char[0]), preg_quote($this->_escape_char[1]),
1000 $this->_escape_char[0], $this->_escape_char[1]
1001 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001002 }
1003 else
1004 {
1005 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001006 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001007 }
1008 }
1009
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001010 foreach ($this->_reserved_identifiers as $id)
1011 {
1012 if (strpos($item, '.'.$id) !== FALSE)
1013 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001014 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 +03001015 }
1016 }
1017
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001018 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 +03001019 }
1020
1021 // --------------------------------------------------------------------
1022
1023 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * Generate an insert string
1025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 * @param string the table upon which the query will be performed
1027 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001028 * @return string
1029 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001030 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001032 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001033
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001034 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001036 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 $values[] = $this->escape($val);
1038 }
Barry Mienydd671972010-10-04 16:33:58 +02001039
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001040 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001041 }
1042
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 // --------------------------------------------------------------------
1044
1045 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001046 * Insert statement
1047 *
1048 * Generates a platform-specific insert string from the supplied data
1049 *
1050 * @param string the table name
1051 * @param array the insert keys
1052 * @param array the insert values
1053 * @return string
1054 */
1055 protected function _insert($table, $keys, $values)
1056 {
1057 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1058 }
1059
1060 // --------------------------------------------------------------------
1061
1062 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 * Generate an update string
1064 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * @param string the table upon which the query will be performed
1066 * @param array an associative array data of key/values
1067 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001068 * @return string
1069 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001070 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001072 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001074 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001078 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001080 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 }
1082
1083 if ( ! is_array($where))
1084 {
1085 $dest = array($where);
1086 }
1087 else
1088 {
1089 $dest = array();
1090 foreach ($where as $key => $val)
1091 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001092 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001093 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001094
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 if ($val !== '')
1096 {
1097 if ( ! $this->_has_operator($key))
1098 {
1099 $key .= ' =';
1100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 $val = ' '.$this->escape($val);
1103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 $dest[] = $prefix.$key.$val;
1106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001108
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001109 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001110 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001111
1112 // --------------------------------------------------------------------
1113
1114 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001115 * Update statement
1116 *
1117 * Generates a platform-specific update string from the supplied data
1118 *
1119 * @param string the table name
1120 * @param array the update data
1121 * @param array the where clause
1122 * @param array the orderby clause
1123 * @param array the limit clause
1124 * @param array the like clause
1125 * @return string
1126 */
1127 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
1128 {
1129 foreach ($values as $key => $val)
1130 {
1131 $valstr[] = $key.' = '.$val;
1132 }
1133
1134 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1135
1136 if ( ! empty($like))
1137 {
1138 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1139 }
1140
1141 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
1142 .$where
1143 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1144 .($limit ? ' LIMIT '.$limit : '');
1145 }
1146
1147 // --------------------------------------------------------------------
1148
1149 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * Tests whether the string has an SQL operator
1151 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * @param string
1153 * @return bool
1154 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001155 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
Andrey Andreevd454f0e2012-06-10 14:51:04 +03001157 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL|BETWEEN)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 }
1159
1160 // --------------------------------------------------------------------
1161
1162 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001163 * Returns the SQL string operator
1164 *
1165 * @param string
1166 * @return string
1167 */
1168 protected function _get_operator($str)
1169 {
1170 return preg_match('/(=|!|<|>| IS NULL| IS NOT NULL| BETWEEN)/i', $str, $match)
1171 ? $match[1] : FALSE;
1172 }
1173
1174 // --------------------------------------------------------------------
1175
1176 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1178 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 * @param string the function name
1180 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001181 * @return mixed
1182 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001183 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001185 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001186
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 if (FALSE === strpos($driver, $function))
1188 {
1189 $function = $driver.$function;
1190 }
Barry Mienydd671972010-10-04 16:33:58 +02001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 if ( ! function_exists($function))
1193 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001194 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001196
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001197 return (func_num_args() > 1)
1198 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1199 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 }
1201
1202 // --------------------------------------------------------------------
1203
1204 /**
1205 * Set Cache Directory Path
1206 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 * @param string the path to the cache directory
1208 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001209 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001210 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 {
1212 $this->cachedir = $path;
1213 }
1214
1215 // --------------------------------------------------------------------
1216
1217 /**
1218 * Enable Query Caching
1219 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001220 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001221 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001222 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001224 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 }
1226
1227 // --------------------------------------------------------------------
1228
1229 /**
1230 * Disable Query Caching
1231 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001232 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001233 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001234 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001236 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 // --------------------------------------------------------------------
1240
1241 /**
1242 * Delete the cache files associated with a particular URI
1243 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001244 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001245 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001246 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001248 return ($this->_cache_init())
1249 ? $this->CACHE->delete($segment_one, $segment_two)
1250 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 }
1252
1253 // --------------------------------------------------------------------
1254
1255 /**
1256 * Delete All cache files
1257 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001258 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001259 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001260 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001262 return ($this->_cache_init())
1263 ? $this->CACHE->delete_all()
1264 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 }
1266
1267 // --------------------------------------------------------------------
1268
1269 /**
1270 * Initialize the Cache Class
1271 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001272 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001273 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001274 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001276 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001278 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001279 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001280 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001281 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001283 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1284 {
1285 return $this->cache_off();
1286 }
Derek Allarde37ab382009-02-03 16:13:57 +00001287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1289 return TRUE;
1290 }
1291
1292 // --------------------------------------------------------------------
1293
1294 /**
1295 * Close DB Connection
1296 *
Barry Mienydd671972010-10-04 16:33:58 +02001297 * @return void
1298 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001299 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001301 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001303 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001304 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 }
Barry Mienydd671972010-10-04 16:33:58 +02001307
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 // --------------------------------------------------------------------
1309
1310 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001311 * Close DB Connection
1312 *
1313 * This method would be overriden by most of the drivers.
1314 *
1315 * @return void
1316 */
1317 protected function _close()
1318 {
1319 $this->conn_id = FALSE;
1320 }
1321
1322 // --------------------------------------------------------------------
1323
1324 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 * Display an error message
1326 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 * @param string the error message
1328 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001329 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001330 * @return string sends the application/error_db.php template
1331 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001332 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 {
Derek Jonese7792202010-03-02 17:24:46 -06001334 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 $LANG->load('db');
1336
1337 $heading = $LANG->line('db_error_heading');
1338
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001339 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001341 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 }
1343 else
1344 {
1345 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Pascal Kriete60f8c392010-08-25 18:03:28 +02001348 // Find the most likely culprit of the error by going through
1349 // the backtrace until the source file is no longer in the
1350 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001351 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001352 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001353 {
1354 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1355 {
1356 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001357 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001358 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001359 break;
1360 }
1361 }
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Jonese7792202010-03-02 17:24:46 -06001363 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 echo $error->show_error($heading, $message, 'error_db');
1365 exit;
1366 }
1367
1368 // --------------------------------------------------------------------
1369
1370 /**
1371 * Protect Identifiers
1372 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001373 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001374 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001376 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001377 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 *
1379 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1380 *
1381 * Or a query with aliasing:
1382 *
1383 * SELECT m.member_id, m.member_name FROM members AS m
1384 *
1385 * Since the column name can include up to four segments (host, DB, table, column)
1386 * or also have an alias prefix, we need to do a bit of work to figure this out and
1387 * insert the table prefix (if it exists) in the proper position, and escape only
1388 * the correct identifiers.
1389 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 * @param string
1391 * @param bool
1392 * @param mixed
1393 * @param bool
1394 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001395 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001396 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 {
1398 if ( ! is_bool($protect_identifiers))
1399 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001400 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 }
Derek Allarde37ab382009-02-03 16:13:57 +00001402
1403 if (is_array($item))
1404 {
1405 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001406 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001407 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001408 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001409 }
1410
1411 return $escaped_array;
1412 }
1413
Derek Allard911d3e02008-12-15 14:08:35 +00001414 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001415 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001416 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001417 // way to deal with this, but I'm not thinking of it -- Rick
1418 if (strpos($item, '(') !== FALSE)
1419 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001420 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001421 }
1422
Andrey Andreevbf940582012-06-10 07:05:05 +03001423 // Convert tabs or multiple spaces into single spaces
1424 $item = preg_replace('/\s+/', ' ', $item);
1425
Andrey Andreevbf940582012-06-10 07:05:05 +03001426 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001427 // Note: strripos() is used in order to support spaces in table names
1428 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001429 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001430 $alias = ($protect_identifiers)
1431 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1432 : substr($item, $offset);
1433 $item = substr($item, 0, $offset);
1434 }
1435 elseif ($offset = strrpos($item, ' '))
1436 {
1437 $alias = ($protect_identifiers)
1438 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1439 : substr($item, $offset);
1440 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001441 }
1442 else
1443 {
1444 $alias = '';
1445 }
1446
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 // Break the string apart if it contains periods, then insert the table prefix
1448 // in the correct location, assuming the period doesn't indicate that we're dealing
1449 // with an alias. While we're at it, we will escape the components
1450 if (strpos($item, '.') !== FALSE)
1451 {
1452 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001453
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001455 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001457 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001458 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 if ($protect_identifiers === TRUE)
1460 {
1461 foreach ($parts as $key => $val)
1462 {
1463 if ( ! in_array($val, $this->_reserved_identifiers))
1464 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001465 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 }
1467 }
Barry Mienydd671972010-10-04 16:33:58 +02001468
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001470 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001471
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 return $item.$alias;
1473 }
Barry Mienydd671972010-10-04 16:33:58 +02001474
Andrey Andreev4c202602012-03-06 20:34:52 +02001475 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001476 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 {
1478 // We now add the table prefix based on some logic.
1479 // Do we have 4 segments (hostname.database.table.column)?
1480 // If so, we add the table prefix to the column name in the 3rd segment.
1481 if (isset($parts[3]))
1482 {
1483 $i = 2;
1484 }
1485 // Do we have 3 segments (database.table.column)?
1486 // If so, we add the table prefix to the column name in 2nd position
1487 elseif (isset($parts[2]))
1488 {
1489 $i = 1;
1490 }
1491 // Do we have 2 segments (table.column)?
1492 // If so, we add the table prefix to the column name in 1st segment
1493 else
1494 {
1495 $i = 0;
1496 }
Barry Mienydd671972010-10-04 16:33:58 +02001497
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 // This flag is set when the supplied $item does not contain a field name.
1499 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001500 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 {
1502 $i++;
1503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Jones55acc8b2009-07-11 03:38:13 +00001505 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001506 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001507 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001508 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001509 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001511 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 {
1513 $parts[$i] = $this->dbprefix.$parts[$i];
1514 }
Barry Mienydd671972010-10-04 16:33:58 +02001515
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 // Put the parts back together
1517 $item = implode('.', $parts);
1518 }
Barry Mienydd671972010-10-04 16:33:58 +02001519
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 if ($protect_identifiers === TRUE)
1521 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001522 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 }
Barry Mienydd671972010-10-04 16:33:58 +02001524
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 return $item.$alias;
1526 }
1527
Andrey Andreev4c202602012-03-06 20:34:52 +02001528 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001529 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001531 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001532 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001533 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001534 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001535 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001537 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 {
1539 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001540 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 }
1542
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001543 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001544 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001545 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 }
Barry Mienydd671972010-10-04 16:33:58 +02001547
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 return $item.$alias;
1549 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001550
Túbal Martín511f2252011-11-24 14:43:45 +01001551 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001552
Túbal Martín511f2252011-11-24 14:43:45 +01001553 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001554 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001555 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001556 *
Túbal Martín511f2252011-11-24 14:43:45 +01001557 * @return void
1558 */
1559 protected function _reset_select()
1560 {
Túbal Martín511f2252011-11-24 14:43:45 +01001561 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001562
1563}
1564
Derek Allard2067d1a2008-11-13 22:59:24 +00001565/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001566/* Location: ./system/database/DB_driver.php */