blob: 373744100dc44a54274792559d8a943bc96eb2cd [file] [log] [blame]
Andrey Andreev4c202602012-03-06 20:34:52 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4c202602012-03-06 20:34:52 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4c202602012-03-06 20:34:52 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Timothy Warren7eeda532012-03-19 16:01:55 -040041abstract class CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreev738f5342012-03-06 20:43:40 +020043 public $dsn;
Andrey Andreevd1add432012-03-06 20:26:01 +020044 public $username;
45 public $password;
46 public $hostname;
47 public $database;
48 public $dbdriver = 'mysql';
49 public $dbprefix = '';
50 public $char_set = 'utf8';
51 public $dbcollat = 'utf8_general_ci';
52 public $autoinit = TRUE; // Whether to automatically initialize the DB
53 public $swap_pre = '';
54 public $port = '';
55 public $pconnect = FALSE;
56 public $conn_id = FALSE;
57 public $result_id = FALSE;
58 public $db_debug = FALSE;
59 public $benchmark = 0;
60 public $query_count = 0;
61 public $bind_marker = '?';
62 public $save_queries = TRUE;
63 public $queries = array();
64 public $query_times = array();
65 public $data_cache = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000066
Andrey Andreevd1add432012-03-06 20:26:01 +020067 public $trans_enabled = TRUE;
68 public $trans_strict = TRUE;
69 protected $_trans_depth = 0;
70 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allard2067d1a2008-11-13 22:59:24 +000071
Andrey Andreevd1add432012-03-06 20:26:01 +020072 public $cache_on = FALSE;
73 public $cachedir = '';
74 public $cache_autodel = FALSE;
75 public $CACHE; // The cache class object
76
77 protected $_protect_identifiers = TRUE;
78 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
79
Timothy Warrene45518d2012-03-06 07:38:00 -050080 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if (is_array($params))
83 {
84 foreach ($params as $key => $val)
85 {
86 $this->$key = $val;
87 }
88 }
89
90 log_message('debug', 'Database Driver Class Initialized');
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
96 * Initialize Database Settings
97 *
Andrey Andreev82e8ac12012-02-22 19:35:34 +020098 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200100 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
102 // If an existing connection resource is available
103 // there is no need to connect and select the database
104 if (is_resource($this->conn_id) OR is_object($this->conn_id))
105 {
106 return TRUE;
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // Connect to the database and set the connection ID
112 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
113
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200114 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if ( ! $this->conn_id)
116 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100117 // Check if there is a failover set
118 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100120 // Go over all the failovers
121 foreach ($this->failover as $failover)
122 {
123 // Replace the current settings with those of the failover
124 foreach ($failover as $key => $val)
125 {
126 $this->$key = $val;
127 }
128
129 // Try to connect
130 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
131
132 // If a connection is made break the foreach loop
133 if ($this->conn_id)
134 {
135 break;
136 }
137 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100139
140 // We still don't have a connection?
141 if ( ! $this->conn_id)
142 {
143 log_message('error', 'Unable to connect to the database');
144
145 if ($this->db_debug)
146 {
147 $this->display_error('db_unable_to_connect');
148 }
149 return FALSE;
150 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // ----------------------------------------------------------------
154
155 // Select the DB... assuming a database name is specified in the config file
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200156 if ($this->database !== '' && ! $this->db_select())
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200158 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200159
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200160 if ($this->db_debug)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200162 $this->display_error('db_unable_to_select', $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200164 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
Andrey Andreev82e8ac12012-02-22 19:35:34 +0200167 // Now we set the character set and that's all
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200168 return $this->db_set_charset($this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
172
173 /**
Andrey Andreev2cae1932012-03-23 16:08:41 +0200174 * Reconnect
175 *
176 * Keep / reestablish the db connection if no queries have been
177 * sent for a length of time exceeding the server's idle timeout.
178 *
179 * This is just a dummy method to allow drivers without such
180 * functionality to not declare it, while others will override it.
181 *
182 * @return void
183 */
184 public function reconnect()
185 {
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * Set client character set
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string
194 * @param string
Andrey Andreev063f5962012-02-27 12:20:52 +0200195 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200197 public function db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200199 if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200201 log_message('error', 'Unable to set database connection charset: '.$charset);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 if ($this->db_debug)
204 {
Andrey Andreev063f5962012-02-27 12:20:52 +0200205 $this->display_error('db_unable_to_set_charset', $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 return FALSE;
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 return TRUE;
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // --------------------------------------------------------------------
215
216 /**
217 * The name of the platform in use (mysql, mssql, etc...)
218 *
Barry Mienydd671972010-10-04 16:33:58 +0200219 * @return string
220 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500221 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return $this->dbdriver;
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200229 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200231 * Returns a string containing the version of the database being used.
232 * Most drivers will override this method.
233 *
Barry Mienydd671972010-10-04 16:33:58 +0200234 * @return string
235 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200236 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200238 if (isset($this->data_cache['version']))
239 {
240 return $this->data_cache['version'];
241 }
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 if (FALSE === ($sql = $this->_version()))
244 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200245 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Derek Allard3683f772009-12-16 17:32:33 +0000247
Andrey Andreev08856b82012-03-03 03:19:28 +0200248 $query = $this->query($sql);
249 $query = $query->row();
250 return $this->data_cache['version'] = $query->ver;
251 }
Derek Allard3683f772009-12-16 17:32:33 +0000252
Andrey Andreev08856b82012-03-03 03:19:28 +0200253 // --------------------------------------------------------------------
254
255 /**
256 * Version number query string
257 *
258 * @return string
259 */
260 protected function _version()
261 {
262 return 'SELECT VERSION() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // --------------------------------------------------------------------
266
267 /**
268 * Execute the query
269 *
270 * Accepts an SQL string as input and returns a result object upon
Andrey Andreev4c202602012-03-06 20:34:52 +0200271 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 * upon successful execution of a "write" type query. Returns boolean
273 * FALSE upon failure, and if the $db_debug variable is set to TRUE
274 * will raise an error.
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string An SQL query string
277 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200278 * @return mixed
279 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500280 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 if ($sql == '')
283 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200284 log_message('error', 'Invalid query: '.$sql);
285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($this->db_debug)
287 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 return $this->display_error('db_invalid_query');
289 }
290 return FALSE;
291 }
292
293 // Verify table prefix and replace if necessary
Andrey Andreev569091c2012-02-09 00:16:17 +0200294 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600295 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200296 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
Derek Jonese7792202010-03-02 17:24:46 -0600298
Ryan Dialef7474c2012-03-01 16:11:36 -0500299 // Compile binds if needed
300 if ($binds !== FALSE)
301 {
302 $sql = $this->compile_binds($sql, $binds);
303 }
304
Andrey Andreev4c202602012-03-06 20:34:52 +0200305 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // we will load the caching class and return the previously
307 // cached query if it exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200308 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 if ($this->_cache_init())
311 {
312 $this->load_rdriver();
313 if (FALSE !== ($cache = $this->CACHE->read($sql)))
314 {
315 return $cache;
316 }
317 }
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Jones37f4b9c2011-07-01 17:56:50 -0500320 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 if ($this->save_queries == TRUE)
322 {
323 $this->queries[] = $sql;
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // Start the Query Timer
327 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // Run the Query
330 if (FALSE === ($this->result_id = $this->simple_query($sql)))
331 {
332 if ($this->save_queries == TRUE)
333 {
334 $this->query_times[] = 0;
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // This will trigger a rollback if transactions are being used
338 $this->_trans_status = FALSE;
339
Andrey Andreev4be5de12012-03-02 15:45:41 +0200340 // Grab the error now, as we might run some additional queries before displaying the error
341 $error = $this->error();
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200342
343 // Log errors
Andrey Andreev4be5de12012-03-02 15:45:41 +0200344 log_message('error', 'Query error: '.$error['message']);
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 if ($this->db_debug)
347 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // We call this function in order to roll-back queries
Andrey Andreev4be5de12012-03-02 15:45:41 +0200349 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200350 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // transactions to remain in limbo.
352 $this->trans_complete();
353
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200354 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 return $this->display_error(
Andrey Andreev4be5de12012-03-02 15:45:41 +0200356 array(
357 'Error Number: '.$error['code'],
358 $error['message'],
359 $sql
360 )
361 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 return FALSE;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // Stop and aggregate the query time results
368 $time_end = list($em, $es) = explode(' ', microtime());
369 $this->benchmark += ($em + $es) - ($sm + $ss);
370
371 if ($this->save_queries == TRUE)
372 {
373 $this->query_times[] = ($em + $es) - ($sm + $ss);
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // Increment the query counter
377 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // Was the query a "write" type?
380 // If so we'll simply return true
381 if ($this->is_write_type($sql) === TRUE)
382 {
383 // If caching is enabled we'll auto-cleanup any
384 // existing files related to this particular URI
Andrey Andreev569091c2012-02-09 00:16:17 +0200385 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
387 $this->CACHE->delete();
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 return TRUE;
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Return TRUE if we don't need to create a result object
394 // Currently only the Oracle driver uses this when stored
395 // procedures are used
396 if ($return_object !== TRUE)
397 {
398 return TRUE;
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
401 // Load and instantiate the result driver
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200402 $driver = $this->load_rdriver();
403 $RES = new $driver($this);
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Jones37f4b9c2011-07-01 17:56:50 -0500405 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // result object and save it to a cache file.
Andrey Andreev569091c2012-02-09 00:16:17 +0200407 if ($this->cache_on == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 // We'll create a new instance of the result object
410 // only without the platform specific driver since
411 // we can't use it with cached data (the query result
412 // resource ID won't be any good once we've cached the
413 // result object, so we'll have to compile the data
414 // and save it)
415 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 $CR->result_object = $RES->result_object();
417 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200418 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // Reset these since cached objects can not utilize resource IDs.
421 $CR->conn_id = NULL;
422 $CR->result_id = NULL;
423
424 $this->CACHE->write($sql, $CR);
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 return $RES;
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Load the result drivers
434 *
Barry Mienydd671972010-10-04 16:33:58 +0200435 * @return string the name of the result class
436 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500437 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 $driver = 'CI_DB_'.$this->dbdriver.'_result';
440
441 if ( ! class_exists($driver))
442 {
Greg Aker3a746652011-04-19 10:59:47 -0500443 include_once(BASEPATH.'database/DB_result.php');
444 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 return $driver;
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // --------------------------------------------------------------------
451
452 /**
453 * Simple Query
Andrey Andreev4c202602012-03-06 20:34:52 +0200454 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 * we only use it when running transaction commands since they do
456 * not require all the features of the main query() function.
457 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200459 * @return mixed
460 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500461 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 if ( ! $this->conn_id)
464 {
465 $this->initialize();
466 }
467
468 return $this->_execute($sql);
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
474 * Disable Transactions
475 * This permits transactions to be disabled at run-time.
476 *
Barry Mienydd671972010-10-04 16:33:58 +0200477 * @return void
478 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500479 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
481 $this->trans_enabled = FALSE;
482 }
483
484 // --------------------------------------------------------------------
485
486 /**
487 * Enable/disable Transaction Strict Mode
488 * When strict mode is enabled, if you are running multiple groups of
489 * transactions, if one group fails all groups will be rolled back.
490 * If strict mode is disabled, each group is treated autonomously, meaning
491 * a failure of one group will not affect any others
492 *
Barry Mienydd671972010-10-04 16:33:58 +0200493 * @return void
494 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500495 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
497 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // --------------------------------------------------------------------
501
502 /**
503 * Start Transaction
504 *
Barry Mienydd671972010-10-04 16:33:58 +0200505 * @return void
506 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500507 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200508 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 if ( ! $this->trans_enabled)
510 {
511 return FALSE;
512 }
513
514 // When transactions are nested we only begin/commit/rollback the outermost ones
515 if ($this->_trans_depth > 0)
516 {
517 $this->_trans_depth += 1;
518 return;
519 }
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500522 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524
525 // --------------------------------------------------------------------
526
527 /**
528 * Complete Transaction
529 *
Barry Mienydd671972010-10-04 16:33:58 +0200530 * @return bool
531 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500532 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 if ( ! $this->trans_enabled)
535 {
536 return FALSE;
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // When transactions are nested we only begin/commit/rollback the outermost ones
540 if ($this->_trans_depth > 1)
541 {
542 $this->_trans_depth -= 1;
543 return TRUE;
544 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500545 else
546 {
547 $this->_trans_depth = 0;
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // The query() function will set this flag to FALSE in the event that a query failed
551 if ($this->_trans_status === FALSE)
552 {
553 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // If we are NOT running in strict mode, we will reset
556 // the _trans_status flag so that subsequent groups of transactions
557 // will be permitted.
558 if ($this->trans_strict === FALSE)
559 {
560 $this->_trans_status = TRUE;
561 }
562
563 log_message('debug', 'DB Transaction Failure');
564 return FALSE;
565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 $this->trans_commit();
568 return TRUE;
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Lets you retrieve the transaction flag to determine if it has failed
575 *
Barry Mienydd671972010-10-04 16:33:58 +0200576 * @return bool
577 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500578 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 return $this->_trans_status;
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Compile Bindings
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @param string the sql statement
589 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200590 * @return string
591 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500592 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 if (strpos($sql, $this->bind_marker) === FALSE)
595 {
596 return $sql;
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 if ( ! is_array($binds))
600 {
601 $binds = array($binds);
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // Get the sql segments around the bind markers
605 $segments = explode($this->bind_marker, $sql);
606
607 // The count of bind should be 1 less then the count of segments
608 // If there are more bind arguments trim it down
609 if (count($binds) >= count($segments)) {
610 $binds = array_slice($binds, 0, count($segments)-1);
611 }
612
613 // Construct the binded query
614 $result = $segments[0];
615 $i = 0;
616 foreach ($binds as $bind)
617 {
618 $result .= $this->escape($bind);
619 $result .= $segments[++$i];
620 }
621
622 return $result;
623 }
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 // --------------------------------------------------------------------
626
627 /**
628 * Determines if a query is a "write" type.
629 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * @param string An SQL query string
Andrey Andreev67f71a42012-03-01 16:18:42 +0200631 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200632 */
Andrey Andreev67f71a42012-03-01 16:18:42 +0200633 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev5fa72982012-03-03 04:13:20 +0200635 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 +0000636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // --------------------------------------------------------------------
639
640 /**
641 * Calculate the aggregate query elapsed time
642 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200643 * @param int The number of decimal places
644 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500646 public 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 Andreev4c202602012-03-06 20:34:52 +0200656 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200657 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500658 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
660 return $this->query_count;
661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // --------------------------------------------------------------------
664
665 /**
666 * Returns the last query that was executed
667 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200668 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200669 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500670 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 return end($this->queries);
673 }
674
675 // --------------------------------------------------------------------
676
677 /**
678 * "Smart" Escape String
679 *
680 * Escapes data based on type
681 * Sets boolean and null types
682 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200684 * @return mixed
685 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500686 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200687 {
Joel Kallman10aa8e62012-03-09 14:54:53 -0500688 if (is_string($str) OR method_exists($str, '__toString'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000690 $str = "'".$this->escape_str($str)."'";
691 }
692 elseif (is_bool($str))
693 {
694 $str = ($str === FALSE) ? 0 : 1;
695 }
696 elseif (is_null($str))
697 {
698 $str = 'NULL';
699 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000700
701 return $str;
702 }
703
704 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600705
Derek Jonese4ed5832009-02-20 21:44:59 +0000706 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000707 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000708 *
709 * Calls the individual driver for platform
710 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200711 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000712 * @param string
713 * @return mixed
714 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500715 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200716 {
717 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000718 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000719
Derek Jonese4ed5832009-02-20 21:44:59 +0000720 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 /**
723 * Primary
724 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200725 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 * position is the primary key
727 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200729 * @return string
730 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500731 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200732 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 if ( ! is_array($fields))
736 {
737 return FALSE;
738 }
739
740 return current($fields);
741 }
742
743 // --------------------------------------------------------------------
744
745 /**
746 * Returns an array of table names
747 *
Barry Mienydd671972010-10-04 16:33:58 +0200748 * @return array
749 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500750 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
752 // Is there a cached result?
753 if (isset($this->data_cache['table_names']))
754 {
755 return $this->data_cache['table_names'];
756 }
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
759 {
760 if ($this->db_debug)
761 {
762 return $this->display_error('db_unsupported_function');
763 }
764 return FALSE;
765 }
766
767 $retval = array();
768 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 if ($query->num_rows() > 0)
771 {
Taufan Aditya18209332012-02-09 16:07:27 +0700772 $table = FALSE;
773 $rows = $query->result_array();
774 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
775
776 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 {
Taufan Aditya18209332012-02-09 16:07:27 +0700778 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
779 }
780
781 foreach ($rows as $row)
782 {
783 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785 }
786
787 $this->data_cache['table_names'] = $retval;
Andrey Andreev4c202602012-03-06 20:34:52 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 return $this->data_cache['table_names'];
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 // --------------------------------------------------------------------
793
794 /**
795 * Determine if a particular table exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500796 *
Andrey Andreev4c202602012-03-06 20:34:52 +0200797 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500799 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200800 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200801 return in_array($this->protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // --------------------------------------------------------------------
805
806 /**
Andrey Andreev569091c2012-02-09 00:16:17 +0200807 * Fetch MySQL Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200810 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500812 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
814 // Is there a cached result?
815 if (isset($this->data_cache['field_names'][$table]))
816 {
817 return $this->data_cache['field_names'][$table];
818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 if ($table == '')
821 {
822 if ($this->db_debug)
823 {
824 return $this->display_error('db_field_param_missing');
825 }
826 return FALSE;
827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Greg Aker1edde302010-01-26 00:17:01 +0000829 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
831 if ($this->db_debug)
832 {
833 return $this->display_error('db_unsupported_function');
834 }
835 return FALSE;
836 }
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500841 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
843 if (isset($row['COLUMN_NAME']))
844 {
845 $retval[] = $row['COLUMN_NAME'];
846 }
847 else
848 {
849 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200850 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 $this->data_cache['field_names'][$table] = $retval;
854 return $this->data_cache['field_names'][$table];
855 }
856
857 // --------------------------------------------------------------------
858
859 /**
860 * Determine if a particular field exists
Timothy Warrene45518d2012-03-06 07:38:00 -0500861 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 * @param string
863 * @param string
Andrey Andreev4c202602012-03-06 20:34:52 +0200864 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500866 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200867 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200868 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 // --------------------------------------------------------------------
872
873 /**
874 * Returns an object with field data
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200877 * @return object
878 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500879 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
881 if ($table == '')
882 {
883 if ($this->db_debug)
884 {
885 return $this->display_error('db_field_param_missing');
886 }
887 return FALSE;
888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200890 $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev569091c2012-02-09 00:16:17 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200893 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000894
895 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200896
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 /**
898 * Generate an insert string
899 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 * @param string the table upon which the query will be performed
901 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200902 * @return string
903 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500904 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
906 $fields = array();
907 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200908
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500909 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
911 $fields[] = $this->_escape_identifiers($key);
912 $values[] = $this->escape($val);
913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200915 return $this->_insert($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200916 }
917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 // --------------------------------------------------------------------
919
920 /**
921 * Generate an update string
922 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 * @param string the table upon which the query will be performed
924 * @param array an associative array data of key/values
925 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200926 * @return string
927 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500928 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
930 if ($where == '')
931 {
Andrey Andreev4c202602012-03-06 20:34:52 +0200932 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 }
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500936 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200938 $fields[$this->protect_identifiers($key)] = $this->escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
940
941 if ( ! is_array($where))
942 {
943 $dest = array($where);
944 }
945 else
946 {
947 $dest = array();
948 foreach ($where as $key => $val)
949 {
950 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200951 $key = $this->protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 if ($val !== '')
954 {
955 if ( ! $this->_has_operator($key))
956 {
957 $key .= ' =';
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 $val = ' '.$this->escape($val);
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 $dest[] = $prefix.$key.$val;
964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000966
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200967 return $this->_update($this->protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200968 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000969
970 // --------------------------------------------------------------------
971
972 /**
973 * Tests whether the string has an SQL operator
974 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 * @param string
976 * @return bool
977 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500978 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200980 $str = trim($str);
981 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
982 {
983 return FALSE;
984 }
985
986 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 }
988
989 // --------------------------------------------------------------------
990
991 /**
992 * Enables a native PHP function to be run, using a platform agnostic wrapper.
993 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 * @param string the function name
995 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200996 * @return mixed
997 */
Timothy Warrene45518d2012-03-06 07:38:00 -0500998 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
1000 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 if (FALSE === strpos($driver, $function))
1003 {
1004 $function = $driver.$function;
1005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 if ( ! function_exists($function))
1008 {
1009 if ($this->db_debug)
1010 {
1011 return $this->display_error('db_unsupported_function');
1012 }
1013 return FALSE;
1014 }
1015 else
1016 {
1017 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1018
Repox71b78092011-12-01 09:19:43 +01001019 if (is_null($args))
1020 {
1021 return call_user_func($function);
1022 }
1023 else
1024 {
1025 return call_user_func_array($function, $args);
1026 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 }
1028 }
1029
1030 // --------------------------------------------------------------------
1031
1032 /**
1033 * Set Cache Directory Path
1034 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 * @param string the path to the cache directory
1036 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001037 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001038 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 {
1040 $this->cachedir = $path;
1041 }
1042
1043 // --------------------------------------------------------------------
1044
1045 /**
1046 * Enable Query Caching
1047 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001048 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001049 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001050 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001052 $this->cache_on = TRUE;
1053 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 }
1055
1056 // --------------------------------------------------------------------
1057
1058 /**
1059 * Disable Query Caching
1060 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001061 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001062 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001063 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001065 $this->cache_on = FALSE;
1066 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 }
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069
1070 // --------------------------------------------------------------------
1071
1072 /**
1073 * Delete the cache files associated with a particular URI
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($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 {
1079 if ( ! $this->_cache_init())
1080 {
1081 return FALSE;
1082 }
1083 return $this->CACHE->delete($segment_one, $segment_two);
1084 }
1085
1086 // --------------------------------------------------------------------
1087
1088 /**
1089 * Delete All cache files
1090 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001091 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001092 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001093 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
1095 if ( ! $this->_cache_init())
1096 {
1097 return FALSE;
1098 }
1099
1100 return $this->CACHE->delete_all();
1101 }
1102
1103 // --------------------------------------------------------------------
1104
1105 /**
1106 * Initialize the Cache Class
1107 *
Andrey Andreev4c202602012-03-06 20:34:52 +02001108 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001109 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001110 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001112 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
1114 return TRUE;
1115 }
Derek Allarde37ab382009-02-03 16:13:57 +00001116
Andrey Andreev569091c2012-02-09 00:16:17 +02001117 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001119 if ( ! @include(BASEPATH.'database/DB_cache.php'))
1120 {
1121 return $this->cache_off();
1122 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
Derek Allarde37ab382009-02-03 16:13:57 +00001124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1126 return TRUE;
1127 }
1128
1129 // --------------------------------------------------------------------
1130
1131 /**
1132 * Close DB Connection
1133 *
Barry Mienydd671972010-10-04 16:33:58 +02001134 * @return void
1135 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001136 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 {
1138 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1139 {
1140 $this->_close($this->conn_id);
1141 }
1142 $this->conn_id = FALSE;
1143 }
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
1146
1147 /**
1148 * Display an error message
1149 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * @param string the error message
1151 * @param string any "swap" values
Andrey Andreev4c202602012-03-06 20:34:52 +02001152 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001153 * @return string sends the application/error_db.php template
1154 */
Timothy Warrene45518d2012-03-06 07:38:00 -05001155 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
Derek Jonese7792202010-03-02 17:24:46 -06001157 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 $LANG->load('db');
1159
1160 $heading = $LANG->line('db_error_heading');
1161
1162 if ($native == TRUE)
1163 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001164 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
1166 else
1167 {
1168 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1169 }
Barry Mienydd671972010-10-04 16:33:58 +02001170
Pascal Kriete60f8c392010-08-25 18:03:28 +02001171 // Find the most likely culprit of the error by going through
1172 // the backtrace until the source file is no longer in the
1173 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001174
Pascal Kriete60f8c392010-08-25 18:03:28 +02001175 $trace = debug_backtrace();
1176
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001177 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001178 {
1179 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1180 {
1181 // Found it - use a relative path for safety
1182 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1183 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001184
Pascal Kriete60f8c392010-08-25 18:03:28 +02001185 break;
1186 }
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188
Derek Jonese7792202010-03-02 17:24:46 -06001189 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 echo $error->show_error($heading, $message, 'error_db');
1191 exit;
1192 }
1193
1194 // --------------------------------------------------------------------
1195
1196 /**
1197 * Protect Identifiers
1198 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001200 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001202 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev4c202602012-03-06 20:34:52 +02001203 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 *
1205 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1206 *
1207 * Or a query with aliasing:
1208 *
1209 * SELECT m.member_id, m.member_name FROM members AS m
1210 *
1211 * Since the column name can include up to four segments (host, DB, table, column)
1212 * or also have an alias prefix, we need to do a bit of work to figure this out and
1213 * insert the table prefix (if it exists) in the proper position, and escape only
1214 * the correct identifiers.
1215 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 * @param string
1217 * @param bool
1218 * @param mixed
1219 * @param bool
1220 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001221 */
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001222 public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
1224 if ( ! is_bool($protect_identifiers))
1225 {
1226 $protect_identifiers = $this->_protect_identifiers;
1227 }
Derek Allarde37ab382009-02-03 16:13:57 +00001228
1229 if (is_array($item))
1230 {
1231 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001232 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001233 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001234 $escaped_array[$this->protect_identifiers($k)] = $this->protect_identifiers($v);
Derek Allarde37ab382009-02-03 16:13:57 +00001235 }
1236
1237 return $escaped_array;
1238 }
1239
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001241 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001242
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 // If the item has an alias declaration we remove it and set it aside.
1244 // Basically we remove everything to the right of the first space
1245 $alias = '';
1246 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001247 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 $alias = strstr($item, " ");
1249 $item = substr($item, 0, - strlen($alias));
1250 }
1251
Derek Allard911d3e02008-12-15 14:08:35 +00001252 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001253 // If a parenthesis is found we know that we do not need to
Andrey Andreev4c202602012-03-06 20:34:52 +02001254 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001255 // way to deal with this, but I'm not thinking of it -- Rick
1256 if (strpos($item, '(') !== FALSE)
1257 {
1258 return $item.$alias;
1259 }
1260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // Break the string apart if it contains periods, then insert the table prefix
1262 // in the correct location, assuming the period doesn't indicate that we're dealing
1263 // with an alias. While we're at it, we will escape the components
1264 if (strpos($item, '.') !== FALSE)
1265 {
1266 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 // Does the first segment of the exploded item match
Andrey Andreev4c202602012-03-06 20:34:52 +02001269 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 // we have nothing more to do other than escape the item
1271 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001272 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 if ($protect_identifiers === TRUE)
1274 {
1275 foreach ($parts as $key => $val)
1276 {
1277 if ( ! in_array($val, $this->_reserved_identifiers))
1278 {
1279 $parts[$key] = $this->_escape_identifiers($val);
1280 }
1281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001284 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 return $item.$alias;
1286 }
Barry Mienydd671972010-10-04 16:33:58 +02001287
Andrey Andreev4c202602012-03-06 20:34:52 +02001288 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 if ($this->dbprefix != '')
1290 {
1291 // We now add the table prefix based on some logic.
1292 // Do we have 4 segments (hostname.database.table.column)?
1293 // If so, we add the table prefix to the column name in the 3rd segment.
1294 if (isset($parts[3]))
1295 {
1296 $i = 2;
1297 }
1298 // Do we have 3 segments (database.table.column)?
1299 // If so, we add the table prefix to the column name in 2nd position
1300 elseif (isset($parts[2]))
1301 {
1302 $i = 1;
1303 }
1304 // Do we have 2 segments (table.column)?
1305 // If so, we add the table prefix to the column name in 1st segment
1306 else
1307 {
1308 $i = 0;
1309 }
Barry Mienydd671972010-10-04 16:33:58 +02001310
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 // This flag is set when the supplied $item does not contain a field name.
1312 // This can happen when this function is being called from a JOIN.
1313 if ($field_exists == FALSE)
1314 {
1315 $i++;
1316 }
Barry Mienydd671972010-10-04 16:33:58 +02001317
Derek Jones55acc8b2009-07-11 03:38:13 +00001318 // Verify table prefix and replace if necessary
1319 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1320 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001321 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001322 }
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 // We only add the table prefix if it does not already exist
1325 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1326 {
1327 $parts[$i] = $this->dbprefix.$parts[$i];
1328 }
Barry Mienydd671972010-10-04 16:33:58 +02001329
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 // Put the parts back together
1331 $item = implode('.', $parts);
1332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 if ($protect_identifiers === TRUE)
1335 {
1336 $item = $this->_escape_identifiers($item);
1337 }
Barry Mienydd671972010-10-04 16:33:58 +02001338
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 return $item.$alias;
1340 }
1341
Andrey Andreev4c202602012-03-06 20:34:52 +02001342 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 if ($this->dbprefix != '')
1344 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001345 // Verify table prefix and replace if necessary
1346 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1347 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001348 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001349 }
1350
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 // Do we prefix an item with no segments?
Andrey Andreev569091c2012-02-09 00:16:17 +02001352 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 {
1354 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001355 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 }
1357
Andrey Andreev569091c2012-02-09 00:16:17 +02001358 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
1360 $item = $this->_escape_identifiers($item);
1361 }
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 return $item.$alias;
1364 }
Andrey Andreev4c202602012-03-06 20:34:52 +02001365
Túbal Martín511f2252011-11-24 14:43:45 +01001366 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001367
Túbal Martín511f2252011-11-24 14:43:45 +01001368 /**
1369 * Dummy method that allows Active Record class to be disabled
1370 *
1371 * This function is used extensively by every db driver.
1372 *
Túbal Martín511f2252011-11-24 14:43:45 +01001373 * @return void
1374 */
Timothy Warren7eeda532012-03-19 16:01:55 -04001375 abstract protected function _reset_select();
Derek Allard2067d1a2008-11-13 22:59:24 +00001376
1377}
1378
Derek Allard2067d1a2008-11-13 22:59:24 +00001379/* End of file DB_driver.php */
Timothy Warrend2ff0bc2012-03-19 16:52:10 -04001380/* Location: ./system/database/DB_driver.php */