blob: 44e7ad8c7e887378b264d34b6b79b8516d6d033e [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
80 // These are use with Oracle
Andrey Andreev569091c2012-02-09 00:16:17 +020081 var $stmt_id;
82 var $curs_id;
83 var $limit_used;
Taufan Aditya18209332012-02-09 16:07:27 +070084
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050087 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * connection settings.
89 *
90 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020091 */
Andrey Andreev569091c2012-02-09 00:16:17 +020092 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
94 if (is_array($params))
95 {
96 foreach ($params as $key => $val)
97 {
98 $this->$key = $val;
99 }
100 }
101
102 log_message('debug', 'Database Driver Class Initialized');
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
106
107 /**
108 * Initialize Database Settings
109 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200110 * @access private Called by the constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @param mixed
Andrey Andreev569091c2012-02-09 00:16:17 +0200112 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200113 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200114 function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
116 // If an existing connection resource is available
117 // there is no need to connect and select the database
118 if (is_resource($this->conn_id) OR is_object($this->conn_id))
119 {
120 return TRUE;
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // Connect to the database and set the connection ID
126 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
127
Felix Balfoort5d581b62011-11-29 15:53:01 +0100128 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if ( ! $this->conn_id)
130 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100131 // Check if there is a failover set
132 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100134 // Go over all the failovers
135 foreach ($this->failover as $failover)
136 {
137 // Replace the current settings with those of the failover
138 foreach ($failover as $key => $val)
139 {
140 $this->$key = $val;
141 }
142
143 // Try to connect
144 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
145
146 // If a connection is made break the foreach loop
147 if ($this->conn_id)
148 {
149 break;
150 }
151 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100153
154 // We still don't have a connection?
155 if ( ! $this->conn_id)
156 {
157 log_message('error', 'Unable to connect to the database');
158
159 if ($this->db_debug)
160 {
161 $this->display_error('db_unable_to_connect');
162 }
163 return FALSE;
164 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
167 // ----------------------------------------------------------------
168
169 // Select the DB... assuming a database name is specified in the config file
170 if ($this->database != '')
171 {
172 if ( ! $this->db_select())
173 {
174 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 if ($this->db_debug)
177 {
178 $this->display_error('db_unable_to_select', $this->database);
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182 else
183 {
184 // We've selected the DB. Now we set the character set
185 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
186 {
187 return FALSE;
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 return TRUE;
191 }
192 }
193
194 return TRUE;
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
200 * Set client character set
201 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200202 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @param string
204 * @param string
Andrey Andreev569091c2012-02-09 00:16:17 +0200205 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200207 function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
209 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
210 {
211 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 if ($this->db_debug)
214 {
215 $this->display_error('db_unable_to_set_charset', $this->char_set);
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 return FALSE;
219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 return TRUE;
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * The name of the platform in use (mysql, mssql, etc...)
228 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200229 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200230 * @return string
231 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200232 function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 return $this->dbdriver;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * version of the database being used
242 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200243 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return string
245 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200246 function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 if (FALSE === ($sql = $this->_version()))
249 {
250 if ($this->db_debug)
251 {
252 return $this->display_error('db_unsupported_function');
253 }
254 return FALSE;
255 }
Derek Allard3683f772009-12-16 17:32:33 +0000256
257 // Some DBs have functions that return the version, and don't run special
258 // SQL queries per se. In these instances, just return the result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200259 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli');
Derek Allard3683f772009-12-16 17:32:33 +0000260
261 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 return $sql;
264 }
Derek Allard3683f772009-12-16 17:32:33 +0000265 else
266 {
267 $query = $this->query($sql);
268 return $query->row('ver');
269 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
275 * Execute the query
276 *
277 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500278 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * upon successful execution of a "write" type query. Returns boolean
280 * FALSE upon failure, and if the $db_debug variable is set to TRUE
281 * will raise an error.
282 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200283 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @param string An SQL query string
285 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200286 * @return mixed
287 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200288 function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
290 if ($sql == '')
291 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200292 log_message('error', 'Invalid query: '.$sql);
293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 if ($this->db_debug)
295 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 return $this->display_error('db_invalid_query');
297 }
298 return FALSE;
299 }
300
301 // Verify table prefix and replace if necessary
Andrey Andreev569091c2012-02-09 00:16:17 +0200302 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600303 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200304 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
Derek Jonese7792202010-03-02 17:24:46 -0600306
Derek Jones37f4b9c2011-07-01 17:56:50 -0500307 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // we will load the caching class and return the previously
309 // cached query if it exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200310 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 if ($this->_cache_init())
313 {
314 $this->load_rdriver();
315 if (FALSE !== ($cache = $this->CACHE->read($sql)))
316 {
317 return $cache;
318 }
319 }
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Compile binds if needed
323 if ($binds !== FALSE)
324 {
325 $sql = $this->compile_binds($sql, $binds);
326 }
327
Derek Jones37f4b9c2011-07-01 17:56:50 -0500328 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 if ($this->save_queries == TRUE)
330 {
331 $this->queries[] = $sql;
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // Start the Query Timer
335 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // Run the Query
338 if (FALSE === ($this->result_id = $this->simple_query($sql)))
339 {
340 if ($this->save_queries == TRUE)
341 {
342 $this->query_times[] = 0;
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // This will trigger a rollback if transactions are being used
346 $this->_trans_status = FALSE;
347
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200348 // Grab the error number and message now, as we might run some
349 // additional queries before displaying the error
350 $error_no = $this->_error_number();
351 $error_msg = $this->_error_message();
352
353 // Log errors
354 log_message('error', 'Query error: '.$error_msg);
355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 if ($this->db_debug)
357 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500359 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200360 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // transactions to remain in limbo.
362 $this->trans_complete();
363
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200364 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 return $this->display_error(
366 array(
367 'Error Number: '.$error_no,
368 $error_msg,
369 $sql
370 )
371 );
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 return FALSE;
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // Stop and aggregate the query time results
378 $time_end = list($em, $es) = explode(' ', microtime());
379 $this->benchmark += ($em + $es) - ($sm + $ss);
380
381 if ($this->save_queries == TRUE)
382 {
383 $this->query_times[] = ($em + $es) - ($sm + $ss);
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // Increment the query counter
387 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // Was the query a "write" type?
390 // If so we'll simply return true
391 if ($this->is_write_type($sql) === TRUE)
392 {
393 // If caching is enabled we'll auto-cleanup any
394 // existing files related to this particular URI
Andrey Andreev569091c2012-02-09 00:16:17 +0200395 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 $this->CACHE->delete();
398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 return TRUE;
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // Return TRUE if we don't need to create a result object
404 // Currently only the Oracle driver uses this when stored
405 // procedures are used
406 if ($return_object !== TRUE)
407 {
408 return TRUE;
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
411 // Load and instantiate the result driver
Barry Mienydd671972010-10-04 16:33:58 +0200412 $driver = $this->load_rdriver();
413 $RES = new $driver();
Andrey Andreev569091c2012-02-09 00:16:17 +0200414 $RES->conn_id = $this->conn_id;
415 $RES->result_id = $this->result_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416
Andrey Andreev569091c2012-02-09 00:16:17 +0200417 if ($this->dbdriver === 'oci8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 $RES->stmt_id = $this->stmt_id;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200420 $RES->curs_id = $this->curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 $RES->limit_used = $this->limit_used;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200422 // Passing the next one by reference to make sure it's updated, if needed:
423 $RES->commit_mode = &$this->_commit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 $this->stmt_id = FALSE;
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Jones37f4b9c2011-07-01 17:56:50 -0500427 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 // result object and save it to a cache file.
Andrey Andreev569091c2012-02-09 00:16:17 +0200429 if ($this->cache_on == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 // We'll create a new instance of the result object
432 // only without the platform specific driver since
433 // we can't use it with cached data (the query result
434 // resource ID won't be any good once we've cached the
435 // result object, so we'll have to compile the data
436 // and save it)
437 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 $CR->result_object = $RES->result_object();
439 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200440 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // Reset these since cached objects can not utilize resource IDs.
443 $CR->conn_id = NULL;
444 $CR->result_id = NULL;
445
446 $this->CACHE->write($sql, $CR);
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 return $RES;
450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Load the result drivers
456 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200457 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200458 * @return string the name of the result class
459 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200460 function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 $driver = 'CI_DB_'.$this->dbdriver.'_result';
463
464 if ( ! class_exists($driver))
465 {
Greg Aker3a746652011-04-19 10:59:47 -0500466 include_once(BASEPATH.'database/DB_result.php');
467 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 return $driver;
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // --------------------------------------------------------------------
474
475 /**
476 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500477 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * we only use it when running transaction commands since they do
479 * not require all the features of the main query() function.
480 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200481 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200483 * @return mixed
484 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200485 function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 if ( ! $this->conn_id)
488 {
489 $this->initialize();
490 }
491
492 return $this->_execute($sql);
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 // --------------------------------------------------------------------
496
497 /**
498 * Disable Transactions
499 * This permits transactions to be disabled at run-time.
500 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200501 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200502 * @return void
503 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200504 function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 $this->trans_enabled = FALSE;
507 }
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Enable/disable Transaction Strict Mode
513 * When strict mode is enabled, if you are running multiple groups of
514 * transactions, if one group fails all groups will be rolled back.
515 * If strict mode is disabled, each group is treated autonomously, meaning
516 * a failure of one group will not affect any others
517 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200518 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200519 * @return void
520 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200521 function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // --------------------------------------------------------------------
527
528 /**
529 * Start Transaction
530 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200531 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200532 * @return void
533 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200534 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200535 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 if ( ! $this->trans_enabled)
537 {
538 return FALSE;
539 }
540
541 // When transactions are nested we only begin/commit/rollback the outermost ones
542 if ($this->_trans_depth > 0)
543 {
544 $this->_trans_depth += 1;
545 return;
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500549 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Complete Transaction
556 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200557 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200558 * @return bool
559 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200560 function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 if ( ! $this->trans_enabled)
563 {
564 return FALSE;
565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 // When transactions are nested we only begin/commit/rollback the outermost ones
568 if ($this->_trans_depth > 1)
569 {
570 $this->_trans_depth -= 1;
571 return TRUE;
572 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500573 else
574 {
575 $this->_trans_depth = 0;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // The query() function will set this flag to FALSE in the event that a query failed
579 if ($this->_trans_status === FALSE)
580 {
581 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // If we are NOT running in strict mode, we will reset
584 // the _trans_status flag so that subsequent groups of transactions
585 // will be permitted.
586 if ($this->trans_strict === FALSE)
587 {
588 $this->_trans_status = TRUE;
589 }
590
591 log_message('debug', 'DB Transaction Failure');
592 return FALSE;
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 $this->trans_commit();
596 return TRUE;
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Lets you retrieve the transaction flag to determine if it has failed
603 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200604 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200605 * @return bool
606 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200607 function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 return $this->_trans_status;
610 }
611
612 // --------------------------------------------------------------------
613
614 /**
615 * Compile Bindings
616 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200617 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * @param string the sql statement
619 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200620 * @return string
621 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200622 function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 if (strpos($sql, $this->bind_marker) === FALSE)
625 {
626 return $sql;
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 if ( ! is_array($binds))
630 {
631 $binds = array($binds);
632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 // Get the sql segments around the bind markers
635 $segments = explode($this->bind_marker, $sql);
636
637 // The count of bind should be 1 less then the count of segments
638 // If there are more bind arguments trim it down
639 if (count($binds) >= count($segments)) {
640 $binds = array_slice($binds, 0, count($segments)-1);
641 }
642
643 // Construct the binded query
644 $result = $segments[0];
645 $i = 0;
646 foreach ($binds as $bind)
647 {
648 $result .= $this->escape($bind);
649 $result .= $segments[++$i];
650 }
651
652 return $result;
653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // --------------------------------------------------------------------
656
657 /**
658 * Determines if a query is a "write" type.
659 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200660 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 * @param string An SQL query string
Andrey Andreev569091c2012-02-09 00:16:17 +0200662 * @return boolean
Barry Mienydd671972010-10-04 16:33:58 +0200663 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200664 function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200666 if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
667 {
668 return FALSE;
669 }
670 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // --------------------------------------------------------------------
674
675 /**
676 * Calculate the aggregate query elapsed time
677 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200678 * @access public
679 * @param integer The number of decimal places
680 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200681 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200682 function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 return number_format($this->benchmark, $decimals);
685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 // --------------------------------------------------------------------
688
689 /**
690 * Returns the total number of queries
691 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200692 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200693 * @return integer
694 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200695 function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 {
697 return $this->query_count;
698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 // --------------------------------------------------------------------
701
702 /**
703 * Returns the last query that was executed
704 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200705 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200706 * @return void
707 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200708 function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
710 return end($this->queries);
711 }
712
713 // --------------------------------------------------------------------
714
715 /**
716 * "Smart" Escape String
717 *
718 * Escapes data based on type
719 * Sets boolean and null types
720 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200721 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200723 * @return mixed
724 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200725 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200726 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000727 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000729 $str = "'".$this->escape_str($str)."'";
730 }
731 elseif (is_bool($str))
732 {
733 $str = ($str === FALSE) ? 0 : 1;
734 }
735 elseif (is_null($str))
736 {
737 $str = 'NULL';
738 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000739
740 return $str;
741 }
742
743 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600744
Derek Jonese4ed5832009-02-20 21:44:59 +0000745 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000746 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000747 *
748 * Calls the individual driver for platform
749 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200750 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200751 * @access public
Derek Jonese4ed5832009-02-20 21:44:59 +0000752 * @param string
753 * @return mixed
754 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200755 function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200756 {
757 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000758 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000759
Derek Jonese4ed5832009-02-20 21:44:59 +0000760 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 /**
763 * Primary
764 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200765 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 * position is the primary key
767 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200768 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200770 * @return string
771 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200772 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200773 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 if ( ! is_array($fields))
777 {
778 return FALSE;
779 }
780
781 return current($fields);
782 }
783
784 // --------------------------------------------------------------------
785
786 /**
787 * Returns an array of table names
788 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200789 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200790 * @return array
791 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200792 function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
794 // Is there a cached result?
795 if (isset($this->data_cache['table_names']))
796 {
797 return $this->data_cache['table_names'];
798 }
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
801 {
802 if ($this->db_debug)
803 {
804 return $this->display_error('db_unsupported_function');
805 }
806 return FALSE;
807 }
808
809 $retval = array();
810 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 if ($query->num_rows() > 0)
813 {
Taufan Aditya18209332012-02-09 16:07:27 +0700814 $table = FALSE;
815 $rows = $query->result_array();
816 $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row))));
817
818 if ($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
Taufan Aditya18209332012-02-09 16:07:27 +0700820 $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name';
821 }
822
823 foreach ($rows as $row)
824 {
825 $retval[] = ( ! $table) ? current($row) : $row[$table];
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 }
827 }
828
829 $this->data_cache['table_names'] = $retval;
Taufan Aditya18209332012-02-09 16:07:27 +0700830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 return $this->data_cache['table_names'];
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 // --------------------------------------------------------------------
835
836 /**
837 * Determine if a particular table exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200838 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 * @return boolean
840 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200841 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200842 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200843 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 // --------------------------------------------------------------------
847
848 /**
Andrey Andreev569091c2012-02-09 00:16:17 +0200849 * Fetch MySQL Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200851 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200853 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200855 function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 {
857 // Is there a cached result?
858 if (isset($this->data_cache['field_names'][$table]))
859 {
860 return $this->data_cache['field_names'][$table];
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 if ($table == '')
864 {
865 if ($this->db_debug)
866 {
867 return $this->display_error('db_field_param_missing');
868 }
869 return FALSE;
870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Greg Aker1edde302010-01-26 00:17:01 +0000872 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
874 if ($this->db_debug)
875 {
876 return $this->display_error('db_unsupported_function');
877 }
878 return FALSE;
879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500884 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
886 if (isset($row['COLUMN_NAME']))
887 {
888 $retval[] = $row['COLUMN_NAME'];
889 }
890 else
891 {
892 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200893 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 $this->data_cache['field_names'][$table] = $retval;
897 return $this->data_cache['field_names'][$table];
898 }
899
900 // --------------------------------------------------------------------
901
902 /**
903 * Determine if a particular field exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200904 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 * @param string
906 * @param string
907 * @return boolean
908 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200909 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200910 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200911 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 // --------------------------------------------------------------------
915
916 /**
917 * Returns an object with field data
918 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200919 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200921 * @return object
922 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200923 function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
925 if ($table == '')
926 {
927 if ($this->db_debug)
928 {
929 return $this->display_error('db_field_param_missing');
930 }
931 return FALSE;
932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev569091c2012-02-09 00:16:17 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200937 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000938
939 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 /**
942 * Generate an insert string
943 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200944 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @param string the table upon which the query will be performed
946 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200947 * @return string
948 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200949 function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
951 $fields = array();
952 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200953
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500954 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
956 $fields[] = $this->_escape_identifiers($key);
957 $values[] = $this->escape($val);
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200961 }
962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 // --------------------------------------------------------------------
964
965 /**
966 * Generate an update string
967 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200968 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 * @param string the table upon which the query will be performed
970 * @param array an associative array data of key/values
971 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200972 * @return string
973 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200974 function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
976 if ($where == '')
977 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200978 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500982 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
985 }
986
987 if ( ! is_array($where))
988 {
989 $dest = array($where);
990 }
991 else
992 {
993 $dest = array();
994 foreach ($where as $key => $val)
995 {
996 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300997 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 if ($val !== '')
1000 {
1001 if ( ! $this->_has_operator($key))
1002 {
1003 $key .= ' =';
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 $val = ' '.$this->escape($val);
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 $dest[] = $prefix.$key.$val;
1010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
1013 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001014 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001015
1016 // --------------------------------------------------------------------
1017
1018 /**
1019 * Tests whether the string has an SQL operator
1020 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001021 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 * @param string
1023 * @return bool
1024 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001025 function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001027 $str = trim($str);
1028 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1029 {
1030 return FALSE;
1031 }
1032
1033 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
1035
1036 // --------------------------------------------------------------------
1037
1038 /**
1039 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1040 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001041 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 * @param string the function name
1043 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001044 * @return mixed
1045 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001046 function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
1048 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 if (FALSE === strpos($driver, $function))
1051 {
1052 $function = $driver.$function;
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 if ( ! function_exists($function))
1056 {
1057 if ($this->db_debug)
1058 {
1059 return $this->display_error('db_unsupported_function');
1060 }
1061 return FALSE;
1062 }
1063 else
1064 {
1065 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1066
Repox71b78092011-12-01 09:19:43 +01001067 if (is_null($args))
1068 {
1069 return call_user_func($function);
1070 }
1071 else
1072 {
1073 return call_user_func_array($function, $args);
1074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
1076 }
1077
1078 // --------------------------------------------------------------------
1079
1080 /**
1081 * Set Cache Directory Path
1082 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001083 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 * @param string the path to the cache directory
1085 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001086 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001087 function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 {
1089 $this->cachedir = $path;
1090 }
1091
1092 // --------------------------------------------------------------------
1093
1094 /**
1095 * Enable Query Caching
1096 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001097 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001099 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001100 function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001102 $this->cache_on = TRUE;
1103 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 }
1105
1106 // --------------------------------------------------------------------
1107
1108 /**
1109 * Disable Query Caching
1110 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001111 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001113 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001114 function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001116 $this->cache_on = FALSE;
1117 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120
1121 // --------------------------------------------------------------------
1122
1123 /**
1124 * Delete the cache files associated with a particular URI
1125 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001126 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001128 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001129 function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
1131 if ( ! $this->_cache_init())
1132 {
1133 return FALSE;
1134 }
1135 return $this->CACHE->delete($segment_one, $segment_two);
1136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
1141 * Delete All cache files
1142 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001143 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001145 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001146 function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
1148 if ( ! $this->_cache_init())
1149 {
1150 return FALSE;
1151 }
1152
1153 return $this->CACHE->delete_all();
1154 }
1155
1156 // --------------------------------------------------------------------
1157
1158 /**
1159 * Initialize the Cache Class
1160 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001161 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001163 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001164 function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001166 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
1168 return TRUE;
1169 }
Derek Allarde37ab382009-02-03 16:13:57 +00001170
Andrey Andreev569091c2012-02-09 00:16:17 +02001171 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001173 if ( ! @include(BASEPATH.'database/DB_cache.php'))
1174 {
1175 return $this->cache_off();
1176 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 }
Derek Allarde37ab382009-02-03 16:13:57 +00001178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1180 return TRUE;
1181 }
1182
1183 // --------------------------------------------------------------------
1184
1185 /**
1186 * Close DB Connection
1187 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001188 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001189 * @return void
1190 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001191 function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 {
1193 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1194 {
1195 $this->_close($this->conn_id);
1196 }
1197 $this->conn_id = FALSE;
1198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 // --------------------------------------------------------------------
1201
1202 /**
1203 * Display an error message
1204 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001205 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 * @param string the error message
1207 * @param string any "swap" values
1208 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001209 * @return string sends the application/error_db.php template
1210 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001211 function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
Derek Jonese7792202010-03-02 17:24:46 -06001213 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 $LANG->load('db');
1215
1216 $heading = $LANG->line('db_error_heading');
1217
1218 if ($native == TRUE)
1219 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001220 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 }
1222 else
1223 {
1224 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1225 }
Barry Mienydd671972010-10-04 16:33:58 +02001226
Pascal Kriete60f8c392010-08-25 18:03:28 +02001227 // Find the most likely culprit of the error by going through
1228 // the backtrace until the source file is no longer in the
1229 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001230
Pascal Kriete60f8c392010-08-25 18:03:28 +02001231 $trace = debug_backtrace();
1232
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001233 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001234 {
1235 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1236 {
1237 // Found it - use a relative path for safety
1238 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1239 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001240
Pascal Kriete60f8c392010-08-25 18:03:28 +02001241 break;
1242 }
1243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Jonese7792202010-03-02 17:24:46 -06001245 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 echo $error->show_error($heading, $message, 'error_db');
1247 exit;
1248 }
1249
1250 // --------------------------------------------------------------------
1251
1252 /**
1253 * Protect Identifiers
1254 *
1255 * This function adds backticks if appropriate based on db type
1256 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001257 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 * @param mixed the item to escape
1259 * @return mixed the item with backticks
1260 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001261 function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
1263 return $this->_protect_identifiers($item, $prefix_single);
1264 }
1265
1266 // --------------------------------------------------------------------
1267
1268 /**
1269 * Protect Identifiers
1270 *
1271 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001272 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001274 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev569091c2012-02-09 00:16:17 +02001275 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 *
1277 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1278 *
1279 * Or a query with aliasing:
1280 *
1281 * SELECT m.member_id, m.member_name FROM members AS m
1282 *
1283 * Since the column name can include up to four segments (host, DB, table, column)
1284 * or also have an alias prefix, we need to do a bit of work to figure this out and
1285 * insert the table prefix (if it exists) in the proper position, and escape only
1286 * the correct identifiers.
1287 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001288 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 * @param string
1290 * @param bool
1291 * @param mixed
1292 * @param bool
1293 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001294 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001295 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 {
1297 if ( ! is_bool($protect_identifiers))
1298 {
1299 $protect_identifiers = $this->_protect_identifiers;
1300 }
Derek Allarde37ab382009-02-03 16:13:57 +00001301
1302 if (is_array($item))
1303 {
1304 $escaped_array = array();
1305
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001306 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001307 {
1308 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1309 }
1310
1311 return $escaped_array;
1312 }
1313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001315 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001316
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 // If the item has an alias declaration we remove it and set it aside.
1318 // Basically we remove everything to the right of the first space
1319 $alias = '';
1320 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001321 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 $alias = strstr($item, " ");
1323 $item = substr($item, 0, - strlen($alias));
1324 }
1325
Derek Allard911d3e02008-12-15 14:08:35 +00001326 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001327 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001328 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001329 // way to deal with this, but I'm not thinking of it -- Rick
1330 if (strpos($item, '(') !== FALSE)
1331 {
1332 return $item.$alias;
1333 }
1334
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // Break the string apart if it contains periods, then insert the table prefix
1336 // in the correct location, assuming the period doesn't indicate that we're dealing
1337 // with an alias. While we're at it, we will escape the components
1338 if (strpos($item, '.') !== FALSE)
1339 {
1340 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001341
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 // Does the first segment of the exploded item match
Andrey Andreev569091c2012-02-09 00:16:17 +02001343 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 // we have nothing more to do other than escape the item
1345 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001346 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 if ($protect_identifiers === TRUE)
1348 {
1349 foreach ($parts as $key => $val)
1350 {
1351 if ( ! in_array($val, $this->_reserved_identifiers))
1352 {
1353 $parts[$key] = $this->_escape_identifiers($val);
1354 }
1355 }
Barry Mienydd671972010-10-04 16:33:58 +02001356
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001358 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 return $item.$alias;
1360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Andrey Andreev569091c2012-02-09 00:16:17 +02001362 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 if ($this->dbprefix != '')
1364 {
1365 // We now add the table prefix based on some logic.
1366 // Do we have 4 segments (hostname.database.table.column)?
1367 // If so, we add the table prefix to the column name in the 3rd segment.
1368 if (isset($parts[3]))
1369 {
1370 $i = 2;
1371 }
1372 // Do we have 3 segments (database.table.column)?
1373 // If so, we add the table prefix to the column name in 2nd position
1374 elseif (isset($parts[2]))
1375 {
1376 $i = 1;
1377 }
1378 // Do we have 2 segments (table.column)?
1379 // If so, we add the table prefix to the column name in 1st segment
1380 else
1381 {
1382 $i = 0;
1383 }
Barry Mienydd671972010-10-04 16:33:58 +02001384
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 // This flag is set when the supplied $item does not contain a field name.
1386 // This can happen when this function is being called from a JOIN.
1387 if ($field_exists == FALSE)
1388 {
1389 $i++;
1390 }
Barry Mienydd671972010-10-04 16:33:58 +02001391
Derek Jones55acc8b2009-07-11 03:38:13 +00001392 // Verify table prefix and replace if necessary
1393 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1394 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001395 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001396 }
Barry Mienydd671972010-10-04 16:33:58 +02001397
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 // We only add the table prefix if it does not already exist
1399 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1400 {
1401 $parts[$i] = $this->dbprefix.$parts[$i];
1402 }
Barry Mienydd671972010-10-04 16:33:58 +02001403
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 // Put the parts back together
1405 $item = implode('.', $parts);
1406 }
Barry Mienydd671972010-10-04 16:33:58 +02001407
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 if ($protect_identifiers === TRUE)
1409 {
1410 $item = $this->_escape_identifiers($item);
1411 }
Barry Mienydd671972010-10-04 16:33:58 +02001412
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 return $item.$alias;
1414 }
1415
Andrey Andreev569091c2012-02-09 00:16:17 +02001416 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 if ($this->dbprefix != '')
1418 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001419 // Verify table prefix and replace if necessary
1420 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1421 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001422 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001423 }
1424
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 // Do we prefix an item with no segments?
Andrey Andreev569091c2012-02-09 00:16:17 +02001426 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 {
1428 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001429 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 }
1431
Andrey Andreev569091c2012-02-09 00:16:17 +02001432 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 {
1434 $item = $this->_escape_identifiers($item);
1435 }
Barry Mienydd671972010-10-04 16:33:58 +02001436
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 return $item.$alias;
1438 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001439}
1440
Derek Allard2067d1a2008-11-13 22:59:24 +00001441/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001442/* Location: ./system/database/DB_driver.php */