blob: 10306d72171c9803f6fb722c2598380f7e4d10bb [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
54 public $swap_pre = '';
55 public $port = '';
56 public $pconnect = FALSE;
57 public $conn_id = FALSE;
58 public $result_id = FALSE;
59 public $db_debug = FALSE;
60 public $benchmark = 0;
61 public $query_count = 0;
62 public $bind_marker = '?';
63 public $save_queries = TRUE;
64 public $queries = array();
65 public $query_times = array();
66 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000067
Andrey Andreevd1add432012-03-06 20:26:01 +020068 public $trans_enabled = TRUE;
69 public $trans_strict = TRUE;
70 protected $_trans_depth = 0;
71 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000072
Andrey Andreevd1add432012-03-06 20:26:01 +020073 public $cache_on = FALSE;
74 public $cachedir = '';
75 public $cache_autodel = FALSE;
76 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020077
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000078 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020079 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
80
Andrey Andreev75546112012-07-05 11:01:29 +030081 /**
Andrey Andreev77a5d942012-07-05 15:42:14 +030082 * The syntax to count rows is slightly different across different
83 * database engines, so this string appears in each driver and is
84 * used for the count_all() and count_all_results() functions.
85 */
86 protected $_count_string = 'SELECT COUNT(*) AS ';
87
88 /**
Andrey Andreev75546112012-07-05 11:01:29 +030089 * Constructor
90 *
91 * @param array
92 * @return void
93 */
Timothy Warrene45518d2012-03-06 07:38:00 -050094 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
96 if (is_array($params))
97 {
98 foreach ($params as $key => $val)
99 {
100 $this->$key = $val;
101 }
102 }
103
104 log_message('debug', 'Database Driver Class Initialized');
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Gints Murans89f77ee2012-05-23 00:23:50 +0300107 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 /**
110 * Initialize Database Settings
111 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200112 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200113 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200114 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200116 /* If an established connection is available, then there's
117 * no need to connect and select the database.
118 *
119 * Depending on the database driver, conn_id can be either
120 * boolean TRUE, a resource or an object.
121 */
122 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
124 return TRUE;
125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100130 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000131
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200132 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 if ( ! $this->conn_id)
134 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100135 // Check if there is a failover set
136 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100138 // Go over all the failovers
139 foreach ($this->failover as $failover)
140 {
141 // Replace the current settings with those of the failover
142 foreach ($failover as $key => $val)
143 {
144 $this->$key = $val;
145 }
146
147 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100148 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100149
150 // If a connection is made break the foreach loop
151 if ($this->conn_id)
152 {
153 break;
154 }
155 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100157
158 // We still don't have a connection?
159 if ( ! $this->conn_id)
160 {
161 log_message('error', 'Unable to connect to the database');
162
163 if ($this->db_debug)
164 {
165 $this->display_error('db_unable_to_connect');
166 }
167 return FALSE;
168 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 // ----------------------------------------------------------------
172
173 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200174 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200176 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200177
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200178 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200180 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200182 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200185 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200186 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // --------------------------------------------------------------------
190
191 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200192 * Reconnect
193 *
194 * Keep / reestablish the db connection if no queries have been
195 * sent for a length of time exceeding the server's idle timeout.
196 *
197 * This is just a dummy method to allow drivers without such
198 * functionality to not declare it, while others will override it.
199 *
200 * @return void
201 */
202 public function reconnect()
203 {
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300209 * Select database
210 *
211 * This is just a dummy method to allow drivers without such
212 * functionality to not declare it, while others will override it.
213 *
214 * @return bool
215 */
216 public function db_select()
217 {
218 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220
221 // --------------------------------------------------------------------
222
223 /**
224 * Set client character set
225 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200227 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200229 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200231 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200233 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 if ($this->db_debug)
236 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200237 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 return FALSE;
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return TRUE;
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // --------------------------------------------------------------------
247
248 /**
249 * The name of the platform in use (mysql, mssql, etc...)
250 *
Barry Mienydd671972010-10-04 16:33:58 +0200251 * @return string
252 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500253 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 return $this->dbdriver;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200261 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200263 * Returns a string containing the version of the database being used.
264 * Most drivers will override this method.
265 *
Barry Mienydd671972010-10-04 16:33:58 +0200266 * @return string
267 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200268 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200270 if (isset($this->data_cache['version']))
271 {
272 return $this->data_cache['version'];
273 }
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 if (FALSE === ($sql = $this->_version()))
276 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200277 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
Derek Allard3683f772009-12-16 17:32:33 +0000279
Andrey Andreev08856b82012-03-03 03:19:28 +0200280 $query = $this->query($sql);
281 $query = $query->row();
282 return $this->data_cache['version'] = $query->ver;
283 }
Derek Allard3683f772009-12-16 17:32:33 +0000284
Andrey Andreev08856b82012-03-03 03:19:28 +0200285 // --------------------------------------------------------------------
286
287 /**
288 * Version number query string
289 *
290 * @return string
291 */
292 protected function _version()
293 {
294 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // --------------------------------------------------------------------
298
299 /**
300 * Execute the query
301 *
302 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200303 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * upon successful execution of a "write" type query. Returns boolean
305 * FALSE upon failure, and if the $db_debug variable is set to TRUE
306 * will raise an error.
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @param string An SQL query string
309 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200310 * @return mixed
311 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300312 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100314 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200316 log_message('error', 'Invalid query: '.$sql);
317
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200318 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300320 elseif ( ! is_bool($return_object))
321 {
322 $return_object = ! $this->is_write_type($sql);
323 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000324
325 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100326 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600327 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200328 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Derek Jonese7792202010-03-02 17:24:46 -0600330
Ryan Dialef7474c2012-03-01 16:11:36 -0500331 // Compile binds if needed
332 if ($binds !== FALSE)
333 {
334 $sql = $this->compile_binds($sql, $binds);
335 }
336
Andrey Andreev4c202602012-03-06 20:34:52 +0200337 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // we will load the caching class and return the previously
339 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300340 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200342 $this->load_rdriver();
343 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200345 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Andrey Andreevb66664b2012-06-27 14:22:10 +0300349 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100350 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 $this->queries[] = $sql;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200356 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // Run the Query
359 if (FALSE === ($this->result_id = $this->simple_query($sql)))
360 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100361 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
363 $this->query_times[] = 0;
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // This will trigger a rollback if transactions are being used
367 $this->_trans_status = FALSE;
368
Andrey Andreev4be5de12012-03-02 15:45:41 +0200369 // Grab the error now, as we might run some additional queries before displaying the error
370 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200371
372 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300373 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 if ($this->db_debug)
376 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200378 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200379 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // transactions to remain in limbo.
381 $this->trans_complete();
382
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200383 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200384 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 return FALSE;
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200391 $time_end = microtime(TRUE);
392 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100394 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
dixyab3cab52012-04-21 00:58:00 +0200396 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // Increment the query counter
400 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200401
Andrey Andreevb66664b2012-06-27 14:22:10 +0300402 // Will we have a result object instantiated? If not - we'll simply return TRUE
403 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300405 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100406 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
408 $this->CACHE->delete();
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 return TRUE;
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 if ($return_object !== TRUE)
416 {
417 return TRUE;
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
420 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200421 $driver = $this->load_rdriver();
422 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200423
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200424 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100426 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 // We'll create a new instance of the result object
429 // only without the platform specific driver since
430 // we can't use it with cached data (the query result
431 // resource ID won't be any good once we've cached the
432 // result object, so we'll have to compile the data
433 // and save it)
434 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 $CR->result_object = $RES->result_object();
436 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200437 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // Reset these since cached objects can not utilize resource IDs.
440 $CR->conn_id = NULL;
441 $CR->result_id = NULL;
442
443 $this->CACHE->write($sql, $CR);
444 }
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 return $RES;
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
452 * Load the result drivers
453 *
Barry Mienydd671972010-10-04 16:33:58 +0200454 * @return string the name of the result class
455 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500456 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 $driver = 'CI_DB_'.$this->dbdriver.'_result';
459
460 if ( ! class_exists($driver))
461 {
Greg Aker3a746652011-04-19 10:59:47 -0500462 include_once(BASEPATH.'database/DB_result.php');
463 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 return $driver;
467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // --------------------------------------------------------------------
470
471 /**
472 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200473 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * we only use it when running transaction commands since they do
475 * not require all the features of the main query() function.
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200478 * @return mixed
479 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500480 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 if ( ! $this->conn_id)
483 {
484 $this->initialize();
485 }
486
487 return $this->_execute($sql);
488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 // --------------------------------------------------------------------
491
492 /**
493 * Disable Transactions
494 * This permits transactions to be disabled at run-time.
495 *
Barry Mienydd671972010-10-04 16:33:58 +0200496 * @return void
497 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500498 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 $this->trans_enabled = FALSE;
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Enable/disable Transaction Strict Mode
507 * When strict mode is enabled, if you are running multiple groups of
508 * transactions, if one group fails all groups will be rolled back.
509 * If strict mode is disabled, each group is treated autonomously, meaning
510 * a failure of one group will not affect any others
511 *
Barry Mienydd671972010-10-04 16:33:58 +0200512 * @return void
513 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500514 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // --------------------------------------------------------------------
520
521 /**
522 * Start Transaction
523 *
Barry Mienydd671972010-10-04 16:33:58 +0200524 * @return void
525 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500526 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200527 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 if ( ! $this->trans_enabled)
529 {
530 return FALSE;
531 }
532
533 // When transactions are nested we only begin/commit/rollback the outermost ones
534 if ($this->_trans_depth > 0)
535 {
536 $this->_trans_depth += 1;
537 return;
538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500541 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
543
544 // --------------------------------------------------------------------
545
546 /**
547 * Complete Transaction
548 *
Barry Mienydd671972010-10-04 16:33:58 +0200549 * @return bool
550 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500551 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
553 if ( ! $this->trans_enabled)
554 {
555 return FALSE;
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 // When transactions are nested we only begin/commit/rollback the outermost ones
559 if ($this->_trans_depth > 1)
560 {
561 $this->_trans_depth -= 1;
562 return TRUE;
563 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500564 else
565 {
566 $this->_trans_depth = 0;
567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // The query() function will set this flag to FALSE in the event that a query failed
570 if ($this->_trans_status === FALSE)
571 {
572 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // If we are NOT running in strict mode, we will reset
575 // the _trans_status flag so that subsequent groups of transactions
576 // will be permitted.
577 if ($this->trans_strict === FALSE)
578 {
579 $this->_trans_status = TRUE;
580 }
581
582 log_message('debug', 'DB Transaction Failure');
583 return FALSE;
584 }
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 $this->trans_commit();
587 return TRUE;
588 }
589
590 // --------------------------------------------------------------------
591
592 /**
593 * Lets you retrieve the transaction flag to determine if it has failed
594 *
Barry Mienydd671972010-10-04 16:33:58 +0200595 * @return bool
596 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500597 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 return $this->_trans_status;
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Compile Bindings
606 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 * @param string the sql statement
608 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200609 * @return string
610 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500611 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300613 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 return $sql;
616 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300617 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300619 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300620 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300622 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200623 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300624 // Make sure we're using numeric keys
625 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300626 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
628
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300629 // We'll need the marker length later
630 $ml = strlen($this->bind_marker);
631
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300632 // Make sure not to replace a chunk inside a string that happens to match the bind marker
633 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300635 $c = preg_match_all('/'.preg_quote($this->bind_marker).'/i',
636 str_replace($matches[0],
637 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
638 $sql, $c),
639 $matches, PREG_OFFSET_CAPTURE);
640
641 // Bind values' count must match the count of markers in the query
642 if ($bind_count !== $c)
643 {
644 return $sql;
645 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300646 }
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300647 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 +0300648 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300649 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
651
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300652 do
653 {
654 $c--;
655 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
656 }
657 while ($c !== 0);
658
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300659 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // --------------------------------------------------------------------
663
664 /**
665 * Determines if a query is a "write" type.
666 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200668 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200669 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200670 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300672 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 +0000673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // --------------------------------------------------------------------
676
677 /**
678 * Calculate the aggregate query elapsed time
679 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200680 * @param int The number of decimal places
681 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200682 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500683 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
685 return number_format($this->benchmark, $decimals);
686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 // --------------------------------------------------------------------
689
690 /**
691 * Returns the total number of queries
692 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200693 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200694 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500695 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 {
697 return $this->query_count;
698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 // --------------------------------------------------------------------
701
702 /**
703 * Returns the last query that was executed
704 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200705 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200706 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500707 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
709 return end($this->queries);
710 }
711
712 // --------------------------------------------------------------------
713
714 /**
715 * "Smart" Escape String
716 *
717 * Escapes data based on type
718 * Sets boolean and null types
719 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200721 * @return mixed
722 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500723 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200724 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500725 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200727 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000728 }
729 elseif (is_bool($str))
730 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200731 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000732 }
733 elseif (is_null($str))
734 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200735 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000736 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000737
738 return $str;
739 }
740
741 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600742
Derek Jonese4ed5832009-02-20 21:44:59 +0000743 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000744 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000745 *
746 * Calls the individual driver for platform
747 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200748 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000749 * @param string
750 * @return mixed
751 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500752 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200753 {
754 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000755 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000756
Derek Jonese4ed5832009-02-20 21:44:59 +0000757 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 /**
760 * Primary
761 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200762 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 * position is the primary key
764 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200766 * @return string
767 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500768 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200769 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200771 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
773
774 // --------------------------------------------------------------------
775
776 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300777 * "Count All" query
778 *
779 * Generates a platform-specific query string that counts all records in
780 * the specified database
781 *
782 * @param string
783 * @return int
784 */
785 public function count_all($table = '')
786 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100787 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300788 {
789 return 0;
790 }
791
792 $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 +0100793 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300794 {
795 return 0;
796 }
797
798 $query = $query->row();
799 $this->_reset_select();
800 return (int) $query->numrows;
801 }
802
803 // --------------------------------------------------------------------
804
805 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 * Returns an array of table names
807 *
Barry Mienydd671972010-10-04 16:33:58 +0200808 * @return array
809 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500810 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
812 // Is there a cached result?
813 if (isset($this->data_cache['table_names']))
814 {
815 return $this->data_cache['table_names'];
816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
819 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200820 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
822
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200823 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200825
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200826 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200828 // Do we know from which column to get the table name?
829 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300831 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200832 {
833 $key = 'table_name';
834 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300835 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200836 {
837 $key = 'TABLE_NAME';
838 }
839 else
840 {
841 /* We have no other choice but to just get the first element's key.
842 * Due to array_shift() accepting it's argument by reference, if
843 * E_STRICT is on, this would trigger a warning. So we'll have to
844 * assign it first.
845 */
846 $key = array_keys($row);
847 $key = array_shift($key);
848 }
Taufan Aditya18209332012-02-09 16:07:27 +0700849 }
850
Andrey Andreev12567e82012-01-25 22:50:33 +0200851 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 return $this->data_cache['table_names'];
855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 // --------------------------------------------------------------------
858
859 /**
860 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500861 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200862 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500864 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200865 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200866 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 }
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 // --------------------------------------------------------------------
870
871 /**
Andrey Andreev75546112012-07-05 11:01:29 +0300872 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200875 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500877 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
879 // Is there a cached result?
880 if (isset($this->data_cache['field_names'][$table]))
881 {
882 return $this->data_cache['field_names'][$table];
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100885 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200887 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Greg Aker1edde302010-01-26 00:17:01 +0000890 if (FALSE === ($sql = $this->_list_columns($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_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200896 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200897
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500898 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200900 // Do we know from where to get the column's name?
901 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300903 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200904 {
905 $key = 'column_name';
906 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300907 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200908 {
909 $key = 'COLUMN_NAME';
910 }
911 else
912 {
913 /* We have no other choice but to just get the first element's key.
914 * Due to array_shift() accepting it's argument by reference, if
915 * E_STRICT is on, this would trigger a warning. So we'll have to
916 * assign it first.
917 */
918 $key = array_keys($row);
919 $key = array_shift($key);
920 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200922
923 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 return $this->data_cache['field_names'][$table];
927 }
928
929 // --------------------------------------------------------------------
930
931 /**
932 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500933 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @param string
935 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200936 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500938 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200939 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200940 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 }
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 // --------------------------------------------------------------------
944
945 /**
946 * Returns an object with field data
947 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200949 * @return object
950 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500951 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100953 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200955 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200958 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200960 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000961
962 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300965 * Escape the SQL Identifiers
966 *
967 * This function escapes column and table names
968 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300969 * @param mixed
970 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300971 */
972 public function escape_identifiers($item)
973 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300974 if ($this->_escape_char === '' OR empty($item))
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300975 {
976 return $item;
977 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300978 elseif (is_array($item))
979 {
980 foreach ($item as $key => $value)
981 {
982 $item[$key] = $this->escape_identifiers($value);
983 }
984
985 return $item;
986 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300987 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +0300988 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +0300989 {
990 return $item;
991 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300992
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300993 static $preg_ec = array();
994
995 if (empty($preg_ec))
996 {
997 if (is_array($this->_escape_char))
998 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +0300999 $preg_ec = array(
1000 preg_quote($this->_escape_char[0]), preg_quote($this->_escape_char[1]),
1001 $this->_escape_char[0], $this->_escape_char[1]
1002 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001003 }
1004 else
1005 {
1006 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001007 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001008 }
1009 }
1010
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001011 foreach ($this->_reserved_identifiers as $id)
1012 {
1013 if (strpos($item, '.'.$id) !== FALSE)
1014 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001015 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 +03001016 }
1017 }
1018
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001019 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 +03001020 }
1021
1022 // --------------------------------------------------------------------
1023
1024 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * Generate an insert string
1026 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 * @param string the table upon which the query will be performed
1028 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001029 * @return string
1030 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001031 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001033 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001034
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001035 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001037 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 $values[] = $this->escape($val);
1039 }
Barry Mienydd671972010-10-04 16:33:58 +02001040
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001041 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001042 }
1043
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 // --------------------------------------------------------------------
1045
1046 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001047 * Insert statement
1048 *
1049 * Generates a platform-specific insert string from the supplied data
1050 *
1051 * @param string the table name
1052 * @param array the insert keys
1053 * @param array the insert values
1054 * @return string
1055 */
1056 protected function _insert($table, $keys, $values)
1057 {
1058 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1059 }
1060
1061 // --------------------------------------------------------------------
1062
1063 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 * Generate an update string
1065 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @param string the table upon which the query will be performed
1067 * @param array an associative array data of key/values
1068 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001069 * @return string
1070 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001071 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001073 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001075 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 }
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001079 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001081 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 }
1083
1084 if ( ! is_array($where))
1085 {
1086 $dest = array($where);
1087 }
1088 else
1089 {
1090 $dest = array();
1091 foreach ($where as $key => $val)
1092 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001093 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001094 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001095
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 if ($val !== '')
1097 {
1098 if ( ! $this->_has_operator($key))
1099 {
1100 $key .= ' =';
1101 }
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 $val = ' '.$this->escape($val);
1104 }
Barry Mienydd671972010-10-04 16:33:58 +02001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 $dest[] = $prefix.$key.$val;
1107 }
Barry Mienydd671972010-10-04 16:33:58 +02001108 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001109
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001110 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001111 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001112
1113 // --------------------------------------------------------------------
1114
1115 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001116 * Update statement
1117 *
1118 * Generates a platform-specific update string from the supplied data
1119 *
1120 * @param string the table name
Andrey Andreev75546112012-07-05 11:01:29 +03001121 * @return string
1122 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001123 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001124 {
1125 foreach ($values as $key => $val)
1126 {
1127 $valstr[] = $key.' = '.$val;
1128 }
1129
Andrey Andreev75546112012-07-05 11:01:29 +03001130 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreevb0478652012-07-18 15:34:46 +03001131 .$this->_compile_where()
1132 .(empty($this->qb_orderby) ? '' : ' ORDER BY '.implode(', ', $this->qb_orderby))
1133 .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001134 }
1135
1136 // --------------------------------------------------------------------
1137
1138 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 * Tests whether the string has an SQL operator
1140 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 * @param string
1142 * @return bool
1143 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001144 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001146 return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 }
1148
1149 // --------------------------------------------------------------------
1150
1151 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001152 * Returns the SQL string operator
1153 *
1154 * @param string
1155 * @return string
1156 */
1157 protected function _get_operator($str)
1158 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001159 static $_operators = array(
Andrey Andreevb0478652012-07-18 15:34:46 +03001160 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1161 '\s*<>?\s*', // <, <>
1162 '\s*>\s*', // >
1163 '\s+IS NULL', // IS NULL
1164 '\s+IS NOT NULL', // IS NOT NULL
1165 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1166 '\s+IN\s*\([^\)]+\)', // IN(list)
1167 '\s+NOT IN\s*\([^\)]+\)' // NOT IN (list)
Andrey Andreev6e704752012-07-18 00:46:33 +03001168 );
1169
Andrey Andreevb0478652012-07-18 15:34:46 +03001170 static $_like = array(
1171 '\s+LIKE\s+\S+', // LIKE 'expr'
1172 '\s+NOT LIKE\s+\S+', // NOT LIKE 'expr'
1173 );
1174
1175 if ($this->_like_escape_str !== '')
1176 {
1177 $_like[0] .= preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)));
1178 $_like[1] .= preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)));
1179 }
1180
1181 $_operators = array_merge($_operators, $_like);
1182
Andrey Andreev6e704752012-07-18 00:46:33 +03001183 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1184 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001185 }
1186
1187 // --------------------------------------------------------------------
1188
1189 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1191 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 * @param string the function name
1193 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001194 * @return mixed
1195 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001196 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001198 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 if (FALSE === strpos($driver, $function))
1201 {
1202 $function = $driver.$function;
1203 }
Barry Mienydd671972010-10-04 16:33:58 +02001204
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 if ( ! function_exists($function))
1206 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001207 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001209
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001210 return (func_num_args() > 1)
1211 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1212 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 }
1214
1215 // --------------------------------------------------------------------
1216
1217 /**
1218 * Set Cache Directory Path
1219 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 * @param string the path to the cache directory
1221 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001222 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001223 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
1225 $this->cachedir = $path;
1226 }
1227
1228 // --------------------------------------------------------------------
1229
1230 /**
1231 * Enable Query Caching
1232 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001233 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001234 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001235 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001237 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 }
1239
1240 // --------------------------------------------------------------------
1241
1242 /**
1243 * Disable Query Caching
1244 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001245 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001246 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001247 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001249 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 }
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Delete the cache files associated with a particular URI
1256 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001257 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001258 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001259 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001261 return ($this->_cache_init())
1262 ? $this->CACHE->delete($segment_one, $segment_two)
1263 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 }
1265
1266 // --------------------------------------------------------------------
1267
1268 /**
1269 * Delete All cache files
1270 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001271 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001272 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001273 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001275 return ($this->_cache_init())
1276 ? $this->CACHE->delete_all()
1277 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 }
1279
1280 // --------------------------------------------------------------------
1281
1282 /**
1283 * Initialize the Cache Class
1284 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001285 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001286 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001287 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001289 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001291 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001292 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001293 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001294 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001296 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1297 {
1298 return $this->cache_off();
1299 }
Derek Allarde37ab382009-02-03 16:13:57 +00001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1302 return TRUE;
1303 }
1304
1305 // --------------------------------------------------------------------
1306
1307 /**
1308 * Close DB Connection
1309 *
Barry Mienydd671972010-10-04 16:33:58 +02001310 * @return void
1311 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001312 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001314 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001316 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001317 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 }
Barry Mienydd671972010-10-04 16:33:58 +02001320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 // --------------------------------------------------------------------
1322
1323 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001324 * Close DB Connection
1325 *
1326 * This method would be overriden by most of the drivers.
1327 *
1328 * @return void
1329 */
1330 protected function _close()
1331 {
1332 $this->conn_id = FALSE;
1333 }
1334
1335 // --------------------------------------------------------------------
1336
1337 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 * Display an error message
1339 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 * @param string the error message
1341 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001342 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001343 * @return string sends the application/error_db.php template
1344 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001345 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 {
Derek Jonese7792202010-03-02 17:24:46 -06001347 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 $LANG->load('db');
1349
1350 $heading = $LANG->line('db_error_heading');
1351
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001352 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001354 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 }
1356 else
1357 {
1358 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1359 }
Barry Mienydd671972010-10-04 16:33:58 +02001360
Pascal Kriete60f8c392010-08-25 18:03:28 +02001361 // Find the most likely culprit of the error by going through
1362 // the backtrace until the source file is no longer in the
1363 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001364 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001365 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001366 {
1367 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1368 {
1369 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001370 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001371 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001372 break;
1373 }
1374 }
Barry Mienydd671972010-10-04 16:33:58 +02001375
Derek Jonese7792202010-03-02 17:24:46 -06001376 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 echo $error->show_error($heading, $message, 'error_db');
1378 exit;
1379 }
1380
1381 // --------------------------------------------------------------------
1382
1383 /**
1384 * Protect Identifiers
1385 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001386 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001387 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001389 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001390 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 *
1392 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1393 *
1394 * Or a query with aliasing:
1395 *
1396 * SELECT m.member_id, m.member_name FROM members AS m
1397 *
1398 * Since the column name can include up to four segments (host, DB, table, column)
1399 * or also have an alias prefix, we need to do a bit of work to figure this out and
1400 * insert the table prefix (if it exists) in the proper position, and escape only
1401 * the correct identifiers.
1402 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 * @param string
1404 * @param bool
1405 * @param mixed
1406 * @param bool
1407 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001408 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001409 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 {
1411 if ( ! is_bool($protect_identifiers))
1412 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001413 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 }
Derek Allarde37ab382009-02-03 16:13:57 +00001415
1416 if (is_array($item))
1417 {
1418 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001419 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001420 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001421 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001422 }
1423
1424 return $escaped_array;
1425 }
1426
Derek Allard911d3e02008-12-15 14:08:35 +00001427 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001428 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001429 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001430 // way to deal with this, but I'm not thinking of it -- Rick
1431 if (strpos($item, '(') !== FALSE)
1432 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001433 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001434 }
1435
Andrey Andreevbf940582012-06-10 07:05:05 +03001436 // Convert tabs or multiple spaces into single spaces
1437 $item = preg_replace('/\s+/', ' ', $item);
1438
Andrey Andreevbf940582012-06-10 07:05:05 +03001439 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001440 // Note: strripos() is used in order to support spaces in table names
1441 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001442 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001443 $alias = ($protect_identifiers)
1444 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1445 : substr($item, $offset);
1446 $item = substr($item, 0, $offset);
1447 }
1448 elseif ($offset = strrpos($item, ' '))
1449 {
1450 $alias = ($protect_identifiers)
1451 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1452 : substr($item, $offset);
1453 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001454 }
1455 else
1456 {
1457 $alias = '';
1458 }
1459
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 // Break the string apart if it contains periods, then insert the table prefix
1461 // in the correct location, assuming the period doesn't indicate that we're dealing
1462 // with an alias. While we're at it, we will escape the components
1463 if (strpos($item, '.') !== FALSE)
1464 {
1465 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001466
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001468 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001470 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001471 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 if ($protect_identifiers === TRUE)
1473 {
1474 foreach ($parts as $key => $val)
1475 {
1476 if ( ! in_array($val, $this->_reserved_identifiers))
1477 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001478 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 }
1480 }
Barry Mienydd671972010-10-04 16:33:58 +02001481
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001483 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001484
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 return $item.$alias;
1486 }
Barry Mienydd671972010-10-04 16:33:58 +02001487
Andrey Andreev4c202602012-03-06 20:34:52 +02001488 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001489 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 {
1491 // We now add the table prefix based on some logic.
1492 // Do we have 4 segments (hostname.database.table.column)?
1493 // If so, we add the table prefix to the column name in the 3rd segment.
1494 if (isset($parts[3]))
1495 {
1496 $i = 2;
1497 }
1498 // Do we have 3 segments (database.table.column)?
1499 // If so, we add the table prefix to the column name in 2nd position
1500 elseif (isset($parts[2]))
1501 {
1502 $i = 1;
1503 }
1504 // Do we have 2 segments (table.column)?
1505 // If so, we add the table prefix to the column name in 1st segment
1506 else
1507 {
1508 $i = 0;
1509 }
Barry Mienydd671972010-10-04 16:33:58 +02001510
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 // This flag is set when the supplied $item does not contain a field name.
1512 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001513 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 {
1515 $i++;
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Jones55acc8b2009-07-11 03:38:13 +00001518 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001519 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001520 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001521 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001522 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001524 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 {
1526 $parts[$i] = $this->dbprefix.$parts[$i];
1527 }
Barry Mienydd671972010-10-04 16:33:58 +02001528
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 // Put the parts back together
1530 $item = implode('.', $parts);
1531 }
Barry Mienydd671972010-10-04 16:33:58 +02001532
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 if ($protect_identifiers === TRUE)
1534 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001535 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 }
Barry Mienydd671972010-10-04 16:33:58 +02001537
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 return $item.$alias;
1539 }
1540
Andrey Andreev4c202602012-03-06 20:34:52 +02001541 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001542 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001544 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001545 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001546 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001547 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001548 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001550 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 {
1552 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001553 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 }
1555
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001556 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001558 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 }
Barry Mienydd671972010-10-04 16:33:58 +02001560
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 return $item.$alias;
1562 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001563
Túbal Martín511f2252011-11-24 14:43:45 +01001564 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001565
Túbal Martín511f2252011-11-24 14:43:45 +01001566 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001567 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001568 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001569 *
Túbal Martín511f2252011-11-24 14:43:45 +01001570 * @return void
1571 */
1572 protected function _reset_select()
1573 {
Túbal Martín511f2252011-11-24 14:43:45 +01001574 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001575
1576}
1577
Derek Allard2067d1a2008-11-13 22:59:24 +00001578/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001579/* Location: ./system/database/DB_driver.php */