blob: 9dd156eab3e020663ed0c0a87ec381a4d7d2b405 [file] [log] [blame]
Andrey Andreev569091c2012-02-09 00:16:17 +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 Andreev569091c2012-02-09 00:16:17 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev569091c2012-02-09 00:16:17 +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
28// ------------------------------------------------------------------------
29
30/**
31 * Database Driver Class
32 *
33 * This is the platform-independent base DB implementation class.
34 * This class will not be called directly. Rather, the adapter
35 * class for the specific database will extend and instantiate it.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_driver {
44
Andrey Andreev569091c2012-02-09 00:16:17 +020045 var $username;
46 var $password;
47 var $hostname;
48 var $database;
49 var $dbdriver = 'mysql';
50 var $dbprefix = '';
51 var $char_set = 'utf8';
52 var $dbcollat = 'utf8_general_ci';
53 var $autoinit = TRUE; // Whether to automatically initialize the DB
54 var $swap_pre = '';
55 var $port = '';
56 var $pconnect = FALSE;
57 var $conn_id = FALSE;
58 var $result_id = FALSE;
59 var $db_debug = FALSE;
60 var $benchmark = 0;
61 var $query_count = 0;
62 var $bind_marker = '?';
63 var $save_queries = TRUE;
64 var $queries = array();
65 var $query_times = array();
66 var $data_cache = array();
67 var $trans_enabled = TRUE;
68 var $trans_strict = TRUE;
69 var $_trans_depth = 0;
70 var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
71 var $cache_on = FALSE;
72 var $cachedir = '';
73 var $cache_autodel = FALSE;
74 var $CACHE; // The cache class object
Derek Allard2067d1a2008-11-13 22:59:24 +000075
Andrey Andreev569091c2012-02-09 00:16:17 +020076 // Private variables
77 var $_protect_identifiers = TRUE;
78 var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050081 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * connection settings.
83 *
84 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020085 */
Andrey Andreev569091c2012-02-09 00:16:17 +020086 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 if (is_array($params))
89 {
90 foreach ($params as $key => $val)
91 {
92 $this->$key = $val;
93 }
94 }
95
96 log_message('debug', 'Database Driver Class Initialized');
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 // --------------------------------------------------------------------
100
101 /**
102 * Initialize Database Settings
103 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200104 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200105 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200106 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
108 // If an existing connection resource is available
109 // there is no need to connect and select the database
110 if (is_resource($this->conn_id) OR is_object($this->conn_id))
111 {
112 return TRUE;
113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 // Connect to the database and set the connection ID
118 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
119
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200120 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 if ( ! $this->conn_id)
122 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100123 // Check if there is a failover set
124 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100126 // Go over all the failovers
127 foreach ($this->failover as $failover)
128 {
129 // Replace the current settings with those of the failover
130 foreach ($failover as $key => $val)
131 {
132 $this->$key = $val;
133 }
134
135 // Try to connect
136 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
137
138 // If a connection is made break the foreach loop
139 if ($this->conn_id)
140 {
141 break;
142 }
143 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100145
146 // We still don't have a connection?
147 if ( ! $this->conn_id)
148 {
149 log_message('error', 'Unable to connect to the database');
150
151 if ($this->db_debug)
152 {
153 $this->display_error('db_unable_to_connect');
154 }
155 return FALSE;
156 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 // ----------------------------------------------------------------
160
161 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200165
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200166 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200168 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200170 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200173 // Now we set the character set and that's all
174 return $this->db_set_charset($this->char_set, $this->dbcollat);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // --------------------------------------------------------------------
178
179 /**
180 * Set client character set
181 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * @param string
183 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200184 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 */
Andrey Andreev063f5962012-02-27 12:20:52 +0200186 public function db_set_charset($charset, $collation = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200188 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset, $collation))
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200190 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 if ($this->db_debug)
193 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200194 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 return FALSE;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 return TRUE;
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // --------------------------------------------------------------------
204
205 /**
206 * The name of the platform in use (mysql, mssql, etc...)
207 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200208 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200209 * @return string
210 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200211 function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 return $this->dbdriver;
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200219 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200221 * Returns a string containing the version of the database being used.
222 * Most drivers will override this method.
223 *
Barry Mienydd671972010-10-04 16:33:58 +0200224 * @return string
225 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200226 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200228 if (isset($this->data_cache['version']))
229 {
230 return $this->data_cache['version'];
231 }
232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 if (FALSE === ($sql = $this->_version()))
234 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200235 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
Derek Allard3683f772009-12-16 17:32:33 +0000237
Andrey Andreev08856b82012-03-03 03:19:28 +0200238 $query = $this->query($sql);
239 $query = $query->row();
240 return $this->data_cache['version'] = $query->ver;
241 }
Derek Allard3683f772009-12-16 17:32:33 +0000242
Andrey Andreev08856b82012-03-03 03:19:28 +0200243 // --------------------------------------------------------------------
244
245 /**
246 * Version number query string
247 *
248 * @return string
249 */
250 protected function _version()
251 {
252 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // --------------------------------------------------------------------
256
257 /**
258 * Execute the query
259 *
260 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500261 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 * upon successful execution of a "write" type query. Returns boolean
263 * FALSE upon failure, and if the $db_debug variable is set to TRUE
264 * will raise an error.
265 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200266 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 * @param string An SQL query string
268 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200269 * @return mixed
270 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200271 function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 if ($sql == '')
274 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200275 log_message('error', 'Invalid query: '.$sql);
276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 if ($this->db_debug)
278 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 return $this->display_error('db_invalid_query');
280 }
281 return FALSE;
282 }
283
284 // Verify table prefix and replace if necessary
Andrey Andreev569091c2012-02-09 00:16:17 +0200285 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600286 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200287 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Derek Jonese7792202010-03-02 17:24:46 -0600289
Ryan Dialef7474c2012-03-01 16:11:36 -0500290 // Compile binds if needed
291 if ($binds !== FALSE)
292 {
293 $sql = $this->compile_binds($sql, $binds);
294 }
295
Derek Jones37f4b9c2011-07-01 17:56:50 -0500296 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // we will load the caching class and return the previously
298 // cached query if it exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200299 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 if ($this->_cache_init())
302 {
303 $this->load_rdriver();
304 if (FALSE !== ($cache = $this->CACHE->read($sql)))
305 {
306 return $cache;
307 }
308 }
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Jones37f4b9c2011-07-01 17:56:50 -0500311 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 if ($this->save_queries == TRUE)
313 {
314 $this->queries[] = $sql;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // Start the Query Timer
318 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Run the Query
321 if (FALSE === ($this->result_id = $this->simple_query($sql)))
322 {
323 if ($this->save_queries == TRUE)
324 {
325 $this->query_times[] = 0;
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // This will trigger a rollback if transactions are being used
329 $this->_trans_status = FALSE;
330
Andrey Andreev4be5de12012-03-02 15:45:41 +0200331 // Grab the error now, as we might run some additional queries before displaying the error
332 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200333
334 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200335 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 if ($this->db_debug)
338 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200340 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200341 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // transactions to remain in limbo.
343 $this->trans_complete();
344
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200345 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 return $this->display_error(
Andrey Andreev4be5de12012-03-02 15:45:41 +0200347 array(
348 'Error Number: '.$error['code'],
349 $error['message'],
350 $sql
351 )
352 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 return FALSE;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // Stop and aggregate the query time results
359 $time_end = list($em, $es) = explode(' ', microtime());
360 $this->benchmark += ($em + $es) - ($sm + $ss);
361
362 if ($this->save_queries == TRUE)
363 {
364 $this->query_times[] = ($em + $es) - ($sm + $ss);
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // Increment the query counter
368 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // Was the query a "write" type?
371 // If so we'll simply return true
372 if ($this->is_write_type($sql) === TRUE)
373 {
374 // If caching is enabled we'll auto-cleanup any
375 // existing files related to this particular URI
Andrey Andreev569091c2012-02-09 00:16:17 +0200376 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $this->CACHE->delete();
379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 return TRUE;
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // Return TRUE if we don't need to create a result object
385 // Currently only the Oracle driver uses this when stored
386 // procedures are used
387 if ($return_object !== TRUE)
388 {
389 return TRUE;
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
392 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200393 $driver = $this->load_rdriver();
394 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Jones37f4b9c2011-07-01 17:56:50 -0500396 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // result object and save it to a cache file.
Andrey Andreev569091c2012-02-09 00:16:17 +0200398 if ($this->cache_on == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 // We'll create a new instance of the result object
401 // only without the platform specific driver since
402 // we can't use it with cached data (the query result
403 // resource ID won't be any good once we've cached the
404 // result object, so we'll have to compile the data
405 // and save it)
406 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 $CR->result_object = $RES->result_object();
408 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200409 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // Reset these since cached objects can not utilize resource IDs.
412 $CR->conn_id = NULL;
413 $CR->result_id = NULL;
414
415 $this->CACHE->write($sql, $CR);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 return $RES;
419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Load the result drivers
425 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200426 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200427 * @return string the name of the result class
428 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200429 function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 $driver = 'CI_DB_'.$this->dbdriver.'_result';
432
433 if ( ! class_exists($driver))
434 {
Greg Aker3a746652011-04-19 10:59:47 -0500435 include_once(BASEPATH.'database/DB_result.php');
436 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 return $driver;
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // --------------------------------------------------------------------
443
444 /**
445 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500446 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * we only use it when running transaction commands since they do
448 * not require all the features of the main query() function.
449 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200450 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200452 * @return mixed
453 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200454 function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 if ( ! $this->conn_id)
457 {
458 $this->initialize();
459 }
460
461 return $this->_execute($sql);
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
465
466 /**
467 * Disable Transactions
468 * This permits transactions to be disabled at run-time.
469 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200470 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200471 * @return void
472 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200473 function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $this->trans_enabled = FALSE;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Enable/disable Transaction Strict Mode
482 * When strict mode is enabled, if you are running multiple groups of
483 * transactions, if one group fails all groups will be rolled back.
484 * If strict mode is disabled, each group is treated autonomously, meaning
485 * a failure of one group will not affect any others
486 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200487 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200488 * @return void
489 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200490 function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 // --------------------------------------------------------------------
496
497 /**
498 * Start Transaction
499 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200500 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200501 * @return void
502 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200503 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200504 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 if ( ! $this->trans_enabled)
506 {
507 return FALSE;
508 }
509
510 // When transactions are nested we only begin/commit/rollback the outermost ones
511 if ($this->_trans_depth > 0)
512 {
513 $this->_trans_depth += 1;
514 return;
515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500518 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Complete Transaction
525 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200526 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200527 * @return bool
528 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200529 function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
531 if ( ! $this->trans_enabled)
532 {
533 return FALSE;
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // When transactions are nested we only begin/commit/rollback the outermost ones
537 if ($this->_trans_depth > 1)
538 {
539 $this->_trans_depth -= 1;
540 return TRUE;
541 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500542 else
543 {
544 $this->_trans_depth = 0;
545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 // The query() function will set this flag to FALSE in the event that a query failed
548 if ($this->_trans_status === FALSE)
549 {
550 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // If we are NOT running in strict mode, we will reset
553 // the _trans_status flag so that subsequent groups of transactions
554 // will be permitted.
555 if ($this->trans_strict === FALSE)
556 {
557 $this->_trans_status = TRUE;
558 }
559
560 log_message('debug', 'DB Transaction Failure');
561 return FALSE;
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 $this->trans_commit();
565 return TRUE;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Lets you retrieve the transaction flag to determine if it has failed
572 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200573 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200574 * @return bool
575 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200576 function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 return $this->_trans_status;
579 }
580
581 // --------------------------------------------------------------------
582
583 /**
584 * Compile Bindings
585 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200586 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @param string the sql statement
588 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200589 * @return string
590 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200591 function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 if (strpos($sql, $this->bind_marker) === FALSE)
594 {
595 return $sql;
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 if ( ! is_array($binds))
599 {
600 $binds = array($binds);
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 // Get the sql segments around the bind markers
604 $segments = explode($this->bind_marker, $sql);
605
606 // The count of bind should be 1 less then the count of segments
607 // If there are more bind arguments trim it down
608 if (count($binds) >= count($segments)) {
609 $binds = array_slice($binds, 0, count($segments)-1);
610 }
611
612 // Construct the binded query
613 $result = $segments[0];
614 $i = 0;
615 foreach ($binds as $bind)
616 {
617 $result .= $this->escape($bind);
618 $result .= $segments[++$i];
619 }
620
621 return $result;
622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // --------------------------------------------------------------------
625
626 /**
627 * Determines if a query is a "write" type.
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200630 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200631 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200632 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200634 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 +0000635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
638
639 /**
640 * Calculate the aggregate query elapsed time
641 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200642 * @access public
643 * @param integer The number of decimal places
644 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200646 function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
648 return number_format($this->benchmark, $decimals);
649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // --------------------------------------------------------------------
652
653 /**
654 * Returns the total number of queries
655 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200656 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200657 * @return integer
658 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200659 function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 return $this->query_count;
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 // --------------------------------------------------------------------
665
666 /**
667 * Returns the last query that was executed
668 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200669 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200670 * @return void
671 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200672 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 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200685 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200687 * @return mixed
688 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200689 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200690 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000691 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000693 $str = "'".$this->escape_str($str)."'";
694 }
695 elseif (is_bool($str))
696 {
697 $str = ($str === FALSE) ? 0 : 1;
698 }
699 elseif (is_null($str))
700 {
701 $str = 'NULL';
702 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000703
704 return $str;
705 }
706
707 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600708
Derek Jonese4ed5832009-02-20 21:44:59 +0000709 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000710 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000711 *
712 * Calls the individual driver for platform
713 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200714 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200715 * @access public
Derek Jonese4ed5832009-02-20 21:44:59 +0000716 * @param string
717 * @return mixed
718 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200719 function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200720 {
721 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000722 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000723
Derek Jonese4ed5832009-02-20 21:44:59 +0000724 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 /**
727 * Primary
728 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200729 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 * position is the primary key
731 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200732 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200734 * @return string
735 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200736 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200737 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 if ( ! is_array($fields))
741 {
742 return FALSE;
743 }
744
745 return current($fields);
746 }
747
748 // --------------------------------------------------------------------
749
750 /**
751 * Returns an array of table names
752 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200753 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200754 * @return array
755 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200756 function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
758 // Is there a cached result?
759 if (isset($this->data_cache['table_names']))
760 {
761 return $this->data_cache['table_names'];
762 }
Barry Mienydd671972010-10-04 16:33:58 +0200763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
765 {
766 if ($this->db_debug)
767 {
768 return $this->display_error('db_unsupported_function');
769 }
770 return FALSE;
771 }
772
773 $retval = array();
774 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 if ($query->num_rows() > 0)
777 {
Taufan Aditya18209332012-02-09 16:07:27 +0700778 $table = FALSE;
779 $rows = $query->result_array();
780 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
781
782 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
Taufan Aditya18209332012-02-09 16:07:27 +0700784 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
785 }
786
787 foreach ($rows as $row)
788 {
789 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 }
791 }
792
793 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 return $this->data_cache['table_names'];
796 }
Barry Mienydd671972010-10-04 16:33:58 +0200797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 // --------------------------------------------------------------------
799
800 /**
801 * Determine if a particular table exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200802 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 * @return boolean
804 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200805 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200806 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200807 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 // --------------------------------------------------------------------
811
812 /**
Andrey Andreev569091c2012-02-09 00:16:17 +0200813 * Fetch MySQL Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200815 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200817 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200819 function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
821 // Is there a cached result?
822 if (isset($this->data_cache['field_names'][$table]))
823 {
824 return $this->data_cache['field_names'][$table];
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 if ($table == '')
828 {
829 if ($this->db_debug)
830 {
831 return $this->display_error('db_field_param_missing');
832 }
833 return FALSE;
834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Greg Aker1edde302010-01-26 00:17:01 +0000836 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
838 if ($this->db_debug)
839 {
840 return $this->display_error('db_unsupported_function');
841 }
842 return FALSE;
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500848 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
850 if (isset($row['COLUMN_NAME']))
851 {
852 $retval[] = $row['COLUMN_NAME'];
853 }
854 else
855 {
856 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200857 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 }
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 $this->data_cache['field_names'][$table] = $retval;
861 return $this->data_cache['field_names'][$table];
862 }
863
864 // --------------------------------------------------------------------
865
866 /**
867 * Determine if a particular field exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200868 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * @param string
870 * @param string
871 * @return boolean
872 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200873 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200874 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200875 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // --------------------------------------------------------------------
879
880 /**
881 * Returns an object with field data
882 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200883 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200885 * @return object
886 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200887 function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
889 if ($table == '')
890 {
891 if ($this->db_debug)
892 {
893 return $this->display_error('db_field_param_missing');
894 }
895 return FALSE;
896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev569091c2012-02-09 00:16:17 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200901 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000902
903 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 /**
906 * Generate an insert string
907 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200908 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * @param string the table upon which the query will be performed
910 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200911 * @return string
912 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200913 function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
915 $fields = array();
916 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200917
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500918 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
920 $fields[] = $this->_escape_identifiers($key);
921 $values[] = $this->escape($val);
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200925 }
926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
928
929 /**
930 * Generate an update string
931 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200932 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @param string the table upon which the query will be performed
934 * @param array an associative array data of key/values
935 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200936 * @return string
937 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200938 function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 if ($where == '')
941 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200942 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500946 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
948 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
949 }
950
951 if ( ! is_array($where))
952 {
953 $dest = array($where);
954 }
955 else
956 {
957 $dest = array();
958 foreach ($where as $key => $val)
959 {
960 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300961 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 if ($val !== '')
964 {
965 if ( ! $this->_has_operator($key))
966 {
967 $key .= ' =';
968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 $val = ' '.$this->escape($val);
971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 $dest[] = $prefix.$key.$val;
974 }
Barry Mienydd671972010-10-04 16:33:58 +0200975 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000976
977 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200978 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000979
980 // --------------------------------------------------------------------
981
982 /**
983 * Tests whether the string has an SQL operator
984 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200985 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 * @param string
987 * @return bool
988 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200989 function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200991 $str = trim($str);
992 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
993 {
994 return FALSE;
995 }
996
997 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
999
1000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1004 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001005 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 * @param string the function name
1007 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001008 * @return mixed
1009 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001010 function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
1012 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 if (FALSE === strpos($driver, $function))
1015 {
1016 $function = $driver.$function;
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 if ( ! function_exists($function))
1020 {
1021 if ($this->db_debug)
1022 {
1023 return $this->display_error('db_unsupported_function');
1024 }
1025 return FALSE;
1026 }
1027 else
1028 {
1029 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1030
Repox71b78092011-12-01 09:19:43 +01001031 if (is_null($args))
1032 {
1033 return call_user_func($function);
1034 }
1035 else
1036 {
1037 return call_user_func_array($function, $args);
1038 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 }
1040 }
1041
1042 // --------------------------------------------------------------------
1043
1044 /**
1045 * Set Cache Directory Path
1046 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001047 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 * @param string the path to the cache directory
1049 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001050 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001051 function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 {
1053 $this->cachedir = $path;
1054 }
1055
1056 // --------------------------------------------------------------------
1057
1058 /**
1059 * Enable Query Caching
1060 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001061 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001063 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001064 function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001066 $this->cache_on = TRUE;
1067 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 }
1069
1070 // --------------------------------------------------------------------
1071
1072 /**
1073 * Disable Query Caching
1074 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001075 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001077 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001078 function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001080 $this->cache_on = FALSE;
1081 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084
1085 // --------------------------------------------------------------------
1086
1087 /**
1088 * Delete the cache files associated with a particular URI
1089 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001090 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001092 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001093 function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
1095 if ( ! $this->_cache_init())
1096 {
1097 return FALSE;
1098 }
1099 return $this->CACHE->delete($segment_one, $segment_two);
1100 }
1101
1102 // --------------------------------------------------------------------
1103
1104 /**
1105 * Delete All cache files
1106 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001107 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001109 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001110 function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
1112 if ( ! $this->_cache_init())
1113 {
1114 return FALSE;
1115 }
1116
1117 return $this->CACHE->delete_all();
1118 }
1119
1120 // --------------------------------------------------------------------
1121
1122 /**
1123 * Initialize the Cache Class
1124 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001125 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001127 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001128 function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001130 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
1132 return TRUE;
1133 }
Derek Allarde37ab382009-02-03 16:13:57 +00001134
Andrey Andreev569091c2012-02-09 00:16:17 +02001135 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001137 if ( ! @include(BASEPATH.'database/DB_cache.php'))
1138 {
1139 return $this->cache_off();
1140 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Derek Allarde37ab382009-02-03 16:13:57 +00001142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1144 return TRUE;
1145 }
1146
1147 // --------------------------------------------------------------------
1148
1149 /**
1150 * Close DB Connection
1151 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001152 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001153 * @return void
1154 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001155 function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
1157 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1158 {
1159 $this->_close($this->conn_id);
1160 }
1161 $this->conn_id = FALSE;
1162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 // --------------------------------------------------------------------
1165
1166 /**
1167 * Display an error message
1168 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001169 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 * @param string the error message
1171 * @param string any "swap" values
1172 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001173 * @return string sends the application/error_db.php template
1174 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001175 function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
Derek Jonese7792202010-03-02 17:24:46 -06001177 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 $LANG->load('db');
1179
1180 $heading = $LANG->line('db_error_heading');
1181
1182 if ($native == TRUE)
1183 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001184 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 }
1186 else
1187 {
1188 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Pascal Kriete60f8c392010-08-25 18:03:28 +02001191 // Find the most likely culprit of the error by going through
1192 // the backtrace until the source file is no longer in the
1193 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001194
Pascal Kriete60f8c392010-08-25 18:03:28 +02001195 $trace = debug_backtrace();
1196
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001197 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001198 {
1199 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1200 {
1201 // Found it - use a relative path for safety
1202 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1203 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001204
Pascal Kriete60f8c392010-08-25 18:03:28 +02001205 break;
1206 }
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Jonese7792202010-03-02 17:24:46 -06001209 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 echo $error->show_error($heading, $message, 'error_db');
1211 exit;
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
1217 * Protect Identifiers
1218 *
1219 * This function adds backticks if appropriate based on db type
1220 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001221 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 * @param mixed the item to escape
1223 * @return mixed the item with backticks
1224 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001225 function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
1227 return $this->_protect_identifiers($item, $prefix_single);
1228 }
1229
1230 // --------------------------------------------------------------------
1231
1232 /**
1233 * Protect Identifiers
1234 *
1235 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001236 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001238 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev569091c2012-02-09 00:16:17 +02001239 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 *
1241 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1242 *
1243 * Or a query with aliasing:
1244 *
1245 * SELECT m.member_id, m.member_name FROM members AS m
1246 *
1247 * Since the column name can include up to four segments (host, DB, table, column)
1248 * or also have an alias prefix, we need to do a bit of work to figure this out and
1249 * insert the table prefix (if it exists) in the proper position, and escape only
1250 * the correct identifiers.
1251 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001252 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 * @param string
1254 * @param bool
1255 * @param mixed
1256 * @param bool
1257 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001258 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001259 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
1261 if ( ! is_bool($protect_identifiers))
1262 {
1263 $protect_identifiers = $this->_protect_identifiers;
1264 }
Derek Allarde37ab382009-02-03 16:13:57 +00001265
1266 if (is_array($item))
1267 {
1268 $escaped_array = array();
1269
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001270 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001271 {
1272 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1273 }
1274
1275 return $escaped_array;
1276 }
1277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001279 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 // If the item has an alias declaration we remove it and set it aside.
1282 // Basically we remove everything to the right of the first space
1283 $alias = '';
1284 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001285 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 $alias = strstr($item, " ");
1287 $item = substr($item, 0, - strlen($alias));
1288 }
1289
Derek Allard911d3e02008-12-15 14:08:35 +00001290 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001291 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001292 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001293 // way to deal with this, but I'm not thinking of it -- Rick
1294 if (strpos($item, '(') !== FALSE)
1295 {
1296 return $item.$alias;
1297 }
1298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 // Break the string apart if it contains periods, then insert the table prefix
1300 // in the correct location, assuming the period doesn't indicate that we're dealing
1301 // with an alias. While we're at it, we will escape the components
1302 if (strpos($item, '.') !== FALSE)
1303 {
1304 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001305
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 // Does the first segment of the exploded item match
Andrey Andreev569091c2012-02-09 00:16:17 +02001307 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 // we have nothing more to do other than escape the item
1309 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001310 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 if ($protect_identifiers === TRUE)
1312 {
1313 foreach ($parts as $key => $val)
1314 {
1315 if ( ! in_array($val, $this->_reserved_identifiers))
1316 {
1317 $parts[$key] = $this->_escape_identifiers($val);
1318 }
1319 }
Barry Mienydd671972010-10-04 16:33:58 +02001320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001322 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 return $item.$alias;
1324 }
Barry Mienydd671972010-10-04 16:33:58 +02001325
Andrey Andreev569091c2012-02-09 00:16:17 +02001326 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 if ($this->dbprefix != '')
1328 {
1329 // We now add the table prefix based on some logic.
1330 // Do we have 4 segments (hostname.database.table.column)?
1331 // If so, we add the table prefix to the column name in the 3rd segment.
1332 if (isset($parts[3]))
1333 {
1334 $i = 2;
1335 }
1336 // Do we have 3 segments (database.table.column)?
1337 // If so, we add the table prefix to the column name in 2nd position
1338 elseif (isset($parts[2]))
1339 {
1340 $i = 1;
1341 }
1342 // Do we have 2 segments (table.column)?
1343 // If so, we add the table prefix to the column name in 1st segment
1344 else
1345 {
1346 $i = 0;
1347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 // This flag is set when the supplied $item does not contain a field name.
1350 // This can happen when this function is being called from a JOIN.
1351 if ($field_exists == FALSE)
1352 {
1353 $i++;
1354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Derek Jones55acc8b2009-07-11 03:38:13 +00001356 // Verify table prefix and replace if necessary
1357 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1358 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001359 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 // We only add the table prefix if it does not already exist
1363 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1364 {
1365 $parts[$i] = $this->dbprefix.$parts[$i];
1366 }
Barry Mienydd671972010-10-04 16:33:58 +02001367
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 // Put the parts back together
1369 $item = implode('.', $parts);
1370 }
Barry Mienydd671972010-10-04 16:33:58 +02001371
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 if ($protect_identifiers === TRUE)
1373 {
1374 $item = $this->_escape_identifiers($item);
1375 }
Barry Mienydd671972010-10-04 16:33:58 +02001376
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 return $item.$alias;
1378 }
1379
Andrey Andreev569091c2012-02-09 00:16:17 +02001380 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 if ($this->dbprefix != '')
1382 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001383 // Verify table prefix and replace if necessary
1384 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1385 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001386 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001387 }
1388
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 // Do we prefix an item with no segments?
Andrey Andreev569091c2012-02-09 00:16:17 +02001390 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 {
1392 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001393 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 }
1395
Andrey Andreev569091c2012-02-09 00:16:17 +02001396 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 {
1398 $item = $this->_escape_identifiers($item);
1399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 return $item.$alias;
1402 }
Túbal Martín511f2252011-11-24 14:43:45 +01001403
1404 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001405
Túbal Martín511f2252011-11-24 14:43:45 +01001406 /**
1407 * Dummy method that allows Active Record class to be disabled
1408 *
1409 * This function is used extensively by every db driver.
1410 *
1411 * @access private
1412 * @return void
1413 */
1414 protected function _reset_select()
1415 {
1416
1417 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001418
1419}
1420
Derek Allard2067d1a2008-11-13 22:59:24 +00001421/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001422/* Location: ./system/database/DB_driver.php */