blob: 380bbc3015195653c048b2687cd776fd032bee5b [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;
48 public $dbdriver = 'mysql';
49 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
Timothy Warrene45518d2012-03-06 07:38:00 -050080 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if (is_array($params))
83 {
84 foreach ($params as $key => $val)
85 {
86 $this->$key = $val;
87 }
88 }
89
90 log_message('debug', 'Database Driver Class Initialized');
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Gints Murans89f77ee2012-05-23 00:23:50 +030093 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -040094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 /**
96 * Initialize Database Settings
97 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +020098 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200100 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200102 /* If an established connection is available, then there's
103 * no need to connect and select the database.
104 *
105 * Depending on the database driver, conn_id can be either
106 * boolean TRUE, a resource or an object.
107 */
108 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
110 return TRUE;
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100116 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000117
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200118 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if ( ! $this->conn_id)
120 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100121 // Check if there is a failover set
122 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100124 // Go over all the failovers
125 foreach ($this->failover as $failover)
126 {
127 // Replace the current settings with those of the failover
128 foreach ($failover as $key => $val)
129 {
130 $this->$key = $val;
131 }
132
133 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100134 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100135
136 // If a connection is made break the foreach loop
137 if ($this->conn_id)
138 {
139 break;
140 }
141 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100143
144 // We still don't have a connection?
145 if ( ! $this->conn_id)
146 {
147 log_message('error', 'Unable to connect to the database');
148
149 if ($this->db_debug)
150 {
151 $this->display_error('db_unable_to_connect');
152 }
153 return FALSE;
154 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // ----------------------------------------------------------------
158
159 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200160 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200163
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200166 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200171 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200172 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 // --------------------------------------------------------------------
176
177 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200178 * Reconnect
179 *
180 * Keep / reestablish the db connection if no queries have been
181 * sent for a length of time exceeding the server's idle timeout.
182 *
183 * This is just a dummy method to allow drivers without such
184 * functionality to not declare it, while others will override it.
185 *
186 * @return void
187 */
188 public function reconnect()
189 {
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300195 * Select database
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 bool
201 */
202 public function db_select()
203 {
204 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Set client character set
211 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200213 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200215 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200217 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200219 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 if ($this->db_debug)
222 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200223 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 return FALSE;
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 return TRUE;
230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
233
234 /**
235 * The name of the platform in use (mysql, mssql, etc...)
236 *
Barry Mienydd671972010-10-04 16:33:58 +0200237 * @return string
238 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500239 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 return $this->dbdriver;
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200247 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200249 * Returns a string containing the version of the database being used.
250 * Most drivers will override this method.
251 *
Barry Mienydd671972010-10-04 16:33:58 +0200252 * @return string
253 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200254 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200256 if (isset($this->data_cache['version']))
257 {
258 return $this->data_cache['version'];
259 }
260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 if (FALSE === ($sql = $this->_version()))
262 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200263 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
Derek Allard3683f772009-12-16 17:32:33 +0000265
Andrey Andreev08856b82012-03-03 03:19:28 +0200266 $query = $this->query($sql);
267 $query = $query->row();
268 return $this->data_cache['version'] = $query->ver;
269 }
Derek Allard3683f772009-12-16 17:32:33 +0000270
Andrey Andreev08856b82012-03-03 03:19:28 +0200271 // --------------------------------------------------------------------
272
273 /**
274 * Version number query string
275 *
276 * @return string
277 */
278 protected function _version()
279 {
280 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
284
285 /**
286 * Execute the query
287 *
288 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200289 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * upon successful execution of a "write" type query. Returns boolean
291 * FALSE upon failure, and if the $db_debug variable is set to TRUE
292 * will raise an error.
293 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @param string An SQL query string
295 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200296 * @return mixed
297 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300298 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100300 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200302 log_message('error', 'Invalid query: '.$sql);
303
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200304 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300306 elseif ( ! is_bool($return_object))
307 {
308 $return_object = ! $this->is_write_type($sql);
309 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000310
311 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100312 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600313 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200314 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
Derek Jonese7792202010-03-02 17:24:46 -0600316
Ryan Dialef7474c2012-03-01 16:11:36 -0500317 // Compile binds if needed
318 if ($binds !== FALSE)
319 {
320 $sql = $this->compile_binds($sql, $binds);
321 }
322
Andrey Andreev4c202602012-03-06 20:34:52 +0200323 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // we will load the caching class and return the previously
325 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300326 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200328 $this->load_rdriver();
329 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200331 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Andrey Andreevb66664b2012-06-27 14:22:10 +0300335 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100336 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $this->queries[] = $sql;
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200342 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // Run the Query
345 if (FALSE === ($this->result_id = $this->simple_query($sql)))
346 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100347 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 $this->query_times[] = 0;
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // This will trigger a rollback if transactions are being used
353 $this->_trans_status = FALSE;
354
Andrey Andreev4be5de12012-03-02 15:45:41 +0200355 // Grab the error now, as we might run some additional queries before displaying the error
356 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200357
358 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300359 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 if ($this->db_debug)
362 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200364 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200365 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // transactions to remain in limbo.
367 $this->trans_complete();
368
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200369 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200370 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 return FALSE;
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200377 $time_end = microtime(TRUE);
378 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000379
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100380 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
dixyab3cab52012-04-21 00:58:00 +0200382 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // Increment the query counter
386 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200387
Andrey Andreevb66664b2012-06-27 14:22:10 +0300388 // Will we have a result object instantiated? If not - we'll simply return TRUE
389 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300391 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100392 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 $this->CACHE->delete();
395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 return TRUE;
398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 if ($return_object !== TRUE)
402 {
403 return TRUE;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
406 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200407 $driver = $this->load_rdriver();
408 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200409
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200410 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100412 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 // We'll create a new instance of the result object
415 // only without the platform specific driver since
416 // we can't use it with cached data (the query result
417 // resource ID won't be any good once we've cached the
418 // result object, so we'll have to compile the data
419 // and save it)
420 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 $CR->result_object = $RES->result_object();
422 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200423 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // Reset these since cached objects can not utilize resource IDs.
426 $CR->conn_id = NULL;
427 $CR->result_id = NULL;
428
429 $this->CACHE->write($sql, $CR);
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 return $RES;
433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Load the result drivers
439 *
Barry Mienydd671972010-10-04 16:33:58 +0200440 * @return string the name of the result class
441 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500442 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 $driver = 'CI_DB_'.$this->dbdriver.'_result';
445
446 if ( ! class_exists($driver))
447 {
Greg Aker3a746652011-04-19 10:59:47 -0500448 include_once(BASEPATH.'database/DB_result.php');
449 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 return $driver;
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
456
457 /**
458 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200459 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * we only use it when running transaction commands since they do
461 * not require all the features of the main query() function.
462 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200464 * @return mixed
465 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500466 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
468 if ( ! $this->conn_id)
469 {
470 $this->initialize();
471 }
472
473 return $this->_execute($sql);
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Disable Transactions
480 * This permits transactions to be disabled at run-time.
481 *
Barry Mienydd671972010-10-04 16:33:58 +0200482 * @return void
483 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500484 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
486 $this->trans_enabled = FALSE;
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Enable/disable Transaction Strict Mode
493 * When strict mode is enabled, if you are running multiple groups of
494 * transactions, if one group fails all groups will be rolled back.
495 * If strict mode is disabled, each group is treated autonomously, meaning
496 * a failure of one group will not affect any others
497 *
Barry Mienydd671972010-10-04 16:33:58 +0200498 * @return void
499 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500500 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
508 * Start Transaction
509 *
Barry Mienydd671972010-10-04 16:33:58 +0200510 * @return void
511 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500512 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200513 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 if ( ! $this->trans_enabled)
515 {
516 return FALSE;
517 }
518
519 // When transactions are nested we only begin/commit/rollback the outermost ones
520 if ($this->_trans_depth > 0)
521 {
522 $this->_trans_depth += 1;
523 return;
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500527 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529
530 // --------------------------------------------------------------------
531
532 /**
533 * Complete Transaction
534 *
Barry Mienydd671972010-10-04 16:33:58 +0200535 * @return bool
536 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500537 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 if ( ! $this->trans_enabled)
540 {
541 return FALSE;
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // When transactions are nested we only begin/commit/rollback the outermost ones
545 if ($this->_trans_depth > 1)
546 {
547 $this->_trans_depth -= 1;
548 return TRUE;
549 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500550 else
551 {
552 $this->_trans_depth = 0;
553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // The query() function will set this flag to FALSE in the event that a query failed
556 if ($this->_trans_status === FALSE)
557 {
558 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 // If we are NOT running in strict mode, we will reset
561 // the _trans_status flag so that subsequent groups of transactions
562 // will be permitted.
563 if ($this->trans_strict === FALSE)
564 {
565 $this->_trans_status = TRUE;
566 }
567
568 log_message('debug', 'DB Transaction Failure');
569 return FALSE;
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 $this->trans_commit();
573 return TRUE;
574 }
575
576 // --------------------------------------------------------------------
577
578 /**
579 * Lets you retrieve the transaction flag to determine if it has failed
580 *
Barry Mienydd671972010-10-04 16:33:58 +0200581 * @return bool
582 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500583 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 return $this->_trans_status;
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Compile Bindings
592 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 * @param string the sql statement
594 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200595 * @return string
596 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500597 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300599 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 return $sql;
602 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300603 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300605 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300606 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300608 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200609 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300610 // Make sure we're using numeric keys
611 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300612 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 }
614
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300615 // We'll need the marker length later
616 $ml = strlen($this->bind_marker);
617
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300618 // Make sure not to replace a chunk inside a string that happens to match the bind marker
619 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300621 $c = preg_match_all('/'.preg_quote($this->bind_marker).'/i',
622 str_replace($matches[0],
623 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
624 $sql, $c),
625 $matches, PREG_OFFSET_CAPTURE);
626
627 // Bind values' count must match the count of markers in the query
628 if ($bind_count !== $c)
629 {
630 return $sql;
631 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300632 }
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300633 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 +0300634 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300635 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
637
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300638 do
639 {
640 $c--;
641 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
642 }
643 while ($c !== 0);
644
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300645 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 // --------------------------------------------------------------------
649
650 /**
651 * Determines if a query is a "write" type.
652 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200654 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200655 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200656 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300658 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 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
662
663 /**
664 * Calculate the aggregate query elapsed time
665 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200666 * @param int The number of decimal places
667 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200668 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500669 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
671 return number_format($this->benchmark, $decimals);
672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 // --------------------------------------------------------------------
675
676 /**
677 * Returns the total number of queries
678 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200679 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200680 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500681 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 return $this->query_count;
684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 // --------------------------------------------------------------------
687
688 /**
689 * Returns the last query that was executed
690 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200691 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200692 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500693 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
695 return end($this->queries);
696 }
697
698 // --------------------------------------------------------------------
699
700 /**
701 * "Smart" Escape String
702 *
703 * Escapes data based on type
704 * Sets boolean and null types
705 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200707 * @return mixed
708 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500709 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200710 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500711 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200713 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000714 }
715 elseif (is_bool($str))
716 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200717 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000718 }
719 elseif (is_null($str))
720 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200721 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000722 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000723
724 return $str;
725 }
726
727 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600728
Derek Jonese4ed5832009-02-20 21:44:59 +0000729 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000730 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000731 *
732 * Calls the individual driver for platform
733 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200734 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000735 * @param string
736 * @return mixed
737 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500738 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200739 {
740 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000741 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000742
Derek Jonese4ed5832009-02-20 21:44:59 +0000743 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 /**
746 * Primary
747 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200748 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 * position is the primary key
750 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200752 * @return string
753 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500754 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200755 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200757 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
759
760 // --------------------------------------------------------------------
761
762 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300763 * "Count All" query
764 *
765 * Generates a platform-specific query string that counts all records in
766 * the specified database
767 *
768 * @param string
769 * @return int
770 */
771 public function count_all($table = '')
772 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100773 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300774 {
775 return 0;
776 }
777
778 $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 +0100779 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300780 {
781 return 0;
782 }
783
784 $query = $query->row();
785 $this->_reset_select();
786 return (int) $query->numrows;
787 }
788
789 // --------------------------------------------------------------------
790
791 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 * Returns an array of table names
793 *
Barry Mienydd671972010-10-04 16:33:58 +0200794 * @return array
795 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500796 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
798 // Is there a cached result?
799 if (isset($this->data_cache['table_names']))
800 {
801 return $this->data_cache['table_names'];
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
805 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200806 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 }
808
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200809 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200811
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200812 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200814 // Do we know from which column to get the table name?
815 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300817 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200818 {
819 $key = 'table_name';
820 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300821 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200822 {
823 $key = 'TABLE_NAME';
824 }
825 else
826 {
827 /* We have no other choice but to just get the first element's key.
828 * Due to array_shift() accepting it's argument by reference, if
829 * E_STRICT is on, this would trigger a warning. So we'll have to
830 * assign it first.
831 */
832 $key = array_keys($row);
833 $key = array_shift($key);
834 }
Taufan Aditya18209332012-02-09 16:07:27 +0700835 }
836
Andrey Andreev12567e82012-01-25 22:50:33 +0200837 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 }
839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 return $this->data_cache['table_names'];
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 // --------------------------------------------------------------------
844
845 /**
846 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500847 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200848 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500850 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200851 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200852 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 // --------------------------------------------------------------------
856
857 /**
858 * Fetch MySQL Field Names
859 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200861 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500863 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 {
865 // Is there a cached result?
866 if (isset($this->data_cache['field_names'][$table]))
867 {
868 return $this->data_cache['field_names'][$table];
869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100871 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200873 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Greg Aker1edde302010-01-26 00:17:01 +0000876 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200878 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200882 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200883
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500884 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200886 // Do we know from where to get the column's name?
887 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300889 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200890 {
891 $key = 'column_name';
892 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300893 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200894 {
895 $key = 'COLUMN_NAME';
896 }
897 else
898 {
899 /* We have no other choice but to just get the first element's key.
900 * Due to array_shift() accepting it's argument by reference, if
901 * E_STRICT is on, this would trigger a warning. So we'll have to
902 * assign it first.
903 */
904 $key = array_keys($row);
905 $key = array_shift($key);
906 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200908
909 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 return $this->data_cache['field_names'][$table];
913 }
914
915 // --------------------------------------------------------------------
916
917 /**
918 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500919 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * @param string
921 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200922 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500924 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200925 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200926 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 // --------------------------------------------------------------------
930
931 /**
932 * Returns an object with field data
933 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200935 * @return object
936 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500937 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100939 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200941 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200944 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200946 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000947
948 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300951 * Escape the SQL Identifiers
952 *
953 * This function escapes column and table names
954 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300955 * @param mixed
956 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300957 */
958 public function escape_identifiers($item)
959 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100960 if ($this->_escape_char === '')
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300961 {
962 return $item;
963 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300964 elseif (is_array($item))
965 {
966 foreach ($item as $key => $value)
967 {
968 $item[$key] = $this->escape_identifiers($value);
969 }
970
971 return $item;
972 }
Andrey Andreev7db0f592012-06-28 17:54:27 +0300973 // Avoid breaking functions inside queries
974 elseif (strpos($item, '(') !== FALSE)
975 {
976 return $item;
977 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300978
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300979 static $preg_ec = array();
980
981 if (empty($preg_ec))
982 {
983 if (is_array($this->_escape_char))
984 {
985 $preg_ec = array(preg_quote($this->_escape_char[0]), preg_quote($this->_escape_char[1]));
986 }
987 else
988 {
989 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
990 }
991 }
992
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300993 foreach ($this->_reserved_identifiers as $id)
994 {
995 if (strpos($item, '.'.$id) !== FALSE)
996 {
Andrey Andreeva593c692012-06-08 17:50:26 +0300997 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\./i', $preg_ec[0].'$1'.$preg_ec[1].'.', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300998 }
999 }
1000
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001001 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?(\.)?/i', $preg_ec[0].'$1'.$preg_ec[1].'$2', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001002 }
1003
1004 // --------------------------------------------------------------------
1005
1006 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 * Generate an insert string
1008 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 * @param string the table upon which the query will be performed
1010 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001011 * @return string
1012 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001013 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001015 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001016
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001017 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001019 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 $values[] = $this->escape($val);
1021 }
Barry Mienydd671972010-10-04 16:33:58 +02001022
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001023 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001024 }
1025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 // --------------------------------------------------------------------
1027
1028 /**
1029 * Generate an update string
1030 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 * @param string the table upon which the query will be performed
1032 * @param array an associative array data of key/values
1033 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001034 * @return string
1035 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001036 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001038 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001040 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 }
Barry Mienydd671972010-10-04 16:33:58 +02001042
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001044 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001046 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
1048
1049 if ( ! is_array($where))
1050 {
1051 $dest = array($where);
1052 }
1053 else
1054 {
1055 $dest = array();
1056 foreach ($where as $key => $val)
1057 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001058 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001059 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 if ($val !== '')
1062 {
1063 if ( ! $this->_has_operator($key))
1064 {
1065 $key .= ' =';
1066 }
Barry Mienydd671972010-10-04 16:33:58 +02001067
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 $val = ' '.$this->escape($val);
1069 }
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 $dest[] = $prefix.$key.$val;
1072 }
Barry Mienydd671972010-10-04 16:33:58 +02001073 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001074
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001075 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001076 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001077
1078 // --------------------------------------------------------------------
1079
1080 /**
1081 * Tests whether the string has an SQL operator
1082 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 * @param string
1084 * @return bool
1085 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001086 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 {
Andrey Andreevd454f0e2012-06-10 14:51:04 +03001088 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL|BETWEEN)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 }
1090
1091 // --------------------------------------------------------------------
1092
1093 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001094 * Returns the SQL string operator
1095 *
1096 * @param string
1097 * @return string
1098 */
1099 protected function _get_operator($str)
1100 {
1101 return preg_match('/(=|!|<|>| IS NULL| IS NOT NULL| BETWEEN)/i', $str, $match)
1102 ? $match[1] : FALSE;
1103 }
1104
1105 // --------------------------------------------------------------------
1106
1107 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1109 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 * @param string the function name
1111 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001112 * @return mixed
1113 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001114 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001116 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 if (FALSE === strpos($driver, $function))
1119 {
1120 $function = $driver.$function;
1121 }
Barry Mienydd671972010-10-04 16:33:58 +02001122
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 if ( ! function_exists($function))
1124 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001125 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001127
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001128 return (func_num_args() > 1)
1129 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1130 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 }
1132
1133 // --------------------------------------------------------------------
1134
1135 /**
1136 * Set Cache Directory Path
1137 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 * @param string the path to the cache directory
1139 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001140 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001141 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
1143 $this->cachedir = $path;
1144 }
1145
1146 // --------------------------------------------------------------------
1147
1148 /**
1149 * Enable Query Caching
1150 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001151 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001152 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001153 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001155 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
1157
1158 // --------------------------------------------------------------------
1159
1160 /**
1161 * Disable Query Caching
1162 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001163 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001164 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001165 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001167 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 }
Barry Mienydd671972010-10-04 16:33:58 +02001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170
1171 // --------------------------------------------------------------------
1172
1173 /**
1174 * Delete the cache files associated with a particular URI
1175 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001176 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001177 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001178 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001180 return ($this->_cache_init())
1181 ? $this->CACHE->delete($segment_one, $segment_two)
1182 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 }
1184
1185 // --------------------------------------------------------------------
1186
1187 /**
1188 * Delete All cache files
1189 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001190 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001191 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001192 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001194 return ($this->_cache_init())
1195 ? $this->CACHE->delete_all()
1196 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 }
1198
1199 // --------------------------------------------------------------------
1200
1201 /**
1202 * Initialize the Cache Class
1203 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001204 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001205 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001206 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001208 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001210 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001211 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001212 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001213 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001215 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1216 {
1217 return $this->cache_off();
1218 }
Derek Allarde37ab382009-02-03 16:13:57 +00001219
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1221 return TRUE;
1222 }
1223
1224 // --------------------------------------------------------------------
1225
1226 /**
1227 * Close DB Connection
1228 *
Barry Mienydd671972010-10-04 16:33:58 +02001229 * @return void
1230 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001231 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001233 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001235 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001236 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 // --------------------------------------------------------------------
1241
1242 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001243 * Close DB Connection
1244 *
1245 * This method would be overriden by most of the drivers.
1246 *
1247 * @return void
1248 */
1249 protected function _close()
1250 {
1251 $this->conn_id = FALSE;
1252 }
1253
1254 // --------------------------------------------------------------------
1255
1256 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 * Display an error message
1258 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 * @param string the error message
1260 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001261 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001262 * @return string sends the application/error_db.php template
1263 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001264 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 {
Derek Jonese7792202010-03-02 17:24:46 -06001266 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 $LANG->load('db');
1268
1269 $heading = $LANG->line('db_error_heading');
1270
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001271 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001273 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 }
1275 else
1276 {
1277 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1278 }
Barry Mienydd671972010-10-04 16:33:58 +02001279
Pascal Kriete60f8c392010-08-25 18:03:28 +02001280 // Find the most likely culprit of the error by going through
1281 // the backtrace until the source file is no longer in the
1282 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001283 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001284 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001285 {
1286 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1287 {
1288 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001289 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001290 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001291 break;
1292 }
1293 }
Barry Mienydd671972010-10-04 16:33:58 +02001294
Derek Jonese7792202010-03-02 17:24:46 -06001295 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 echo $error->show_error($heading, $message, 'error_db');
1297 exit;
1298 }
1299
1300 // --------------------------------------------------------------------
1301
1302 /**
1303 * Protect Identifiers
1304 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001305 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001306 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001308 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001309 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 *
1311 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1312 *
1313 * Or a query with aliasing:
1314 *
1315 * SELECT m.member_id, m.member_name FROM members AS m
1316 *
1317 * Since the column name can include up to four segments (host, DB, table, column)
1318 * or also have an alias prefix, we need to do a bit of work to figure this out and
1319 * insert the table prefix (if it exists) in the proper position, and escape only
1320 * the correct identifiers.
1321 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 * @param string
1323 * @param bool
1324 * @param mixed
1325 * @param bool
1326 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001327 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001328 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
1330 if ( ! is_bool($protect_identifiers))
1331 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001332 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 }
Derek Allarde37ab382009-02-03 16:13:57 +00001334
1335 if (is_array($item))
1336 {
1337 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001338 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001339 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001340 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001341 }
1342
1343 return $escaped_array;
1344 }
1345
Derek Allard911d3e02008-12-15 14:08:35 +00001346 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001347 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001348 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001349 // way to deal with this, but I'm not thinking of it -- Rick
1350 if (strpos($item, '(') !== FALSE)
1351 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001352 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001353 }
1354
Andrey Andreevbf940582012-06-10 07:05:05 +03001355 // Convert tabs or multiple spaces into single spaces
1356 $item = preg_replace('/\s+/', ' ', $item);
1357
Andrey Andreevbf940582012-06-10 07:05:05 +03001358 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001359 // Note: strripos() is used in order to support spaces in table names
1360 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001361 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001362 $alias = ($protect_identifiers)
1363 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1364 : substr($item, $offset);
1365 $item = substr($item, 0, $offset);
1366 }
1367 elseif ($offset = strrpos($item, ' '))
1368 {
1369 $alias = ($protect_identifiers)
1370 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1371 : substr($item, $offset);
1372 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001373 }
1374 else
1375 {
1376 $alias = '';
1377 }
1378
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 // Break the string apart if it contains periods, then insert the table prefix
1380 // in the correct location, assuming the period doesn't indicate that we're dealing
1381 // with an alias. While we're at it, we will escape the components
1382 if (strpos($item, '.') !== FALSE)
1383 {
1384 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001385
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001387 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001389 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001390 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 if ($protect_identifiers === TRUE)
1392 {
1393 foreach ($parts as $key => $val)
1394 {
1395 if ( ! in_array($val, $this->_reserved_identifiers))
1396 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001397 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 }
1399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001402 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001403
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 return $item.$alias;
1405 }
Barry Mienydd671972010-10-04 16:33:58 +02001406
Andrey Andreev4c202602012-03-06 20:34:52 +02001407 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001408 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 {
1410 // We now add the table prefix based on some logic.
1411 // Do we have 4 segments (hostname.database.table.column)?
1412 // If so, we add the table prefix to the column name in the 3rd segment.
1413 if (isset($parts[3]))
1414 {
1415 $i = 2;
1416 }
1417 // Do we have 3 segments (database.table.column)?
1418 // If so, we add the table prefix to the column name in 2nd position
1419 elseif (isset($parts[2]))
1420 {
1421 $i = 1;
1422 }
1423 // Do we have 2 segments (table.column)?
1424 // If so, we add the table prefix to the column name in 1st segment
1425 else
1426 {
1427 $i = 0;
1428 }
Barry Mienydd671972010-10-04 16:33:58 +02001429
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 // This flag is set when the supplied $item does not contain a field name.
1431 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001432 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 {
1434 $i++;
1435 }
Barry Mienydd671972010-10-04 16:33:58 +02001436
Derek Jones55acc8b2009-07-11 03:38:13 +00001437 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001438 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001439 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001440 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001441 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001443 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 {
1445 $parts[$i] = $this->dbprefix.$parts[$i];
1446 }
Barry Mienydd671972010-10-04 16:33:58 +02001447
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 // Put the parts back together
1449 $item = implode('.', $parts);
1450 }
Barry Mienydd671972010-10-04 16:33:58 +02001451
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 if ($protect_identifiers === TRUE)
1453 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001454 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 }
Barry Mienydd671972010-10-04 16:33:58 +02001456
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 return $item.$alias;
1458 }
1459
Andrey Andreev4c202602012-03-06 20:34:52 +02001460 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001461 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001463 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001464 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001465 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001466 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001467 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001469 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
1471 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001472 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 }
1474
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001475 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001477 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 }
Barry Mienydd671972010-10-04 16:33:58 +02001479
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 return $item.$alias;
1481 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001482
Túbal Martín511f2252011-11-24 14:43:45 +01001483 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001484
Túbal Martín511f2252011-11-24 14:43:45 +01001485 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001486 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001487 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001488 *
Túbal Martín511f2252011-11-24 14:43:45 +01001489 * @return void
1490 */
1491 protected function _reset_select()
1492 {
Túbal Martín511f2252011-11-24 14:43:45 +01001493 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001494
1495}
1496
Derek Allard2067d1a2008-11-13 22:59:24 +00001497/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001498/* Location: ./system/database/DB_driver.php */