blob: d326282c81eb84857603eade2bdc95290aa1b32a [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';
Andrey Andreevfbba54e2012-06-23 20:26:31 +030049 public $subdriver;
Andrey Andreevd1add432012-03-06 20:26:01 +020050 public $dbprefix = '';
51 public $char_set = 'utf8';
52 public $dbcollat = 'utf8_general_ci';
53 public $autoinit = TRUE; // Whether to automatically initialize the DB
54 public $swap_pre = '';
55 public $port = '';
56 public $pconnect = FALSE;
57 public $conn_id = FALSE;
58 public $result_id = FALSE;
59 public $db_debug = FALSE;
60 public $benchmark = 0;
61 public $query_count = 0;
62 public $bind_marker = '?';
63 public $save_queries = TRUE;
64 public $queries = array();
65 public $query_times = array();
66 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000067
Andrey Andreevd1add432012-03-06 20:26:01 +020068 public $trans_enabled = TRUE;
69 public $trans_strict = TRUE;
70 protected $_trans_depth = 0;
71 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000072
Andrey Andreevd1add432012-03-06 20:26:01 +020073 public $cache_on = FALSE;
74 public $cachedir = '';
75 public $cache_autodel = FALSE;
76 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020077
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000078 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020079 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
80
Timothy Warrene45518d2012-03-06 07:38:00 -050081 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
83 if (is_array($params))
84 {
85 foreach ($params as $key => $val)
86 {
87 $this->$key = $val;
88 }
89 }
90
91 log_message('debug', 'Database Driver Class Initialized');
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Gints Murans89f77ee2012-05-23 00:23:50 +030094 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -040095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 /**
97 * Initialize Database Settings
98 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +020099 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200100 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200101 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200103 /* If an established connection is available, then there's
104 * no need to connect and select the database.
105 *
106 * Depending on the database driver, conn_id can be either
107 * boolean TRUE, a resource or an object.
108 */
109 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 return TRUE;
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100117 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000118
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200119 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 if ( ! $this->conn_id)
121 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100122 // Check if there is a failover set
123 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100125 // Go over all the failovers
126 foreach ($this->failover as $failover)
127 {
128 // Replace the current settings with those of the failover
129 foreach ($failover as $key => $val)
130 {
131 $this->$key = $val;
132 }
133
134 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100135 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100136
137 // If a connection is made break the foreach loop
138 if ($this->conn_id)
139 {
140 break;
141 }
142 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100144
145 // We still don't have a connection?
146 if ( ! $this->conn_id)
147 {
148 log_message('error', 'Unable to connect to the database');
149
150 if ($this->db_debug)
151 {
152 $this->display_error('db_unable_to_connect');
153 }
154 return FALSE;
155 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157
158 // ----------------------------------------------------------------
159
160 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200161 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200163 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200164
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200165 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200167 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200169 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200172 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200173 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 // --------------------------------------------------------------------
177
178 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200179 * Reconnect
180 *
181 * Keep / reestablish the db connection if no queries have been
182 * sent for a length of time exceeding the server's idle timeout.
183 *
184 * This is just a dummy method to allow drivers without such
185 * functionality to not declare it, while others will override it.
186 *
187 * @return void
188 */
189 public function reconnect()
190 {
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300196 * Select database
197 *
198 * This is just a dummy method to allow drivers without such
199 * functionality to not declare it, while others will override it.
200 *
201 * @return bool
202 */
203 public function db_select()
204 {
205 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Set client character set
212 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200214 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200216 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200218 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200220 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 if ($this->db_debug)
223 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200224 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 return FALSE;
228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 return TRUE;
231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // --------------------------------------------------------------------
234
235 /**
236 * The name of the platform in use (mysql, mssql, etc...)
237 *
Barry Mienydd671972010-10-04 16:33:58 +0200238 * @return string
239 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500240 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 return $this->dbdriver;
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200248 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200250 * Returns a string containing the version of the database being used.
251 * Most drivers will override this method.
252 *
Barry Mienydd671972010-10-04 16:33:58 +0200253 * @return string
254 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200255 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200257 if (isset($this->data_cache['version']))
258 {
259 return $this->data_cache['version'];
260 }
261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 if (FALSE === ($sql = $this->_version()))
263 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200264 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Derek Allard3683f772009-12-16 17:32:33 +0000266
Andrey Andreev08856b82012-03-03 03:19:28 +0200267 $query = $this->query($sql);
268 $query = $query->row();
269 return $this->data_cache['version'] = $query->ver;
270 }
Derek Allard3683f772009-12-16 17:32:33 +0000271
Andrey Andreev08856b82012-03-03 03:19:28 +0200272 // --------------------------------------------------------------------
273
274 /**
275 * Version number query string
276 *
277 * @return string
278 */
279 protected function _version()
280 {
281 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // --------------------------------------------------------------------
285
286 /**
287 * Execute the query
288 *
289 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200290 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * upon successful execution of a "write" type query. Returns boolean
292 * FALSE upon failure, and if the $db_debug variable is set to TRUE
293 * will raise an error.
294 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @param string An SQL query string
296 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200297 * @return mixed
298 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500299 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100301 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200303 log_message('error', 'Invalid query: '.$sql);
304
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200305 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307
308 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100309 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600310 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200311 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Derek Jonese7792202010-03-02 17:24:46 -0600313
Ryan Dialef7474c2012-03-01 16:11:36 -0500314 // Compile binds if needed
315 if ($binds !== FALSE)
316 {
317 $sql = $this->compile_binds($sql, $binds);
318 }
319
Andrey Andreev4c202602012-03-06 20:34:52 +0200320 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // we will load the caching class and return the previously
322 // cached query if it exists
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100323 if ($this->cache_on === TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200325 $this->load_rdriver();
326 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200328 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Jones37f4b9c2011-07-01 17:56:50 -0500332 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100333 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 $this->queries[] = $sql;
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200339 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // Run the Query
342 if (FALSE === ($this->result_id = $this->simple_query($sql)))
343 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100344 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 $this->query_times[] = 0;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // This will trigger a rollback if transactions are being used
350 $this->_trans_status = FALSE;
351
Andrey Andreev4be5de12012-03-02 15:45:41 +0200352 // Grab the error now, as we might run some additional queries before displaying the error
353 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200354
355 // Log errors
Michiel Vugteveen857b00f2012-06-15 15:26:01 +0200356 log_message('error', 'Query error: '.$error['message'] . ' - Invalid query: ' . $sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 if ($this->db_debug)
359 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200361 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200362 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // transactions to remain in limbo.
364 $this->trans_complete();
365
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200366 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200367 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 return FALSE;
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200374 $time_end = microtime(TRUE);
375 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100377 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
dixyab3cab52012-04-21 00:58:00 +0200379 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // Increment the query counter
383 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // Was the query a "write" type?
386 // If so we'll simply return true
387 if ($this->is_write_type($sql) === TRUE)
388 {
389 // If caching is enabled we'll auto-cleanup any
390 // existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100391 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 $this->CACHE->delete();
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 return TRUE;
397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // Return TRUE if we don't need to create a result object
400 // Currently only the Oracle driver uses this when stored
401 // procedures are used
402 if ($return_object !== TRUE)
403 {
404 return TRUE;
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
407 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200408 $driver = $this->load_rdriver();
409 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200410
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200411 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100413 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 // We'll create a new instance of the result object
416 // only without the platform specific driver since
417 // we can't use it with cached data (the query result
418 // resource ID won't be any good once we've cached the
419 // result object, so we'll have to compile the data
420 // and save it)
421 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 $CR->result_object = $RES->result_object();
423 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200424 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // Reset these since cached objects can not utilize resource IDs.
427 $CR->conn_id = NULL;
428 $CR->result_id = NULL;
429
430 $this->CACHE->write($sql, $CR);
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 return $RES;
434 }
435
436 // --------------------------------------------------------------------
437
438 /**
439 * Load the result drivers
440 *
Barry Mienydd671972010-10-04 16:33:58 +0200441 * @return string the name of the result class
442 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500443 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
445 $driver = 'CI_DB_'.$this->dbdriver.'_result';
446
447 if ( ! class_exists($driver))
448 {
Greg Aker3a746652011-04-19 10:59:47 -0500449 include_once(BASEPATH.'database/DB_result.php');
450 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 return $driver;
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // --------------------------------------------------------------------
457
458 /**
459 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200460 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * we only use it when running transaction commands since they do
462 * not require all the features of the main query() function.
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200465 * @return mixed
466 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500467 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 if ( ! $this->conn_id)
470 {
471 $this->initialize();
472 }
473
474 return $this->_execute($sql);
475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // --------------------------------------------------------------------
478
479 /**
480 * Disable Transactions
481 * This permits transactions to be disabled at run-time.
482 *
Barry Mienydd671972010-10-04 16:33:58 +0200483 * @return void
484 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500485 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 $this->trans_enabled = FALSE;
488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Enable/disable Transaction Strict Mode
494 * When strict mode is enabled, if you are running multiple groups of
495 * transactions, if one group fails all groups will be rolled back.
496 * If strict mode is disabled, each group is treated autonomously, meaning
497 * a failure of one group will not affect any others
498 *
Barry Mienydd671972010-10-04 16:33:58 +0200499 * @return void
500 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500501 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
503 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // --------------------------------------------------------------------
507
508 /**
509 * Start Transaction
510 *
Barry Mienydd671972010-10-04 16:33:58 +0200511 * @return void
512 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500513 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200514 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 if ( ! $this->trans_enabled)
516 {
517 return FALSE;
518 }
519
520 // When transactions are nested we only begin/commit/rollback the outermost ones
521 if ($this->_trans_depth > 0)
522 {
523 $this->_trans_depth += 1;
524 return;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500528 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
530
531 // --------------------------------------------------------------------
532
533 /**
534 * Complete Transaction
535 *
Barry Mienydd671972010-10-04 16:33:58 +0200536 * @return bool
537 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500538 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
540 if ( ! $this->trans_enabled)
541 {
542 return FALSE;
543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 // When transactions are nested we only begin/commit/rollback the outermost ones
546 if ($this->_trans_depth > 1)
547 {
548 $this->_trans_depth -= 1;
549 return TRUE;
550 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500551 else
552 {
553 $this->_trans_depth = 0;
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // The query() function will set this flag to FALSE in the event that a query failed
557 if ($this->_trans_status === FALSE)
558 {
559 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 // If we are NOT running in strict mode, we will reset
562 // the _trans_status flag so that subsequent groups of transactions
563 // will be permitted.
564 if ($this->trans_strict === FALSE)
565 {
566 $this->_trans_status = TRUE;
567 }
568
569 log_message('debug', 'DB Transaction Failure');
570 return FALSE;
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 $this->trans_commit();
574 return TRUE;
575 }
576
577 // --------------------------------------------------------------------
578
579 /**
580 * Lets you retrieve the transaction flag to determine if it has failed
581 *
Barry Mienydd671972010-10-04 16:33:58 +0200582 * @return bool
583 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500584 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 return $this->_trans_status;
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
592 * Compile Bindings
593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @param string the sql statement
595 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200596 * @return string
597 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500598 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300600 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 return $sql;
603 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300604 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300606 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300607 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300609 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200610 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300611 // Make sure we're using numeric keys
612 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300613 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
615
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300616 // We'll need the marker length later
617 $ml = strlen($this->bind_marker);
618
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300619 // Make sure not to replace a chunk inside a string that happens to match the bind marker
620 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300622 $c = preg_match_all('/'.preg_quote($this->bind_marker).'/i',
623 str_replace($matches[0],
624 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
625 $sql, $c),
626 $matches, PREG_OFFSET_CAPTURE);
627
628 // Bind values' count must match the count of markers in the query
629 if ($bind_count !== $c)
630 {
631 return $sql;
632 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300633 }
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300634 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 +0300635 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300636 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 }
638
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300639 do
640 {
641 $c--;
642 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
643 }
644 while ($c !== 0);
645
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300646 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 // --------------------------------------------------------------------
650
651 /**
652 * Determines if a query is a "write" type.
653 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200655 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200656 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200657 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300659 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 +0000660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // --------------------------------------------------------------------
663
664 /**
665 * Calculate the aggregate query elapsed time
666 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200667 * @param int The number of decimal places
668 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200669 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500670 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 return number_format($this->benchmark, $decimals);
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // --------------------------------------------------------------------
676
677 /**
678 * Returns the total number of queries
679 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200680 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200681 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500682 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 return $this->query_count;
685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 // --------------------------------------------------------------------
688
689 /**
690 * Returns the last query that was executed
691 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200692 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200693 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500694 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
696 return end($this->queries);
697 }
698
699 // --------------------------------------------------------------------
700
701 /**
702 * "Smart" Escape String
703 *
704 * Escapes data based on type
705 * Sets boolean and null types
706 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200708 * @return mixed
709 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500710 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200711 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500712 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200714 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000715 }
716 elseif (is_bool($str))
717 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200718 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000719 }
720 elseif (is_null($str))
721 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200722 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000723 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000724
725 return $str;
726 }
727
728 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600729
Derek Jonese4ed5832009-02-20 21:44:59 +0000730 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000731 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000732 *
733 * Calls the individual driver for platform
734 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200735 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000736 * @param string
737 * @return mixed
738 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500739 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200740 {
741 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000742 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000743
Derek Jonese4ed5832009-02-20 21:44:59 +0000744 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 /**
747 * Primary
748 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200749 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * position is the primary key
751 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200753 * @return string
754 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500755 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200756 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200758 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
760
761 // --------------------------------------------------------------------
762
763 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300764 * "Count All" query
765 *
766 * Generates a platform-specific query string that counts all records in
767 * the specified database
768 *
769 * @param string
770 * @return int
771 */
772 public function count_all($table = '')
773 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100774 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300775 {
776 return 0;
777 }
778
779 $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 +0100780 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300781 {
782 return 0;
783 }
784
785 $query = $query->row();
786 $this->_reset_select();
787 return (int) $query->numrows;
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 * Returns an array of table names
794 *
Barry Mienydd671972010-10-04 16:33:58 +0200795 * @return array
796 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500797 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 {
799 // Is there a cached result?
800 if (isset($this->data_cache['table_names']))
801 {
802 return $this->data_cache['table_names'];
803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
806 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200807 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
809
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200810 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200812
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200813 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200815 // Do we know from which column to get the table name?
816 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300818 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200819 {
820 $key = 'table_name';
821 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300822 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200823 {
824 $key = 'TABLE_NAME';
825 }
826 else
827 {
828 /* We have no other choice but to just get the first element's key.
829 * Due to array_shift() accepting it's argument by reference, if
830 * E_STRICT is on, this would trigger a warning. So we'll have to
831 * assign it first.
832 */
833 $key = array_keys($row);
834 $key = array_shift($key);
835 }
Taufan Aditya18209332012-02-09 16:07:27 +0700836 }
837
Andrey Andreev12567e82012-01-25 22:50:33 +0200838 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 return $this->data_cache['table_names'];
842 }
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 // --------------------------------------------------------------------
845
846 /**
847 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500848 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200849 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500851 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200852 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200853 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // --------------------------------------------------------------------
857
858 /**
859 * Fetch MySQL Field Names
860 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200862 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500864 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
866 // Is there a cached result?
867 if (isset($this->data_cache['field_names'][$table]))
868 {
869 return $this->data_cache['field_names'][$table];
870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100872 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200874 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Greg Aker1edde302010-01-26 00:17:01 +0000877 if (FALSE === ($sql = $this->_list_columns($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_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200883 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200884
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500885 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200887 // Do we know from where to get the column's name?
888 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300890 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200891 {
892 $key = 'column_name';
893 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300894 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200895 {
896 $key = 'COLUMN_NAME';
897 }
898 else
899 {
900 /* We have no other choice but to just get the first element's key.
901 * Due to array_shift() accepting it's argument by reference, if
902 * E_STRICT is on, this would trigger a warning. So we'll have to
903 * assign it first.
904 */
905 $key = array_keys($row);
906 $key = array_shift($key);
907 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200909
910 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 return $this->data_cache['field_names'][$table];
914 }
915
916 // --------------------------------------------------------------------
917
918 /**
919 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500920 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @param string
922 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200923 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500925 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200926 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200927 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 }
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 // --------------------------------------------------------------------
931
932 /**
933 * Returns an object with field data
934 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200936 * @return object
937 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500938 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100940 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200942 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200945 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200947 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000948
949 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300952 * Escape the SQL Identifiers
953 *
954 * This function escapes column and table names
955 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300956 * @param mixed
957 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300958 */
959 public function escape_identifiers($item)
960 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100961 if ($this->_escape_char === '')
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300962 {
963 return $item;
964 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300965 elseif (is_array($item))
966 {
967 foreach ($item as $key => $value)
968 {
969 $item[$key] = $this->escape_identifiers($value);
970 }
971
972 return $item;
973 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300974
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300975 static $preg_ec = array();
976
977 if (empty($preg_ec))
978 {
979 if (is_array($this->_escape_char))
980 {
981 $preg_ec = array(preg_quote($this->_escape_char[0]), preg_quote($this->_escape_char[1]));
982 }
983 else
984 {
985 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
986 }
987 }
988
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300989 foreach ($this->_reserved_identifiers as $id)
990 {
991 if (strpos($item, '.'.$id) !== FALSE)
992 {
Andrey Andreeva593c692012-06-08 17:50:26 +0300993 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 +0300994 }
995 }
996
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300997 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 +0300998 }
999
1000 // --------------------------------------------------------------------
1001
1002 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 * Generate an insert string
1004 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 * @param string the table upon which the query will be performed
1006 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001007 * @return string
1008 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001009 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001011 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001012
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001013 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001015 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 $values[] = $this->escape($val);
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001019 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001020 }
1021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 // --------------------------------------------------------------------
1023
1024 /**
1025 * Generate an update string
1026 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 * @param string the table upon which the query will be performed
1028 * @param array an associative array data of key/values
1029 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001030 * @return string
1031 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001032 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001034 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001036 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 }
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001040 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001042 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
1044
1045 if ( ! is_array($where))
1046 {
1047 $dest = array($where);
1048 }
1049 else
1050 {
1051 $dest = array();
1052 foreach ($where as $key => $val)
1053 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001054 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001055 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 if ($val !== '')
1058 {
1059 if ( ! $this->_has_operator($key))
1060 {
1061 $key .= ' =';
1062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 $val = ' '.$this->escape($val);
1065 }
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 $dest[] = $prefix.$key.$val;
1068 }
Barry Mienydd671972010-10-04 16:33:58 +02001069 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001070
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001071 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001072 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001073
1074 // --------------------------------------------------------------------
1075
1076 /**
1077 * Tests whether the string has an SQL operator
1078 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * @param string
1080 * @return bool
1081 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001082 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 {
Andrey Andreevd454f0e2012-06-10 14:51:04 +03001084 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL|BETWEEN)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
1086
1087 // --------------------------------------------------------------------
1088
1089 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001090 * Returns the SQL string operator
1091 *
1092 * @param string
1093 * @return string
1094 */
1095 protected function _get_operator($str)
1096 {
1097 return preg_match('/(=|!|<|>| IS NULL| IS NOT NULL| BETWEEN)/i', $str, $match)
1098 ? $match[1] : FALSE;
1099 }
1100
1101 // --------------------------------------------------------------------
1102
1103 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1105 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 * @param string the function name
1107 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001108 * @return mixed
1109 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001110 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001112 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 if (FALSE === strpos($driver, $function))
1115 {
1116 $function = $driver.$function;
1117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 if ( ! function_exists($function))
1120 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001121 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001123
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001124 return (func_num_args() > 1)
1125 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1126 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 }
1128
1129 // --------------------------------------------------------------------
1130
1131 /**
1132 * Set Cache Directory Path
1133 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 * @param string the path to the cache directory
1135 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001136 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001137 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
1139 $this->cachedir = $path;
1140 }
1141
1142 // --------------------------------------------------------------------
1143
1144 /**
1145 * Enable Query Caching
1146 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001147 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001148 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001149 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001151 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 }
1153
1154 // --------------------------------------------------------------------
1155
1156 /**
1157 * Disable Query Caching
1158 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001159 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001160 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001161 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001163 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166
1167 // --------------------------------------------------------------------
1168
1169 /**
1170 * Delete the cache files associated with a particular URI
1171 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001172 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001173 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001174 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001176 return ($this->_cache_init())
1177 ? $this->CACHE->delete($segment_one, $segment_two)
1178 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
1180
1181 // --------------------------------------------------------------------
1182
1183 /**
1184 * Delete All cache files
1185 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001186 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001187 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001188 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001190 return ($this->_cache_init())
1191 ? $this->CACHE->delete_all()
1192 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
1194
1195 // --------------------------------------------------------------------
1196
1197 /**
1198 * Initialize the Cache Class
1199 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001200 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001201 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001202 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001204 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001206 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001207 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001208 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001209 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001211 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1212 {
1213 return $this->cache_off();
1214 }
Derek Allarde37ab382009-02-03 16:13:57 +00001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1217 return TRUE;
1218 }
1219
1220 // --------------------------------------------------------------------
1221
1222 /**
1223 * Close DB Connection
1224 *
Barry Mienydd671972010-10-04 16:33:58 +02001225 * @return void
1226 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001227 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001229 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001231 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001232 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 }
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 // --------------------------------------------------------------------
1237
1238 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001239 * Close DB Connection
1240 *
1241 * This method would be overriden by most of the drivers.
1242 *
1243 * @return void
1244 */
1245 protected function _close()
1246 {
1247 $this->conn_id = FALSE;
1248 }
1249
1250 // --------------------------------------------------------------------
1251
1252 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 * Display an error message
1254 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 * @param string the error message
1256 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001257 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001258 * @return string sends the application/error_db.php template
1259 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001260 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
Derek Jonese7792202010-03-02 17:24:46 -06001262 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 $LANG->load('db');
1264
1265 $heading = $LANG->line('db_error_heading');
1266
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001267 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001269 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 }
1271 else
1272 {
1273 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275
Pascal Kriete60f8c392010-08-25 18:03:28 +02001276 // Find the most likely culprit of the error by going through
1277 // the backtrace until the source file is no longer in the
1278 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001279 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001280 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001281 {
1282 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1283 {
1284 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001285 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001286 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001287 break;
1288 }
1289 }
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Jonese7792202010-03-02 17:24:46 -06001291 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 echo $error->show_error($heading, $message, 'error_db');
1293 exit;
1294 }
1295
1296 // --------------------------------------------------------------------
1297
1298 /**
1299 * Protect Identifiers
1300 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001301 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001302 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001304 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001305 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 *
1307 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1308 *
1309 * Or a query with aliasing:
1310 *
1311 * SELECT m.member_id, m.member_name FROM members AS m
1312 *
1313 * Since the column name can include up to four segments (host, DB, table, column)
1314 * or also have an alias prefix, we need to do a bit of work to figure this out and
1315 * insert the table prefix (if it exists) in the proper position, and escape only
1316 * the correct identifiers.
1317 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 * @param string
1319 * @param bool
1320 * @param mixed
1321 * @param bool
1322 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001323 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001324 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 {
1326 if ( ! is_bool($protect_identifiers))
1327 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001328 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 }
Derek Allarde37ab382009-02-03 16:13:57 +00001330
1331 if (is_array($item))
1332 {
1333 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001334 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001335 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001336 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001337 }
1338
1339 return $escaped_array;
1340 }
1341
Derek Allard911d3e02008-12-15 14:08:35 +00001342 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001343 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001344 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001345 // way to deal with this, but I'm not thinking of it -- Rick
1346 if (strpos($item, '(') !== FALSE)
1347 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001348 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001349 }
1350
Andrey Andreevbf940582012-06-10 07:05:05 +03001351 // Convert tabs or multiple spaces into single spaces
1352 $item = preg_replace('/\s+/', ' ', $item);
1353
Andrey Andreevbf940582012-06-10 07:05:05 +03001354 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001355 // Note: strripos() is used in order to support spaces in table names
1356 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001357 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001358 $alias = ($protect_identifiers)
1359 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1360 : substr($item, $offset);
1361 $item = substr($item, 0, $offset);
1362 }
1363 elseif ($offset = strrpos($item, ' '))
1364 {
1365 $alias = ($protect_identifiers)
1366 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1367 : substr($item, $offset);
1368 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001369 }
1370 else
1371 {
1372 $alias = '';
1373 }
1374
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 // Break the string apart if it contains periods, then insert the table prefix
1376 // in the correct location, assuming the period doesn't indicate that we're dealing
1377 // with an alias. While we're at it, we will escape the components
1378 if (strpos($item, '.') !== FALSE)
1379 {
1380 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001381
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001383 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001385 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001386 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 if ($protect_identifiers === TRUE)
1388 {
1389 foreach ($parts as $key => $val)
1390 {
1391 if ( ! in_array($val, $this->_reserved_identifiers))
1392 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001393 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 }
1395 }
Barry Mienydd671972010-10-04 16:33:58 +02001396
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001398 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001399
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 return $item.$alias;
1401 }
Barry Mienydd671972010-10-04 16:33:58 +02001402
Andrey Andreev4c202602012-03-06 20:34:52 +02001403 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001404 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 {
1406 // We now add the table prefix based on some logic.
1407 // Do we have 4 segments (hostname.database.table.column)?
1408 // If so, we add the table prefix to the column name in the 3rd segment.
1409 if (isset($parts[3]))
1410 {
1411 $i = 2;
1412 }
1413 // Do we have 3 segments (database.table.column)?
1414 // If so, we add the table prefix to the column name in 2nd position
1415 elseif (isset($parts[2]))
1416 {
1417 $i = 1;
1418 }
1419 // Do we have 2 segments (table.column)?
1420 // If so, we add the table prefix to the column name in 1st segment
1421 else
1422 {
1423 $i = 0;
1424 }
Barry Mienydd671972010-10-04 16:33:58 +02001425
Derek Allard2067d1a2008-11-13 22:59:24 +00001426 // This flag is set when the supplied $item does not contain a field name.
1427 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001428 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 {
1430 $i++;
1431 }
Barry Mienydd671972010-10-04 16:33:58 +02001432
Derek Jones55acc8b2009-07-11 03:38:13 +00001433 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001434 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001435 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001436 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001437 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001439 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 {
1441 $parts[$i] = $this->dbprefix.$parts[$i];
1442 }
Barry Mienydd671972010-10-04 16:33:58 +02001443
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 // Put the parts back together
1445 $item = implode('.', $parts);
1446 }
Barry Mienydd671972010-10-04 16:33:58 +02001447
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 if ($protect_identifiers === TRUE)
1449 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001450 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 }
Barry Mienydd671972010-10-04 16:33:58 +02001452
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 return $item.$alias;
1454 }
1455
Andrey Andreev4c202602012-03-06 20:34:52 +02001456 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001457 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001459 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001460 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001461 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001462 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001463 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001465 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 {
1467 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001468 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 }
1470
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001471 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001473 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 }
Barry Mienydd671972010-10-04 16:33:58 +02001475
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 return $item.$alias;
1477 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001478
Túbal Martín511f2252011-11-24 14:43:45 +01001479 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001480
Túbal Martín511f2252011-11-24 14:43:45 +01001481 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001482 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001483 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001484 *
Túbal Martín511f2252011-11-24 14:43:45 +01001485 * @return void
1486 */
1487 protected function _reset_select()
1488 {
Túbal Martín511f2252011-11-24 14:43:45 +01001489 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001490
1491}
1492
Derek Allard2067d1a2008-11-13 22:59:24 +00001493/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001494/* Location: ./system/database/DB_driver.php */