blob: daca08f0f91c5cb264ede0f12b625fdb5a40d5dc [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;
84
85
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050088 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * connection settings.
90 *
91 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020092 */
Andrey Andreev569091c2012-02-09 00:16:17 +020093 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 if (is_array($params))
96 {
97 foreach ($params as $key => $val)
98 {
99 $this->$key = $val;
100 }
101 }
102
103 log_message('debug', 'Database Driver Class Initialized');
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
109 * Initialize Database Settings
110 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200111 * @access private Called by the constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 * @param mixed
Andrey Andreev569091c2012-02-09 00:16:17 +0200113 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200115 function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 // If an existing connection resource is available
118 // there is no need to connect and select the database
119 if (is_resource($this->conn_id) OR is_object($this->conn_id))
120 {
121 return TRUE;
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // Connect to the database and set the connection ID
127 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
128
Felix Balfoort5d581b62011-11-29 15:53:01 +0100129 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if ( ! $this->conn_id)
131 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100132 // Check if there is a failover set
133 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100135 // Go over all the failovers
136 foreach ($this->failover as $failover)
137 {
138 // Replace the current settings with those of the failover
139 foreach ($failover as $key => $val)
140 {
141 $this->$key = $val;
142 }
143
144 // Try to connect
145 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
146
147 // If a connection is made break the foreach loop
148 if ($this->conn_id)
149 {
150 break;
151 }
152 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100154
155 // We still don't have a connection?
156 if ( ! $this->conn_id)
157 {
158 log_message('error', 'Unable to connect to the database');
159
160 if ($this->db_debug)
161 {
162 $this->display_error('db_unable_to_connect');
163 }
164 return FALSE;
165 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
168 // ----------------------------------------------------------------
169
170 // Select the DB... assuming a database name is specified in the config file
171 if ($this->database != '')
172 {
173 if ( ! $this->db_select())
174 {
175 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if ($this->db_debug)
178 {
179 $this->display_error('db_unable_to_select', $this->database);
180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183 else
184 {
185 // We've selected the DB. Now we set the character set
186 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
187 {
188 return FALSE;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 return TRUE;
192 }
193 }
194
195 return TRUE;
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // --------------------------------------------------------------------
199
200 /**
201 * Set client character set
202 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200203 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @param string
205 * @param string
Andrey Andreev569091c2012-02-09 00:16:17 +0200206 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200208 function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
211 {
212 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 if ($this->db_debug)
215 {
216 $this->display_error('db_unable_to_set_charset', $this->char_set);
217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 return FALSE;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 return TRUE;
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * The name of the platform in use (mysql, mssql, etc...)
229 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200230 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200231 * @return string
232 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200233 function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
235 return $this->dbdriver;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500241 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * version of the database being used
243 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200244 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200245 * @return string
246 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200247 function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 if (FALSE === ($sql = $this->_version()))
250 {
251 if ($this->db_debug)
252 {
253 return $this->display_error('db_unsupported_function');
254 }
255 return FALSE;
256 }
Derek Allard3683f772009-12-16 17:32:33 +0000257
258 // Some DBs have functions that return the version, and don't run special
259 // SQL queries per se. In these instances, just return the result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200260 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli');
Derek Allard3683f772009-12-16 17:32:33 +0000261
262 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 return $sql;
265 }
Derek Allard3683f772009-12-16 17:32:33 +0000266 else
267 {
268 $query = $this->query($sql);
269 return $query->row('ver');
270 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * Execute the query
277 *
278 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500279 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * upon successful execution of a "write" type query. Returns boolean
281 * FALSE upon failure, and if the $db_debug variable is set to TRUE
282 * will raise an error.
283 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200284 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @param string An SQL query string
286 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200287 * @return mixed
288 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200289 function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 if ($sql == '')
292 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200293 log_message('error', 'Invalid query: '.$sql);
294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 if ($this->db_debug)
296 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 return $this->display_error('db_invalid_query');
298 }
299 return FALSE;
300 }
301
302 // Verify table prefix and replace if necessary
Andrey Andreev569091c2012-02-09 00:16:17 +0200303 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600304 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200305 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
Derek Jonese7792202010-03-02 17:24:46 -0600307
Derek Jones37f4b9c2011-07-01 17:56:50 -0500308 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // we will load the caching class and return the previously
310 // cached query if it exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200311 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 if ($this->_cache_init())
314 {
315 $this->load_rdriver();
316 if (FALSE !== ($cache = $this->CACHE->read($sql)))
317 {
318 return $cache;
319 }
320 }
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Compile binds if needed
324 if ($binds !== FALSE)
325 {
326 $sql = $this->compile_binds($sql, $binds);
327 }
328
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 if ($this->save_queries == TRUE)
331 {
332 $this->queries[] = $sql;
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // Start the Query Timer
336 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Run the Query
339 if (FALSE === ($this->result_id = $this->simple_query($sql)))
340 {
341 if ($this->save_queries == TRUE)
342 {
343 $this->query_times[] = 0;
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // This will trigger a rollback if transactions are being used
347 $this->_trans_status = FALSE;
348
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200349 // Grab the error number and message now, as we might run some
350 // additional queries before displaying the error
351 $error_no = $this->_error_number();
352 $error_msg = $this->_error_message();
353
354 // Log errors
355 log_message('error', 'Query error: '.$error_msg);
356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 if ($this->db_debug)
358 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500360 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200361 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // transactions to remain in limbo.
363 $this->trans_complete();
364
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200365 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 return $this->display_error(
367 array(
368 'Error Number: '.$error_no,
369 $error_msg,
370 $sql
371 )
372 );
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 return FALSE;
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // Stop and aggregate the query time results
379 $time_end = list($em, $es) = explode(' ', microtime());
380 $this->benchmark += ($em + $es) - ($sm + $ss);
381
382 if ($this->save_queries == TRUE)
383 {
384 $this->query_times[] = ($em + $es) - ($sm + $ss);
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // Increment the query counter
388 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // Was the query a "write" type?
391 // If so we'll simply return true
392 if ($this->is_write_type($sql) === TRUE)
393 {
394 // If caching is enabled we'll auto-cleanup any
395 // existing files related to this particular URI
Andrey Andreev569091c2012-02-09 00:16:17 +0200396 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 $this->CACHE->delete();
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 return TRUE;
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // Return TRUE if we don't need to create a result object
405 // Currently only the Oracle driver uses this when stored
406 // procedures are used
407 if ($return_object !== TRUE)
408 {
409 return TRUE;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
412 // Load and instantiate the result driver
Barry Mienydd671972010-10-04 16:33:58 +0200413 $driver = $this->load_rdriver();
414 $RES = new $driver();
Andrey Andreev569091c2012-02-09 00:16:17 +0200415 $RES->conn_id = $this->conn_id;
416 $RES->result_id = $this->result_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417
Andrey Andreev569091c2012-02-09 00:16:17 +0200418 if ($this->dbdriver === 'oci8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 $RES->stmt_id = $this->stmt_id;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200421 $RES->curs_id = $this->curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 $RES->limit_used = $this->limit_used;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200423 // Passing the next one by reference to make sure it's updated, if needed:
424 $RES->commit_mode = &$this->_commit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 $this->stmt_id = FALSE;
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Jones37f4b9c2011-07-01 17:56:50 -0500428 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 // result object and save it to a cache file.
Andrey Andreev569091c2012-02-09 00:16:17 +0200430 if ($this->cache_on == TRUE AND $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 // We'll create a new instance of the result object
433 // only without the platform specific driver since
434 // we can't use it with cached data (the query result
435 // resource ID won't be any good once we've cached the
436 // result object, so we'll have to compile the data
437 // and save it)
438 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 $CR->result_object = $RES->result_object();
440 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200441 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // Reset these since cached objects can not utilize resource IDs.
444 $CR->conn_id = NULL;
445 $CR->result_id = NULL;
446
447 $this->CACHE->write($sql, $CR);
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 return $RES;
451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * Load the result drivers
457 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200458 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200459 * @return string the name of the result class
460 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200461 function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 $driver = 'CI_DB_'.$this->dbdriver.'_result';
464
465 if ( ! class_exists($driver))
466 {
Greg Aker3a746652011-04-19 10:59:47 -0500467 include_once(BASEPATH.'database/DB_result.php');
468 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 return $driver;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
475
476 /**
477 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500478 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * we only use it when running transaction commands since they do
480 * not require all the features of the main query() function.
481 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200482 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200484 * @return mixed
485 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200486 function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 if ( ! $this->conn_id)
489 {
490 $this->initialize();
491 }
492
493 return $this->_execute($sql);
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
497
498 /**
499 * Disable Transactions
500 * This permits transactions to be disabled at run-time.
501 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200502 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200503 * @return void
504 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200505 function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 {
507 $this->trans_enabled = FALSE;
508 }
509
510 // --------------------------------------------------------------------
511
512 /**
513 * Enable/disable Transaction Strict Mode
514 * When strict mode is enabled, if you are running multiple groups of
515 * transactions, if one group fails all groups will be rolled back.
516 * If strict mode is disabled, each group is treated autonomously, meaning
517 * a failure of one group will not affect any others
518 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200519 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200520 * @return void
521 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200522 function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // --------------------------------------------------------------------
528
529 /**
530 * Start Transaction
531 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200532 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200533 * @return void
534 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200535 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200536 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 if ( ! $this->trans_enabled)
538 {
539 return FALSE;
540 }
541
542 // When transactions are nested we only begin/commit/rollback the outermost ones
543 if ($this->_trans_depth > 0)
544 {
545 $this->_trans_depth += 1;
546 return;
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500550 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552
553 // --------------------------------------------------------------------
554
555 /**
556 * Complete Transaction
557 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200558 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200559 * @return bool
560 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200561 function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
563 if ( ! $this->trans_enabled)
564 {
565 return FALSE;
566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 // When transactions are nested we only begin/commit/rollback the outermost ones
569 if ($this->_trans_depth > 1)
570 {
571 $this->_trans_depth -= 1;
572 return TRUE;
573 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500574 else
575 {
576 $this->_trans_depth = 0;
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // The query() function will set this flag to FALSE in the event that a query failed
580 if ($this->_trans_status === FALSE)
581 {
582 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // If we are NOT running in strict mode, we will reset
585 // the _trans_status flag so that subsequent groups of transactions
586 // will be permitted.
587 if ($this->trans_strict === FALSE)
588 {
589 $this->_trans_status = TRUE;
590 }
591
592 log_message('debug', 'DB Transaction Failure');
593 return FALSE;
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 $this->trans_commit();
597 return TRUE;
598 }
599
600 // --------------------------------------------------------------------
601
602 /**
603 * Lets you retrieve the transaction flag to determine if it has failed
604 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200605 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200606 * @return bool
607 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200608 function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 return $this->_trans_status;
611 }
612
613 // --------------------------------------------------------------------
614
615 /**
616 * Compile Bindings
617 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200618 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @param string the sql statement
620 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200621 * @return string
622 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200623 function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 if (strpos($sql, $this->bind_marker) === FALSE)
626 {
627 return $sql;
628 }
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 if ( ! is_array($binds))
631 {
632 $binds = array($binds);
633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // Get the sql segments around the bind markers
636 $segments = explode($this->bind_marker, $sql);
637
638 // The count of bind should be 1 less then the count of segments
639 // If there are more bind arguments trim it down
640 if (count($binds) >= count($segments)) {
641 $binds = array_slice($binds, 0, count($segments)-1);
642 }
643
644 // Construct the binded query
645 $result = $segments[0];
646 $i = 0;
647 foreach ($binds as $bind)
648 {
649 $result .= $this->escape($bind);
650 $result .= $segments[++$i];
651 }
652
653 return $result;
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 // --------------------------------------------------------------------
657
658 /**
659 * Determines if a query is a "write" type.
660 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200661 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 * @param string An SQL query string
Andrey Andreev569091c2012-02-09 00:16:17 +0200663 * @return boolean
Barry Mienydd671972010-10-04 16:33:58 +0200664 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200665 function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200667 if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
668 {
669 return FALSE;
670 }
671 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 // --------------------------------------------------------------------
675
676 /**
677 * Calculate the aggregate query elapsed time
678 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200679 * @access public
680 * @param integer The number of decimal places
681 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200682 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200683 function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
685 return number_format($this->benchmark, $decimals);
686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 // --------------------------------------------------------------------
689
690 /**
691 * Returns the total number of queries
692 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200693 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200694 * @return integer
695 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200696 function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
698 return $this->query_count;
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * Returns the last query that was executed
705 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200706 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200707 * @return void
708 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200709 function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 return end($this->queries);
712 }
713
714 // --------------------------------------------------------------------
715
716 /**
717 * "Smart" Escape String
718 *
719 * Escapes data based on type
720 * Sets boolean and null types
721 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200722 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200724 * @return mixed
725 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200726 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200727 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000728 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000730 $str = "'".$this->escape_str($str)."'";
731 }
732 elseif (is_bool($str))
733 {
734 $str = ($str === FALSE) ? 0 : 1;
735 }
736 elseif (is_null($str))
737 {
738 $str = 'NULL';
739 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000740
741 return $str;
742 }
743
744 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600745
Derek Jonese4ed5832009-02-20 21:44:59 +0000746 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000747 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000748 *
749 * Calls the individual driver for platform
750 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200751 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200752 * @access public
Derek Jonese4ed5832009-02-20 21:44:59 +0000753 * @param string
754 * @return mixed
755 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200756 function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200757 {
758 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000759 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000760
Derek Jonese4ed5832009-02-20 21:44:59 +0000761 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 /**
764 * Primary
765 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200766 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 * position is the primary key
768 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200769 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200771 * @return string
772 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200773 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200774 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 if ( ! is_array($fields))
778 {
779 return FALSE;
780 }
781
782 return current($fields);
783 }
784
785 // --------------------------------------------------------------------
786
787 /**
788 * Returns an array of table names
789 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200790 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200791 * @return array
792 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200793 function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 // Is there a cached result?
796 if (isset($this->data_cache['table_names']))
797 {
798 return $this->data_cache['table_names'];
799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
802 {
803 if ($this->db_debug)
804 {
805 return $this->display_error('db_unsupported_function');
806 }
807 return FALSE;
808 }
809
810 $retval = array();
811 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 if ($query->num_rows() > 0)
814 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500815 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
817 if (isset($row['TABLE_NAME']))
818 {
819 $retval[] = $row['TABLE_NAME'];
820 }
821 else
822 {
823 $retval[] = array_shift($row);
824 }
825 }
826 }
827
828 $this->data_cache['table_names'] = $retval;
829 return $this->data_cache['table_names'];
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 // --------------------------------------------------------------------
833
834 /**
835 * Determine if a particular table exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200836 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 * @return boolean
838 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200839 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200840 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200841 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 }
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 // --------------------------------------------------------------------
845
846 /**
Andrey Andreev569091c2012-02-09 00:16:17 +0200847 * Fetch MySQL Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200849 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200851 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200853 function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
855 // Is there a cached result?
856 if (isset($this->data_cache['field_names'][$table]))
857 {
858 return $this->data_cache['field_names'][$table];
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 if ($table == '')
862 {
863 if ($this->db_debug)
864 {
865 return $this->display_error('db_field_param_missing');
866 }
867 return FALSE;
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Greg Aker1edde302010-01-26 00:17:01 +0000870 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
872 if ($this->db_debug)
873 {
874 return $this->display_error('db_unsupported_function');
875 }
876 return FALSE;
877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500882 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 {
884 if (isset($row['COLUMN_NAME']))
885 {
886 $retval[] = $row['COLUMN_NAME'];
887 }
888 else
889 {
890 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200891 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 $this->data_cache['field_names'][$table] = $retval;
895 return $this->data_cache['field_names'][$table];
896 }
897
898 // --------------------------------------------------------------------
899
900 /**
901 * Determine if a particular field exists
Andrey Andreev569091c2012-02-09 00:16:17 +0200902 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 * @param string
904 * @param string
905 * @return boolean
906 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200907 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200908 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200909 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
913
914 /**
915 * Returns an object with field data
916 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200917 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200919 * @return object
920 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200921 function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
923 if ($table == '')
924 {
925 if ($this->db_debug)
926 {
927 return $this->display_error('db_field_param_missing');
928 }
929 return FALSE;
930 }
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Andrey Andreev569091c2012-02-09 00:16:17 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200935 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000936
937 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 /**
940 * Generate an insert string
941 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200942 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 * @param string the table upon which the query will be performed
944 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200945 * @return string
946 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200947 function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 $fields = array();
950 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200951
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500952 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
954 $fields[] = $this->_escape_identifiers($key);
955 $values[] = $this->escape($val);
956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200959 }
960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // --------------------------------------------------------------------
962
963 /**
964 * Generate an update string
965 *
Andrey Andreev569091c2012-02-09 00:16:17 +0200966 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 * @param string the table upon which the query will be performed
968 * @param array an associative array data of key/values
969 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200970 * @return string
971 */
Andrey Andreev569091c2012-02-09 00:16:17 +0200972 function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
974 if ($where == '')
975 {
Andrey Andreev569091c2012-02-09 00:16:17 +0200976 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 }
Barry Mienydd671972010-10-04 16:33:58 +0200978
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500980 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
983 }
984
985 if ( ! is_array($where))
986 {
987 $dest = array($where);
988 }
989 else
990 {
991 $dest = array();
992 foreach ($where as $key => $val)
993 {
994 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300995 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 if ($val !== '')
998 {
999 if ( ! $this->_has_operator($key))
1000 {
1001 $key .= ' =';
1002 }
Barry Mienydd671972010-10-04 16:33:58 +02001003
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 $val = ' '.$this->escape($val);
1005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 $dest[] = $prefix.$key.$val;
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001010
1011 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +02001012 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001013
1014 // --------------------------------------------------------------------
1015
1016 /**
1017 * Tests whether the string has an SQL operator
1018 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001019 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * @param string
1021 * @return bool
1022 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001023 function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001025 $str = trim($str);
1026 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1027 {
1028 return FALSE;
1029 }
1030
1031 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 }
1033
1034 // --------------------------------------------------------------------
1035
1036 /**
1037 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1038 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001039 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 * @param string the function name
1041 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001042 * @return mixed
1043 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001044 function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
1046 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 if (FALSE === strpos($driver, $function))
1049 {
1050 $function = $driver.$function;
1051 }
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 if ( ! function_exists($function))
1054 {
1055 if ($this->db_debug)
1056 {
1057 return $this->display_error('db_unsupported_function');
1058 }
1059 return FALSE;
1060 }
1061 else
1062 {
1063 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1064
Repox71b78092011-12-01 09:19:43 +01001065 if (is_null($args))
1066 {
1067 return call_user_func($function);
1068 }
1069 else
1070 {
1071 return call_user_func_array($function, $args);
1072 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 }
1074 }
1075
1076 // --------------------------------------------------------------------
1077
1078 /**
1079 * Set Cache Directory Path
1080 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001081 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 * @param string the path to the cache directory
1083 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001084 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001085 function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 {
1087 $this->cachedir = $path;
1088 }
1089
1090 // --------------------------------------------------------------------
1091
1092 /**
1093 * Enable Query Caching
1094 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001095 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001097 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001098 function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001100 $this->cache_on = TRUE;
1101 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 }
1103
1104 // --------------------------------------------------------------------
1105
1106 /**
1107 * Disable Query Caching
1108 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001109 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001111 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001112 function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001114 $this->cache_on = FALSE;
1115 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118
1119 // --------------------------------------------------------------------
1120
1121 /**
1122 * Delete the cache files associated with a particular URI
1123 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001124 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001126 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001127 function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
1129 if ( ! $this->_cache_init())
1130 {
1131 return FALSE;
1132 }
1133 return $this->CACHE->delete($segment_one, $segment_two);
1134 }
1135
1136 // --------------------------------------------------------------------
1137
1138 /**
1139 * Delete All cache files
1140 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001141 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001143 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001144 function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 {
1146 if ( ! $this->_cache_init())
1147 {
1148 return FALSE;
1149 }
1150
1151 return $this->CACHE->delete_all();
1152 }
1153
1154 // --------------------------------------------------------------------
1155
1156 /**
1157 * Initialize the Cache Class
1158 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001159 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001161 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001162 function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001164 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
1166 return TRUE;
1167 }
Derek Allarde37ab382009-02-03 16:13:57 +00001168
Andrey Andreev569091c2012-02-09 00:16:17 +02001169 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001171 if ( ! @include(BASEPATH.'database/DB_cache.php'))
1172 {
1173 return $this->cache_off();
1174 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 }
Derek Allarde37ab382009-02-03 16:13:57 +00001176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1178 return TRUE;
1179 }
1180
1181 // --------------------------------------------------------------------
1182
1183 /**
1184 * Close DB Connection
1185 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001186 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001187 * @return void
1188 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001189 function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
1191 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1192 {
1193 $this->_close($this->conn_id);
1194 }
1195 $this->conn_id = FALSE;
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 // --------------------------------------------------------------------
1199
1200 /**
1201 * Display an error message
1202 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001203 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 * @param string the error message
1205 * @param string any "swap" values
1206 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001207 * @return string sends the application/error_db.php template
1208 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001209 function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 {
Derek Jonese7792202010-03-02 17:24:46 -06001211 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 $LANG->load('db');
1213
1214 $heading = $LANG->line('db_error_heading');
1215
1216 if ($native == TRUE)
1217 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001218 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
1220 else
1221 {
1222 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Pascal Kriete60f8c392010-08-25 18:03:28 +02001225 // Find the most likely culprit of the error by going through
1226 // the backtrace until the source file is no longer in the
1227 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001228
Pascal Kriete60f8c392010-08-25 18:03:28 +02001229 $trace = debug_backtrace();
1230
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001231 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001232 {
1233 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1234 {
1235 // Found it - use a relative path for safety
1236 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1237 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001238
Pascal Kriete60f8c392010-08-25 18:03:28 +02001239 break;
1240 }
1241 }
Barry Mienydd671972010-10-04 16:33:58 +02001242
Derek Jonese7792202010-03-02 17:24:46 -06001243 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 echo $error->show_error($heading, $message, 'error_db');
1245 exit;
1246 }
1247
1248 // --------------------------------------------------------------------
1249
1250 /**
1251 * Protect Identifiers
1252 *
1253 * This function adds backticks if appropriate based on db type
1254 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001255 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 * @param mixed the item to escape
1257 * @return mixed the item with backticks
1258 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001259 function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
1261 return $this->_protect_identifiers($item, $prefix_single);
1262 }
1263
1264 // --------------------------------------------------------------------
1265
1266 /**
1267 * Protect Identifiers
1268 *
1269 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001270 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001272 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev569091c2012-02-09 00:16:17 +02001273 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 *
1275 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1276 *
1277 * Or a query with aliasing:
1278 *
1279 * SELECT m.member_id, m.member_name FROM members AS m
1280 *
1281 * Since the column name can include up to four segments (host, DB, table, column)
1282 * or also have an alias prefix, we need to do a bit of work to figure this out and
1283 * insert the table prefix (if it exists) in the proper position, and escape only
1284 * the correct identifiers.
1285 *
Andrey Andreev569091c2012-02-09 00:16:17 +02001286 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 * @param string
1288 * @param bool
1289 * @param mixed
1290 * @param bool
1291 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001292 */
Andrey Andreev569091c2012-02-09 00:16:17 +02001293 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 {
1295 if ( ! is_bool($protect_identifiers))
1296 {
1297 $protect_identifiers = $this->_protect_identifiers;
1298 }
Derek Allarde37ab382009-02-03 16:13:57 +00001299
1300 if (is_array($item))
1301 {
1302 $escaped_array = array();
1303
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001304 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001305 {
1306 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1307 }
1308
1309 return $escaped_array;
1310 }
1311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001313 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 // If the item has an alias declaration we remove it and set it aside.
1316 // Basically we remove everything to the right of the first space
1317 $alias = '';
1318 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001319 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 $alias = strstr($item, " ");
1321 $item = substr($item, 0, - strlen($alias));
1322 }
1323
Derek Allard911d3e02008-12-15 14:08:35 +00001324 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001325 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001326 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001327 // way to deal with this, but I'm not thinking of it -- Rick
1328 if (strpos($item, '(') !== FALSE)
1329 {
1330 return $item.$alias;
1331 }
1332
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 // Break the string apart if it contains periods, then insert the table prefix
1334 // in the correct location, assuming the period doesn't indicate that we're dealing
1335 // with an alias. While we're at it, we will escape the components
1336 if (strpos($item, '.') !== FALSE)
1337 {
1338 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001339
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 // Does the first segment of the exploded item match
Andrey Andreev569091c2012-02-09 00:16:17 +02001341 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 // we have nothing more to do other than escape the item
1343 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001344 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 if ($protect_identifiers === TRUE)
1346 {
1347 foreach ($parts as $key => $val)
1348 {
1349 if ( ! in_array($val, $this->_reserved_identifiers))
1350 {
1351 $parts[$key] = $this->_escape_identifiers($val);
1352 }
1353 }
Barry Mienydd671972010-10-04 16:33:58 +02001354
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001356 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 return $item.$alias;
1358 }
Barry Mienydd671972010-10-04 16:33:58 +02001359
Andrey Andreev569091c2012-02-09 00:16:17 +02001360 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 if ($this->dbprefix != '')
1362 {
1363 // We now add the table prefix based on some logic.
1364 // Do we have 4 segments (hostname.database.table.column)?
1365 // If so, we add the table prefix to the column name in the 3rd segment.
1366 if (isset($parts[3]))
1367 {
1368 $i = 2;
1369 }
1370 // Do we have 3 segments (database.table.column)?
1371 // If so, we add the table prefix to the column name in 2nd position
1372 elseif (isset($parts[2]))
1373 {
1374 $i = 1;
1375 }
1376 // Do we have 2 segments (table.column)?
1377 // If so, we add the table prefix to the column name in 1st segment
1378 else
1379 {
1380 $i = 0;
1381 }
Barry Mienydd671972010-10-04 16:33:58 +02001382
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 // This flag is set when the supplied $item does not contain a field name.
1384 // This can happen when this function is being called from a JOIN.
1385 if ($field_exists == FALSE)
1386 {
1387 $i++;
1388 }
Barry Mienydd671972010-10-04 16:33:58 +02001389
Derek Jones55acc8b2009-07-11 03:38:13 +00001390 // Verify table prefix and replace if necessary
1391 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1392 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001393 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001394 }
Barry Mienydd671972010-10-04 16:33:58 +02001395
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 // We only add the table prefix if it does not already exist
1397 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1398 {
1399 $parts[$i] = $this->dbprefix.$parts[$i];
1400 }
Barry Mienydd671972010-10-04 16:33:58 +02001401
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 // Put the parts back together
1403 $item = implode('.', $parts);
1404 }
Barry Mienydd671972010-10-04 16:33:58 +02001405
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 if ($protect_identifiers === TRUE)
1407 {
1408 $item = $this->_escape_identifiers($item);
1409 }
Barry Mienydd671972010-10-04 16:33:58 +02001410
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 return $item.$alias;
1412 }
1413
Andrey Andreev569091c2012-02-09 00:16:17 +02001414 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 if ($this->dbprefix != '')
1416 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001417 // Verify table prefix and replace if necessary
1418 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1419 {
Andrey Andreev569091c2012-02-09 00:16:17 +02001420 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001421 }
1422
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 // Do we prefix an item with no segments?
Andrey Andreev569091c2012-02-09 00:16:17 +02001424 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 {
1426 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001427 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 }
1429
Andrey Andreev569091c2012-02-09 00:16:17 +02001430 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 {
1432 $item = $this->_escape_identifiers($item);
1433 }
Barry Mienydd671972010-10-04 16:33:58 +02001434
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 return $item.$alias;
1436 }
1437
1438
1439}
1440
Andrey Andreev569091c2012-02-09 00:16:17 +02001441
Derek Allard2067d1a2008-11-13 22:59:24 +00001442/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001443/* Location: ./system/database/DB_driver.php */