blob: 9f1a0b895feee493c81a0a9e3c755c817d35e8d5 [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
Derek Allard2067d1a2008-11-13 22:59:24 +000076
Andrey Andreevd1add432012-03-06 20:26:01 +020077 protected $_protect_identifiers = TRUE;
78 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000079
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
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
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 {
102 // If an existing connection resource is available
103 // there is no need to connect and select the database
104 if (is_resource($this->conn_id) OR is_object($this->conn_id))
105 {
106 return TRUE;
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // Connect to the database and set the connection ID
112 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
113
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200114 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if ( ! $this->conn_id)
116 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100117 // Check if there is a failover set
118 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100120 // Go over all the failovers
121 foreach ($this->failover as $failover)
122 {
123 // Replace the current settings with those of the failover
124 foreach ($failover as $key => $val)
125 {
126 $this->$key = $val;
127 }
128
129 // Try to connect
130 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
131
132 // If a connection is made break the foreach loop
133 if ($this->conn_id)
134 {
135 break;
136 }
137 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100139
140 // We still don't have a connection?
141 if ( ! $this->conn_id)
142 {
143 log_message('error', 'Unable to connect to the database');
144
145 if ($this->db_debug)
146 {
147 $this->display_error('db_unable_to_connect');
148 }
149 return FALSE;
150 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // ----------------------------------------------------------------
154
155 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200156 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200158 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200159
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200160 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200167 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200168 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
172
173 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200174 * Reconnect
175 *
176 * Keep / reestablish the db connection if no queries have been
177 * sent for a length of time exceeding the server's idle timeout.
178 *
179 * This is just a dummy method to allow drivers without such
180 * functionality to not declare it, while others will override it.
181 *
182 * @return void
183 */
184 public function reconnect()
185 {
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * Set client character set
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string
194 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200195 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200197 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200199 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200201 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 if ($this->db_debug)
204 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200205 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 return FALSE;
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 return TRUE;
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // --------------------------------------------------------------------
215
216 /**
217 * The name of the platform in use (mysql, mssql, etc...)
218 *
Barry Mienydd671972010-10-04 16:33:58 +0200219 * @return string
220 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500221 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return $this->dbdriver;
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200229 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200231 * Returns a string containing the version of the database being used.
232 * Most drivers will override this method.
233 *
Barry Mienydd671972010-10-04 16:33:58 +0200234 * @return string
235 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200236 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200238 if (isset($this->data_cache['version']))
239 {
240 return $this->data_cache['version'];
241 }
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 if (FALSE === ($sql = $this->_version()))
244 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200245 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Derek Allard3683f772009-12-16 17:32:33 +0000247
Andrey Andreev08856b82012-03-03 03:19:28 +0200248 $query = $this->query($sql);
249 $query = $query->row();
250 return $this->data_cache['version'] = $query->ver;
251 }
Derek Allard3683f772009-12-16 17:32:33 +0000252
Andrey Andreev08856b82012-03-03 03:19:28 +0200253 // --------------------------------------------------------------------
254
255 /**
256 * Version number query string
257 *
258 * @return string
259 */
260 protected function _version()
261 {
262 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // --------------------------------------------------------------------
266
267 /**
268 * Execute the query
269 *
270 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200271 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 * upon successful execution of a "write" type query. Returns boolean
273 * FALSE upon failure, and if the $db_debug variable is set to TRUE
274 * will raise an error.
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string An SQL query string
277 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200278 * @return mixed
279 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500280 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 if ($sql == '')
283 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200284 log_message('error', 'Invalid query: '.$sql);
285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($this->db_debug)
287 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 return $this->display_error('db_invalid_query');
289 }
290 return FALSE;
291 }
292
293 // Verify table prefix and replace if necessary
294 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600295 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
297 }
Derek Jonese7792202010-03-02 17:24:46 -0600298
Ryan Dialef7474c2012-03-01 16:11:36 -0500299 // Compile binds if needed
300 if ($binds !== FALSE)
301 {
302 $sql = $this->compile_binds($sql, $binds);
303 }
304
Andrey Andreev4c202602012-03-06 20:34:52 +0200305 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // we will load the caching class and return the previously
307 // cached query if it exists
308 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
309 {
310 if ($this->_cache_init())
311 {
312 $this->load_rdriver();
313 if (FALSE !== ($cache = $this->CACHE->read($sql)))
314 {
315 return $cache;
316 }
317 }
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Jones37f4b9c2011-07-01 17:56:50 -0500320 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 if ($this->save_queries == TRUE)
322 {
323 $this->queries[] = $sql;
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // Start the Query Timer
327 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // Run the Query
330 if (FALSE === ($this->result_id = $this->simple_query($sql)))
331 {
332 if ($this->save_queries == TRUE)
333 {
334 $this->query_times[] = 0;
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // This will trigger a rollback if transactions are being used
338 $this->_trans_status = FALSE;
339
Andrey Andreev4be5de12012-03-02 15:45:41 +0200340 // Grab the error now, as we might run some additional queries before displaying the error
341 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200342
343 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200344 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 if ($this->db_debug)
347 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200349 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200350 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // transactions to remain in limbo.
352 $this->trans_complete();
353
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200354 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 return $this->display_error(
Andrey Andreev4be5de12012-03-02 15:45:41 +0200356 array(
357 'Error Number: '.$error['code'],
358 $error['message'],
359 $sql
360 )
361 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 return FALSE;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // Stop and aggregate the query time results
368 $time_end = list($em, $es) = explode(' ', microtime());
369 $this->benchmark += ($em + $es) - ($sm + $ss);
370
371 if ($this->save_queries == TRUE)
372 {
373 $this->query_times[] = ($em + $es) - ($sm + $ss);
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // Increment the query counter
377 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // Was the query a "write" type?
380 // If so we'll simply return true
381 if ($this->is_write_type($sql) === TRUE)
382 {
383 // If caching is enabled we'll auto-cleanup any
384 // existing files related to this particular URI
385 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
386 {
387 $this->CACHE->delete();
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 return TRUE;
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Return TRUE if we don't need to create a result object
394 // Currently only the Oracle driver uses this when stored
395 // procedures are used
396 if ($return_object !== TRUE)
397 {
398 return TRUE;
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
401 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200402 $driver = $this->load_rdriver();
403 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Jones37f4b9c2011-07-01 17:56:50 -0500407 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // result object and save it to a cache file.
409 if ($this->cache_on == TRUE AND $this->_cache_init())
410 {
411 // We'll create a new instance of the result object
412 // only without the platform specific driver since
413 // we can't use it with cached data (the query result
414 // resource ID won't be any good once we've cached the
415 // result object, so we'll have to compile the data
416 // and save it)
417 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200418 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 $CR->result_object = $RES->result_object();
420 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // Reset these since cached objects can not utilize resource IDs.
423 $CR->conn_id = NULL;
424 $CR->result_id = NULL;
425
426 $this->CACHE->write($sql, $CR);
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 return $RES;
430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Load the result drivers
436 *
Barry Mienydd671972010-10-04 16:33:58 +0200437 * @return string the name of the result class
438 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500439 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 $driver = 'CI_DB_'.$this->dbdriver.'_result';
442
443 if ( ! class_exists($driver))
444 {
Greg Aker3a746652011-04-19 10:59:47 -0500445 include_once(BASEPATH.'database/DB_result.php');
446 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 return $driver;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
453
454 /**
455 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200456 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * we only use it when running transaction commands since they do
458 * not require all the features of the main query() function.
459 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200461 * @return mixed
462 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500463 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 if ( ! $this->conn_id)
466 {
467 $this->initialize();
468 }
469
470 return $this->_execute($sql);
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // --------------------------------------------------------------------
474
475 /**
476 * Disable Transactions
477 * This permits transactions to be disabled at run-time.
478 *
Barry Mienydd671972010-10-04 16:33:58 +0200479 * @return void
480 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500481 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 $this->trans_enabled = FALSE;
484 }
485
486 // --------------------------------------------------------------------
487
488 /**
489 * Enable/disable Transaction Strict Mode
490 * When strict mode is enabled, if you are running multiple groups of
491 * transactions, if one group fails all groups will be rolled back.
492 * If strict mode is disabled, each group is treated autonomously, meaning
493 * a failure of one group will not affect any others
494 *
Barry Mienydd671972010-10-04 16:33:58 +0200495 * @return void
496 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500497 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 // --------------------------------------------------------------------
503
504 /**
505 * Start Transaction
506 *
Barry Mienydd671972010-10-04 16:33:58 +0200507 * @return void
508 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500509 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200510 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 if ( ! $this->trans_enabled)
512 {
513 return FALSE;
514 }
515
516 // When transactions are nested we only begin/commit/rollback the outermost ones
517 if ($this->_trans_depth > 0)
518 {
519 $this->_trans_depth += 1;
520 return;
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500524 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526
527 // --------------------------------------------------------------------
528
529 /**
530 * Complete Transaction
531 *
Barry Mienydd671972010-10-04 16:33:58 +0200532 * @return bool
533 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500534 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
536 if ( ! $this->trans_enabled)
537 {
538 return FALSE;
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 // When transactions are nested we only begin/commit/rollback the outermost ones
542 if ($this->_trans_depth > 1)
543 {
544 $this->_trans_depth -= 1;
545 return TRUE;
546 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500547 else
548 {
549 $this->_trans_depth = 0;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // The query() function will set this flag to FALSE in the event that a query failed
553 if ($this->_trans_status === FALSE)
554 {
555 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 // If we are NOT running in strict mode, we will reset
558 // the _trans_status flag so that subsequent groups of transactions
559 // will be permitted.
560 if ($this->trans_strict === FALSE)
561 {
562 $this->_trans_status = TRUE;
563 }
564
565 log_message('debug', 'DB Transaction Failure');
566 return FALSE;
567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 $this->trans_commit();
570 return TRUE;
571 }
572
573 // --------------------------------------------------------------------
574
575 /**
576 * Lets you retrieve the transaction flag to determine if it has failed
577 *
Barry Mienydd671972010-10-04 16:33:58 +0200578 * @return bool
579 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500580 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 return $this->_trans_status;
583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Compile Bindings
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @param string the sql statement
591 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200592 * @return string
593 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500594 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 if (strpos($sql, $this->bind_marker) === FALSE)
597 {
598 return $sql;
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 if ( ! is_array($binds))
602 {
603 $binds = array($binds);
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // Get the sql segments around the bind markers
607 $segments = explode($this->bind_marker, $sql);
608
609 // The count of bind should be 1 less then the count of segments
610 // If there are more bind arguments trim it down
611 if (count($binds) >= count($segments)) {
612 $binds = array_slice($binds, 0, count($segments)-1);
613 }
614
615 // Construct the binded query
616 $result = $segments[0];
617 $i = 0;
618 foreach ($binds as $bind)
619 {
620 $result .= $this->escape($bind);
621 $result .= $segments[++$i];
622 }
623
624 return $result;
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 // --------------------------------------------------------------------
628
629 /**
630 * Determines if a query is a "write" type.
631 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200633 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200634 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200635 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200637 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|OPTIMIZE|REINDEX)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 // --------------------------------------------------------------------
641
642 /**
643 * Calculate the aggregate query elapsed time
644 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200645 * @param int The number of decimal places
646 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200647 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500648 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 return number_format($this->benchmark, $decimals);
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
656 * Returns the total number of queries
657 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200658 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200659 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500660 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
662 return $this->query_count;
663 }
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 // --------------------------------------------------------------------
666
667 /**
668 * Returns the last query that was executed
669 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200670 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200671 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500672 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
674 return end($this->queries);
675 }
676
677 // --------------------------------------------------------------------
678
679 /**
680 * "Smart" Escape String
681 *
682 * Escapes data based on type
683 * Sets boolean and null types
684 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200686 * @return mixed
687 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500688 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200689 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500690 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000692 $str = "'".$this->escape_str($str)."'";
693 }
694 elseif (is_bool($str))
695 {
696 $str = ($str === FALSE) ? 0 : 1;
697 }
698 elseif (is_null($str))
699 {
700 $str = 'NULL';
701 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000702
703 return $str;
704 }
705
706 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600707
Derek Jonese4ed5832009-02-20 21:44:59 +0000708 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000709 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000710 *
711 * Calls the individual driver for platform
712 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200713 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000714 * @param string
715 * @return mixed
716 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500717 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200718 {
719 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000720 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000721
Derek Jonese4ed5832009-02-20 21:44:59 +0000722 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600723
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 /**
725 * Primary
726 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200727 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * position is the primary key
729 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200731 * @return string
732 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500733 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200734 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 if ( ! is_array($fields))
738 {
739 return FALSE;
740 }
741
742 return current($fields);
743 }
744
745 // --------------------------------------------------------------------
746
747 /**
748 * Returns an array of table names
749 *
Barry Mienydd671972010-10-04 16:33:58 +0200750 * @return array
751 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500752 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
754 // Is there a cached result?
755 if (isset($this->data_cache['table_names']))
756 {
757 return $this->data_cache['table_names'];
758 }
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
761 {
762 if ($this->db_debug)
763 {
764 return $this->display_error('db_unsupported_function');
765 }
766 return FALSE;
767 }
768
769 $retval = array();
770 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 if ($query->num_rows() > 0)
773 {
Taufan Aditya18209332012-02-09 16:07:27 +0700774 $table = FALSE;
775 $rows = $query->result_array();
776 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
777
778 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
Taufan Aditya18209332012-02-09 16:07:27 +0700780 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
781 }
782
783 foreach ($rows as $row)
784 {
785 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
787 }
788
789 $this->data_cache['table_names'] = $retval;
Andrey Andreev4c202602012-03-06 20:34:52 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 return $this->data_cache['table_names'];
792 }
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 // --------------------------------------------------------------------
795
796 /**
797 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500798 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200799 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500801 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200802 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200803 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 // --------------------------------------------------------------------
807
808 /**
809 * Fetch MySQL Field Names
810 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200812 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500814 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
816 // Is there a cached result?
817 if (isset($this->data_cache['field_names'][$table]))
818 {
819 return $this->data_cache['field_names'][$table];
820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 if ($table == '')
823 {
824 if ($this->db_debug)
825 {
826 return $this->display_error('db_field_param_missing');
827 }
828 return FALSE;
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Greg Aker1edde302010-01-26 00:17:01 +0000831 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
833 if ($this->db_debug)
834 {
835 return $this->display_error('db_unsupported_function');
836 }
837 return FALSE;
838 }
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500843 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
845 if (isset($row['COLUMN_NAME']))
846 {
847 $retval[] = $row['COLUMN_NAME'];
848 }
849 else
850 {
851 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200852 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 $this->data_cache['field_names'][$table] = $retval;
856 return $this->data_cache['field_names'][$table];
857 }
858
859 // --------------------------------------------------------------------
860
861 /**
862 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500863 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 * @param string
865 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200866 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500868 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200869 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
871 }
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 // --------------------------------------------------------------------
874
875 /**
876 * Returns an object with field data
877 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200879 * @return object
880 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500881 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
883 if ($table == '')
884 {
885 if ($this->db_debug)
886 {
887 return $this->display_error('db_field_param_missing');
888 }
889 return FALSE;
890 }
Barry Mienydd671972010-10-04 16:33:58 +0200891
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200892 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000893
894 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200895 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000896
897 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 /**
900 * Generate an insert string
901 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 * @param string the table upon which the query will be performed
903 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200904 * @return string
905 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500906 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
908 $fields = array();
909 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200910
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500911 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
913 $fields[] = $this->_escape_identifiers($key);
914 $values[] = $this->escape($val);
915 }
Barry Mienydd671972010-10-04 16:33:58 +0200916
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200917 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200918 }
919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 // --------------------------------------------------------------------
921
922 /**
923 * Generate an update string
924 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @param string the table upon which the query will be performed
926 * @param array an associative array data of key/values
927 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200928 * @return string
929 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500930 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 if ($where == '')
933 {
Andrey Andreev4c202602012-03-06 20:34:52 +0200934 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500938 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200940 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 }
942
943 if ( ! is_array($where))
944 {
945 $dest = array($where);
946 }
947 else
948 {
949 $dest = array();
950 foreach ($where as $key => $val)
951 {
952 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200953 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 if ($val !== '')
956 {
957 if ( ! $this->_has_operator($key))
958 {
959 $key .= ' =';
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 $val = ' '.$this->escape($val);
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 $dest[] = $prefix.$key.$val;
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000968
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200969 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200970 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000971
972 // --------------------------------------------------------------------
973
974 /**
975 * Tests whether the string has an SQL operator
976 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @param string
978 * @return bool
979 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500980 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 $str = trim($str);
983 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
984 {
985 return FALSE;
986 }
987
988 return TRUE;
989 }
990
991 // --------------------------------------------------------------------
992
993 /**
994 * Enables a native PHP function to be run, using a platform agnostic wrapper.
995 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 * @param string the function name
997 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200998 * @return mixed
999 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001000 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
1002 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001003
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 if (FALSE === strpos($driver, $function))
1005 {
1006 $function = $driver.$function;
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 if ( ! function_exists($function))
1010 {
1011 if ($this->db_debug)
1012 {
1013 return $this->display_error('db_unsupported_function');
1014 }
1015 return FALSE;
1016 }
1017 else
1018 {
1019 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1020
Repox71b78092011-12-01 09:19:43 +01001021 if (is_null($args))
1022 {
1023 return call_user_func($function);
1024 }
1025 else
1026 {
1027 return call_user_func_array($function, $args);
1028 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 }
1030 }
1031
1032 // --------------------------------------------------------------------
1033
1034 /**
1035 * Set Cache Directory Path
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string the path to the cache directory
1038 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001039 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001040 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
1042 $this->cachedir = $path;
1043 }
1044
1045 // --------------------------------------------------------------------
1046
1047 /**
1048 * Enable Query Caching
1049 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001050 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001051 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001052 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 {
1054 $this->cache_on = TRUE;
1055 return TRUE;
1056 }
1057
1058 // --------------------------------------------------------------------
1059
1060 /**
1061 * Disable Query Caching
1062 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001063 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001064 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001065 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 {
1067 $this->cache_on = FALSE;
1068 return FALSE;
1069 }
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071
1072 // --------------------------------------------------------------------
1073
1074 /**
1075 * Delete the cache files associated with a particular URI
1076 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001077 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001078 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001079 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
1081 if ( ! $this->_cache_init())
1082 {
1083 return FALSE;
1084 }
1085 return $this->CACHE->delete($segment_one, $segment_two);
1086 }
1087
1088 // --------------------------------------------------------------------
1089
1090 /**
1091 * Delete All cache files
1092 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001093 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001094 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001095 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
1097 if ( ! $this->_cache_init())
1098 {
1099 return FALSE;
1100 }
1101
1102 return $this->CACHE->delete_all();
1103 }
1104
1105 // --------------------------------------------------------------------
1106
1107 /**
1108 * Initialize the Cache Class
1109 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001110 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001111 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001112 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
1114 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1115 {
1116 return TRUE;
1117 }
Derek Allarde37ab382009-02-03 16:13:57 +00001118
1119 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
Greg Aker3a746652011-04-19 10:59:47 -05001121 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001122 {
1123 return $this->cache_off();
1124 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 }
Derek Allarde37ab382009-02-03 16:13:57 +00001126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1128 return TRUE;
1129 }
1130
1131 // --------------------------------------------------------------------
1132
1133 /**
1134 * Close DB Connection
1135 *
Barry Mienydd671972010-10-04 16:33:58 +02001136 * @return void
1137 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001138 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
1140 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1141 {
1142 $this->_close($this->conn_id);
1143 }
1144 $this->conn_id = FALSE;
1145 }
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // --------------------------------------------------------------------
1148
1149 /**
1150 * Display an error message
1151 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * @param string the error message
1153 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001154 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001155 * @return string sends the application/error_db.php template
1156 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001157 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Derek Jonese7792202010-03-02 17:24:46 -06001159 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 $LANG->load('db');
1161
1162 $heading = $LANG->line('db_error_heading');
1163
1164 if ($native == TRUE)
1165 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001166 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 }
1168 else
1169 {
1170 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1171 }
Barry Mienydd671972010-10-04 16:33:58 +02001172
Pascal Kriete60f8c392010-08-25 18:03:28 +02001173 // Find the most likely culprit of the error by going through
1174 // the backtrace until the source file is no longer in the
1175 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001176
Pascal Kriete60f8c392010-08-25 18:03:28 +02001177 $trace = debug_backtrace();
1178
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001179 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001180 {
1181 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1182 {
1183 // Found it - use a relative path for safety
1184 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1185 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001186
Pascal Kriete60f8c392010-08-25 18:03:28 +02001187 break;
1188 }
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Jonese7792202010-03-02 17:24:46 -06001191 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 echo $error->show_error($heading, $message, 'error_db');
1193 exit;
1194 }
1195
1196 // --------------------------------------------------------------------
1197
1198 /**
1199 * Protect Identifiers
1200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001202 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001204 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001205 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 *
1207 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1208 *
1209 * Or a query with aliasing:
1210 *
1211 * SELECT m.member_id, m.member_name FROM members AS m
1212 *
1213 * Since the column name can include up to four segments (host, DB, table, column)
1214 * or also have an alias prefix, we need to do a bit of work to figure this out and
1215 * insert the table prefix (if it exists) in the proper position, and escape only
1216 * the correct identifiers.
1217 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 * @param string
1219 * @param bool
1220 * @param mixed
1221 * @param bool
1222 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001223 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001224 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
1226 if ( ! is_bool($protect_identifiers))
1227 {
1228 $protect_identifiers = $this->_protect_identifiers;
1229 }
Derek Allarde37ab382009-02-03 16:13:57 +00001230
1231 if (is_array($item))
1232 {
1233 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001234 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001235 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001236 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001237 }
1238
1239 return $escaped_array;
1240 }
1241
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001243 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // If the item has an alias declaration we remove it and set it aside.
1246 // Basically we remove everything to the right of the first space
1247 $alias = '';
1248 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001249 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 $alias = strstr($item, " ");
1251 $item = substr($item, 0, - strlen($alias));
1252 }
1253
Derek Allard911d3e02008-12-15 14:08:35 +00001254 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001255 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001256 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001257 // way to deal with this, but I'm not thinking of it -- Rick
1258 if (strpos($item, '(') !== FALSE)
1259 {
1260 return $item.$alias;
1261 }
1262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 // Break the string apart if it contains periods, then insert the table prefix
1264 // in the correct location, assuming the period doesn't indicate that we're dealing
1265 // with an alias. While we're at it, we will escape the components
1266 if (strpos($item, '.') !== FALSE)
1267 {
1268 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001271 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 // we have nothing more to do other than escape the item
1273 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001274 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 if ($protect_identifiers === TRUE)
1276 {
1277 foreach ($parts as $key => $val)
1278 {
1279 if ( ! in_array($val, $this->_reserved_identifiers))
1280 {
1281 $parts[$key] = $this->_escape_identifiers($val);
1282 }
1283 }
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001286 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 return $item.$alias;
1288 }
Barry Mienydd671972010-10-04 16:33:58 +02001289
Andrey Andreev4c202602012-03-06 20:34:52 +02001290 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 if ($this->dbprefix != '')
1292 {
1293 // We now add the table prefix based on some logic.
1294 // Do we have 4 segments (hostname.database.table.column)?
1295 // If so, we add the table prefix to the column name in the 3rd segment.
1296 if (isset($parts[3]))
1297 {
1298 $i = 2;
1299 }
1300 // Do we have 3 segments (database.table.column)?
1301 // If so, we add the table prefix to the column name in 2nd position
1302 elseif (isset($parts[2]))
1303 {
1304 $i = 1;
1305 }
1306 // Do we have 2 segments (table.column)?
1307 // If so, we add the table prefix to the column name in 1st segment
1308 else
1309 {
1310 $i = 0;
1311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 // This flag is set when the supplied $item does not contain a field name.
1314 // This can happen when this function is being called from a JOIN.
1315 if ($field_exists == FALSE)
1316 {
1317 $i++;
1318 }
Barry Mienydd671972010-10-04 16:33:58 +02001319
Derek Jones55acc8b2009-07-11 03:38:13 +00001320 // Verify table prefix and replace if necessary
1321 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1322 {
1323 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1324 }
Barry Mienydd671972010-10-04 16:33:58 +02001325
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 // We only add the table prefix if it does not already exist
1327 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1328 {
1329 $parts[$i] = $this->dbprefix.$parts[$i];
1330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 // Put the parts back together
1333 $item = implode('.', $parts);
1334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 if ($protect_identifiers === TRUE)
1337 {
1338 $item = $this->_escape_identifiers($item);
1339 }
Barry Mienydd671972010-10-04 16:33:58 +02001340
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 return $item.$alias;
1342 }
1343
Andrey Andreev4c202602012-03-06 20:34:52 +02001344 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 if ($this->dbprefix != '')
1346 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001347 // Verify table prefix and replace if necessary
1348 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1349 {
1350 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1351 }
1352
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 // Do we prefix an item with no segments?
1354 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1355 {
1356 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001357 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 }
1359
1360 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1361 {
1362 $item = $this->_escape_identifiers($item);
1363 }
Barry Mienydd671972010-10-04 16:33:58 +02001364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 return $item.$alias;
1366 }
1367
Túbal Martín511f2252011-11-24 14:43:45 +01001368 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001369
Túbal Martín511f2252011-11-24 14:43:45 +01001370 /**
1371 * Dummy method that allows Active Record class to be disabled
1372 *
1373 * This function is used extensively by every db driver.
1374 *
Túbal Martín511f2252011-11-24 14:43:45 +01001375 * @return void
1376 */
Timothy Warren7eeda532012-03-19 16:01:55 -04001377 abstract protected function _reset_select();
Derek Allard2067d1a2008-11-13 22:59:24 +00001378
1379}
1380
Derek Allard2067d1a2008-11-13 22:59:24 +00001381/* End of file DB_driver.php */
Timothy Warrend2ff0bc2012-03-19 16:52:10 -04001382/* Location: ./system/database/DB_driver.php */