blob: e8286aaa137e2a32092b9503728b9bb19a10a6fb [file] [log] [blame]
Andrey Andreev4c202602012-03-06 20:34:52 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Timothy Warren7eeda532012-03-19 16:01:55 -040041abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreev738f5342012-03-06 20:43:40 +020043 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
Andrey Andreev75546112012-07-05 11:01:29 +030048 public $dbdriver = 'mysqli';
Andrey Andreevfbba54e2012-06-23 20:26:31 +030049 public $subdriver;
Andrey Andreevd1add432012-03-06 20:26:01 +020050 public $dbprefix = '';
51 public $char_set = 'utf8';
52 public $dbcollat = 'utf8_general_ci';
53 public $autoinit = TRUE; // Whether to automatically initialize the DB
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030054 public $encrypt = FALSE;
Andrey Andreevd1add432012-03-06 20:26:01 +020055 public $swap_pre = '';
56 public $port = '';
57 public $pconnect = FALSE;
58 public $conn_id = FALSE;
59 public $result_id = FALSE;
60 public $db_debug = FALSE;
61 public $benchmark = 0;
62 public $query_count = 0;
63 public $bind_marker = '?';
64 public $save_queries = TRUE;
65 public $queries = array();
66 public $query_times = array();
67 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000068
Andrey Andreevd1add432012-03-06 20:26:01 +020069 public $trans_enabled = TRUE;
70 public $trans_strict = TRUE;
71 protected $_trans_depth = 0;
72 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000073
Andrey Andreevd1add432012-03-06 20:26:01 +020074 public $cache_on = FALSE;
75 public $cachedir = '';
76 public $cache_autodel = FALSE;
77 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020078
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000079 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020080 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
81
Andrey Andreev2ea33c32012-10-04 12:37:51 +030082 // clause and character used for LIKE escape sequences
83 protected $_like_escape_str = " ESCAPE '%s' ";
84 protected $_like_escape_chr = '!';
85
Andrey Andreev75546112012-07-05 11:01:29 +030086 /**
Andrey Andreev77a5d942012-07-05 15:42:14 +030087 * The syntax to count rows is slightly different across different
88 * database engines, so this string appears in each driver and is
89 * used for the count_all() and count_all_results() functions.
90 */
91 protected $_count_string = 'SELECT COUNT(*) AS ';
92
93 /**
Andrey Andreev75546112012-07-05 11:01:29 +030094 * Constructor
95 *
96 * @param array
97 * @return void
98 */
Timothy Warrene45518d2012-03-06 07:38:00 -050099 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 if (is_array($params))
102 {
103 foreach ($params as $key => $val)
104 {
105 $this->$key = $val;
106 }
107 }
108
109 log_message('debug', 'Database Driver Class Initialized');
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Gints Murans89f77ee2012-05-23 00:23:50 +0300112 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 /**
115 * Initialize Database Settings
116 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200117 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200118 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200119 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200121 /* If an established connection is available, then there's
122 * no need to connect and select the database.
123 *
124 * Depending on the database driver, conn_id can be either
125 * boolean TRUE, a resource or an object.
126 */
127 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
129 return TRUE;
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100135 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000136
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200137 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 if ( ! $this->conn_id)
139 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100140 // Check if there is a failover set
141 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100143 // Go over all the failovers
144 foreach ($this->failover as $failover)
145 {
146 // Replace the current settings with those of the failover
147 foreach ($failover as $key => $val)
148 {
149 $this->$key = $val;
150 }
151
152 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100153 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100154
155 // If a connection is made break the foreach loop
156 if ($this->conn_id)
157 {
158 break;
159 }
160 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100162
163 // We still don't have a connection?
164 if ( ! $this->conn_id)
165 {
166 log_message('error', 'Unable to connect to the database');
167
168 if ($this->db_debug)
169 {
170 $this->display_error('db_unable_to_connect');
171 }
172 return FALSE;
173 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 // ----------------------------------------------------------------
177
178 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200179 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200181 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200182
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200183 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200185 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200187 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200190 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200191 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // --------------------------------------------------------------------
195
196 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200197 * Reconnect
198 *
199 * Keep / reestablish the db connection if no queries have been
200 * sent for a length of time exceeding the server's idle timeout.
201 *
202 * This is just a dummy method to allow drivers without such
203 * functionality to not declare it, while others will override it.
204 *
205 * @return void
206 */
207 public function reconnect()
208 {
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300214 * Select database
215 *
216 * This is just a dummy method to allow drivers without such
217 * functionality to not declare it, while others will override it.
218 *
219 * @return bool
220 */
221 public function db_select()
222 {
223 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Set client character set
230 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200232 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200234 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200236 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200238 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 if ($this->db_debug)
241 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200242 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 return FALSE;
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 return TRUE;
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 // --------------------------------------------------------------------
252
253 /**
254 * The name of the platform in use (mysql, mssql, etc...)
255 *
Barry Mienydd671972010-10-04 16:33:58 +0200256 * @return string
257 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500258 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 return $this->dbdriver;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200266 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200268 * Returns a string containing the version of the database being used.
269 * Most drivers will override this method.
270 *
Barry Mienydd671972010-10-04 16:33:58 +0200271 * @return string
272 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200273 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200275 if (isset($this->data_cache['version']))
276 {
277 return $this->data_cache['version'];
278 }
279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 if (FALSE === ($sql = $this->_version()))
281 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200282 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
Derek Allard3683f772009-12-16 17:32:33 +0000284
Andrey Andreev08856b82012-03-03 03:19:28 +0200285 $query = $this->query($sql);
286 $query = $query->row();
287 return $this->data_cache['version'] = $query->ver;
288 }
Derek Allard3683f772009-12-16 17:32:33 +0000289
Andrey Andreev08856b82012-03-03 03:19:28 +0200290 // --------------------------------------------------------------------
291
292 /**
293 * Version number query string
294 *
295 * @return string
296 */
297 protected function _version()
298 {
299 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
303
304 /**
305 * Execute the query
306 *
307 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200308 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * upon successful execution of a "write" type query. Returns boolean
310 * FALSE upon failure, and if the $db_debug variable is set to TRUE
311 * will raise an error.
312 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300313 * @param string $sql
314 * @param array $binds = FALSE An array of binding data
315 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200316 * @return mixed
317 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300318 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100320 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200322 log_message('error', 'Invalid query: '.$sql);
323
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200324 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300326 elseif ( ! is_bool($return_object))
327 {
328 $return_object = ! $this->is_write_type($sql);
329 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000330
331 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100332 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600333 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200334 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Derek Jonese7792202010-03-02 17:24:46 -0600336
Ryan Dialef7474c2012-03-01 16:11:36 -0500337 // Compile binds if needed
338 if ($binds !== FALSE)
339 {
340 $sql = $this->compile_binds($sql, $binds);
341 }
342
Andrey Andreev4c202602012-03-06 20:34:52 +0200343 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // we will load the caching class and return the previously
345 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300346 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200348 $this->load_rdriver();
349 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200351 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Andrey Andreevb66664b2012-06-27 14:22:10 +0300355 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100356 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
358 $this->queries[] = $sql;
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200362 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // Run the Query
365 if (FALSE === ($this->result_id = $this->simple_query($sql)))
366 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100367 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 $this->query_times[] = 0;
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // This will trigger a rollback if transactions are being used
373 $this->_trans_status = FALSE;
374
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 // Grab the error now, as we might run some additional queries before displaying the error
376 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200377
378 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300379 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 if ($this->db_debug)
382 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200384 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200385 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // transactions to remain in limbo.
387 $this->trans_complete();
388
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200389 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200390 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 return FALSE;
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200397 $time_end = microtime(TRUE);
398 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000399
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100400 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
dixyab3cab52012-04-21 00:58:00 +0200402 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // Increment the query counter
406 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200407
Andrey Andreevb66664b2012-06-27 14:22:10 +0300408 // Will we have a result object instantiated? If not - we'll simply return TRUE
409 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300411 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100412 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 $this->CACHE->delete();
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 return TRUE;
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if ($return_object !== TRUE)
422 {
423 return TRUE;
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
426 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200427 $driver = $this->load_rdriver();
428 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200429
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200430 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100432 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 // We'll create a new instance of the result object
435 // only without the platform specific driver since
436 // we can't use it with cached data (the query result
437 // resource ID won't be any good once we've cached the
438 // result object, so we'll have to compile the data
439 // and save it)
440 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 $CR->result_object = $RES->result_object();
442 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200443 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // Reset these since cached objects can not utilize resource IDs.
446 $CR->conn_id = NULL;
447 $CR->result_id = NULL;
448
449 $this->CACHE->write($sql, $CR);
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 return $RES;
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Load the result drivers
459 *
Barry Mienydd671972010-10-04 16:33:58 +0200460 * @return string the name of the result class
461 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500462 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 $driver = 'CI_DB_'.$this->dbdriver.'_result';
465
466 if ( ! class_exists($driver))
467 {
Greg Aker3a746652011-04-19 10:59:47 -0500468 include_once(BASEPATH.'database/DB_result.php');
469 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 return $driver;
473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
478 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200479 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * we only use it when running transaction commands since they do
481 * not require all the features of the main query() function.
482 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200484 * @return mixed
485 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500486 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 if ( ! $this->conn_id)
489 {
490 $this->initialize();
491 }
492
493 return $this->_execute($sql);
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
497
498 /**
499 * Disable Transactions
500 * This permits transactions to be disabled at run-time.
501 *
Barry Mienydd671972010-10-04 16:33:58 +0200502 * @return void
503 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500504 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 $this->trans_enabled = FALSE;
507 }
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Enable/disable Transaction Strict Mode
513 * When strict mode is enabled, if you are running multiple groups of
514 * transactions, if one group fails all groups will be rolled back.
515 * If strict mode is disabled, each group is treated autonomously, meaning
516 * a failure of one group will not affect any others
517 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300518 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200519 * @return void
520 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500521 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // --------------------------------------------------------------------
527
528 /**
529 * Start Transaction
530 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300531 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200532 * @return void
533 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500534 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200535 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 if ( ! $this->trans_enabled)
537 {
538 return FALSE;
539 }
540
541 // When transactions are nested we only begin/commit/rollback the outermost ones
542 if ($this->_trans_depth > 0)
543 {
544 $this->_trans_depth += 1;
545 return;
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500549 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Complete Transaction
556 *
Barry Mienydd671972010-10-04 16:33:58 +0200557 * @return bool
558 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500559 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
561 if ( ! $this->trans_enabled)
562 {
563 return FALSE;
564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 // When transactions are nested we only begin/commit/rollback the outermost ones
567 if ($this->_trans_depth > 1)
568 {
569 $this->_trans_depth -= 1;
570 return TRUE;
571 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500572 else
573 {
574 $this->_trans_depth = 0;
575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 // The query() function will set this flag to FALSE in the event that a query failed
578 if ($this->_trans_status === FALSE)
579 {
580 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // If we are NOT running in strict mode, we will reset
583 // the _trans_status flag so that subsequent groups of transactions
584 // will be permitted.
585 if ($this->trans_strict === FALSE)
586 {
587 $this->_trans_status = TRUE;
588 }
589
590 log_message('debug', 'DB Transaction Failure');
591 return FALSE;
592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 $this->trans_commit();
595 return TRUE;
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Lets you retrieve the transaction flag to determine if it has failed
602 *
Barry Mienydd671972010-10-04 16:33:58 +0200603 * @return bool
604 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500605 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
607 return $this->_trans_status;
608 }
609
610 // --------------------------------------------------------------------
611
612 /**
613 * Compile Bindings
614 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @param string the sql statement
616 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200617 * @return string
618 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500619 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300621 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 return $sql;
624 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300625 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300627 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300628 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300630 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200631 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300632 // Make sure we're using numeric keys
633 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300634 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
636
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300637 // We'll need the marker length later
638 $ml = strlen($this->bind_marker);
639
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300640 // Make sure not to replace a chunk inside a string that happens to match the bind marker
641 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300643 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300644 str_replace($matches[0],
645 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
646 $sql, $c),
647 $matches, PREG_OFFSET_CAPTURE);
648
649 // Bind values' count must match the count of markers in the query
650 if ($bind_count !== $c)
651 {
652 return $sql;
653 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300654 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300655 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 +0300656 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300657 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 }
659
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300660 do
661 {
662 $c--;
663 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
664 }
665 while ($c !== 0);
666
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300667 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 // --------------------------------------------------------------------
671
672 /**
673 * Determines if a query is a "write" type.
674 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200676 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200677 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200678 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
Andrey Andreev9e31f8f2012-10-05 17:31:46 +0300680 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 // --------------------------------------------------------------------
684
685 /**
686 * Calculate the aggregate query elapsed time
687 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200688 * @param int The number of decimal places
689 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200690 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500691 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 return number_format($this->benchmark, $decimals);
694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // --------------------------------------------------------------------
697
698 /**
699 * Returns the total number of queries
700 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200701 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200702 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500703 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
705 return $this->query_count;
706 }
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 // --------------------------------------------------------------------
709
710 /**
711 * Returns the last query that was executed
712 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200713 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200714 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500715 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
717 return end($this->queries);
718 }
719
720 // --------------------------------------------------------------------
721
722 /**
723 * "Smart" Escape String
724 *
725 * Escapes data based on type
726 * Sets boolean and null types
727 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200729 * @return mixed
730 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500731 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200732 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500733 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200735 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000736 }
737 elseif (is_bool($str))
738 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200739 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000740 }
741 elseif (is_null($str))
742 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200743 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000744 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000745
746 return $str;
747 }
748
749 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600750
Derek Jonese4ed5832009-02-20 21:44:59 +0000751 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000752 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000753 *
754 * Calls the individual driver for platform
755 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200756 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000757 * @param string
758 * @return mixed
759 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500760 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200761 {
762 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000763 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000764
Derek Jonese4ed5832009-02-20 21:44:59 +0000765 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600766
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 /**
768 * Primary
769 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200770 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 * position is the primary key
772 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200774 * @return string
775 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500776 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200777 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200779 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
781
782 // --------------------------------------------------------------------
783
784 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300785 * "Count All" query
786 *
787 * Generates a platform-specific query string that counts all records in
788 * the specified database
789 *
790 * @param string
791 * @return int
792 */
793 public function count_all($table = '')
794 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100795 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300796 {
797 return 0;
798 }
799
800 $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 +0100801 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300802 {
803 return 0;
804 }
805
806 $query = $query->row();
807 $this->_reset_select();
808 return (int) $query->numrows;
809 }
810
811 // --------------------------------------------------------------------
812
813 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 * Returns an array of table names
815 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300816 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200817 * @return array
818 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500819 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
821 // Is there a cached result?
822 if (isset($this->data_cache['table_names']))
823 {
824 return $this->data_cache['table_names'];
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
828 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200829 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 }
831
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200832 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200834
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200835 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200837 // Do we know from which column to get the table name?
838 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300840 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200841 {
842 $key = 'table_name';
843 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300844 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200845 {
846 $key = 'TABLE_NAME';
847 }
848 else
849 {
850 /* We have no other choice but to just get the first element's key.
851 * Due to array_shift() accepting it's argument by reference, if
852 * E_STRICT is on, this would trigger a warning. So we'll have to
853 * assign it first.
854 */
855 $key = array_keys($row);
856 $key = array_shift($key);
857 }
Taufan Aditya18209332012-02-09 16:07:27 +0700858 }
859
Andrey Andreev12567e82012-01-25 22:50:33 +0200860 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 }
862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 return $this->data_cache['table_names'];
864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 // --------------------------------------------------------------------
867
868 /**
869 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500870 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300871 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +0200872 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500874 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200875 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200876 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
880
881 /**
Andrey Andreev75546112012-07-05 11:01:29 +0300882 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200885 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500887 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
889 // Is there a cached result?
890 if (isset($this->data_cache['field_names'][$table]))
891 {
892 return $this->data_cache['field_names'][$table];
893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100895 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200897 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Greg Aker1edde302010-01-26 00:17:01 +0000900 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200902 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200906 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200907
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500908 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200910 // Do we know from where to get the column's name?
911 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300913 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200914 {
915 $key = 'column_name';
916 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300917 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200918 {
919 $key = 'COLUMN_NAME';
920 }
921 else
922 {
923 /* We have no other choice but to just get the first element's key.
924 * Due to array_shift() accepting it's argument by reference, if
925 * E_STRICT is on, this would trigger a warning. So we'll have to
926 * assign it first.
927 */
928 $key = array_keys($row);
929 $key = array_shift($key);
930 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200932
933 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 return $this->data_cache['field_names'][$table];
937 }
938
939 // --------------------------------------------------------------------
940
941 /**
942 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500943 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 * @param string
945 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200946 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500948 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200949 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200950 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 // --------------------------------------------------------------------
954
955 /**
956 * Returns an object with field data
957 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200959 * @return object
960 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500961 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100963 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200965 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200968 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200970 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000971
972 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300975 * Escape the SQL Identifiers
976 *
977 * This function escapes column and table names
978 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300979 * @param mixed
980 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300981 */
982 public function escape_identifiers($item)
983 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300984 if ($this->_escape_char === '' OR empty($item))
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300985 {
986 return $item;
987 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300988 elseif (is_array($item))
989 {
990 foreach ($item as $key => $value)
991 {
992 $item[$key] = $this->escape_identifiers($value);
993 }
994
995 return $item;
996 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300997 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +0300998 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +0300999 {
1000 return $item;
1001 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001002
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001003 static $preg_ec = array();
1004
1005 if (empty($preg_ec))
1006 {
1007 if (is_array($this->_escape_char))
1008 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001009 $preg_ec = array(
Andrey Andreeve4742582012-10-25 13:25:13 +03001010 preg_quote($this->_escape_char[0], '/'), preg_quote($this->_escape_char[1], '/'),
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001011 $this->_escape_char[0], $this->_escape_char[1]
1012 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001013 }
1014 else
1015 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001016 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001017 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001018 }
1019 }
1020
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001021 foreach ($this->_reserved_identifiers as $id)
1022 {
1023 if (strpos($item, '.'.$id) !== FALSE)
1024 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001025 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\./i', $preg_ec[2].'$1'.$preg_ec[3].'.', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001026 }
1027 }
1028
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001029 return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?(\.)?/i', $preg_ec[2].'$1'.$preg_ec[3].'$2', $item);
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001030 }
1031
1032 // --------------------------------------------------------------------
1033
1034 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 * Generate an insert string
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string the table upon which the query will be performed
1038 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001039 * @return string
1040 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001041 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001043 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001044
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001045 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001047 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 $values[] = $this->escape($val);
1049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001051 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001052 }
1053
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 // --------------------------------------------------------------------
1055
1056 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001057 * Insert statement
1058 *
1059 * Generates a platform-specific insert string from the supplied data
1060 *
1061 * @param string the table name
1062 * @param array the insert keys
1063 * @param array the insert values
1064 * @return string
1065 */
1066 protected function _insert($table, $keys, $values)
1067 {
1068 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1069 }
1070
1071 // --------------------------------------------------------------------
1072
1073 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 * Generate an update string
1075 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 * @param string the table upon which the query will be performed
1077 * @param array an associative array data of key/values
1078 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001079 * @return string
1080 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001081 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001083 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001085 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 }
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001089 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001091 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 }
1093
1094 if ( ! is_array($where))
1095 {
1096 $dest = array($where);
1097 }
1098 else
1099 {
1100 $dest = array();
1101 foreach ($where as $key => $val)
1102 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001103 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001104 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 if ($val !== '')
1107 {
1108 if ( ! $this->_has_operator($key))
1109 {
1110 $key .= ' =';
1111 }
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 $val = ' '.$this->escape($val);
1114 }
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 $dest[] = $prefix.$key.$val;
1117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001119
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001120 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001121 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001122
1123 // --------------------------------------------------------------------
1124
1125 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001126 * Update statement
1127 *
1128 * Generates a platform-specific update string from the supplied data
1129 *
1130 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001131 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001132 * @return string
1133 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001134 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001135 {
1136 foreach ($values as $key => $val)
1137 {
1138 $valstr[] = $key.' = '.$val;
1139 }
1140
Andrey Andreev75546112012-07-05 11:01:29 +03001141 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001142 .$this->_compile_wh('qb_where')
1143 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001144 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001145 }
1146
1147 // --------------------------------------------------------------------
1148
1149 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * Tests whether the string has an SQL operator
1151 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * @param string
1153 * @return bool
1154 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001155 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001157 return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 }
1159
1160 // --------------------------------------------------------------------
1161
1162 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001163 * Returns the SQL string operator
1164 *
1165 * @param string
1166 * @return string
1167 */
1168 protected function _get_operator($str)
1169 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001170 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001171
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001172 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001173 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001174 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001175 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001176 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001177 $_operators = array(
1178 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1179 '\s*<>?\s*', // <, <>
1180 '\s*>\s*', // >
1181 '\s+IS NULL', // IS NULL
1182 '\s+IS NOT NULL', // IS NOT NULL
1183 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1184 '\s+IN\s*\([^\)]+\)', // IN(list)
1185 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1186 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1187 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1188 );
1189
1190 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001191
Andrey Andreev6e704752012-07-18 00:46:33 +03001192 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1193 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001194 }
1195
1196 // --------------------------------------------------------------------
1197
1198 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1200 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001201 * @param string $function the function name
1202 * @param mixed $param,... optional parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001203 * @return mixed
1204 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001205 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001207 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 if (FALSE === strpos($driver, $function))
1210 {
1211 $function = $driver.$function;
1212 }
Barry Mienydd671972010-10-04 16:33:58 +02001213
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 if ( ! function_exists($function))
1215 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001216 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001218
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001219 return (func_num_args() > 1)
1220 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1221 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 }
1223
1224 // --------------------------------------------------------------------
1225
1226 /**
1227 * Set Cache Directory Path
1228 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 * @param string the path to the cache directory
1230 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001231 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001232 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
1234 $this->cachedir = $path;
1235 }
1236
1237 // --------------------------------------------------------------------
1238
1239 /**
1240 * Enable Query Caching
1241 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001242 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001243 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001244 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001246 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
1252 * Disable Query Caching
1253 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001254 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001255 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001256 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001258 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 }
Barry Mienydd671972010-10-04 16:33:58 +02001260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // --------------------------------------------------------------------
1262
1263 /**
1264 * Delete the cache files associated with a particular URI
1265 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001266 * @param string $segment_one = ''
1267 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001268 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001269 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001270 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001272 return ($this->_cache_init())
1273 ? $this->CACHE->delete($segment_one, $segment_two)
1274 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 }
1276
1277 // --------------------------------------------------------------------
1278
1279 /**
1280 * Delete All cache files
1281 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001282 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001283 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001284 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001286 return ($this->_cache_init())
1287 ? $this->CACHE->delete_all()
1288 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 }
1290
1291 // --------------------------------------------------------------------
1292
1293 /**
1294 * Initialize the Cache Class
1295 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001296 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001297 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001298 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001300 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001302 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001303 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001304 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001305 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001307 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1308 {
1309 return $this->cache_off();
1310 }
Derek Allarde37ab382009-02-03 16:13:57 +00001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1313 return TRUE;
1314 }
1315
1316 // --------------------------------------------------------------------
1317
1318 /**
1319 * Close DB Connection
1320 *
Barry Mienydd671972010-10-04 16:33:58 +02001321 * @return void
1322 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001323 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001325 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001327 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001328 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 // --------------------------------------------------------------------
1333
1334 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001335 * Close DB Connection
1336 *
1337 * This method would be overriden by most of the drivers.
1338 *
1339 * @return void
1340 */
1341 protected function _close()
1342 {
1343 $this->conn_id = FALSE;
1344 }
1345
1346 // --------------------------------------------------------------------
1347
1348 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 * Display an error message
1350 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 * @param string the error message
1352 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001353 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001354 * @return string sends the application/error_db.php template
1355 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001356 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 {
Derek Jonese7792202010-03-02 17:24:46 -06001358 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 $LANG->load('db');
1360
1361 $heading = $LANG->line('db_error_heading');
1362
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001363 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001365 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 }
1367 else
1368 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001369 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 }
Barry Mienydd671972010-10-04 16:33:58 +02001371
Pascal Kriete60f8c392010-08-25 18:03:28 +02001372 // Find the most likely culprit of the error by going through
1373 // the backtrace until the source file is no longer in the
1374 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001375 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001376 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001377 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001378 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1379 if (DIRECTORY_SEPARATOR !== '/')
1380 {
1381 $call['file'] = str_replace('\\', '/', $call['file']);
1382 }
1383
Andrey Andreev70b78992012-10-09 10:36:04 +03001384 if (isset($call['file'], $call['class']) && strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') !== FALSE)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001385 {
1386 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001387 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001388 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001389 break;
1390 }
1391 }
Barry Mienydd671972010-10-04 16:33:58 +02001392
Derek Jonese7792202010-03-02 17:24:46 -06001393 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 echo $error->show_error($heading, $message, 'error_db');
1395 exit;
1396 }
1397
1398 // --------------------------------------------------------------------
1399
1400 /**
1401 * Protect Identifiers
1402 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001403 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001404 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001406 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001407 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 *
1409 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1410 *
1411 * Or a query with aliasing:
1412 *
1413 * SELECT m.member_id, m.member_name FROM members AS m
1414 *
1415 * Since the column name can include up to four segments (host, DB, table, column)
1416 * or also have an alias prefix, we need to do a bit of work to figure this out and
1417 * insert the table prefix (if it exists) in the proper position, and escape only
1418 * the correct identifiers.
1419 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 * @param string
1421 * @param bool
1422 * @param mixed
1423 * @param bool
1424 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001425 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001426 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 {
1428 if ( ! is_bool($protect_identifiers))
1429 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001430 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 }
Derek Allarde37ab382009-02-03 16:13:57 +00001432
1433 if (is_array($item))
1434 {
1435 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001436 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001437 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001438 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001439 }
1440
1441 return $escaped_array;
1442 }
1443
Derek Allard911d3e02008-12-15 14:08:35 +00001444 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001445 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001446 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001447 // way to deal with this, but I'm not thinking of it -- Rick
1448 if (strpos($item, '(') !== FALSE)
1449 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001450 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001451 }
1452
Andrey Andreevbf940582012-06-10 07:05:05 +03001453 // Convert tabs or multiple spaces into single spaces
1454 $item = preg_replace('/\s+/', ' ', $item);
1455
Andrey Andreevbf940582012-06-10 07:05:05 +03001456 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001457 // Note: strripos() is used in order to support spaces in table names
1458 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001459 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001460 $alias = ($protect_identifiers)
1461 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1462 : substr($item, $offset);
1463 $item = substr($item, 0, $offset);
1464 }
1465 elseif ($offset = strrpos($item, ' '))
1466 {
1467 $alias = ($protect_identifiers)
1468 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1469 : substr($item, $offset);
1470 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001471 }
1472 else
1473 {
1474 $alias = '';
1475 }
1476
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 // Break the string apart if it contains periods, then insert the table prefix
1478 // in the correct location, assuming the period doesn't indicate that we're dealing
1479 // with an alias. While we're at it, we will escape the components
1480 if (strpos($item, '.') !== FALSE)
1481 {
1482 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001483
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001485 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001487 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001488 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 if ($protect_identifiers === TRUE)
1490 {
1491 foreach ($parts as $key => $val)
1492 {
1493 if ( ! in_array($val, $this->_reserved_identifiers))
1494 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001495 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 }
1497 }
Barry Mienydd671972010-10-04 16:33:58 +02001498
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001500 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001501
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 return $item.$alias;
1503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Andrey Andreev4c202602012-03-06 20:34:52 +02001505 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001506 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 {
1508 // We now add the table prefix based on some logic.
1509 // Do we have 4 segments (hostname.database.table.column)?
1510 // If so, we add the table prefix to the column name in the 3rd segment.
1511 if (isset($parts[3]))
1512 {
1513 $i = 2;
1514 }
1515 // Do we have 3 segments (database.table.column)?
1516 // If so, we add the table prefix to the column name in 2nd position
1517 elseif (isset($parts[2]))
1518 {
1519 $i = 1;
1520 }
1521 // Do we have 2 segments (table.column)?
1522 // If so, we add the table prefix to the column name in 1st segment
1523 else
1524 {
1525 $i = 0;
1526 }
Barry Mienydd671972010-10-04 16:33:58 +02001527
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 // This flag is set when the supplied $item does not contain a field name.
1529 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001530 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 {
1532 $i++;
1533 }
Barry Mienydd671972010-10-04 16:33:58 +02001534
Derek Jones55acc8b2009-07-11 03:38:13 +00001535 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001536 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001537 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001538 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001539 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001541 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
1543 $parts[$i] = $this->dbprefix.$parts[$i];
1544 }
Barry Mienydd671972010-10-04 16:33:58 +02001545
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 // Put the parts back together
1547 $item = implode('.', $parts);
1548 }
Barry Mienydd671972010-10-04 16:33:58 +02001549
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 if ($protect_identifiers === TRUE)
1551 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001552 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 }
Barry Mienydd671972010-10-04 16:33:58 +02001554
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 return $item.$alias;
1556 }
1557
Andrey Andreev4c202602012-03-06 20:34:52 +02001558 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001559 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001561 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001562 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001563 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001564 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001565 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001567 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 {
1569 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001570 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 }
1572
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001573 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001575 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 }
Barry Mienydd671972010-10-04 16:33:58 +02001577
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 return $item.$alias;
1579 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001580
Túbal Martín511f2252011-11-24 14:43:45 +01001581 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001582
Túbal Martín511f2252011-11-24 14:43:45 +01001583 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001584 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001585 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001586 *
Túbal Martín511f2252011-11-24 14:43:45 +01001587 * @return void
1588 */
1589 protected function _reset_select()
1590 {
Túbal Martín511f2252011-11-24 14:43:45 +01001591 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001592
1593}
1594
Derek Allard2067d1a2008-11-13 22:59:24 +00001595/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001596/* Location: ./system/database/DB_driver.php */