blob: ebf828c4e433732f8f61e2fb170bf91898731f6e [file] [log] [blame]
Andrey Andreev4c202602012-03-06 20:34:52 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Timothy Warren7eeda532012-03-19 16:01:55 -040041abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreev738f5342012-03-06 20:43:40 +020043 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
Andrey Andreev75546112012-07-05 11:01:29 +030048 public $dbdriver = 'mysqli';
Andrey Andreevd1add432012-03-06 20:26:01 +020049 public $dbprefix = '';
50 public $char_set = 'utf8';
51 public $dbcollat = 'utf8_general_ci';
52 public $autoinit = TRUE; // Whether to automatically initialize the DB
53 public $swap_pre = '';
54 public $port = '';
55 public $pconnect = FALSE;
56 public $conn_id = FALSE;
57 public $result_id = FALSE;
58 public $db_debug = FALSE;
59 public $benchmark = 0;
60 public $query_count = 0;
61 public $bind_marker = '?';
62 public $save_queries = TRUE;
63 public $queries = array();
64 public $query_times = array();
65 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000066
Andrey Andreevd1add432012-03-06 20:26:01 +020067 public $trans_enabled = TRUE;
68 public $trans_strict = TRUE;
69 protected $_trans_depth = 0;
70 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000071
Andrey Andreevd1add432012-03-06 20:26:01 +020072 public $cache_on = FALSE;
73 public $cachedir = '';
74 public $cache_autodel = FALSE;
75 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020076
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000077 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020078 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
Andrey Andreev75546112012-07-05 11:01:29 +030080 /**
81 * Constructor
82 *
83 * @param array
84 * @return void
85 */
Timothy Warrene45518d2012-03-06 07:38:00 -050086 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 if (is_array($params))
89 {
90 foreach ($params as $key => $val)
91 {
92 $this->$key = $val;
93 }
94 }
95
96 log_message('debug', 'Database Driver Class Initialized');
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Gints Murans89f77ee2012-05-23 00:23:50 +030099 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 /**
102 * Initialize Database Settings
103 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200104 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200105 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200106 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200108 /* If an established connection is available, then there's
109 * no need to connect and select the database.
110 *
111 * Depending on the database driver, conn_id can be either
112 * boolean TRUE, a resource or an object.
113 */
114 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
116 return TRUE;
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100122 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000123
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200124 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if ( ! $this->conn_id)
126 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100127 // Check if there is a failover set
128 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100130 // Go over all the failovers
131 foreach ($this->failover as $failover)
132 {
133 // Replace the current settings with those of the failover
134 foreach ($failover as $key => $val)
135 {
136 $this->$key = $val;
137 }
138
139 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100140 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100141
142 // If a connection is made break the foreach loop
143 if ($this->conn_id)
144 {
145 break;
146 }
147 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100149
150 // We still don't have a connection?
151 if ( ! $this->conn_id)
152 {
153 log_message('error', 'Unable to connect to the database');
154
155 if ($this->db_debug)
156 {
157 $this->display_error('db_unable_to_connect');
158 }
159 return FALSE;
160 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162
163 // ----------------------------------------------------------------
164
165 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200166 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200169
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200170 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200172 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200174 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200177 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200178 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200184 * Reconnect
185 *
186 * Keep / reestablish the db connection if no queries have been
187 * sent for a length of time exceeding the server's idle timeout.
188 *
189 * This is just a dummy method to allow drivers without such
190 * functionality to not declare it, while others will override it.
191 *
192 * @return void
193 */
194 public function reconnect()
195 {
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300201 * Select database
202 *
203 * This is just a dummy method to allow drivers without such
204 * functionality to not declare it, while others will override it.
205 *
206 * @return bool
207 */
208 public function db_select()
209 {
210 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Set client character set
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200219 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200221 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200223 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200225 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 if ($this->db_debug)
228 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200229 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 return FALSE;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 return TRUE;
236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 // --------------------------------------------------------------------
239
240 /**
241 * The name of the platform in use (mysql, mssql, etc...)
242 *
Barry Mienydd671972010-10-04 16:33:58 +0200243 * @return string
244 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500245 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 return $this->dbdriver;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200253 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200255 * Returns a string containing the version of the database being used.
256 * Most drivers will override this method.
257 *
Barry Mienydd671972010-10-04 16:33:58 +0200258 * @return string
259 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200260 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200262 if (isset($this->data_cache['version']))
263 {
264 return $this->data_cache['version'];
265 }
266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 if (FALSE === ($sql = $this->_version()))
268 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200269 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Derek Allard3683f772009-12-16 17:32:33 +0000271
Andrey Andreev08856b82012-03-03 03:19:28 +0200272 $query = $this->query($sql);
273 $query = $query->row();
274 return $this->data_cache['version'] = $query->ver;
275 }
Derek Allard3683f772009-12-16 17:32:33 +0000276
Andrey Andreev08856b82012-03-03 03:19:28 +0200277 // --------------------------------------------------------------------
278
279 /**
280 * Version number query string
281 *
282 * @return string
283 */
284 protected function _version()
285 {
286 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // --------------------------------------------------------------------
290
291 /**
292 * Execute the query
293 *
294 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200295 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * upon successful execution of a "write" type query. Returns boolean
297 * FALSE upon failure, and if the $db_debug variable is set to TRUE
298 * will raise an error.
299 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @param string An SQL query string
301 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200302 * @return mixed
303 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300304 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100306 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200308 log_message('error', 'Invalid query: '.$sql);
309
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200310 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300312 elseif ( ! is_bool($return_object))
313 {
314 $return_object = ! $this->is_write_type($sql);
315 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
317 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100318 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600319 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200320 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
Derek Jonese7792202010-03-02 17:24:46 -0600322
Ryan Dialef7474c2012-03-01 16:11:36 -0500323 // Compile binds if needed
324 if ($binds !== FALSE)
325 {
326 $sql = $this->compile_binds($sql, $binds);
327 }
328
Andrey Andreev4c202602012-03-06 20:34:52 +0200329 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // we will load the caching class and return the previously
331 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300332 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200334 $this->load_rdriver();
335 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200337 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Andrey Andreevb66664b2012-06-27 14:22:10 +0300341 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100342 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 $this->queries[] = $sql;
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200348 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // Run the Query
351 if (FALSE === ($this->result_id = $this->simple_query($sql)))
352 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100353 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 $this->query_times[] = 0;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // This will trigger a rollback if transactions are being used
359 $this->_trans_status = FALSE;
360
Andrey Andreev4be5de12012-03-02 15:45:41 +0200361 // Grab the error now, as we might run some additional queries before displaying the error
362 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200363
364 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300365 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 if ($this->db_debug)
368 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200370 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200371 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // transactions to remain in limbo.
373 $this->trans_complete();
374
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200375 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200376 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 return FALSE;
380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200383 $time_end = microtime(TRUE);
384 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100386 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
dixyab3cab52012-04-21 00:58:00 +0200388 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // Increment the query counter
392 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200393
Andrey Andreevb66664b2012-06-27 14:22:10 +0300394 // Will we have a result object instantiated? If not - we'll simply return TRUE
395 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300397 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100398 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 $this->CACHE->delete();
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return TRUE;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ($return_object !== TRUE)
408 {
409 return TRUE;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
412 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200413 $driver = $this->load_rdriver();
414 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200415
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200416 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100418 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 // We'll create a new instance of the result object
421 // only without the platform specific driver since
422 // we can't use it with cached data (the query result
423 // resource ID won't be any good once we've cached the
424 // result object, so we'll have to compile the data
425 // and save it)
426 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 $CR->result_object = $RES->result_object();
428 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200429 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // Reset these since cached objects can not utilize resource IDs.
432 $CR->conn_id = NULL;
433 $CR->result_id = NULL;
434
435 $this->CACHE->write($sql, $CR);
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 return $RES;
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Load the result drivers
445 *
Barry Mienydd671972010-10-04 16:33:58 +0200446 * @return string the name of the result class
447 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500448 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 $driver = 'CI_DB_'.$this->dbdriver.'_result';
451
452 if ( ! class_exists($driver))
453 {
Greg Aker3a746652011-04-19 10:59:47 -0500454 include_once(BASEPATH.'database/DB_result.php');
455 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 return $driver;
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200465 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * we only use it when running transaction commands since they do
467 * not require all the features of the main query() function.
468 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200470 * @return mixed
471 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500472 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
474 if ( ! $this->conn_id)
475 {
476 $this->initialize();
477 }
478
479 return $this->_execute($sql);
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Disable Transactions
486 * This permits transactions to be disabled at run-time.
487 *
Barry Mienydd671972010-10-04 16:33:58 +0200488 * @return void
489 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500490 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 $this->trans_enabled = FALSE;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Enable/disable Transaction Strict Mode
499 * When strict mode is enabled, if you are running multiple groups of
500 * transactions, if one group fails all groups will be rolled back.
501 * If strict mode is disabled, each group is treated autonomously, meaning
502 * a failure of one group will not affect any others
503 *
Barry Mienydd671972010-10-04 16:33:58 +0200504 * @return void
505 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500506 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 // --------------------------------------------------------------------
512
513 /**
514 * Start Transaction
515 *
Barry Mienydd671972010-10-04 16:33:58 +0200516 * @return void
517 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500518 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200519 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 if ( ! $this->trans_enabled)
521 {
522 return FALSE;
523 }
524
525 // When transactions are nested we only begin/commit/rollback the outermost ones
526 if ($this->_trans_depth > 0)
527 {
528 $this->_trans_depth += 1;
529 return;
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500533 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Complete Transaction
540 *
Barry Mienydd671972010-10-04 16:33:58 +0200541 * @return bool
542 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500543 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
545 if ( ! $this->trans_enabled)
546 {
547 return FALSE;
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // When transactions are nested we only begin/commit/rollback the outermost ones
551 if ($this->_trans_depth > 1)
552 {
553 $this->_trans_depth -= 1;
554 return TRUE;
555 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500556 else
557 {
558 $this->_trans_depth = 0;
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 // The query() function will set this flag to FALSE in the event that a query failed
562 if ($this->_trans_status === FALSE)
563 {
564 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 // If we are NOT running in strict mode, we will reset
567 // the _trans_status flag so that subsequent groups of transactions
568 // will be permitted.
569 if ($this->trans_strict === FALSE)
570 {
571 $this->_trans_status = TRUE;
572 }
573
574 log_message('debug', 'DB Transaction Failure');
575 return FALSE;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 $this->trans_commit();
579 return TRUE;
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Lets you retrieve the transaction flag to determine if it has failed
586 *
Barry Mienydd671972010-10-04 16:33:58 +0200587 * @return bool
588 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500589 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 return $this->_trans_status;
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
597 * Compile Bindings
598 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 * @param string the sql statement
600 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200601 * @return string
602 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500603 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300605 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
607 return $sql;
608 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300609 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300611 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300612 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300614 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200615 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300616 // Make sure we're using numeric keys
617 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300618 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 }
620
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300621 // We'll need the marker length later
622 $ml = strlen($this->bind_marker);
623
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300624 // Make sure not to replace a chunk inside a string that happens to match the bind marker
625 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300627 $c = preg_match_all('/'.preg_quote($this->bind_marker).'/i',
628 str_replace($matches[0],
629 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
630 $sql, $c),
631 $matches, PREG_OFFSET_CAPTURE);
632
633 // Bind values' count must match the count of markers in the query
634 if ($bind_count !== $c)
635 {
636 return $sql;
637 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300638 }
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300639 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 +0300640 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300641 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
643
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300644 do
645 {
646 $c--;
647 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
648 }
649 while ($c !== 0);
650
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300651 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 }
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 // --------------------------------------------------------------------
655
656 /**
657 * Determines if a query is a "write" type.
658 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200660 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200661 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200662 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300664 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 +0000665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Calculate the aggregate query elapsed time
671 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200672 * @param int The number of decimal places
673 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200674 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500675 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 return number_format($this->benchmark, $decimals);
678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // --------------------------------------------------------------------
681
682 /**
683 * Returns the total number of queries
684 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200685 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200686 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500687 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 return $this->query_count;
690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 // --------------------------------------------------------------------
693
694 /**
695 * Returns the last query that was executed
696 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200697 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200698 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500699 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
701 return end($this->queries);
702 }
703
704 // --------------------------------------------------------------------
705
706 /**
707 * "Smart" Escape String
708 *
709 * Escapes data based on type
710 * Sets boolean and null types
711 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200713 * @return mixed
714 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500715 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200716 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500717 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200719 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000720 }
721 elseif (is_bool($str))
722 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200723 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000724 }
725 elseif (is_null($str))
726 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200727 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000728 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000729
730 return $str;
731 }
732
733 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600734
Derek Jonese4ed5832009-02-20 21:44:59 +0000735 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000736 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000737 *
738 * Calls the individual driver for platform
739 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200740 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000741 * @param string
742 * @return mixed
743 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500744 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200745 {
746 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000747 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000748
Derek Jonese4ed5832009-02-20 21:44:59 +0000749 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 /**
752 * Primary
753 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200754 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 * position is the primary key
756 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200758 * @return string
759 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500760 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200761 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200763 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
765
766 // --------------------------------------------------------------------
767
768 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300769 * "Count All" query
770 *
771 * Generates a platform-specific query string that counts all records in
772 * the specified database
773 *
774 * @param string
775 * @return int
776 */
777 public function count_all($table = '')
778 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100779 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300780 {
781 return 0;
782 }
783
784 $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 +0100785 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300786 {
787 return 0;
788 }
789
790 $query = $query->row();
791 $this->_reset_select();
792 return (int) $query->numrows;
793 }
794
795 // --------------------------------------------------------------------
796
797 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 * Returns an array of table names
799 *
Barry Mienydd671972010-10-04 16:33:58 +0200800 * @return array
801 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500802 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
804 // Is there a cached result?
805 if (isset($this->data_cache['table_names']))
806 {
807 return $this->data_cache['table_names'];
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
811 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200812 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
814
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200815 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200817
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200818 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200820 // Do we know from which column to get the table name?
821 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300823 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200824 {
825 $key = 'table_name';
826 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300827 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200828 {
829 $key = 'TABLE_NAME';
830 }
831 else
832 {
833 /* We have no other choice but to just get the first element's key.
834 * Due to array_shift() accepting it's argument by reference, if
835 * E_STRICT is on, this would trigger a warning. So we'll have to
836 * assign it first.
837 */
838 $key = array_keys($row);
839 $key = array_shift($key);
840 }
Taufan Aditya18209332012-02-09 16:07:27 +0700841 }
842
Andrey Andreev12567e82012-01-25 22:50:33 +0200843 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 return $this->data_cache['table_names'];
847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 // --------------------------------------------------------------------
850
851 /**
852 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500853 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200854 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500856 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200857 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200858 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
862
863 /**
Andrey Andreev75546112012-07-05 11:01:29 +0300864 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200867 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500869 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
871 // Is there a cached result?
872 if (isset($this->data_cache['field_names'][$table]))
873 {
874 return $this->data_cache['field_names'][$table];
875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100877 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200879 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
Barry Mienydd671972010-10-04 16:33:58 +0200881
Greg Aker1edde302010-01-26 00:17:01 +0000882 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200884 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200888 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200889
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500890 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200892 // Do we know from where to get the column's name?
893 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300895 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200896 {
897 $key = 'column_name';
898 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300899 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200900 {
901 $key = 'COLUMN_NAME';
902 }
903 else
904 {
905 /* We have no other choice but to just get the first element's key.
906 * Due to array_shift() accepting it's argument by reference, if
907 * E_STRICT is on, this would trigger a warning. So we'll have to
908 * assign it first.
909 */
910 $key = array_keys($row);
911 $key = array_shift($key);
912 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200914
915 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 }
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 return $this->data_cache['field_names'][$table];
919 }
920
921 // --------------------------------------------------------------------
922
923 /**
924 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @param string
927 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200928 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500930 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200931 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200932 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 }
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 // --------------------------------------------------------------------
936
937 /**
938 * Returns an object with field data
939 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200941 * @return object
942 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500943 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100945 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200947 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 }
Barry Mienydd671972010-10-04 16:33:58 +0200949
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200950 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200952 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000953
954 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300957 * Escape the SQL Identifiers
958 *
959 * This function escapes column and table names
960 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300961 * @param mixed
962 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300963 */
964 public function escape_identifiers($item)
965 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100966 if ($this->_escape_char === '')
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300967 {
968 return $item;
969 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300970 elseif (is_array($item))
971 {
972 foreach ($item as $key => $value)
973 {
974 $item[$key] = $this->escape_identifiers($value);
975 }
976
977 return $item;
978 }
Andrey Andreev7db0f592012-06-28 17:54:27 +0300979 // Avoid breaking functions inside queries
980 elseif (strpos($item, '(') !== FALSE)
981 {
982 return $item;
983 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300984
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300985 static $preg_ec = array();
986
987 if (empty($preg_ec))
988 {
989 if (is_array($this->_escape_char))
990 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +0300991 $preg_ec = array(
992 preg_quote($this->_escape_char[0]), preg_quote($this->_escape_char[1]),
993 $this->_escape_char[0], $this->_escape_char[1]
994 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300995 }
996 else
997 {
998 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
Andrey Andreev2636c1c2012-07-02 14:53:39 +0300999 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001000 }
1001 }
1002
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001003 foreach ($this->_reserved_identifiers as $id)
1004 {
1005 if (strpos($item, '.'.$id) !== FALSE)
1006 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001007 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 +03001008 }
1009 }
1010
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001011 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 +03001012 }
1013
1014 // --------------------------------------------------------------------
1015
1016 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 * Generate an insert string
1018 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 * @param string the table upon which the query will be performed
1020 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001021 * @return string
1022 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001023 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001025 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001026
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001027 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001029 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 $values[] = $this->escape($val);
1031 }
Barry Mienydd671972010-10-04 16:33:58 +02001032
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001033 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001034 }
1035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 // --------------------------------------------------------------------
1037
1038 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001039 * Insert statement
1040 *
1041 * Generates a platform-specific insert string from the supplied data
1042 *
1043 * @param string the table name
1044 * @param array the insert keys
1045 * @param array the insert values
1046 * @return string
1047 */
1048 protected function _insert($table, $keys, $values)
1049 {
1050 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1051 }
1052
1053 // --------------------------------------------------------------------
1054
1055 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 * Generate an update string
1057 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 * @param string the table upon which the query will be performed
1059 * @param array an associative array data of key/values
1060 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001061 * @return string
1062 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001063 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001065 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001067 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 }
Barry Mienydd671972010-10-04 16:33:58 +02001069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001071 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001073 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
1075
1076 if ( ! is_array($where))
1077 {
1078 $dest = array($where);
1079 }
1080 else
1081 {
1082 $dest = array();
1083 foreach ($where as $key => $val)
1084 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001085 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001086 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 if ($val !== '')
1089 {
1090 if ( ! $this->_has_operator($key))
1091 {
1092 $key .= ' =';
1093 }
Barry Mienydd671972010-10-04 16:33:58 +02001094
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 $val = ' '.$this->escape($val);
1096 }
Barry Mienydd671972010-10-04 16:33:58 +02001097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 $dest[] = $prefix.$key.$val;
1099 }
Barry Mienydd671972010-10-04 16:33:58 +02001100 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001101
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001102 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001103 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001104
1105 // --------------------------------------------------------------------
1106
1107 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001108 * Update statement
1109 *
1110 * Generates a platform-specific update string from the supplied data
1111 *
1112 * @param string the table name
1113 * @param array the update data
1114 * @param array the where clause
1115 * @param array the orderby clause
1116 * @param array the limit clause
1117 * @param array the like clause
1118 * @return string
1119 */
1120 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
1121 {
1122 foreach ($values as $key => $val)
1123 {
1124 $valstr[] = $key.' = '.$val;
1125 }
1126
1127 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1128
1129 if ( ! empty($like))
1130 {
1131 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1132 }
1133
1134 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
1135 .$where
1136 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1137 .($limit ? ' LIMIT '.$limit : '');
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 * Tests whether the string has an SQL operator
1144 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 * @param string
1146 * @return bool
1147 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001148 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 {
Andrey Andreevd454f0e2012-06-10 14:51:04 +03001150 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL|BETWEEN)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 }
1152
1153 // --------------------------------------------------------------------
1154
1155 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001156 * Returns the SQL string operator
1157 *
1158 * @param string
1159 * @return string
1160 */
1161 protected function _get_operator($str)
1162 {
1163 return preg_match('/(=|!|<|>| IS NULL| IS NOT NULL| BETWEEN)/i', $str, $match)
1164 ? $match[1] : FALSE;
1165 }
1166
1167 // --------------------------------------------------------------------
1168
1169 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1171 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 * @param string the function name
1173 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001174 * @return mixed
1175 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001176 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001178 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 if (FALSE === strpos($driver, $function))
1181 {
1182 $function = $driver.$function;
1183 }
Barry Mienydd671972010-10-04 16:33:58 +02001184
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 if ( ! function_exists($function))
1186 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001187 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001189
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001190 return (func_num_args() > 1)
1191 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1192 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
1194
1195 // --------------------------------------------------------------------
1196
1197 /**
1198 * Set Cache Directory Path
1199 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 * @param string the path to the cache directory
1201 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001202 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001203 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
1205 $this->cachedir = $path;
1206 }
1207
1208 // --------------------------------------------------------------------
1209
1210 /**
1211 * Enable Query Caching
1212 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001213 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001214 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001215 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001217 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 }
1219
1220 // --------------------------------------------------------------------
1221
1222 /**
1223 * Disable Query Caching
1224 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001225 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001226 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001227 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001229 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // --------------------------------------------------------------------
1233
1234 /**
1235 * Delete the cache files associated with a particular URI
1236 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001237 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001238 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001239 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001241 return ($this->_cache_init())
1242 ? $this->CACHE->delete($segment_one, $segment_two)
1243 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 }
1245
1246 // --------------------------------------------------------------------
1247
1248 /**
1249 * Delete All cache files
1250 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001251 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001252 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001253 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001255 return ($this->_cache_init())
1256 ? $this->CACHE->delete_all()
1257 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 }
1259
1260 // --------------------------------------------------------------------
1261
1262 /**
1263 * Initialize the Cache Class
1264 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001265 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001266 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001267 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001269 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001271 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001272 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001273 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001274 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001276 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1277 {
1278 return $this->cache_off();
1279 }
Derek Allarde37ab382009-02-03 16:13:57 +00001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1282 return TRUE;
1283 }
1284
1285 // --------------------------------------------------------------------
1286
1287 /**
1288 * Close DB Connection
1289 *
Barry Mienydd671972010-10-04 16:33:58 +02001290 * @return void
1291 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001292 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001294 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001296 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001297 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 }
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 // --------------------------------------------------------------------
1302
1303 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001304 * Close DB Connection
1305 *
1306 * This method would be overriden by most of the drivers.
1307 *
1308 * @return void
1309 */
1310 protected function _close()
1311 {
1312 $this->conn_id = FALSE;
1313 }
1314
1315 // --------------------------------------------------------------------
1316
1317 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 * Display an error message
1319 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 * @param string the error message
1321 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001322 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001323 * @return string sends the application/error_db.php template
1324 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001325 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 {
Derek Jonese7792202010-03-02 17:24:46 -06001327 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 $LANG->load('db');
1329
1330 $heading = $LANG->line('db_error_heading');
1331
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001332 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001334 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 }
1336 else
1337 {
1338 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1339 }
Barry Mienydd671972010-10-04 16:33:58 +02001340
Pascal Kriete60f8c392010-08-25 18:03:28 +02001341 // Find the most likely culprit of the error by going through
1342 // the backtrace until the source file is no longer in the
1343 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001344 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001345 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001346 {
1347 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1348 {
1349 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001350 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001351 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001352 break;
1353 }
1354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Derek Jonese7792202010-03-02 17:24:46 -06001356 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 echo $error->show_error($heading, $message, 'error_db');
1358 exit;
1359 }
1360
1361 // --------------------------------------------------------------------
1362
1363 /**
1364 * Protect Identifiers
1365 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001366 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001367 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001369 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001370 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 *
1372 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1373 *
1374 * Or a query with aliasing:
1375 *
1376 * SELECT m.member_id, m.member_name FROM members AS m
1377 *
1378 * Since the column name can include up to four segments (host, DB, table, column)
1379 * or also have an alias prefix, we need to do a bit of work to figure this out and
1380 * insert the table prefix (if it exists) in the proper position, and escape only
1381 * the correct identifiers.
1382 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 * @param string
1384 * @param bool
1385 * @param mixed
1386 * @param bool
1387 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001388 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001389 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 {
1391 if ( ! is_bool($protect_identifiers))
1392 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001393 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 }
Derek Allarde37ab382009-02-03 16:13:57 +00001395
1396 if (is_array($item))
1397 {
1398 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001399 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001400 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001401 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001402 }
1403
1404 return $escaped_array;
1405 }
1406
Derek Allard911d3e02008-12-15 14:08:35 +00001407 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001408 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001409 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001410 // way to deal with this, but I'm not thinking of it -- Rick
1411 if (strpos($item, '(') !== FALSE)
1412 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001413 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001414 }
1415
Andrey Andreevbf940582012-06-10 07:05:05 +03001416 // Convert tabs or multiple spaces into single spaces
1417 $item = preg_replace('/\s+/', ' ', $item);
1418
Andrey Andreevbf940582012-06-10 07:05:05 +03001419 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001420 // Note: strripos() is used in order to support spaces in table names
1421 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001422 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001423 $alias = ($protect_identifiers)
1424 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1425 : substr($item, $offset);
1426 $item = substr($item, 0, $offset);
1427 }
1428 elseif ($offset = strrpos($item, ' '))
1429 {
1430 $alias = ($protect_identifiers)
1431 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1432 : substr($item, $offset);
1433 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001434 }
1435 else
1436 {
1437 $alias = '';
1438 }
1439
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 // Break the string apart if it contains periods, then insert the table prefix
1441 // in the correct location, assuming the period doesn't indicate that we're dealing
1442 // with an alias. While we're at it, we will escape the components
1443 if (strpos($item, '.') !== FALSE)
1444 {
1445 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001446
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001448 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001450 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001451 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 if ($protect_identifiers === TRUE)
1453 {
1454 foreach ($parts as $key => $val)
1455 {
1456 if ( ! in_array($val, $this->_reserved_identifiers))
1457 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001458 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 }
1460 }
Barry Mienydd671972010-10-04 16:33:58 +02001461
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001463 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001464
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 return $item.$alias;
1466 }
Barry Mienydd671972010-10-04 16:33:58 +02001467
Andrey Andreev4c202602012-03-06 20:34:52 +02001468 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001469 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
1471 // We now add the table prefix based on some logic.
1472 // Do we have 4 segments (hostname.database.table.column)?
1473 // If so, we add the table prefix to the column name in the 3rd segment.
1474 if (isset($parts[3]))
1475 {
1476 $i = 2;
1477 }
1478 // Do we have 3 segments (database.table.column)?
1479 // If so, we add the table prefix to the column name in 2nd position
1480 elseif (isset($parts[2]))
1481 {
1482 $i = 1;
1483 }
1484 // Do we have 2 segments (table.column)?
1485 // If so, we add the table prefix to the column name in 1st segment
1486 else
1487 {
1488 $i = 0;
1489 }
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 // This flag is set when the supplied $item does not contain a field name.
1492 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001493 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 {
1495 $i++;
1496 }
Barry Mienydd671972010-10-04 16:33:58 +02001497
Derek Jones55acc8b2009-07-11 03:38:13 +00001498 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001499 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001500 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001501 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001502 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001504 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 {
1506 $parts[$i] = $this->dbprefix.$parts[$i];
1507 }
Barry Mienydd671972010-10-04 16:33:58 +02001508
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 // Put the parts back together
1510 $item = implode('.', $parts);
1511 }
Barry Mienydd671972010-10-04 16:33:58 +02001512
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 if ($protect_identifiers === TRUE)
1514 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001515 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 return $item.$alias;
1519 }
1520
Andrey Andreev4c202602012-03-06 20:34:52 +02001521 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001522 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001524 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001525 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001526 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001527 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001528 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001530 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 {
1532 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001533 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 }
1535
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001536 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001538 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 }
Barry Mienydd671972010-10-04 16:33:58 +02001540
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 return $item.$alias;
1542 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001543
Túbal Martín511f2252011-11-24 14:43:45 +01001544 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001545
Túbal Martín511f2252011-11-24 14:43:45 +01001546 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001547 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001548 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001549 *
Túbal Martín511f2252011-11-24 14:43:45 +01001550 * @return void
1551 */
1552 protected function _reset_select()
1553 {
Túbal Martín511f2252011-11-24 14:43:45 +01001554 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001555
1556}
1557
Derek Allard2067d1a2008-11-13 22:59:24 +00001558/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001559/* Location: ./system/database/DB_driver.php */