blob: acba9c1878a4d629ce29d1ec08beca4d24e92b3d [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 Andreev2ea33c32012-10-04 12:37:51 +030082 // clause and character used for LIKE escape sequences
83 protected $_like_escape_str = " ESCAPE '%s' ";
84 protected $_like_escape_chr = '!';
85
Andrey Andreev75546112012-07-05 11:01:29 +030086 /**
Andrey Andreev77a5d942012-07-05 15:42:14 +030087 * The syntax to count rows is slightly different across different
88 * database engines, so this string appears in each driver and is
89 * used for the count_all() and count_all_results() functions.
90 */
91 protected $_count_string = 'SELECT COUNT(*) AS ';
92
93 /**
Andrey Andreev75546112012-07-05 11:01:29 +030094 * Constructor
95 *
96 * @param array
97 * @return void
98 */
Timothy Warrene45518d2012-03-06 07:38:00 -050099 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 if (is_array($params))
102 {
103 foreach ($params as $key => $val)
104 {
105 $this->$key = $val;
106 }
107 }
108
109 log_message('debug', 'Database Driver Class Initialized');
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Gints Murans89f77ee2012-05-23 00:23:50 +0300112 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 /**
115 * Initialize Database Settings
116 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200117 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200118 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200119 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200121 /* If an established connection is available, then there's
122 * no need to connect and select the database.
123 *
124 * Depending on the database driver, conn_id can be either
125 * boolean TRUE, a resource or an object.
126 */
127 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
129 return TRUE;
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100135 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000136
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200137 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 if ( ! $this->conn_id)
139 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100140 // Check if there is a failover set
141 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100143 // Go over all the failovers
144 foreach ($this->failover as $failover)
145 {
146 // Replace the current settings with those of the failover
147 foreach ($failover as $key => $val)
148 {
149 $this->$key = $val;
150 }
151
152 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100153 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100154
155 // If a connection is made break the foreach loop
156 if ($this->conn_id)
157 {
158 break;
159 }
160 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100162
163 // We still don't have a connection?
164 if ( ! $this->conn_id)
165 {
166 log_message('error', 'Unable to connect to the database');
167
168 if ($this->db_debug)
169 {
170 $this->display_error('db_unable_to_connect');
171 }
172 return FALSE;
173 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 // ----------------------------------------------------------------
177
178 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200179 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200181 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200182
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200183 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200185 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200187 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200190 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200191 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // --------------------------------------------------------------------
195
196 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200197 * Reconnect
198 *
199 * Keep / reestablish the db connection if no queries have been
200 * sent for a length of time exceeding the server's idle timeout.
201 *
202 * This is just a dummy method to allow drivers without such
203 * functionality to not declare it, while others will override it.
204 *
205 * @return void
206 */
207 public function reconnect()
208 {
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300214 * Select database
215 *
216 * This is just a dummy method to allow drivers without such
217 * functionality to not declare it, while others will override it.
218 *
219 * @return bool
220 */
221 public function db_select()
222 {
223 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Set client character set
230 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200232 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200234 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200236 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200238 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 if ($this->db_debug)
241 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200242 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 return FALSE;
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 return TRUE;
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 // --------------------------------------------------------------------
252
253 /**
254 * The name of the platform in use (mysql, mssql, etc...)
255 *
Barry Mienydd671972010-10-04 16:33:58 +0200256 * @return string
257 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500258 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 return $this->dbdriver;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200266 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200268 * Returns a string containing the version of the database being used.
269 * Most drivers will override this method.
270 *
Barry Mienydd671972010-10-04 16:33:58 +0200271 * @return string
272 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200273 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200275 if (isset($this->data_cache['version']))
276 {
277 return $this->data_cache['version'];
278 }
279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 if (FALSE === ($sql = $this->_version()))
281 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200282 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
Derek Allard3683f772009-12-16 17:32:33 +0000284
Andrey Andreev08856b82012-03-03 03:19:28 +0200285 $query = $this->query($sql);
286 $query = $query->row();
287 return $this->data_cache['version'] = $query->ver;
288 }
Derek Allard3683f772009-12-16 17:32:33 +0000289
Andrey Andreev08856b82012-03-03 03:19:28 +0200290 // --------------------------------------------------------------------
291
292 /**
293 * Version number query string
294 *
295 * @return string
296 */
297 protected function _version()
298 {
299 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
303
304 /**
305 * Execute the query
306 *
307 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200308 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * upon successful execution of a "write" type query. Returns boolean
310 * FALSE upon failure, and if the $db_debug variable is set to TRUE
311 * will raise an error.
312 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * @param string An SQL query string
314 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200315 * @return mixed
316 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300317 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100319 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200321 log_message('error', 'Invalid query: '.$sql);
322
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200323 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300325 elseif ( ! is_bool($return_object))
326 {
327 $return_object = ! $this->is_write_type($sql);
328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
330 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100331 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600332 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200333 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Derek Jonese7792202010-03-02 17:24:46 -0600335
Ryan Dialef7474c2012-03-01 16:11:36 -0500336 // Compile binds if needed
337 if ($binds !== FALSE)
338 {
339 $sql = $this->compile_binds($sql, $binds);
340 }
341
Andrey Andreev4c202602012-03-06 20:34:52 +0200342 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // we will load the caching class and return the previously
344 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300345 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200347 $this->load_rdriver();
348 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200350 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Andrey Andreevb66664b2012-06-27 14:22:10 +0300354 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100355 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 $this->queries[] = $sql;
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200361 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Run the Query
364 if (FALSE === ($this->result_id = $this->simple_query($sql)))
365 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100366 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 $this->query_times[] = 0;
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 // This will trigger a rollback if transactions are being used
372 $this->_trans_status = FALSE;
373
Andrey Andreev4be5de12012-03-02 15:45:41 +0200374 // Grab the error now, as we might run some additional queries before displaying the error
375 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200376
377 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300378 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 if ($this->db_debug)
381 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200383 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200384 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // transactions to remain in limbo.
386 $this->trans_complete();
387
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200388 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200389 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 return FALSE;
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200396 $time_end = microtime(TRUE);
397 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100399 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
dixyab3cab52012-04-21 00:58:00 +0200401 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // Increment the query counter
405 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200406
Andrey Andreevb66664b2012-06-27 14:22:10 +0300407 // Will we have a result object instantiated? If not - we'll simply return TRUE
408 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300410 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100411 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 $this->CACHE->delete();
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 return TRUE;
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 if ($return_object !== TRUE)
421 {
422 return TRUE;
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
425 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200426 $driver = $this->load_rdriver();
427 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200428
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200429 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100431 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 // We'll create a new instance of the result object
434 // only without the platform specific driver since
435 // we can't use it with cached data (the query result
436 // resource ID won't be any good once we've cached the
437 // result object, so we'll have to compile the data
438 // and save it)
439 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 $CR->result_object = $RES->result_object();
441 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200442 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // Reset these since cached objects can not utilize resource IDs.
445 $CR->conn_id = NULL;
446 $CR->result_id = NULL;
447
448 $this->CACHE->write($sql, $CR);
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 return $RES;
452 }
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Load the result drivers
458 *
Barry Mienydd671972010-10-04 16:33:58 +0200459 * @return string the name of the result class
460 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500461 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 $driver = 'CI_DB_'.$this->dbdriver.'_result';
464
465 if ( ! class_exists($driver))
466 {
Greg Aker3a746652011-04-19 10:59:47 -0500467 include_once(BASEPATH.'database/DB_result.php');
468 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 return $driver;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
475
476 /**
477 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200478 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * we only use it when running transaction commands since they do
480 * not require all the features of the main query() function.
481 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200483 * @return mixed
484 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500485 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 if ( ! $this->conn_id)
488 {
489 $this->initialize();
490 }
491
492 return $this->_execute($sql);
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 // --------------------------------------------------------------------
496
497 /**
498 * Disable Transactions
499 * This permits transactions to be disabled at run-time.
500 *
Barry Mienydd671972010-10-04 16:33:58 +0200501 * @return void
502 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500503 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 $this->trans_enabled = FALSE;
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Enable/disable Transaction Strict Mode
512 * When strict mode is enabled, if you are running multiple groups of
513 * transactions, if one group fails all groups will be rolled back.
514 * If strict mode is disabled, each group is treated autonomously, meaning
515 * a failure of one group will not affect any others
516 *
Barry Mienydd671972010-10-04 16:33:58 +0200517 * @return void
518 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500519 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 // --------------------------------------------------------------------
525
526 /**
527 * Start Transaction
528 *
Barry Mienydd671972010-10-04 16:33:58 +0200529 * @return void
530 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500531 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200532 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 if ( ! $this->trans_enabled)
534 {
535 return FALSE;
536 }
537
538 // When transactions are nested we only begin/commit/rollback the outermost ones
539 if ($this->_trans_depth > 0)
540 {
541 $this->_trans_depth += 1;
542 return;
543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500546 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548
549 // --------------------------------------------------------------------
550
551 /**
552 * Complete Transaction
553 *
Barry Mienydd671972010-10-04 16:33:58 +0200554 * @return bool
555 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500556 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 if ( ! $this->trans_enabled)
559 {
560 return FALSE;
561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // When transactions are nested we only begin/commit/rollback the outermost ones
564 if ($this->_trans_depth > 1)
565 {
566 $this->_trans_depth -= 1;
567 return TRUE;
568 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500569 else
570 {
571 $this->_trans_depth = 0;
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // The query() function will set this flag to FALSE in the event that a query failed
575 if ($this->_trans_status === FALSE)
576 {
577 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // If we are NOT running in strict mode, we will reset
580 // the _trans_status flag so that subsequent groups of transactions
581 // will be permitted.
582 if ($this->trans_strict === FALSE)
583 {
584 $this->_trans_status = TRUE;
585 }
586
587 log_message('debug', 'DB Transaction Failure');
588 return FALSE;
589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 $this->trans_commit();
592 return TRUE;
593 }
594
595 // --------------------------------------------------------------------
596
597 /**
598 * Lets you retrieve the transaction flag to determine if it has failed
599 *
Barry Mienydd671972010-10-04 16:33:58 +0200600 * @return bool
601 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500602 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 return $this->_trans_status;
605 }
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Compile Bindings
611 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 * @param string the sql statement
613 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200614 * @return string
615 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500616 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300618 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 return $sql;
621 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300622 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300624 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300625 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300627 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200628 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300629 // Make sure we're using numeric keys
630 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300631 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
633
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300634 // We'll need the marker length later
635 $ml = strlen($this->bind_marker);
636
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300637 // Make sure not to replace a chunk inside a string that happens to match the bind marker
638 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300640 $c = preg_match_all('/'.preg_quote($this->bind_marker).'/i',
641 str_replace($matches[0],
642 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
643 $sql, $c),
644 $matches, PREG_OFFSET_CAPTURE);
645
646 // Bind values' count must match the count of markers in the query
647 if ($bind_count !== $c)
648 {
649 return $sql;
650 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300651 }
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300652 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 +0300653 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300654 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
656
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300657 do
658 {
659 $c--;
660 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
661 }
662 while ($c !== 0);
663
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300664 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Determines if a query is a "write" type.
671 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200673 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200674 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200675 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300677 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 +0000678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // --------------------------------------------------------------------
681
682 /**
683 * Calculate the aggregate query elapsed time
684 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200685 * @param int The number of decimal places
686 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200687 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500688 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
690 return number_format($this->benchmark, $decimals);
691 }
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 // --------------------------------------------------------------------
694
695 /**
696 * Returns the total number of queries
697 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200698 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200699 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500700 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
702 return $this->query_count;
703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // --------------------------------------------------------------------
706
707 /**
708 * Returns the last query that was executed
709 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200710 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200711 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500712 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
714 return end($this->queries);
715 }
716
717 // --------------------------------------------------------------------
718
719 /**
720 * "Smart" Escape String
721 *
722 * Escapes data based on type
723 * Sets boolean and null types
724 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200726 * @return mixed
727 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500728 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200729 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500730 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200732 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000733 }
734 elseif (is_bool($str))
735 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200736 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000737 }
738 elseif (is_null($str))
739 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200740 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000741 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000742
743 return $str;
744 }
745
746 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600747
Derek Jonese4ed5832009-02-20 21:44:59 +0000748 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000749 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000750 *
751 * Calls the individual driver for platform
752 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200753 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000754 * @param string
755 * @return mixed
756 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500757 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200758 {
759 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000760 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000761
Derek Jonese4ed5832009-02-20 21:44:59 +0000762 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 /**
765 * Primary
766 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200767 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 * position is the primary key
769 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200771 * @return string
772 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500773 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200774 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200776 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
778
779 // --------------------------------------------------------------------
780
781 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300782 * "Count All" query
783 *
784 * Generates a platform-specific query string that counts all records in
785 * the specified database
786 *
787 * @param string
788 * @return int
789 */
790 public function count_all($table = '')
791 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100792 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300793 {
794 return 0;
795 }
796
797 $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 +0100798 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300799 {
800 return 0;
801 }
802
803 $query = $query->row();
804 $this->_reset_select();
805 return (int) $query->numrows;
806 }
807
808 // --------------------------------------------------------------------
809
810 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 * Returns an array of table names
812 *
Barry Mienydd671972010-10-04 16:33:58 +0200813 * @return array
814 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500815 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
817 // Is there a cached result?
818 if (isset($this->data_cache['table_names']))
819 {
820 return $this->data_cache['table_names'];
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
824 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200825 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 }
827
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200828 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200830
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200831 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200833 // Do we know from which column to get the table name?
834 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300836 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200837 {
838 $key = 'table_name';
839 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300840 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200841 {
842 $key = 'TABLE_NAME';
843 }
844 else
845 {
846 /* We have no other choice but to just get the first element's key.
847 * Due to array_shift() accepting it's argument by reference, if
848 * E_STRICT is on, this would trigger a warning. So we'll have to
849 * assign it first.
850 */
851 $key = array_keys($row);
852 $key = array_shift($key);
853 }
Taufan Aditya18209332012-02-09 16:07:27 +0700854 }
855
Andrey Andreev12567e82012-01-25 22:50:33 +0200856 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 }
858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 return $this->data_cache['table_names'];
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 // --------------------------------------------------------------------
863
864 /**
865 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500866 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200867 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500869 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200870 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200871 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 // --------------------------------------------------------------------
875
876 /**
Andrey Andreev75546112012-07-05 11:01:29 +0300877 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200880 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500882 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 {
884 // Is there a cached result?
885 if (isset($this->data_cache['field_names'][$table]))
886 {
887 return $this->data_cache['field_names'][$table];
888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100890 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200892 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Greg Aker1edde302010-01-26 00:17:01 +0000895 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200897 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200901 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200902
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500903 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200905 // Do we know from where to get the column's name?
906 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300908 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200909 {
910 $key = 'column_name';
911 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300912 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200913 {
914 $key = 'COLUMN_NAME';
915 }
916 else
917 {
918 /* We have no other choice but to just get the first element's key.
919 * Due to array_shift() accepting it's argument by reference, if
920 * E_STRICT is on, this would trigger a warning. So we'll have to
921 * assign it first.
922 */
923 $key = array_keys($row);
924 $key = array_shift($key);
925 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200927
928 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 return $this->data_cache['field_names'][$table];
932 }
933
934 // --------------------------------------------------------------------
935
936 /**
937 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500938 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @param string
940 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200941 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500943 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200944 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200945 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // --------------------------------------------------------------------
949
950 /**
951 * Returns an object with field data
952 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200954 * @return object
955 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500956 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100958 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200960 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200963 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200965 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000966
967 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300970 * Escape the SQL Identifiers
971 *
972 * This function escapes column and table names
973 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300974 * @param mixed
975 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300976 */
977 public function escape_identifiers($item)
978 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300979 if ($this->_escape_char === '' OR empty($item))
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300980 {
981 return $item;
982 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300983 elseif (is_array($item))
984 {
985 foreach ($item as $key => $value)
986 {
987 $item[$key] = $this->escape_identifiers($value);
988 }
989
990 return $item;
991 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300992 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +0300993 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +0300994 {
995 return $item;
996 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300997
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300998 static $preg_ec = array();
999
1000 if (empty($preg_ec))
1001 {
1002 if (is_array($this->_escape_char))
1003 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001004 $preg_ec = array(
1005 preg_quote($this->_escape_char[0]), preg_quote($this->_escape_char[1]),
1006 $this->_escape_char[0], $this->_escape_char[1]
1007 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001008 }
1009 else
1010 {
1011 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001012 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001013 }
1014 }
1015
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001016 foreach ($this->_reserved_identifiers as $id)
1017 {
1018 if (strpos($item, '.'.$id) !== FALSE)
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].'.', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001021 }
1022 }
1023
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001024 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 +03001025 }
1026
1027 // --------------------------------------------------------------------
1028
1029 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 * Generate an insert string
1031 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 * @param string the table upon which the query will be performed
1033 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001034 * @return string
1035 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001036 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001038 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001039
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001040 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001042 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 $values[] = $this->escape($val);
1044 }
Barry Mienydd671972010-10-04 16:33:58 +02001045
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001046 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001047 }
1048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 // --------------------------------------------------------------------
1050
1051 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001052 * Insert statement
1053 *
1054 * Generates a platform-specific insert string from the supplied data
1055 *
1056 * @param string the table name
1057 * @param array the insert keys
1058 * @param array the insert values
1059 * @return string
1060 */
1061 protected function _insert($table, $keys, $values)
1062 {
1063 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1064 }
1065
1066 // --------------------------------------------------------------------
1067
1068 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 * Generate an update string
1070 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @param string the table upon which the query will be performed
1072 * @param array an associative array data of key/values
1073 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001074 * @return string
1075 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001076 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001078 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001080 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001084 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001086 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
1088
1089 if ( ! is_array($where))
1090 {
1091 $dest = array($where);
1092 }
1093 else
1094 {
1095 $dest = array();
1096 foreach ($where as $key => $val)
1097 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001098 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001099 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001100
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 if ($val !== '')
1102 {
1103 if ( ! $this->_has_operator($key))
1104 {
1105 $key .= ' =';
1106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 $val = ' '.$this->escape($val);
1109 }
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 $dest[] = $prefix.$key.$val;
1112 }
Barry Mienydd671972010-10-04 16:33:58 +02001113 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001114
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001115 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001116 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001117
1118 // --------------------------------------------------------------------
1119
1120 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001121 * Update statement
1122 *
1123 * Generates a platform-specific update string from the supplied data
1124 *
1125 * @param string the table name
1126 * @param array the update data
1127 * @param array the where clause
1128 * @param array the orderby clause
1129 * @param array the limit clause
1130 * @param array the like clause
1131 * @return string
1132 */
1133 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
1134 {
1135 foreach ($values as $key => $val)
1136 {
1137 $valstr[] = $key.' = '.$val;
1138 }
1139
1140 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1141
1142 if ( ! empty($like))
1143 {
1144 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1145 }
1146
1147 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
1148 .$where
1149 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1150 .($limit ? ' LIMIT '.$limit : '');
1151 }
1152
1153 // --------------------------------------------------------------------
1154
1155 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 * Tests whether the string has an SQL operator
1157 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 * @param string
1159 * @return bool
1160 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001161 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Andrey Andreevd454f0e2012-06-10 14:51:04 +03001163 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL|BETWEEN)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
1165
1166 // --------------------------------------------------------------------
1167
1168 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001169 * Returns the SQL string operator
1170 *
1171 * @param string
1172 * @return string
1173 */
1174 protected function _get_operator($str)
1175 {
1176 return preg_match('/(=|!|<|>| IS NULL| IS NOT NULL| BETWEEN)/i', $str, $match)
1177 ? $match[1] : FALSE;
1178 }
1179
1180 // --------------------------------------------------------------------
1181
1182 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1184 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 * @param string the function name
1186 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001187 * @return mixed
1188 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001189 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001191 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 if (FALSE === strpos($driver, $function))
1194 {
1195 $function = $driver.$function;
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 if ( ! function_exists($function))
1199 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001200 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001202
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001203 return (func_num_args() > 1)
1204 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1205 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 }
1207
1208 // --------------------------------------------------------------------
1209
1210 /**
1211 * Set Cache Directory Path
1212 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 * @param string the path to the cache directory
1214 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001215 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001216 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
1218 $this->cachedir = $path;
1219 }
1220
1221 // --------------------------------------------------------------------
1222
1223 /**
1224 * Enable Query Caching
1225 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001226 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001227 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001228 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001230 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
1232
1233 // --------------------------------------------------------------------
1234
1235 /**
1236 * Disable Query Caching
1237 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001238 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001239 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001240 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001242 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // --------------------------------------------------------------------
1246
1247 /**
1248 * Delete the cache files associated with a particular URI
1249 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001250 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001251 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001252 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001254 return ($this->_cache_init())
1255 ? $this->CACHE->delete($segment_one, $segment_two)
1256 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 }
1258
1259 // --------------------------------------------------------------------
1260
1261 /**
1262 * Delete All cache files
1263 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001264 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001265 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001266 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001268 return ($this->_cache_init())
1269 ? $this->CACHE->delete_all()
1270 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 }
1272
1273 // --------------------------------------------------------------------
1274
1275 /**
1276 * Initialize the Cache Class
1277 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001278 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001279 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001280 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001282 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001284 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001285 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001286 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001287 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001289 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1290 {
1291 return $this->cache_off();
1292 }
Derek Allarde37ab382009-02-03 16:13:57 +00001293
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1295 return TRUE;
1296 }
1297
1298 // --------------------------------------------------------------------
1299
1300 /**
1301 * Close DB Connection
1302 *
Barry Mienydd671972010-10-04 16:33:58 +02001303 * @return void
1304 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001305 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001307 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001309 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001310 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 }
Barry Mienydd671972010-10-04 16:33:58 +02001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 // --------------------------------------------------------------------
1315
1316 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001317 * Close DB Connection
1318 *
1319 * This method would be overriden by most of the drivers.
1320 *
1321 * @return void
1322 */
1323 protected function _close()
1324 {
1325 $this->conn_id = FALSE;
1326 }
1327
1328 // --------------------------------------------------------------------
1329
1330 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 * Display an error message
1332 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 * @param string the error message
1334 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001335 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001336 * @return string sends the application/error_db.php template
1337 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001338 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 {
Derek Jonese7792202010-03-02 17:24:46 -06001340 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 $LANG->load('db');
1342
1343 $heading = $LANG->line('db_error_heading');
1344
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001345 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001347 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 }
1349 else
1350 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001351 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 }
Barry Mienydd671972010-10-04 16:33:58 +02001353
Pascal Kriete60f8c392010-08-25 18:03:28 +02001354 // Find the most likely culprit of the error by going through
1355 // the backtrace until the source file is no longer in the
1356 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001357 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001358 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001359 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001360 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1361 if (DIRECTORY_SEPARATOR !== '/')
1362 {
1363 $call['file'] = str_replace('\\', '/', $call['file']);
1364 }
1365
1366 if (isset($call['file'], $call['class']) && strpos($call['file'], $basepath.'database') === FALSE && strpos($call['class'], 'Loader') !== FALSE)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001367 {
1368 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001369 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001370 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001371 break;
1372 }
1373 }
Barry Mienydd671972010-10-04 16:33:58 +02001374
Derek Jonese7792202010-03-02 17:24:46 -06001375 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 echo $error->show_error($heading, $message, 'error_db');
1377 exit;
1378 }
1379
1380 // --------------------------------------------------------------------
1381
1382 /**
1383 * Protect Identifiers
1384 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001385 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001386 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001388 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001389 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 *
1391 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1392 *
1393 * Or a query with aliasing:
1394 *
1395 * SELECT m.member_id, m.member_name FROM members AS m
1396 *
1397 * Since the column name can include up to four segments (host, DB, table, column)
1398 * or also have an alias prefix, we need to do a bit of work to figure this out and
1399 * insert the table prefix (if it exists) in the proper position, and escape only
1400 * the correct identifiers.
1401 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 * @param string
1403 * @param bool
1404 * @param mixed
1405 * @param bool
1406 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001407 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001408 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 {
1410 if ( ! is_bool($protect_identifiers))
1411 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001412 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
Derek Allarde37ab382009-02-03 16:13:57 +00001414
1415 if (is_array($item))
1416 {
1417 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001418 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001419 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001420 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001421 }
1422
1423 return $escaped_array;
1424 }
1425
Derek Allard911d3e02008-12-15 14:08:35 +00001426 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001427 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001428 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001429 // way to deal with this, but I'm not thinking of it -- Rick
1430 if (strpos($item, '(') !== FALSE)
1431 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001432 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001433 }
1434
Andrey Andreevbf940582012-06-10 07:05:05 +03001435 // Convert tabs or multiple spaces into single spaces
1436 $item = preg_replace('/\s+/', ' ', $item);
1437
Andrey Andreevbf940582012-06-10 07:05:05 +03001438 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001439 // Note: strripos() is used in order to support spaces in table names
1440 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001441 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001442 $alias = ($protect_identifiers)
1443 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1444 : substr($item, $offset);
1445 $item = substr($item, 0, $offset);
1446 }
1447 elseif ($offset = strrpos($item, ' '))
1448 {
1449 $alias = ($protect_identifiers)
1450 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1451 : substr($item, $offset);
1452 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001453 }
1454 else
1455 {
1456 $alias = '';
1457 }
1458
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 // Break the string apart if it contains periods, then insert the table prefix
1460 // in the correct location, assuming the period doesn't indicate that we're dealing
1461 // with an alias. While we're at it, we will escape the components
1462 if (strpos($item, '.') !== FALSE)
1463 {
1464 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001465
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001467 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001469 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001470 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 if ($protect_identifiers === TRUE)
1472 {
1473 foreach ($parts as $key => $val)
1474 {
1475 if ( ! in_array($val, $this->_reserved_identifiers))
1476 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001477 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 }
1479 }
Barry Mienydd671972010-10-04 16:33:58 +02001480
Derek Allard2067d1a2008-11-13 22:59:24 +00001481 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001482 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001483
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 return $item.$alias;
1485 }
Barry Mienydd671972010-10-04 16:33:58 +02001486
Andrey Andreev4c202602012-03-06 20:34:52 +02001487 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001488 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 {
1490 // We now add the table prefix based on some logic.
1491 // Do we have 4 segments (hostname.database.table.column)?
1492 // If so, we add the table prefix to the column name in the 3rd segment.
1493 if (isset($parts[3]))
1494 {
1495 $i = 2;
1496 }
1497 // Do we have 3 segments (database.table.column)?
1498 // If so, we add the table prefix to the column name in 2nd position
1499 elseif (isset($parts[2]))
1500 {
1501 $i = 1;
1502 }
1503 // Do we have 2 segments (table.column)?
1504 // If so, we add the table prefix to the column name in 1st segment
1505 else
1506 {
1507 $i = 0;
1508 }
Barry Mienydd671972010-10-04 16:33:58 +02001509
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 // This flag is set when the supplied $item does not contain a field name.
1511 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001512 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
1514 $i++;
1515 }
Barry Mienydd671972010-10-04 16:33:58 +02001516
Derek Jones55acc8b2009-07-11 03:38:13 +00001517 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001518 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001519 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001520 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001521 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001523 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 {
1525 $parts[$i] = $this->dbprefix.$parts[$i];
1526 }
Barry Mienydd671972010-10-04 16:33:58 +02001527
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 // Put the parts back together
1529 $item = implode('.', $parts);
1530 }
Barry Mienydd671972010-10-04 16:33:58 +02001531
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 if ($protect_identifiers === TRUE)
1533 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001534 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 }
Barry Mienydd671972010-10-04 16:33:58 +02001536
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 return $item.$alias;
1538 }
1539
Andrey Andreev4c202602012-03-06 20:34:52 +02001540 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001541 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001543 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001544 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001545 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001546 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001547 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001549 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 {
1551 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001552 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 }
1554
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001555 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001557 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 }
Barry Mienydd671972010-10-04 16:33:58 +02001559
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 return $item.$alias;
1561 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001562
Túbal Martín511f2252011-11-24 14:43:45 +01001563 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001564
Túbal Martín511f2252011-11-24 14:43:45 +01001565 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001566 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001567 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001568 *
Túbal Martín511f2252011-11-24 14:43:45 +01001569 * @return void
1570 */
1571 protected function _reset_select()
1572 {
Túbal Martín511f2252011-11-24 14:43:45 +01001573 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001574
1575}
1576
Derek Allard2067d1a2008-11-13 22:59:24 +00001577/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001578/* Location: ./system/database/DB_driver.php */