blob: 39c19cdf7f6dc82c8c4b80e0134a3e9d2aec3c7d [file] [log] [blame]
Andrey Andreev4c202602012-03-06 20:34:52 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Timothy Warren7eeda532012-03-19 16:01:55 -040041abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreev738f5342012-03-06 20:43:40 +020043 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
48 public $dbdriver = 'mysql';
49 public $dbprefix = '';
50 public $char_set = 'utf8';
51 public $dbcollat = 'utf8_general_ci';
52 public $autoinit = TRUE; // Whether to automatically initialize the DB
53 public $swap_pre = '';
54 public $port = '';
55 public $pconnect = FALSE;
56 public $conn_id = FALSE;
57 public $result_id = FALSE;
58 public $db_debug = FALSE;
59 public $benchmark = 0;
60 public $query_count = 0;
61 public $bind_marker = '?';
62 public $save_queries = TRUE;
63 public $queries = array();
64 public $query_times = array();
65 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000066
Andrey Andreevd1add432012-03-06 20:26:01 +020067 public $trans_enabled = TRUE;
68 public $trans_strict = TRUE;
69 protected $_trans_depth = 0;
70 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000071
Andrey Andreevd1add432012-03-06 20:26:01 +020072 public $cache_on = FALSE;
73 public $cachedir = '';
74 public $cache_autodel = FALSE;
75 public $CACHE; // The cache class object
Barry Mienydd671972010-10-04 16:33:58 +020076
Jamie Rumbelowbcee50f2012-03-08 13:00:15 +000077 protected $_protect_identifiers = TRUE;
Andrey Andreevd1add432012-03-06 20:26:01 +020078 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
Timothy Warrene45518d2012-03-06 07:38:00 -050080 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if (is_array($params))
83 {
84 foreach ($params as $key => $val)
85 {
86 $this->$key = $val;
87 }
88 }
89
90 log_message('debug', 'Database Driver Class Initialized');
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Gints Murans89f77ee2012-05-23 00:23:50 +030093 // --------------------------------------------------------------------
Root36237d82012-05-21 18:30:00 -040094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 /**
96 * Initialize Database Settings
97 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +020098 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200100 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200102 /* If an established connection is available, then there's
103 * no need to connect and select the database.
104 *
105 * Depending on the database driver, conn_id can be either
106 * boolean TRUE, a resource or an object.
107 */
108 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
110 return TRUE;
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // Connect to the database and set the connection ID
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100116 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Derek Allard2067d1a2008-11-13 22:59:24 +0000117
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200118 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if ( ! $this->conn_id)
120 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100121 // Check if there is a failover set
122 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100124 // Go over all the failovers
125 foreach ($this->failover as $failover)
126 {
127 // Replace the current settings with those of the failover
128 foreach ($failover as $key => $val)
129 {
130 $this->$key = $val;
131 }
132
133 // Try to connect
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100134 $this->conn_id = ($this->pconnect === FALSE) ? $this->db_connect() : $this->db_pconnect();
Felix Balfoort5d581b62011-11-29 15:53:01 +0100135
136 // If a connection is made break the foreach loop
137 if ($this->conn_id)
138 {
139 break;
140 }
141 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100143
144 // We still don't have a connection?
145 if ( ! $this->conn_id)
146 {
147 log_message('error', 'Unable to connect to the database');
148
149 if ($this->db_debug)
150 {
151 $this->display_error('db_unable_to_connect');
152 }
153 return FALSE;
154 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // ----------------------------------------------------------------
158
159 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200160 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200163
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200166 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200171 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200172 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 // --------------------------------------------------------------------
176
177 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200178 * Reconnect
179 *
180 * Keep / reestablish the db connection if no queries have been
181 * sent for a length of time exceeding the server's idle timeout.
182 *
183 * This is just a dummy method to allow drivers without such
184 * functionality to not declare it, while others will override it.
185 *
186 * @return void
187 */
188 public function reconnect()
189 {
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
Andrey Andreevbee66442012-03-28 15:28:19 +0300195 * Select database
196 *
197 * This is just a dummy method to allow drivers without such
198 * functionality to not declare it, while others will override it.
199 *
200 * @return bool
201 */
202 public function db_select()
203 {
204 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Set client character set
211 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200213 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200215 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200217 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200219 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 if ($this->db_debug)
222 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200223 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 return FALSE;
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 return TRUE;
230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
233
234 /**
235 * The name of the platform in use (mysql, mssql, etc...)
236 *
Barry Mienydd671972010-10-04 16:33:58 +0200237 * @return string
238 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500239 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 return $this->dbdriver;
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200247 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200249 * Returns a string containing the version of the database being used.
250 * Most drivers will override this method.
251 *
Barry Mienydd671972010-10-04 16:33:58 +0200252 * @return string
253 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200254 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200256 if (isset($this->data_cache['version']))
257 {
258 return $this->data_cache['version'];
259 }
260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 if (FALSE === ($sql = $this->_version()))
262 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200263 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
Derek Allard3683f772009-12-16 17:32:33 +0000265
Andrey Andreev08856b82012-03-03 03:19:28 +0200266 $query = $this->query($sql);
267 $query = $query->row();
268 return $this->data_cache['version'] = $query->ver;
269 }
Derek Allard3683f772009-12-16 17:32:33 +0000270
Andrey Andreev08856b82012-03-03 03:19:28 +0200271 // --------------------------------------------------------------------
272
273 /**
274 * Version number query string
275 *
276 * @return string
277 */
278 protected function _version()
279 {
280 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
284
285 /**
286 * Execute the query
287 *
288 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200289 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * upon successful execution of a "write" type query. Returns boolean
291 * FALSE upon failure, and if the $db_debug variable is set to TRUE
292 * will raise an error.
293 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @param string An SQL query string
295 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200296 * @return mixed
297 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500298 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100300 if ($sql === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200302 log_message('error', 'Invalid query: '.$sql);
303
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200304 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
306
307 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100308 if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
Derek Jonese7792202010-03-02 17:24:46 -0600309 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200310 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Derek Jonese7792202010-03-02 17:24:46 -0600312
Ryan Dialef7474c2012-03-01 16:11:36 -0500313 // Compile binds if needed
314 if ($binds !== FALSE)
315 {
316 $sql = $this->compile_binds($sql, $binds);
317 }
318
Andrey Andreev4c202602012-03-06 20:34:52 +0200319 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // we will load the caching class and return the previously
321 // cached query if it exists
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100322 if ($this->cache_on === TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200324 $this->load_rdriver();
325 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200327 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Jones37f4b9c2011-07-01 17:56:50 -0500331 // Save the query for debugging
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100332 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $this->queries[] = $sql;
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // Start the Query Timer
dixyab3cab52012-04-21 00:58:00 +0200338 $time_start = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // Run the Query
341 if (FALSE === ($this->result_id = $this->simple_query($sql)))
342 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100343 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 $this->query_times[] = 0;
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // This will trigger a rollback if transactions are being used
349 $this->_trans_status = FALSE;
350
Andrey Andreev4be5de12012-03-02 15:45:41 +0200351 // Grab the error now, as we might run some additional queries before displaying the error
352 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200353
354 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200355 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 if ($this->db_debug)
358 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200360 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200361 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // transactions to remain in limbo.
363 $this->trans_complete();
364
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200365 // Display errors
Andrey Andreev27984582012-03-03 04:43:10 +0200366 return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 return FALSE;
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // Stop and aggregate the query time results
dixyab3cab52012-04-21 00:58:00 +0200373 $time_end = microtime(TRUE);
374 $this->benchmark += $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000375
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100376 if ($this->save_queries === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
dixyab3cab52012-04-21 00:58:00 +0200378 $this->query_times[] = $time_end - $time_start;
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // Increment the query counter
382 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // Was the query a "write" type?
385 // If so we'll simply return true
386 if ($this->is_write_type($sql) === TRUE)
387 {
388 // If caching is enabled we'll auto-cleanup any
389 // existing files related to this particular URI
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100390 if ($this->cache_on === TRUE && $this->cache_autodel === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 $this->CACHE->delete();
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 return TRUE;
396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // Return TRUE if we don't need to create a result object
399 // Currently only the Oracle driver uses this when stored
400 // procedures are used
401 if ($return_object !== TRUE)
402 {
403 return TRUE;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
406 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200407 $driver = $this->load_rdriver();
408 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200409
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200410 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // result object and save it to a cache file.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100412 if ($this->cache_on === TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 // We'll create a new instance of the result object
415 // only without the platform specific driver since
416 // we can't use it with cached data (the query result
417 // resource ID won't be any good once we've cached the
418 // result object, so we'll have to compile the data
419 // and save it)
420 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 $CR->result_object = $RES->result_object();
422 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200423 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // Reset these since cached objects can not utilize resource IDs.
426 $CR->conn_id = NULL;
427 $CR->result_id = NULL;
428
429 $this->CACHE->write($sql, $CR);
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 return $RES;
433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Load the result drivers
439 *
Barry Mienydd671972010-10-04 16:33:58 +0200440 * @return string the name of the result class
441 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500442 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 $driver = 'CI_DB_'.$this->dbdriver.'_result';
445
446 if ( ! class_exists($driver))
447 {
Greg Aker3a746652011-04-19 10:59:47 -0500448 include_once(BASEPATH.'database/DB_result.php');
449 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 return $driver;
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
456
457 /**
458 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200459 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * we only use it when running transaction commands since they do
461 * not require all the features of the main query() function.
462 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200464 * @return mixed
465 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500466 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
468 if ( ! $this->conn_id)
469 {
470 $this->initialize();
471 }
472
473 return $this->_execute($sql);
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Disable Transactions
480 * This permits transactions to be disabled at run-time.
481 *
Barry Mienydd671972010-10-04 16:33:58 +0200482 * @return void
483 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500484 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
486 $this->trans_enabled = FALSE;
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Enable/disable Transaction Strict Mode
493 * When strict mode is enabled, if you are running multiple groups of
494 * transactions, if one group fails all groups will be rolled back.
495 * If strict mode is disabled, each group is treated autonomously, meaning
496 * a failure of one group will not affect any others
497 *
Barry Mienydd671972010-10-04 16:33:58 +0200498 * @return void
499 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500500 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
508 * Start Transaction
509 *
Barry Mienydd671972010-10-04 16:33:58 +0200510 * @return void
511 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500512 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200513 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 if ( ! $this->trans_enabled)
515 {
516 return FALSE;
517 }
518
519 // When transactions are nested we only begin/commit/rollback the outermost ones
520 if ($this->_trans_depth > 0)
521 {
522 $this->_trans_depth += 1;
523 return;
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500527 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529
530 // --------------------------------------------------------------------
531
532 /**
533 * Complete Transaction
534 *
Barry Mienydd671972010-10-04 16:33:58 +0200535 * @return bool
536 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500537 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 if ( ! $this->trans_enabled)
540 {
541 return FALSE;
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // When transactions are nested we only begin/commit/rollback the outermost ones
545 if ($this->_trans_depth > 1)
546 {
547 $this->_trans_depth -= 1;
548 return TRUE;
549 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500550 else
551 {
552 $this->_trans_depth = 0;
553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // The query() function will set this flag to FALSE in the event that a query failed
556 if ($this->_trans_status === FALSE)
557 {
558 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 // If we are NOT running in strict mode, we will reset
561 // the _trans_status flag so that subsequent groups of transactions
562 // will be permitted.
563 if ($this->trans_strict === FALSE)
564 {
565 $this->_trans_status = TRUE;
566 }
567
568 log_message('debug', 'DB Transaction Failure');
569 return FALSE;
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 $this->trans_commit();
573 return TRUE;
574 }
575
576 // --------------------------------------------------------------------
577
578 /**
579 * Lets you retrieve the transaction flag to determine if it has failed
580 *
Barry Mienydd671972010-10-04 16:33:58 +0200581 * @return bool
582 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500583 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 return $this->_trans_status;
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Compile Bindings
592 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 * @param string the sql statement
594 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200595 * @return string
596 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500597 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 if (strpos($sql, $this->bind_marker) === FALSE)
600 {
601 return $sql;
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 if ( ! is_array($binds))
605 {
606 $binds = array($binds);
607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 // Get the sql segments around the bind markers
610 $segments = explode($this->bind_marker, $sql);
611
612 // The count of bind should be 1 less then the count of segments
613 // If there are more bind arguments trim it down
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200614 if (count($binds) >= count($segments))
615 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 $binds = array_slice($binds, 0, count($segments)-1);
617 }
618
619 // Construct the binded query
620 $result = $segments[0];
621 $i = 0;
622 foreach ($binds as $bind)
623 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200624 $result .= $this->escape($bind).$segments[++$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 }
626
627 return $result;
628 }
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 // --------------------------------------------------------------------
631
632 /**
633 * Determines if a query is a "write" type.
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200636 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200637 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200638 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300640 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 // --------------------------------------------------------------------
644
645 /**
646 * Calculate the aggregate query elapsed time
647 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200648 * @param int The number of decimal places
649 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200650 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500651 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
653 return number_format($this->benchmark, $decimals);
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 // --------------------------------------------------------------------
657
658 /**
659 * Returns the total number of queries
660 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200661 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200662 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500663 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
665 return $this->query_count;
666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // --------------------------------------------------------------------
669
670 /**
671 * Returns the last query that was executed
672 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200673 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200674 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500675 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 return end($this->queries);
678 }
679
680 // --------------------------------------------------------------------
681
682 /**
683 * "Smart" Escape String
684 *
685 * Escapes data based on type
686 * Sets boolean and null types
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200689 * @return mixed
690 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500691 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200692 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500693 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200695 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000696 }
697 elseif (is_bool($str))
698 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200699 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000700 }
701 elseif (is_null($str))
702 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200703 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000704 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000705
706 return $str;
707 }
708
709 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600710
Derek Jonese4ed5832009-02-20 21:44:59 +0000711 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000712 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000713 *
714 * Calls the individual driver for platform
715 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200716 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000717 * @param string
718 * @return mixed
719 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500720 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200721 {
722 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000723 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000724
Derek Jonese4ed5832009-02-20 21:44:59 +0000725 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 /**
728 * Primary
729 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200730 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 * position is the primary key
732 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200734 * @return string
735 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500736 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200737 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200739 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741
742 // --------------------------------------------------------------------
743
744 /**
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300745 * "Count All" query
746 *
747 * Generates a platform-specific query string that counts all records in
748 * the specified database
749 *
750 * @param string
751 * @return int
752 */
753 public function count_all($table = '')
754 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100755 if ($table === '')
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300756 {
757 return 0;
758 }
759
760 $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 +0100761 if ($query->num_rows() === 0)
Andrey Andreev16bb9bd2012-05-26 18:21:14 +0300762 {
763 return 0;
764 }
765
766 $query = $query->row();
767 $this->_reset_select();
768 return (int) $query->numrows;
769 }
770
771 // --------------------------------------------------------------------
772
773 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * Returns an array of table names
775 *
Barry Mienydd671972010-10-04 16:33:58 +0200776 * @return array
777 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500778 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 // Is there a cached result?
781 if (isset($this->data_cache['table_names']))
782 {
783 return $this->data_cache['table_names'];
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
787 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200788 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
790
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200791 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200793
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200794 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200796 // Do we know from which column to get the table name?
797 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300799 if (isset($row['table_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200800 {
801 $key = 'table_name';
802 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300803 elseif (isset($row['TABLE_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200804 {
805 $key = 'TABLE_NAME';
806 }
807 else
808 {
809 /* We have no other choice but to just get the first element's key.
810 * Due to array_shift() accepting it's argument by reference, if
811 * E_STRICT is on, this would trigger a warning. So we'll have to
812 * assign it first.
813 */
814 $key = array_keys($row);
815 $key = array_shift($key);
816 }
Taufan Aditya18209332012-02-09 16:07:27 +0700817 }
818
Andrey Andreev12567e82012-01-25 22:50:33 +0200819 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 }
821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 return $this->data_cache['table_names'];
823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 // --------------------------------------------------------------------
826
827 /**
828 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500829 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200830 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500832 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200833 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200834 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 }
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 // --------------------------------------------------------------------
838
839 /**
840 * Fetch MySQL Field Names
841 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200843 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500845 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
847 // Is there a cached result?
848 if (isset($this->data_cache['field_names'][$table]))
849 {
850 return $this->data_cache['field_names'][$table];
851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100853 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200855 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Greg Aker1edde302010-01-26 00:17:01 +0000858 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200860 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200864 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200865
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500866 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200868 // Do we know from where to get the column's name?
869 if ( ! isset($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300871 if (isset($row['column_name']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200872 {
873 $key = 'column_name';
874 }
Andrey Andreev6781a5f2012-03-28 14:50:51 +0300875 elseif (isset($row['COLUMN_NAME']))
Andrey Andreev12567e82012-01-25 22:50:33 +0200876 {
877 $key = 'COLUMN_NAME';
878 }
879 else
880 {
881 /* We have no other choice but to just get the first element's key.
882 * Due to array_shift() accepting it's argument by reference, if
883 * E_STRICT is on, this would trigger a warning. So we'll have to
884 * assign it first.
885 */
886 $key = array_keys($row);
887 $key = array_shift($key);
888 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 }
Andrey Andreev12567e82012-01-25 22:50:33 +0200890
891 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 return $this->data_cache['field_names'][$table];
895 }
896
897 // --------------------------------------------------------------------
898
899 /**
900 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500901 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 * @param string
903 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200904 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500906 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200907 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200908 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
912
913 /**
914 * Returns an object with field data
915 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200917 * @return object
918 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500919 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100921 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200923 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
Barry Mienydd671972010-10-04 16:33:58 +0200925
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200926 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200928 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000929
930 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 /**
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300933 * Escape the SQL Identifiers
934 *
935 * This function escapes column and table names
936 *
937 * @param string
938 * @return string
939 */
940 public function escape_identifiers($item)
941 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100942 if ($this->_escape_char === '')
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300943 {
944 return $item;
945 }
946
947 foreach ($this->_reserved_identifiers as $id)
948 {
949 if (strpos($item, '.'.$id) !== FALSE)
950 {
951 $item = str_replace('.', $this->_escape_char.'.', $item);
952
953 // remove duplicates if the user already included the escape
954 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
955 }
956 }
957
958 if (strpos($item, '.') !== FALSE)
959 {
960 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
961 }
962
963 // remove duplicates if the user already included the escape
964 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
965 }
966
967 // --------------------------------------------------------------------
968
969 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 * Generate an insert string
971 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 * @param string the table upon which the query will be performed
973 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200974 * @return string
975 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500976 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200978 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200979
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500980 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300982 $fields[] = $this->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 $values[] = $this->escape($val);
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200986 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200987 }
988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 // --------------------------------------------------------------------
990
991 /**
992 * Generate an update string
993 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 * @param string the table upon which the query will be performed
995 * @param array an associative array data of key/values
996 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200997 * @return string
998 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500999 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001001 if ($where === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
Andrey Andreev4c202602012-03-06 20:34:52 +02001003 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001007 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001009 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
1011
1012 if ( ! is_array($where))
1013 {
1014 $dest = array($where);
1015 }
1016 else
1017 {
1018 $dest = array();
1019 foreach ($where as $key => $val)
1020 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001021 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001022 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 if ($val !== '')
1025 {
1026 if ( ! $this->_has_operator($key))
1027 {
1028 $key .= ' =';
1029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 $val = ' '.$this->escape($val);
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 $dest[] = $prefix.$key.$val;
1035 }
Barry Mienydd671972010-10-04 16:33:58 +02001036 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001037
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001038 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001039 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001040
1041 // --------------------------------------------------------------------
1042
1043 /**
1044 * Tests whether the string has an SQL operator
1045 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 * @param string
1047 * @return bool
1048 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001049 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001051 return (bool) preg_match('/(\s|<|>|!|=|IS NULL|IS NOT NULL)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 }
1053
1054 // --------------------------------------------------------------------
1055
1056 /**
1057 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1058 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 * @param string the function name
1060 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001061 * @return mixed
1062 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001063 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001065 $driver = ($this->dbdriver === 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 if (FALSE === strpos($driver, $function))
1068 {
1069 $function = $driver.$function;
1070 }
Barry Mienydd671972010-10-04 16:33:58 +02001071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 if ( ! function_exists($function))
1073 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001074 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001076
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001077 return (func_num_args() > 1)
1078 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1079 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 }
1081
1082 // --------------------------------------------------------------------
1083
1084 /**
1085 * Set Cache Directory Path
1086 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * @param string the path to the cache directory
1088 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001089 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001090 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 {
1092 $this->cachedir = $path;
1093 }
1094
1095 // --------------------------------------------------------------------
1096
1097 /**
1098 * Enable Query Caching
1099 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001100 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001101 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001102 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001104 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 }
1106
1107 // --------------------------------------------------------------------
1108
1109 /**
1110 * Disable Query Caching
1111 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001112 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001113 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001114 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001116 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119
1120 // --------------------------------------------------------------------
1121
1122 /**
1123 * Delete the cache files associated with a particular URI
1124 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001125 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001126 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001127 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001129 return ($this->_cache_init())
1130 ? $this->CACHE->delete($segment_one, $segment_two)
1131 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 }
1133
1134 // --------------------------------------------------------------------
1135
1136 /**
1137 * Delete All cache files
1138 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001139 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001140 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001141 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
Andrey Andreev043f2602012-03-06 20:16:42 +02001143 return ($this->_cache_init())
1144 ? $this->CACHE->delete_all()
1145 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 }
1147
1148 // --------------------------------------------------------------------
1149
1150 /**
1151 * Initialize the Cache Class
1152 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001153 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001154 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001155 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001157 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001159 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001160 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001161 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001162 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001164 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1165 {
1166 return $this->cache_off();
1167 }
Derek Allarde37ab382009-02-03 16:13:57 +00001168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1170 return TRUE;
1171 }
1172
1173 // --------------------------------------------------------------------
1174
1175 /**
1176 * Close DB Connection
1177 *
Barry Mienydd671972010-10-04 16:33:58 +02001178 * @return void
1179 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001180 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001182 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Andrey Andreev79922c02012-05-23 12:27:17 +03001184 $this->_close();
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001185 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 // --------------------------------------------------------------------
1190
1191 /**
Andrey Andreev79922c02012-05-23 12:27:17 +03001192 * Close DB Connection
1193 *
1194 * This method would be overriden by most of the drivers.
1195 *
1196 * @return void
1197 */
1198 protected function _close()
1199 {
1200 $this->conn_id = FALSE;
1201 }
1202
1203 // --------------------------------------------------------------------
1204
1205 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 * Display an error message
1207 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 * @param string the error message
1209 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001210 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001211 * @return string sends the application/error_db.php template
1212 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001213 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
Derek Jonese7792202010-03-02 17:24:46 -06001215 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 $LANG->load('db');
1217
1218 $heading = $LANG->line('db_error_heading');
1219
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001220 if ($native === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001222 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 }
1224 else
1225 {
1226 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1227 }
Barry Mienydd671972010-10-04 16:33:58 +02001228
Pascal Kriete60f8c392010-08-25 18:03:28 +02001229 // Find the most likely culprit of the error by going through
1230 // the backtrace until the source file is no longer in the
1231 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001232 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001233 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001234 {
1235 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1236 {
1237 // Found it - use a relative path for safety
1238 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1239 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001240 break;
1241 }
1242 }
Barry Mienydd671972010-10-04 16:33:58 +02001243
Derek Jonese7792202010-03-02 17:24:46 -06001244 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 echo $error->show_error($heading, $message, 'error_db');
1246 exit;
1247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
1252 * Protect Identifiers
1253 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001254 * This function is used extensively by the Query Builder class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001255 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001257 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001258 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 *
1260 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1261 *
1262 * Or a query with aliasing:
1263 *
1264 * SELECT m.member_id, m.member_name FROM members AS m
1265 *
1266 * Since the column name can include up to four segments (host, DB, table, column)
1267 * or also have an alias prefix, we need to do a bit of work to figure this out and
1268 * insert the table prefix (if it exists) in the proper position, and escape only
1269 * the correct identifiers.
1270 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 * @param string
1272 * @param bool
1273 * @param mixed
1274 * @param bool
1275 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001276 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001277 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 {
1279 if ( ! is_bool($protect_identifiers))
1280 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +00001281 $protect_identifiers = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 }
Derek Allarde37ab382009-02-03 16:13:57 +00001283
1284 if (is_array($item))
1285 {
1286 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001287 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001288 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001289 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001290 }
1291
1292 return $escaped_array;
1293 }
1294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001296 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 // If the item has an alias declaration we remove it and set it aside.
1299 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001301 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001302 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 $item = substr($item, 0, - strlen($alias));
1304 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001305 else
1306 {
1307 $alias = '';
1308 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001309
Derek Allard911d3e02008-12-15 14:08:35 +00001310 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001311 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001312 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001313 // way to deal with this, but I'm not thinking of it -- Rick
1314 if (strpos($item, '(') !== FALSE)
1315 {
1316 return $item.$alias;
1317 }
1318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 // Break the string apart if it contains periods, then insert the table prefix
1320 // in the correct location, assuming the period doesn't indicate that we're dealing
1321 // with an alias. While we're at it, we will escape the components
1322 if (strpos($item, '.') !== FALSE)
1323 {
1324 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001325
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001327 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 // we have nothing more to do other than escape the item
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001329 if (in_array($parts[0], $this->qb_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001330 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 if ($protect_identifiers === TRUE)
1332 {
1333 foreach ($parts as $key => $val)
1334 {
1335 if ( ! in_array($val, $this->_reserved_identifiers))
1336 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001337 $parts[$key] = $this->escape_identifiers($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 }
1339 }
Barry Mienydd671972010-10-04 16:33:58 +02001340
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001342 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 return $item.$alias;
1345 }
Barry Mienydd671972010-10-04 16:33:58 +02001346
Andrey Andreev4c202602012-03-06 20:34:52 +02001347 // Is there a table prefix defined in the config file? If not, no need to do anything
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001348 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 {
1350 // We now add the table prefix based on some logic.
1351 // Do we have 4 segments (hostname.database.table.column)?
1352 // If so, we add the table prefix to the column name in the 3rd segment.
1353 if (isset($parts[3]))
1354 {
1355 $i = 2;
1356 }
1357 // Do we have 3 segments (database.table.column)?
1358 // If so, we add the table prefix to the column name in 2nd position
1359 elseif (isset($parts[2]))
1360 {
1361 $i = 1;
1362 }
1363 // Do we have 2 segments (table.column)?
1364 // If so, we add the table prefix to the column name in 1st segment
1365 else
1366 {
1367 $i = 0;
1368 }
Barry Mienydd671972010-10-04 16:33:58 +02001369
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 // This flag is set when the supplied $item does not contain a field name.
1371 // This can happen when this function is being called from a JOIN.
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001372 if ($field_exists === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 {
1374 $i++;
1375 }
Barry Mienydd671972010-10-04 16:33:58 +02001376
Derek Jones55acc8b2009-07-11 03:38:13 +00001377 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001378 if ($this->swap_pre !== '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001379 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001380 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001381 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001383 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 {
1385 $parts[$i] = $this->dbprefix.$parts[$i];
1386 }
Barry Mienydd671972010-10-04 16:33:58 +02001387
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 // Put the parts back together
1389 $item = implode('.', $parts);
1390 }
Barry Mienydd671972010-10-04 16:33:58 +02001391
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 if ($protect_identifiers === TRUE)
1393 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001394 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 }
Barry Mienydd671972010-10-04 16:33:58 +02001396
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 return $item.$alias;
1398 }
1399
Andrey Andreev4c202602012-03-06 20:34:52 +02001400 // Is there a table prefix? If not, no need to insert it
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001401 if ($this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001403 // Verify table prefix and replace if necessary
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001404 if ($this->swap_pre !== '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001405 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001406 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001407 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 // Do we prefix an item with no segments?
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001409 elseif ($prefix_single === TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 {
1411 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001412 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
1414
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001415 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +03001417 $item = $this->escape_identifiers($item);
Derek Allard2067d1a2008-11-13 22:59:24 +00001418 }
Barry Mienydd671972010-10-04 16:33:58 +02001419
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 return $item.$alias;
1421 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001422
Túbal Martín511f2252011-11-24 14:43:45 +01001423 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001424
Túbal Martín511f2252011-11-24 14:43:45 +01001425 /**
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00001426 * Dummy method that allows Query Builder class to be disabled
Andrey Andreev16bb9bd2012-05-26 18:21:14 +03001427 * and keep count_all() working.
Túbal Martín511f2252011-11-24 14:43:45 +01001428 *
Túbal Martín511f2252011-11-24 14:43:45 +01001429 * @return void
1430 */
1431 protected function _reset_select()
1432 {
Túbal Martín511f2252011-11-24 14:43:45 +01001433 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001434
1435}
1436
Derek Allard2067d1a2008-11-13 22:59:24 +00001437/* End of file DB_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +03001438/* Location: ./system/database/DB_driver.php */