blob: 515e9cbb12ae1f923e2af8b1068d70b21d52494a [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Database Driver Class
31 *
32 * This is the platform-independent base DB implementation class.
33 * This class will not be called directly. Rather, the adapter
34 * class for the specific database will extend and instantiate it.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
Timothy Warren7eeda532012-03-19 16:01:55 -040042abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000043
Andrey Andreev738f5342012-03-06 20:43:40 +020044 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020045 public $username;
46 public $password;
47 public $hostname;
48 public $database;
Andrey Andreev75546112012-07-05 11:01:29 +030049 public $dbdriver = 'mysqli';
Andrey Andreevfbba54e2012-06-23 20:26:31 +030050 public $subdriver;
Andrey Andreevd1add432012-03-06 20:26:01 +020051 public $dbprefix = '';
52 public $char_set = 'utf8';
53 public $dbcollat = 'utf8_general_ci';
54 public $autoinit = TRUE; // Whether to automatically initialize the DB
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030055 public $encrypt = FALSE;
Andrey Andreevd1add432012-03-06 20:26:01 +020056 public $swap_pre = '';
57 public $port = '';
58 public $pconnect = FALSE;
59 public $conn_id = FALSE;
60 public $result_id = FALSE;
61 public $db_debug = FALSE;
62 public $benchmark = 0;
63 public $query_count = 0;
64 public $bind_marker = '?';
65 public $save_queries = TRUE;
66 public $queries = array();
67 public $query_times = array();
68 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000069
Andrey Andreevd1add432012-03-06 20:26:01 +020070 public $trans_enabled = TRUE;
71 public $trans_strict = TRUE;
72 protected $_trans_depth = 0;
73 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000074
Andrey Andreevd1add432012-03-06 20:26:01 +020075 public $cache_on = FALSE;
76 public $cachedir = '';
77 public $cache_autodel = FALSE;
78 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020079
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000080 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020081 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
82
Andrey Andreev2ea33c32012-10-04 12:37:51 +030083 // clause and character used for LIKE escape sequences
84 protected $_like_escape_str = " ESCAPE '%s' ";
85 protected $_like_escape_chr = '!';
86
Andrey Andreev75546112012-07-05 11:01:29 +030087 /**
Andrey Andreev77a5d942012-07-05 15:42:14 +030088 * The syntax to count rows is slightly different across different
89 * database engines, so this string appears in each driver and is
90 * used for the count_all() and count_all_results() functions.
91 */
92 protected $_count_string = 'SELECT COUNT(*) AS ';
93
94 /**
Andrey Andreev75546112012-07-05 11:01:29 +030095 * Constructor
96 *
97 * @param array
98 * @return void
99 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500100 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
102 if (is_array($params))
103 {
104 foreach ($params as $key => $val)
105 {
106 $this->$key = $val;
107 }
108 }
109
110 log_message('debug', 'Database Driver Class Initialized');
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Gints Murans89f77ee2012-05-23 00:23:50 +0300113 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -0400114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 /**
116 * Initialize Database Settings
117 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200118 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200119 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200120 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200122 /* If an established connection is available, then there's
123 * no need to connect and select the database.
124 *
125 * Depending on the database driver, conn_id can be either
126 * boolean TRUE, a resource or an object.
127 */
128 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 return TRUE;
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100136 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000137
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200138 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 if ( ! $this->conn_id)
140 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100141 // Check if there is a failover set
142 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100144 // Go over all the failovers
145 foreach ($this->failover as $failover)
146 {
147 // Replace the current settings with those of the failover
148 foreach ($failover as $key => $val)
149 {
150 $this->$key = $val;
151 }
152
153 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100154 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100155
156 // If a connection is made break the foreach loop
157 if ($this->conn_id)
158 {
159 break;
160 }
161 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100163
164 // We still don't have a connection?
165 if ( ! $this->conn_id)
166 {
167 log_message('error', 'Unable to connect to the database');
168
169 if ($this->db_debug)
170 {
171 $this->display_error('db_unable_to_connect');
172 }
173 return FALSE;
174 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
177 // ----------------------------------------------------------------
178
179 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200180 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200182 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200183
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200184 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200186 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200188 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
190
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200191 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200192 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // --------------------------------------------------------------------
196
197 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200198 * Reconnect
199 *
200 * Keep / reestablish the db connection if no queries have been
201 * sent for a length of time exceeding the server's idle timeout.
202 *
203 * This is just a dummy method to allow drivers without such
204 * functionality to not declare it, while others will override it.
205 *
206 * @return void
207 */
208 public function reconnect()
209 {
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300215 * Select database
216 *
217 * This is just a dummy method to allow drivers without such
218 * functionality to not declare it, while others will override it.
219 *
220 * @return bool
221 */
222 public function db_select()
223 {
224 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Set client character set
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200233 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200235 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200237 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200239 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 if ($this->db_debug)
242 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200243 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 return FALSE;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 return TRUE;
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // --------------------------------------------------------------------
253
254 /**
255 * The name of the platform in use (mysql, mssql, etc...)
256 *
Barry Mienydd671972010-10-04 16:33:58 +0200257 * @return string
258 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500259 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 return $this->dbdriver;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200267 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200269 * Returns a string containing the version of the database being used.
270 * Most drivers will override this method.
271 *
Barry Mienydd671972010-10-04 16:33:58 +0200272 * @return string
273 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200274 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200276 if (isset($this->data_cache['version']))
277 {
278 return $this->data_cache['version'];
279 }
280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 if (FALSE === ($sql = $this->_version()))
282 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200283 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
Derek Allard3683f772009-12-16 17:32:33 +0000285
Andrey Andreev08856b82012-03-03 03:19:28 +0200286 $query = $this->query($sql);
287 $query = $query->row();
288 return $this->data_cache['version'] = $query->ver;
289 }
Derek Allard3683f772009-12-16 17:32:33 +0000290
Andrey Andreev08856b82012-03-03 03:19:28 +0200291 // --------------------------------------------------------------------
292
293 /**
294 * Version number query string
295 *
296 * @return string
297 */
298 protected function _version()
299 {
300 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 // --------------------------------------------------------------------
304
305 /**
306 * Execute the query
307 *
308 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200309 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * upon successful execution of a "write" type query. Returns boolean
311 * FALSE upon failure, and if the $db_debug variable is set to TRUE
312 * will raise an error.
313 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300314 * @param string $sql
315 * @param array $binds = FALSE An array of binding data
316 * @param bool $return_object = NULL
Barry Mienydd671972010-10-04 16:33:58 +0200317 * @return mixed
318 */
Andrey Andreevb66664b2012-06-27 14:22:10 +0300319 public function query($sql, $binds = FALSE, $return_object = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100321 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200323 log_message('error', 'Invalid query: '.$sql);
324
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200325 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
Andrey Andreevb66664b2012-06-27 14:22:10 +0300327 elseif ( ! is_bool($return_object))
328 {
329 $return_object = ! $this->is_write_type($sql);
330 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
332 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100333 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600334 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200335 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Derek Jonese7792202010-03-02 17:24:46 -0600337
Ryan Dialef7474c2012-03-01 16:11:36 -0500338 // Compile binds if needed
339 if ($binds !== FALSE)
340 {
341 $sql = $this->compile_binds($sql, $binds);
342 }
343
Andrey Andreev4c202602012-03-06 20:34:52 +0200344 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // we will load the caching class and return the previously
346 // cached query if it exists
Andrey Andreevb66664b2012-06-27 14:22:10 +0300347 if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200349 $this->load_rdriver();
350 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200352 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreevb66664b2012-06-27 14:22:10 +0300356 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100357 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 $this->queries[] = $sql;
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200363 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // Run the Query
366 if (FALSE === ($this->result_id = $this->simple_query($sql)))
367 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100368 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $this->query_times[] = 0;
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // This will trigger a rollback if transactions are being used
374 $this->_trans_status = FALSE;
375
Andrey Andreev4be5de12012-03-02 15:45:41 +0200376 // Grab the error now, as we might run some additional queries before displaying the error
377 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200378
379 // Log errors
Andrey Andreevb66664b2012-06-27 14:22:10 +0300380 log_message('error', 'Query error: '.$error['message'].' - Invalid query: '.$sql);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 if ($this->db_debug)
383 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200386 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // transactions to remain in limbo.
388 $this->trans_complete();
389
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200390 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200391 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 return FALSE;
395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200398 $time_end = microtime(TRUE);
399 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000400
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100401 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
dixyab3cab52012-04-21 00:58:00 +0200403 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // Increment the query counter
407 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200408
Andrey Andreevb66664b2012-06-27 14:22:10 +0300409 // Will we have a result object instantiated? If not - we'll simply return TRUE
410 if ($return_object !== TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Andrey Andreevb66664b2012-06-27 14:22:10 +0300412 // If caching is enabled we'll auto-cleanup any existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100413 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 $this->CACHE->delete();
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 return TRUE;
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // Return TRUE if we don't need to create a result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 if ($return_object !== TRUE)
423 {
424 return TRUE;
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
427 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200428 $driver = $this->load_rdriver();
429 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200430
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200431 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100433 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 // We'll create a new instance of the result object
436 // only without the platform specific driver since
437 // we can't use it with cached data (the query result
438 // resource ID won't be any good once we've cached the
439 // result object, so we'll have to compile the data
440 // and save it)
441 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $CR->result_object = $RES->result_object();
443 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200444 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 // Reset these since cached objects can not utilize resource IDs.
447 $CR->conn_id = NULL;
448 $CR->result_id = NULL;
449
450 $this->CACHE->write($sql, $CR);
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 return $RES;
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Load the result drivers
460 *
Barry Mienydd671972010-10-04 16:33:58 +0200461 * @return string the name of the result class
462 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500463 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 $driver = 'CI_DB_'.$this->dbdriver.'_result';
466
467 if ( ! class_exists($driver))
468 {
Greg Aker3a746652011-04-19 10:59:47 -0500469 include_once(BASEPATH.'database/DB_result.php');
470 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 return $driver;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200480 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * we only use it when running transaction commands since they do
482 * not require all the features of the main query() function.
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200485 * @return mixed
486 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500487 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 if ( ! $this->conn_id)
490 {
491 $this->initialize();
492 }
493
494 return $this->_execute($sql);
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
498
499 /**
500 * Disable Transactions
501 * This permits transactions to be disabled at run-time.
502 *
Barry Mienydd671972010-10-04 16:33:58 +0200503 * @return void
504 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500505 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 {
507 $this->trans_enabled = FALSE;
508 }
509
510 // --------------------------------------------------------------------
511
512 /**
513 * Enable/disable Transaction Strict Mode
514 * When strict mode is enabled, if you are running multiple groups of
515 * transactions, if one group fails all groups will be rolled back.
516 * If strict mode is disabled, each group is treated autonomously, meaning
517 * a failure of one group will not affect any others
518 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300519 * @param bool $mode = TRUE
Barry Mienydd671972010-10-04 16:33:58 +0200520 * @return void
521 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500522 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // --------------------------------------------------------------------
528
529 /**
530 * Start Transaction
531 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300532 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200533 * @return void
534 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500535 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200536 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 if ( ! $this->trans_enabled)
538 {
539 return FALSE;
540 }
541
542 // When transactions are nested we only begin/commit/rollback the outermost ones
543 if ($this->_trans_depth > 0)
544 {
545 $this->_trans_depth += 1;
546 return;
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500550 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552
553 // --------------------------------------------------------------------
554
555 /**
556 * Complete Transaction
557 *
Barry Mienydd671972010-10-04 16:33:58 +0200558 * @return bool
559 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500560 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 if ( ! $this->trans_enabled)
563 {
564 return FALSE;
565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 // When transactions are nested we only begin/commit/rollback the outermost ones
568 if ($this->_trans_depth > 1)
569 {
570 $this->_trans_depth -= 1;
571 return TRUE;
572 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500573 else
574 {
575 $this->_trans_depth = 0;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // The query() function will set this flag to FALSE in the event that a query failed
579 if ($this->_trans_status === FALSE)
580 {
581 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // If we are NOT running in strict mode, we will reset
584 // the _trans_status flag so that subsequent groups of transactions
585 // will be permitted.
586 if ($this->trans_strict === FALSE)
587 {
588 $this->_trans_status = TRUE;
589 }
590
591 log_message('debug', 'DB Transaction Failure');
592 return FALSE;
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 $this->trans_commit();
596 return TRUE;
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Lets you retrieve the transaction flag to determine if it has failed
603 *
Barry Mienydd671972010-10-04 16:33:58 +0200604 * @return bool
605 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500606 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
608 return $this->_trans_status;
609 }
610
611 // --------------------------------------------------------------------
612
613 /**
614 * Compile Bindings
615 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 * @param string the sql statement
617 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200618 * @return string
619 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500620 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300622 if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 return $sql;
625 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300626 elseif ( ! is_array($binds))
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300628 $binds = array($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300629 $bind_count = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300631 else
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200632 {
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300633 // Make sure we're using numeric keys
634 $binds = array_values($binds);
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300635 $bind_count = count($binds);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
637
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300638 // We'll need the marker length later
639 $ml = strlen($this->bind_marker);
640
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300641 // Make sure not to replace a chunk inside a string that happens to match the bind marker
642 if ($c = preg_match_all("/'[^']*'/i", $sql, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
Andrey Andreeve4742582012-10-25 13:25:13 +0300644 $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300645 str_replace($matches[0],
646 str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
647 $sql, $c),
648 $matches, PREG_OFFSET_CAPTURE);
649
650 // Bind values' count must match the count of markers in the query
651 if ($bind_count !== $c)
652 {
653 return $sql;
654 }
Andrey Andreev10cbdf02012-06-13 13:32:30 +0300655 }
Andrey Andreeve4742582012-10-25 13:25:13 +0300656 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 +0300657 {
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300658 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660
Andrey Andreevaf915ce2012-06-13 19:03:06 +0300661 do
662 {
663 $c--;
664 $sql = substr_replace($sql, $this->escape($binds[$c]), $matches[0][$c][1], $ml);
665 }
666 while ($c !== 0);
667
Andrey Andreev4e9538f2012-06-12 14:16:53 +0300668 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 }
Barry Mienydd671972010-10-04 16:33:58 +0200670
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 // --------------------------------------------------------------------
672
673 /**
674 * Determines if a query is a "write" type.
675 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200677 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200678 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200679 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
Andrey Andreev9e31f8f2012-10-05 17:31:46 +0300681 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 +0000682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // --------------------------------------------------------------------
685
686 /**
687 * Calculate the aggregate query elapsed time
688 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200689 * @param int The number of decimal places
690 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200691 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500692 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
694 return number_format($this->benchmark, $decimals);
695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 // --------------------------------------------------------------------
698
699 /**
700 * Returns the total number of queries
701 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200702 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200703 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500704 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
706 return $this->query_count;
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // --------------------------------------------------------------------
710
711 /**
712 * Returns the last query that was executed
713 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200714 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200715 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500716 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 {
718 return end($this->queries);
719 }
720
721 // --------------------------------------------------------------------
722
723 /**
724 * "Smart" Escape String
725 *
726 * Escapes data based on type
727 * Sets boolean and null types
728 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200730 * @return mixed
731 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500732 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200733 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500734 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200736 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000737 }
738 elseif (is_bool($str))
739 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200740 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000741 }
742 elseif (is_null($str))
743 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200744 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000745 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000746
747 return $str;
748 }
749
750 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600751
Derek Jonese4ed5832009-02-20 21:44:59 +0000752 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000753 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000754 *
755 * Calls the individual driver for platform
756 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200757 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000758 * @param string
759 * @return mixed
760 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500761 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200762 {
763 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000764 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000765
Derek Jonese4ed5832009-02-20 21:44:59 +0000766 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 /**
769 * Primary
770 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200771 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 * position is the primary key
773 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200775 * @return string
776 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500777 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200778 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200780 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 }
782
783 // --------------------------------------------------------------------
784
785 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300786 * "Count All" query
787 *
788 * Generates a platform-specific query string that counts all records in
789 * the specified database
790 *
791 * @param string
792 * @return int
793 */
794 public function count_all($table = '')
795 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100796 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300797 {
798 return 0;
799 }
800
801 $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 +0100802 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300803 {
804 return 0;
805 }
806
807 $query = $query->row();
808 $this->_reset_select();
809 return (int) $query->numrows;
810 }
811
812 // --------------------------------------------------------------------
813
814 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 * Returns an array of table names
816 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300817 * @param string $constrain_by_prefix = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200818 * @return array
819 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500820 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 {
822 // Is there a cached result?
823 if (isset($this->data_cache['table_names']))
824 {
825 return $this->data_cache['table_names'];
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
829 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200830 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 }
832
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200833 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200835
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200836 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200838 // Do we know from which column to get the table name?
839 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300841 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200842 {
843 $key = 'table_name';
844 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300845 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200846 {
847 $key = 'TABLE_NAME';
848 }
849 else
850 {
851 /* We have no other choice but to just get the first element's key.
852 * Due to array_shift() accepting it's argument by reference, if
853 * E_STRICT is on, this would trigger a warning. So we'll have to
854 * assign it first.
855 */
856 $key = array_keys($row);
857 $key = array_shift($key);
858 }
Taufan Aditya18209332012-02-09 16:07:27 +0700859 }
860
Andrey Andreev12567e82012-01-25 22:50:33 +0200861 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 }
863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 return $this->data_cache['table_names'];
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
868
869 /**
870 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500871 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300872 * @param string $table_name
Andrey Andreev4c202602012-03-06 20:34:52 +0200873 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500875 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200876 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200877 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 }
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 // --------------------------------------------------------------------
881
882 /**
Andrey Andreev75546112012-07-05 11:01:29 +0300883 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200886 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500888 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
890 // Is there a cached result?
891 if (isset($this->data_cache['field_names'][$table]))
892 {
893 return $this->data_cache['field_names'][$table];
894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100896 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200898 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Greg Aker1edde302010-01-26 00:17:01 +0000901 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200903 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200907 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200908
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500909 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200911 // Do we know from where to get the column's name?
912 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300914 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200915 {
916 $key = 'column_name';
917 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300918 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200919 {
920 $key = 'COLUMN_NAME';
921 }
922 else
923 {
924 /* We have no other choice but to just get the first element's key.
925 * Due to array_shift() accepting it's argument by reference, if
926 * E_STRICT is on, this would trigger a warning. So we'll have to
927 * assign it first.
928 */
929 $key = array_keys($row);
930 $key = array_shift($key);
931 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200933
934 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 return $this->data_cache['field_names'][$table];
938 }
939
940 // --------------------------------------------------------------------
941
942 /**
943 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500944 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @param string
946 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200947 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500949 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200950 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200951 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 }
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 // --------------------------------------------------------------------
955
956 /**
957 * Returns an object with field data
958 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200960 * @return object
961 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500962 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100964 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200966 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200969 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200971 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000972
973 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300976 * Escape the SQL Identifiers
977 *
978 * This function escapes column and table names
979 *
Andrey Andreev9637b402012-06-08 15:39:24 +0300980 * @param mixed
981 * @return mixed
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300982 */
983 public function escape_identifiers($item)
984 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300985 if ($this->_escape_char === '' OR empty($item))
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300986 {
987 return $item;
988 }
Andrey Andreev9637b402012-06-08 15:39:24 +0300989 elseif (is_array($item))
990 {
991 foreach ($item as $key => $value)
992 {
993 $item[$key] = $this->escape_identifiers($value);
994 }
995
996 return $item;
997 }
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300998 // Avoid breaking functions and literal values inside queries
Andrey Andreev9859cb02012-07-13 13:07:58 +0300999 elseif (ctype_digit($item) OR $item[0] === "'" OR ($this->_escape_char !== '"' && $item[0] === '"') OR strpos($item, '(') !== FALSE)
Andrey Andreev7db0f592012-06-28 17:54:27 +03001000 {
1001 return $item;
1002 }
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001003
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001004 static $preg_ec = array();
1005
1006 if (empty($preg_ec))
1007 {
1008 if (is_array($this->_escape_char))
1009 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001010 $preg_ec = array(
Andrey Andreeve4742582012-10-25 13:25:13 +03001011 preg_quote($this->_escape_char[0], '/'), preg_quote($this->_escape_char[1], '/'),
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001012 $this->_escape_char[0], $this->_escape_char[1]
1013 );
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001014 }
1015 else
1016 {
Andrey Andreeve4742582012-10-25 13:25:13 +03001017 $preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001018 $preg_ec[2] = $preg_ec[3] = $this->_escape_char;
Andrey Andreev082ee2b2012-06-08 15:26:34 +03001019 }
1020 }
1021
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001022 foreach ($this->_reserved_identifiers as $id)
1023 {
1024 if (strpos($item, '.'.$id) !== FALSE)
1025 {
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001026 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 +03001027 }
1028 }
1029
Andrey Andreev2636c1c2012-07-02 14:53:39 +03001030 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 +03001031 }
1032
1033 // --------------------------------------------------------------------
1034
1035 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 * Generate an insert string
1037 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 * @param string the table upon which the query will be performed
1039 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +02001040 * @return string
1041 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001042 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001044 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +02001045
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001046 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001048 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 $values[] = $this->escape($val);
1050 }
Barry Mienydd671972010-10-04 16:33:58 +02001051
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001052 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +02001053 }
1054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 // --------------------------------------------------------------------
1056
1057 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001058 * Insert statement
1059 *
1060 * Generates a platform-specific insert string from the supplied data
1061 *
1062 * @param string the table name
1063 * @param array the insert keys
1064 * @param array the insert values
1065 * @return string
1066 */
1067 protected function _insert($table, $keys, $values)
1068 {
1069 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1070 }
1071
1072 // --------------------------------------------------------------------
1073
1074 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 * Generate an update string
1076 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 * @param string the table upon which the query will be performed
1078 * @param array an associative array data of key/values
1079 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +02001080 * @return string
1081 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001082 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 {
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001084 if (empty($where))
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001086 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Andrey Andreev87f4dc22012-11-01 01:11:22 +02001089 $this->where($where);
1090
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001092 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001094 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 }
1096
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001097 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001098 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001099
1100 // --------------------------------------------------------------------
1101
1102 /**
Andrey Andreev75546112012-07-05 11:01:29 +03001103 * Update statement
1104 *
1105 * Generates a platform-specific update string from the supplied data
1106 *
1107 * @param string the table name
Andrey Andreev822317b2012-07-19 16:00:32 +03001108 * @param array the update data
Andrey Andreev75546112012-07-05 11:01:29 +03001109 * @return string
1110 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001111 protected function _update($table, $values)
Andrey Andreev75546112012-07-05 11:01:29 +03001112 {
1113 foreach ($values as $key => $val)
1114 {
1115 $valstr[] = $key.' = '.$val;
1116 }
1117
Andrey Andreev75546112012-07-05 11:01:29 +03001118 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev2d486232012-07-19 14:46:51 +03001119 .$this->_compile_wh('qb_where')
1120 .$this->_compile_order_by()
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001121 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreev75546112012-07-05 11:01:29 +03001122 }
1123
1124 // --------------------------------------------------------------------
1125
1126 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 * Tests whether the string has an SQL operator
1128 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 * @param string
1130 * @return bool
1131 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001132 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001134 return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 }
1136
1137 // --------------------------------------------------------------------
1138
1139 /**
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001140 * Returns the SQL string operator
1141 *
1142 * @param string
1143 * @return string
1144 */
1145 protected function _get_operator($str)
1146 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001147 static $_operators;
Andrey Andreev6e704752012-07-18 00:46:33 +03001148
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001149 if (empty($_operators))
Andrey Andreevb0478652012-07-18 15:34:46 +03001150 {
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001151 $_les = ($this->_like_escape_str !== '')
Andrey Andreeve4742582012-10-25 13:25:13 +03001152 ? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001153 : '';
Andrey Andreeve8be24b2012-07-19 16:11:17 +03001154 $_operators = array(
1155 '\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
1156 '\s*<>?\s*', // <, <>
1157 '\s*>\s*', // >
1158 '\s+IS NULL', // IS NULL
1159 '\s+IS NOT NULL', // IS NOT NULL
1160 '\s+BETWEEN\s+\S+\s+AND\s+\S+', // BETWEEN value AND value
1161 '\s+IN\s*\([^\)]+\)', // IN(list)
1162 '\s+NOT IN\s*\([^\)]+\)', // NOT IN (list)
1163 '\s+LIKE\s+\S+'.$_les, // LIKE 'expr'[ ESCAPE '%s']
1164 '\s+NOT LIKE\s+\S+'.$_les // NOT LIKE 'expr'[ ESCAPE '%s']
1165 );
1166
1167 }
Andrey Andreevb0478652012-07-18 15:34:46 +03001168
Andrey Andreev6e704752012-07-18 00:46:33 +03001169 return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
1170 ? $match[0] : FALSE;
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001171 }
1172
1173 // --------------------------------------------------------------------
1174
1175 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1177 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001178 * @param string $function the function name
1179 * @param mixed $param,... optional parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001180 * @return mixed
1181 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001182 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001184 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 if (FALSE === strpos($driver, $function))
1187 {
1188 $function = $driver.$function;
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 if ( ! function_exists($function))
1192 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001193 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001195
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001196 return (func_num_args() > 1)
1197 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1198 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 }
1200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Set Cache Directory Path
1205 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 * @param string the path to the cache directory
1207 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001208 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001209 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 {
1211 $this->cachedir = $path;
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
1217 * Enable Query Caching
1218 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001219 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001220 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001221 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001223 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 }
1225
1226 // --------------------------------------------------------------------
1227
1228 /**
1229 * Disable Query Caching
1230 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001231 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001232 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001233 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001235 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 // --------------------------------------------------------------------
1239
1240 /**
1241 * Delete the cache files associated with a particular URI
1242 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001243 * @param string $segment_one = ''
1244 * @param string $segment_two = ''
Andrey Andreev4c202602012-03-06 20:34:52 +02001245 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001246 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001247 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001249 return ($this->_cache_init())
1250 ? $this->CACHE->delete($segment_one, $segment_two)
1251 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 }
1253
1254 // --------------------------------------------------------------------
1255
1256 /**
1257 * Delete All cache files
1258 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001259 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001260 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001261 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001263 return ($this->_cache_init())
1264 ? $this->CACHE->delete_all()
1265 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 }
1267
1268 // --------------------------------------------------------------------
1269
1270 /**
1271 * Initialize the Cache Class
1272 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001273 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001274 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001275 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001277 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001279 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001280 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001281 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001282 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001284 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1285 {
1286 return $this->cache_off();
1287 }
Derek Allarde37ab382009-02-03 16:13:57 +00001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1290 return TRUE;
1291 }
1292
1293 // --------------------------------------------------------------------
1294
1295 /**
1296 * Close DB Connection
1297 *
Barry Mienydd671972010-10-04 16:33:58 +02001298 * @return void
1299 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001300 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001302 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001304 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001305 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 // --------------------------------------------------------------------
1310
1311 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001312 * Close DB Connection
1313 *
1314 * This method would be overriden by most of the drivers.
1315 *
1316 * @return void
1317 */
1318 protected function _close()
1319 {
1320 $this->conn_id = FALSE;
1321 }
1322
1323 // --------------------------------------------------------------------
1324
1325 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 * Display an error message
1327 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 * @param string the error message
1329 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001330 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001331 * @return string sends the application/error_db.php template
1332 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001333 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 {
Derek Jonese7792202010-03-02 17:24:46 -06001335 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 $LANG->load('db');
1337
1338 $heading = $LANG->line('db_error_heading');
1339
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001340 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001342 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 }
1344 else
1345 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001346 $message = is_array($error) ? $error : array(str_replace('%s', $swap, $LANG->line($error)));
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Pascal Kriete60f8c392010-08-25 18:03:28 +02001349 // Find the most likely culprit of the error by going through
1350 // the backtrace until the source file is no longer in the
1351 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001352 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001353 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001354 {
Andrey Andreev1194ad72012-10-05 17:05:46 +03001355 // We'll need this on Windows, as APPPATH and BASEPATH will always use forward slashes
1356 if (DIRECTORY_SEPARATOR !== '/')
1357 {
1358 $call['file'] = str_replace('\\', '/', $call['file']);
1359 }
1360
Andrey Andreev70b78992012-10-09 10:36:04 +03001361 if (isset($call['file'], $call['class']) && strpos($call['file'], BASEPATH.'database') === FALSE && strpos($call['class'], 'Loader') !== FALSE)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001362 {
1363 // Found it - use a relative path for safety
Andrey Andreev2f0dce02012-06-20 11:05:01 +03001364 $message[] = 'Filename: '.str_replace(array(APPPATH, BASEPATH), '', $call['file']);
Pascal Kriete60f8c392010-08-25 18:03:28 +02001365 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001366 break;
1367 }
1368 }
Barry Mienydd671972010-10-04 16:33:58 +02001369
Derek Jonese7792202010-03-02 17:24:46 -06001370 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 echo $error->show_error($heading, $message, 'error_db');
1372 exit;
1373 }
1374
1375 // --------------------------------------------------------------------
1376
1377 /**
1378 * Protect Identifiers
1379 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001380 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001381 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001383 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001384 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 *
1386 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1387 *
1388 * Or a query with aliasing:
1389 *
1390 * SELECT m.member_id, m.member_name FROM members AS m
1391 *
1392 * Since the column name can include up to four segments (host, DB, table, column)
1393 * or also have an alias prefix, we need to do a bit of work to figure this out and
1394 * insert the table prefix (if it exists) in the proper position, and escape only
1395 * the correct identifiers.
1396 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 * @param string
1398 * @param bool
1399 * @param mixed
1400 * @param bool
1401 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001402 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001403 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 {
1405 if ( ! is_bool($protect_identifiers))
1406 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001407 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 }
Derek Allarde37ab382009-02-03 16:13:57 +00001409
1410 if (is_array($item))
1411 {
1412 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001413 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001414 {
Andrey Andreevbf940582012-06-10 07:05:05 +03001415 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v, $prefix_single, $protect_identifiers, $field_exists);
Derek Allarde37ab382009-02-03 16:13:57 +00001416 }
1417
1418 return $escaped_array;
1419 }
1420
Derek Allard911d3e02008-12-15 14:08:35 +00001421 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001422 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001423 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001424 // way to deal with this, but I'm not thinking of it -- Rick
1425 if (strpos($item, '(') !== FALSE)
1426 {
Andrey Andreev4db16322012-06-10 15:12:02 +03001427 return $item;
Derek Allard911d3e02008-12-15 14:08:35 +00001428 }
1429
Andrey Andreevbf940582012-06-10 07:05:05 +03001430 // Convert tabs or multiple spaces into single spaces
1431 $item = preg_replace('/\s+/', ' ', $item);
1432
Andrey Andreevbf940582012-06-10 07:05:05 +03001433 // If the item has an alias declaration we remove it and set it aside.
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001434 // Note: strripos() is used in order to support spaces in table names
1435 if ($offset = strripos($item, ' AS '))
Andrey Andreevbf940582012-06-10 07:05:05 +03001436 {
Andrey Andreev929fd2d2012-06-17 17:29:57 +03001437 $alias = ($protect_identifiers)
1438 ? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1439 : substr($item, $offset);
1440 $item = substr($item, 0, $offset);
1441 }
1442 elseif ($offset = strrpos($item, ' '))
1443 {
1444 $alias = ($protect_identifiers)
1445 ? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1446 : substr($item, $offset);
1447 $item = substr($item, 0, $offset);
Andrey Andreevbf940582012-06-10 07:05:05 +03001448 }
1449 else
1450 {
1451 $alias = '';
1452 }
1453
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 // Break the string apart if it contains periods, then insert the table prefix
1455 // in the correct location, assuming the period doesn't indicate that we're dealing
1456 // with an alias. While we're at it, we will escape the components
1457 if (strpos($item, '.') !== FALSE)
1458 {
1459 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001460
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001462 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001464 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001465 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 if ($protect_identifiers === TRUE)
1467 {
1468 foreach ($parts as $key => $val)
1469 {
1470 if ( ! in_array($val, $this->_reserved_identifiers))
1471 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001472 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 }
1474 }
Barry Mienydd671972010-10-04 16:33:58 +02001475
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001477 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001478
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 return $item.$alias;
1480 }
Barry Mienydd671972010-10-04 16:33:58 +02001481
Andrey Andreev4c202602012-03-06 20:34:52 +02001482 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001483 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 {
1485 // We now add the table prefix based on some logic.
1486 // Do we have 4 segments (hostname.database.table.column)?
1487 // If so, we add the table prefix to the column name in the 3rd segment.
1488 if (isset($parts[3]))
1489 {
1490 $i = 2;
1491 }
1492 // Do we have 3 segments (database.table.column)?
1493 // If so, we add the table prefix to the column name in 2nd position
1494 elseif (isset($parts[2]))
1495 {
1496 $i = 1;
1497 }
1498 // Do we have 2 segments (table.column)?
1499 // If so, we add the table prefix to the column name in 1st segment
1500 else
1501 {
1502 $i = 0;
1503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 // This flag is set when the supplied $item does not contain a field name.
1506 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001507 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 {
1509 $i++;
1510 }
Barry Mienydd671972010-10-04 16:33:58 +02001511
Derek Jones55acc8b2009-07-11 03:38:13 +00001512 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001513 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001514 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001515 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001516 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001518 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001519 {
1520 $parts[$i] = $this->dbprefix.$parts[$i];
1521 }
Barry Mienydd671972010-10-04 16:33:58 +02001522
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 // Put the parts back together
1524 $item = implode('.', $parts);
1525 }
Barry Mienydd671972010-10-04 16:33:58 +02001526
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 if ($protect_identifiers === TRUE)
1528 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001529 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 }
Barry Mienydd671972010-10-04 16:33:58 +02001531
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 return $item.$alias;
1533 }
1534
Andrey Andreev4c202602012-03-06 20:34:52 +02001535 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001536 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001538 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001539 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001540 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001541 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001542 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001544 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 {
1546 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001547 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 }
1549
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001550 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 {
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 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001557
Túbal Martín511f2252011-11-24 14:43:45 +01001558 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001559
Túbal Martín511f2252011-11-24 14:43:45 +01001560 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001561 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001562 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001563 *
Túbal Martín511f2252011-11-24 14:43:45 +01001564 * @return void
1565 */
1566 protected function _reset_select()
1567 {
Túbal Martín511f2252011-11-24 14:43:45 +01001568 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001569
1570}
1571
Derek Allard2067d1a2008-11-13 22:59:24 +00001572/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001573/* Location: ./system/database/DB_driver.php */