blob: af496aa7fcbf050420cd17ad8ce26d0691856770 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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 */
41class CI_DB_driver {
42
Andrey Andreevd1add432012-03-06 20:26:01 +020043 public $username;
44 public $password;
45 public $hostname;
46 public $database;
47 public $dbdriver = 'mysql';
48 public $dbprefix = '';
49 public $char_set = 'utf8';
50 public $dbcollat = 'utf8_general_ci';
51 public $autoinit = TRUE; // Whether to automatically initialize the DB
52 public $swap_pre = '';
53 public $port = '';
54 public $pconnect = FALSE;
55 public $conn_id = FALSE;
56 public $result_id = FALSE;
57 public $db_debug = FALSE;
58 public $benchmark = 0;
59 public $query_count = 0;
60 public $bind_marker = '?';
61 public $save_queries = TRUE;
62 public $queries = array();
63 public $query_times = array();
64 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000065
Andrey Andreevd1add432012-03-06 20:26:01 +020066 public $trans_enabled = TRUE;
67 public $trans_strict = TRUE;
68 protected $_trans_depth = 0;
69 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000070
Andrey Andreevd1add432012-03-06 20:26:01 +020071 public $cache_on = FALSE;
72 public $cachedir = '';
73 public $cache_autodel = FALSE;
74 public $CACHE; // The cache class object
75
76 protected $_protect_identifiers = TRUE;
77 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
78
Timothy Warrene45518d2012-03-06 07:38:00 -050079 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
81 if (is_array($params))
82 {
83 foreach ($params as $key => $val)
84 {
85 $this->$key = $val;
86 }
87 }
88
89 log_message('debug', 'Database Driver Class Initialized');
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 // --------------------------------------------------------------------
93
94 /**
95 * Initialize Database Settings
96 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +020097 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020098 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +020099 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 // If an existing connection resource is available
102 // there is no need to connect and select the database
103 if (is_resource($this->conn_id) OR is_object($this->conn_id))
104 {
105 return TRUE;
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // Connect to the database and set the connection ID
111 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
112
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200113 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if ( ! $this->conn_id)
115 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100116 // Check if there is a failover set
117 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100119 // Go over all the failovers
120 foreach ($this->failover as $failover)
121 {
122 // Replace the current settings with those of the failover
123 foreach ($failover as $key => $val)
124 {
125 $this->$key = $val;
126 }
127
128 // Try to connect
129 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
130
131 // If a connection is made break the foreach loop
132 if ($this->conn_id)
133 {
134 break;
135 }
136 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100138
139 // We still don't have a connection?
140 if ( ! $this->conn_id)
141 {
142 log_message('error', 'Unable to connect to the database');
143
144 if ($this->db_debug)
145 {
146 $this->display_error('db_unable_to_connect');
147 }
148 return FALSE;
149 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
152 // ----------------------------------------------------------------
153
154 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200155 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200157 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200158
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200159 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200161 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200163 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200166 // Now we set the character set and that's all
167 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // --------------------------------------------------------------------
171
172 /**
173 * Set client character set
174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string
176 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200177 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 */
Andrey Andreev063f5962012-02-27 12:20:52 +0200179 public function db_set_charset($charset, $collation = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200181 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset, $collation))
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200183 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 if ($this->db_debug)
186 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200187 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 return FALSE;
191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 return TRUE;
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // --------------------------------------------------------------------
197
198 /**
199 * The name of the platform in use (mysql, mssql, etc...)
200 *
Barry Mienydd671972010-10-04 16:33:58 +0200201 * @return string
202 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500203 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 return $this->dbdriver;
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200211 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200213 * Returns a string containing the version of the database being used.
214 * Most drivers will override this method.
215 *
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return string
217 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200218 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200220 if (isset($this->data_cache['version']))
221 {
222 return $this->data_cache['version'];
223 }
224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 if (FALSE === ($sql = $this->_version()))
226 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200227 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Derek Allard3683f772009-12-16 17:32:33 +0000229
Andrey Andreev08856b82012-03-03 03:19:28 +0200230 $query = $this->query($sql);
231 $query = $query->row();
232 return $this->data_cache['version'] = $query->ver;
233 }
Derek Allard3683f772009-12-16 17:32:33 +0000234
Andrey Andreev08856b82012-03-03 03:19:28 +0200235 // --------------------------------------------------------------------
236
237 /**
238 * Version number query string
239 *
240 * @return string
241 */
242 protected function _version()
243 {
244 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // --------------------------------------------------------------------
248
249 /**
250 * Execute the query
251 *
252 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200253 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * upon successful execution of a "write" type query. Returns boolean
255 * FALSE upon failure, and if the $db_debug variable is set to TRUE
256 * will raise an error.
257 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * @param string An SQL query string
259 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200260 * @return mixed
261 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500262 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 if ($sql == '')
265 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200266 log_message('error', 'Invalid query: '.$sql);
267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 if ($this->db_debug)
269 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 return $this->display_error('db_invalid_query');
271 }
272 return FALSE;
273 }
274
275 // Verify table prefix and replace if necessary
276 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600277 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
279 }
Derek Jonese7792202010-03-02 17:24:46 -0600280
Ryan Dialef7474c2012-03-01 16:11:36 -0500281 // Compile binds if needed
282 if ($binds !== FALSE)
283 {
284 $sql = $this->compile_binds($sql, $binds);
285 }
286
Andrey Andreev4c202602012-03-06 20:34:52 +0200287 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // we will load the caching class and return the previously
289 // cached query if it exists
290 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
291 {
292 if ($this->_cache_init())
293 {
294 $this->load_rdriver();
295 if (FALSE !== ($cache = $this->CACHE->read($sql)))
296 {
297 return $cache;
298 }
299 }
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jones37f4b9c2011-07-01 17:56:50 -0500302 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 if ($this->save_queries == TRUE)
304 {
305 $this->queries[] = $sql;
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // Start the Query Timer
309 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // Run the Query
312 if (FALSE === ($this->result_id = $this->simple_query($sql)))
313 {
314 if ($this->save_queries == TRUE)
315 {
316 $this->query_times[] = 0;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // This will trigger a rollback if transactions are being used
320 $this->_trans_status = FALSE;
321
Andrey Andreev4be5de12012-03-02 15:45:41 +0200322 // Grab the error now, as we might run some additional queries before displaying the error
323 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200324
325 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200326 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 if ($this->db_debug)
329 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200331 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200332 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // transactions to remain in limbo.
334 $this->trans_complete();
335
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200336 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 return $this->display_error(
Andrey Andreev4be5de12012-03-02 15:45:41 +0200338 array(
339 'Error Number: '.$error['code'],
340 $error['message'],
341 $sql
342 )
343 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 return FALSE;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // Stop and aggregate the query time results
350 $time_end = list($em, $es) = explode(' ', microtime());
351 $this->benchmark += ($em + $es) - ($sm + $ss);
352
353 if ($this->save_queries == TRUE)
354 {
355 $this->query_times[] = ($em + $es) - ($sm + $ss);
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // Increment the query counter
359 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // Was the query a "write" type?
362 // If so we'll simply return true
363 if ($this->is_write_type($sql) === TRUE)
364 {
365 // If caching is enabled we'll auto-cleanup any
366 // existing files related to this particular URI
367 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
368 {
369 $this->CACHE->delete();
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 return TRUE;
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // Return TRUE if we don't need to create a result object
376 // Currently only the Oracle driver uses this when stored
377 // procedures are used
378 if ($return_object !== TRUE)
379 {
380 return TRUE;
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
383 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200384 $driver = $this->load_rdriver();
385 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Jones37f4b9c2011-07-01 17:56:50 -0500389 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // result object and save it to a cache file.
391 if ($this->cache_on == TRUE AND $this->_cache_init())
392 {
393 // We'll create a new instance of the result object
394 // only without the platform specific driver since
395 // we can't use it with cached data (the query result
396 // resource ID won't be any good once we've cached the
397 // result object, so we'll have to compile the data
398 // and save it)
399 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200400 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 $CR->result_object = $RES->result_object();
402 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // Reset these since cached objects can not utilize resource IDs.
405 $CR->conn_id = NULL;
406 $CR->result_id = NULL;
407
408 $this->CACHE->write($sql, $CR);
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 return $RES;
412 }
413
414 // --------------------------------------------------------------------
415
416 /**
417 * Load the result drivers
418 *
Barry Mienydd671972010-10-04 16:33:58 +0200419 * @return string the name of the result class
420 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500421 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $driver = 'CI_DB_'.$this->dbdriver.'_result';
424
425 if ( ! class_exists($driver))
426 {
Greg Aker3a746652011-04-19 10:59:47 -0500427 include_once(BASEPATH.'database/DB_result.php');
428 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 return $driver;
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
435
436 /**
437 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200438 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * we only use it when running transaction commands since they do
440 * not require all the features of the main query() function.
441 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200443 * @return mixed
444 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500445 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 if ( ! $this->conn_id)
448 {
449 $this->initialize();
450 }
451
452 return $this->_execute($sql);
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
456
457 /**
458 * Disable Transactions
459 * This permits transactions to be disabled at run-time.
460 *
Barry Mienydd671972010-10-04 16:33:58 +0200461 * @return void
462 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500463 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 $this->trans_enabled = FALSE;
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Enable/disable Transaction Strict Mode
472 * When strict mode is enabled, if you are running multiple groups of
473 * transactions, if one group fails all groups will be rolled back.
474 * If strict mode is disabled, each group is treated autonomously, meaning
475 * a failure of one group will not affect any others
476 *
Barry Mienydd671972010-10-04 16:33:58 +0200477 * @return void
478 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500479 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
481 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
485
486 /**
487 * Start Transaction
488 *
Barry Mienydd671972010-10-04 16:33:58 +0200489 * @return void
490 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500491 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200492 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 if ( ! $this->trans_enabled)
494 {
495 return FALSE;
496 }
497
498 // When transactions are nested we only begin/commit/rollback the outermost ones
499 if ($this->_trans_depth > 0)
500 {
501 $this->_trans_depth += 1;
502 return;
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500506 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Complete Transaction
513 *
Barry Mienydd671972010-10-04 16:33:58 +0200514 * @return bool
515 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500516 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 if ( ! $this->trans_enabled)
519 {
520 return FALSE;
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 // When transactions are nested we only begin/commit/rollback the outermost ones
524 if ($this->_trans_depth > 1)
525 {
526 $this->_trans_depth -= 1;
527 return TRUE;
528 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500529 else
530 {
531 $this->_trans_depth = 0;
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 // The query() function will set this flag to FALSE in the event that a query failed
535 if ($this->_trans_status === FALSE)
536 {
537 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // If we are NOT running in strict mode, we will reset
540 // the _trans_status flag so that subsequent groups of transactions
541 // will be permitted.
542 if ($this->trans_strict === FALSE)
543 {
544 $this->_trans_status = TRUE;
545 }
546
547 log_message('debug', 'DB Transaction Failure');
548 return FALSE;
549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 $this->trans_commit();
552 return TRUE;
553 }
554
555 // --------------------------------------------------------------------
556
557 /**
558 * Lets you retrieve the transaction flag to determine if it has failed
559 *
Barry Mienydd671972010-10-04 16:33:58 +0200560 * @return bool
561 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500562 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 {
564 return $this->_trans_status;
565 }
566
567 // --------------------------------------------------------------------
568
569 /**
570 * Compile Bindings
571 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 * @param string the sql statement
573 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200574 * @return string
575 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500576 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 if (strpos($sql, $this->bind_marker) === FALSE)
579 {
580 return $sql;
581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 if ( ! is_array($binds))
584 {
585 $binds = array($binds);
586 }
Barry Mienydd671972010-10-04 16:33:58 +0200587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 // Get the sql segments around the bind markers
589 $segments = explode($this->bind_marker, $sql);
590
591 // The count of bind should be 1 less then the count of segments
592 // If there are more bind arguments trim it down
593 if (count($binds) >= count($segments)) {
594 $binds = array_slice($binds, 0, count($segments)-1);
595 }
596
597 // Construct the binded query
598 $result = $segments[0];
599 $i = 0;
600 foreach ($binds as $bind)
601 {
602 $result .= $this->escape($bind);
603 $result .= $segments[++$i];
604 }
605
606 return $result;
607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 // --------------------------------------------------------------------
610
611 /**
612 * Determines if a query is a "write" type.
613 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200615 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200616 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200617 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200619 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 +0000620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // --------------------------------------------------------------------
623
624 /**
625 * Calculate the aggregate query elapsed time
626 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200627 * @param int The number of decimal places
628 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200629 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500630 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
632 return number_format($this->benchmark, $decimals);
633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // --------------------------------------------------------------------
636
637 /**
638 * Returns the total number of queries
639 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200640 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200641 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500642 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 return $this->query_count;
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // --------------------------------------------------------------------
648
649 /**
650 * Returns the last query that was executed
651 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200652 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200653 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500654 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 return end($this->queries);
657 }
658
659 // --------------------------------------------------------------------
660
661 /**
662 * "Smart" Escape String
663 *
664 * Escapes data based on type
665 * Sets boolean and null types
666 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200668 * @return mixed
669 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500670 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200671 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000672 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000674 $str = "'".$this->escape_str($str)."'";
675 }
676 elseif (is_bool($str))
677 {
678 $str = ($str === FALSE) ? 0 : 1;
679 }
680 elseif (is_null($str))
681 {
682 $str = 'NULL';
683 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000684
685 return $str;
686 }
687
688 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600689
Derek Jonese4ed5832009-02-20 21:44:59 +0000690 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000691 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000692 *
693 * Calls the individual driver for platform
694 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200695 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000696 * @param string
697 * @return mixed
698 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500699 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200700 {
701 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000702 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000703
Derek Jonese4ed5832009-02-20 21:44:59 +0000704 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 /**
707 * Primary
708 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200709 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 * position is the primary key
711 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200713 * @return string
714 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500715 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200716 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 if ( ! is_array($fields))
720 {
721 return FALSE;
722 }
723
724 return current($fields);
725 }
726
727 // --------------------------------------------------------------------
728
729 /**
730 * Returns an array of table names
731 *
Barry Mienydd671972010-10-04 16:33:58 +0200732 * @return array
733 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500734 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 {
736 // Is there a cached result?
737 if (isset($this->data_cache['table_names']))
738 {
739 return $this->data_cache['table_names'];
740 }
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
743 {
744 if ($this->db_debug)
745 {
746 return $this->display_error('db_unsupported_function');
747 }
748 return FALSE;
749 }
750
751 $retval = array();
752 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 if ($query->num_rows() > 0)
755 {
Taufan Aditya18209332012-02-09 16:07:27 +0700756 $table = FALSE;
757 $rows = $query->result_array();
758 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
759
760 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
Taufan Aditya18209332012-02-09 16:07:27 +0700762 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
763 }
764
765 foreach ($rows as $row)
766 {
767 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 }
769 }
770
771 $this->data_cache['table_names'] = $retval;
Andrey Andreev4c202602012-03-06 20:34:52 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 return $this->data_cache['table_names'];
774 }
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 // --------------------------------------------------------------------
777
778 /**
779 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500780 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200781 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500783 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200784 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200785 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 // --------------------------------------------------------------------
789
790 /**
791 * Fetch MySQL Field Names
792 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200794 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500796 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
798 // Is there a cached result?
799 if (isset($this->data_cache['field_names'][$table]))
800 {
801 return $this->data_cache['field_names'][$table];
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 if ($table == '')
805 {
806 if ($this->db_debug)
807 {
808 return $this->display_error('db_field_param_missing');
809 }
810 return FALSE;
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Greg Aker1edde302010-01-26 00:17:01 +0000813 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
815 if ($this->db_debug)
816 {
817 return $this->display_error('db_unsupported_function');
818 }
819 return FALSE;
820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500825 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 if (isset($row['COLUMN_NAME']))
828 {
829 $retval[] = $row['COLUMN_NAME'];
830 }
831 else
832 {
833 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200834 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 }
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 $this->data_cache['field_names'][$table] = $retval;
838 return $this->data_cache['field_names'][$table];
839 }
840
841 // --------------------------------------------------------------------
842
843 /**
844 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500845 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 * @param string
847 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200848 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500850 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200851 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 // --------------------------------------------------------------------
856
857 /**
858 * Returns an object with field data
859 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200861 * @return object
862 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500863 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 {
865 if ($table == '')
866 {
867 if ($this->db_debug)
868 {
869 return $this->display_error('db_field_param_missing');
870 }
871 return FALSE;
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200874 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000875
876 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200877 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000878
879 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 /**
882 * Generate an insert string
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string the table upon which the query will be performed
885 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200886 * @return string
887 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500888 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
890 $fields = array();
891 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200892
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500893 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
895 $fields[] = $this->_escape_identifiers($key);
896 $values[] = $this->escape($val);
897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200899 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200900 }
901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 // --------------------------------------------------------------------
903
904 /**
905 * Generate an update string
906 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 * @param string the table upon which the query will be performed
908 * @param array an associative array data of key/values
909 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200910 * @return string
911 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500912 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
914 if ($where == '')
915 {
Andrey Andreev4c202602012-03-06 20:34:52 +0200916 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500920 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200922 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
924
925 if ( ! is_array($where))
926 {
927 $dest = array($where);
928 }
929 else
930 {
931 $dest = array();
932 foreach ($where as $key => $val)
933 {
934 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200935 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 if ($val !== '')
938 {
939 if ( ! $this->_has_operator($key))
940 {
941 $key .= ' =';
942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 $val = ' '.$this->escape($val);
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 $dest[] = $prefix.$key.$val;
948 }
Barry Mienydd671972010-10-04 16:33:58 +0200949 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000950
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200951 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200952 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000953
954 // --------------------------------------------------------------------
955
956 /**
957 * Tests whether the string has an SQL operator
958 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 * @param string
960 * @return bool
961 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500962 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
964 $str = trim($str);
965 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
966 {
967 return FALSE;
968 }
969
970 return TRUE;
971 }
972
973 // --------------------------------------------------------------------
974
975 /**
976 * Enables a native PHP function to be run, using a platform agnostic wrapper.
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @param string the function name
979 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200980 * @return mixed
981 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500982 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 if (FALSE === strpos($driver, $function))
987 {
988 $function = $driver.$function;
989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 if ( ! function_exists($function))
992 {
993 if ($this->db_debug)
994 {
995 return $this->display_error('db_unsupported_function');
996 }
997 return FALSE;
998 }
999 else
1000 {
1001 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1002
Repox71b78092011-12-01 09:19:43 +01001003 if (is_null($args))
1004 {
1005 return call_user_func($function);
1006 }
1007 else
1008 {
1009 return call_user_func_array($function, $args);
1010 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
1012 }
1013
1014 // --------------------------------------------------------------------
1015
1016 /**
1017 * Set Cache Directory Path
1018 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 * @param string the path to the cache directory
1020 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001021 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001022 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 $this->cachedir = $path;
1025 }
1026
1027 // --------------------------------------------------------------------
1028
1029 /**
1030 * Enable Query Caching
1031 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001032 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001033 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001034 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 {
1036 $this->cache_on = TRUE;
1037 return TRUE;
1038 }
1039
1040 // --------------------------------------------------------------------
1041
1042 /**
1043 * Disable Query Caching
1044 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001045 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001046 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001047 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
1049 $this->cache_on = FALSE;
1050 return FALSE;
1051 }
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
1054 // --------------------------------------------------------------------
1055
1056 /**
1057 * Delete the cache files associated with a particular URI
1058 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001059 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001060 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001061 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
1063 if ( ! $this->_cache_init())
1064 {
1065 return FALSE;
1066 }
1067 return $this->CACHE->delete($segment_one, $segment_two);
1068 }
1069
1070 // --------------------------------------------------------------------
1071
1072 /**
1073 * Delete All cache files
1074 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001075 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001076 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001077 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 {
1079 if ( ! $this->_cache_init())
1080 {
1081 return FALSE;
1082 }
1083
1084 return $this->CACHE->delete_all();
1085 }
1086
1087 // --------------------------------------------------------------------
1088
1089 /**
1090 * Initialize the Cache Class
1091 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001092 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001093 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001094 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
1096 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1097 {
1098 return TRUE;
1099 }
Derek Allarde37ab382009-02-03 16:13:57 +00001100
1101 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 {
Greg Aker3a746652011-04-19 10:59:47 -05001103 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001104 {
1105 return $this->cache_off();
1106 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 }
Derek Allarde37ab382009-02-03 16:13:57 +00001108
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1110 return TRUE;
1111 }
1112
1113 // --------------------------------------------------------------------
1114
1115 /**
1116 * Close DB Connection
1117 *
Barry Mienydd671972010-10-04 16:33:58 +02001118 * @return void
1119 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001120 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
1122 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1123 {
1124 $this->_close($this->conn_id);
1125 }
1126 $this->conn_id = FALSE;
1127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 // --------------------------------------------------------------------
1130
1131 /**
1132 * Display an error message
1133 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 * @param string the error message
1135 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001136 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001137 * @return string sends the application/error_db.php template
1138 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001139 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Derek Jonese7792202010-03-02 17:24:46 -06001141 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 $LANG->load('db');
1143
1144 $heading = $LANG->line('db_error_heading');
1145
1146 if ($native == TRUE)
1147 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001148 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 }
1150 else
1151 {
1152 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1153 }
Barry Mienydd671972010-10-04 16:33:58 +02001154
Pascal Kriete60f8c392010-08-25 18:03:28 +02001155 // Find the most likely culprit of the error by going through
1156 // the backtrace until the source file is no longer in the
1157 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001158
Pascal Kriete60f8c392010-08-25 18:03:28 +02001159 $trace = debug_backtrace();
1160
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001161 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001162 {
1163 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1164 {
1165 // Found it - use a relative path for safety
1166 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1167 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001168
Pascal Kriete60f8c392010-08-25 18:03:28 +02001169 break;
1170 }
1171 }
Barry Mienydd671972010-10-04 16:33:58 +02001172
Derek Jonese7792202010-03-02 17:24:46 -06001173 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 echo $error->show_error($heading, $message, 'error_db');
1175 exit;
1176 }
1177
1178 // --------------------------------------------------------------------
1179
1180 /**
1181 * Protect Identifiers
1182 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001184 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001186 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001187 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 *
1189 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1190 *
1191 * Or a query with aliasing:
1192 *
1193 * SELECT m.member_id, m.member_name FROM members AS m
1194 *
1195 * Since the column name can include up to four segments (host, DB, table, column)
1196 * or also have an alias prefix, we need to do a bit of work to figure this out and
1197 * insert the table prefix (if it exists) in the proper position, and escape only
1198 * the correct identifiers.
1199 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 * @param string
1201 * @param bool
1202 * @param mixed
1203 * @param bool
1204 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001205 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001206 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
1208 if ( ! is_bool($protect_identifiers))
1209 {
1210 $protect_identifiers = $this->_protect_identifiers;
1211 }
Derek Allarde37ab382009-02-03 16:13:57 +00001212
1213 if (is_array($item))
1214 {
1215 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001216 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001217 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001218 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001219 }
1220
1221 return $escaped_array;
1222 }
1223
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001225 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001226
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 // If the item has an alias declaration we remove it and set it aside.
1228 // Basically we remove everything to the right of the first space
1229 $alias = '';
1230 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001231 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 $alias = strstr($item, " ");
1233 $item = substr($item, 0, - strlen($alias));
1234 }
1235
Derek Allard911d3e02008-12-15 14:08:35 +00001236 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001237 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001238 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001239 // way to deal with this, but I'm not thinking of it -- Rick
1240 if (strpos($item, '(') !== FALSE)
1241 {
1242 return $item.$alias;
1243 }
1244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // Break the string apart if it contains periods, then insert the table prefix
1246 // in the correct location, assuming the period doesn't indicate that we're dealing
1247 // with an alias. While we're at it, we will escape the components
1248 if (strpos($item, '.') !== FALSE)
1249 {
1250 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001253 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 // we have nothing more to do other than escape the item
1255 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001256 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 if ($protect_identifiers === TRUE)
1258 {
1259 foreach ($parts as $key => $val)
1260 {
1261 if ( ! in_array($val, $this->_reserved_identifiers))
1262 {
1263 $parts[$key] = $this->_escape_identifiers($val);
1264 }
1265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001268 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 return $item.$alias;
1270 }
Barry Mienydd671972010-10-04 16:33:58 +02001271
Andrey Andreev4c202602012-03-06 20:34:52 +02001272 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 if ($this->dbprefix != '')
1274 {
1275 // We now add the table prefix based on some logic.
1276 // Do we have 4 segments (hostname.database.table.column)?
1277 // If so, we add the table prefix to the column name in the 3rd segment.
1278 if (isset($parts[3]))
1279 {
1280 $i = 2;
1281 }
1282 // Do we have 3 segments (database.table.column)?
1283 // If so, we add the table prefix to the column name in 2nd position
1284 elseif (isset($parts[2]))
1285 {
1286 $i = 1;
1287 }
1288 // Do we have 2 segments (table.column)?
1289 // If so, we add the table prefix to the column name in 1st segment
1290 else
1291 {
1292 $i = 0;
1293 }
Barry Mienydd671972010-10-04 16:33:58 +02001294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 // This flag is set when the supplied $item does not contain a field name.
1296 // This can happen when this function is being called from a JOIN.
1297 if ($field_exists == FALSE)
1298 {
1299 $i++;
1300 }
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Jones55acc8b2009-07-11 03:38:13 +00001302 // Verify table prefix and replace if necessary
1303 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1304 {
1305 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1306 }
Barry Mienydd671972010-10-04 16:33:58 +02001307
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 // We only add the table prefix if it does not already exist
1309 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1310 {
1311 $parts[$i] = $this->dbprefix.$parts[$i];
1312 }
Barry Mienydd671972010-10-04 16:33:58 +02001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 // Put the parts back together
1315 $item = implode('.', $parts);
1316 }
Barry Mienydd671972010-10-04 16:33:58 +02001317
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 if ($protect_identifiers === TRUE)
1319 {
1320 $item = $this->_escape_identifiers($item);
1321 }
Barry Mienydd671972010-10-04 16:33:58 +02001322
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 return $item.$alias;
1324 }
1325
Andrey Andreev4c202602012-03-06 20:34:52 +02001326 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 if ($this->dbprefix != '')
1328 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001329 // Verify table prefix and replace if necessary
1330 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1331 {
1332 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1333 }
1334
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // Do we prefix an item with no segments?
1336 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1337 {
1338 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001339 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 }
1341
1342 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1343 {
1344 $item = $this->_escape_identifiers($item);
1345 }
Barry Mienydd671972010-10-04 16:33:58 +02001346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 return $item.$alias;
1348 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001349
Túbal Martín511f2252011-11-24 14:43:45 +01001350 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001351
Túbal Martín511f2252011-11-24 14:43:45 +01001352 /**
1353 * Dummy method that allows Active Record class to be disabled
1354 *
1355 * This function is used extensively by every db driver.
1356 *
Túbal Martín511f2252011-11-24 14:43:45 +01001357 * @return void
1358 */
1359 protected function _reset_select()
1360 {
Túbal Martín511f2252011-11-24 14:43:45 +01001361 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001362
1363}
1364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365/* End of file DB_driver.php */
Timothy Warren6f531dc2011-10-10 10:10:46 -04001366/* Location: ./system/database/DB_driver.php */