blob: a30c8226d16b171292e88b57f9505cb39caabb1d [file] [log] [blame]
Andrey Andreev24abcb92012-01-05 20:40:15 +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 Andreev24abcb92012-01-05 20:40:15 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24abcb92012-01-05 20:40:15 +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 Andreev24abcb92012-01-05 20:40:15 +020045 public $username;
46 public $password;
47 public $hostname;
48 public $database;
49 public $dbdriver = 'mysql';
50 public $dbprefix = '';
51 public $char_set = 'utf8';
52 public $dbcollat = 'utf8_general_ci';
53 public $autoinit = TRUE; // Whether to automatically initialize the DB
54 public $swap_pre = '';
55 public $port = '';
56 public $pconnect = FALSE;
57 public $conn_id = FALSE;
58 public $result_id = FALSE;
59 public $db_debug = FALSE;
60 public $benchmark = 0;
61 public $query_count = 0;
62 public $bind_marker = '?';
63 public $save_queries = TRUE;
64 public $queries = array();
65 public $query_times = array();
66 public $data_cache = array();
67 public $trans_enabled = TRUE;
68 public $trans_strict = TRUE;
69 protected $_trans_depth = 0;
70 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
71 public $cache_on = FALSE;
72 public $cachedir = '';
73 public $cache_autodel = FALSE;
74 public $CACHE; // The cache class object
Derek Allard2067d1a2008-11-13 22:59:24 +000075
Andrey Andreev24abcb92012-01-05 20:40:15 +020076 protected $_protect_identifiers = TRUE;
77 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000078
79 // These are use with Oracle
Andrey Andreev24abcb92012-01-05 20:40:15 +020080 public $stmt_id;
81 public $curs_id;
82 public $limit_used;
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050085 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * connection settings.
87 *
88 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020089 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020090 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
92 if (is_array($params))
93 {
94 foreach ($params as $key => $val)
95 {
96 $this->$key = $val;
97 }
98 }
99
100 log_message('debug', 'Database Driver Class Initialized');
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // --------------------------------------------------------------------
104
105 /**
106 * Initialize Database Settings
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @param mixed
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200109 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200110 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200111 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
113 // If an existing connection resource is available
114 // there is no need to connect and select the database
115 if (is_resource($this->conn_id) OR is_object($this->conn_id))
116 {
117 return TRUE;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // Connect to the database and set the connection ID
123 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
124
Felix Balfoort5d581b62011-11-29 15:53:01 +0100125 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 if ( ! $this->conn_id)
127 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100128 // Check if there is a failover set
129 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100131 // Go over all the failovers
132 foreach ($this->failover as $failover)
133 {
134 // Replace the current settings with those of the failover
135 foreach ($failover as $key => $val)
136 {
137 $this->$key = $val;
138 }
139
140 // Try to connect
141 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
142
143 // If a connection is made break the foreach loop
144 if ($this->conn_id)
145 {
146 break;
147 }
148 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100150
151 // We still don't have a connection?
152 if ( ! $this->conn_id)
153 {
154 log_message('error', 'Unable to connect to the database');
155
156 if ($this->db_debug)
157 {
158 $this->display_error('db_unable_to_connect');
159 }
160 return FALSE;
161 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 // ----------------------------------------------------------------
165
166 // Select the DB... assuming a database name is specified in the config file
167 if ($this->database != '')
168 {
169 if ( ! $this->db_select())
170 {
171 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 if ($this->db_debug)
174 {
175 $this->display_error('db_unable_to_select', $this->database);
176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179 else
180 {
181 // We've selected the DB. Now we set the character set
182 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
183 {
184 return FALSE;
185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 return TRUE;
188 }
189 }
190
191 return TRUE;
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // --------------------------------------------------------------------
195
196 /**
197 * Set client character set
198 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 * @param string
200 * @param string
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200201 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200203 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
206 {
207 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 if ($this->db_debug)
210 {
211 $this->display_error('db_unable_to_set_charset', $this->char_set);
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 return FALSE;
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 return TRUE;
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // --------------------------------------------------------------------
221
222 /**
223 * The name of the platform in use (mysql, mssql, etc...)
224 *
Barry Mienydd671972010-10-04 16:33:58 +0200225 * @return string
226 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200227 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return $this->dbdriver;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500235 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * version of the database being used
237 *
Barry Mienydd671972010-10-04 16:33:58 +0200238 * @return string
239 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200240 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 if (FALSE === ($sql = $this->_version()))
243 {
244 if ($this->db_debug)
245 {
246 return $this->display_error('db_unsupported_function');
247 }
248 return FALSE;
249 }
Derek Allard3683f772009-12-16 17:32:33 +0000250
251 // Some DBs have functions that return the version, and don't run special
252 // SQL queries per se. In these instances, just return the result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200253 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli');
Derek Allard3683f772009-12-16 17:32:33 +0000254
255 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 return $sql;
258 }
Derek Allard3683f772009-12-16 17:32:33 +0000259 else
260 {
261 $query = $this->query($sql);
262 return $query->row('ver');
263 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
267
268 /**
269 * Execute the query
270 *
271 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500272 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * upon successful execution of a "write" type query. Returns boolean
274 * FALSE upon failure, and if the $db_debug variable is set to TRUE
275 * will raise an error.
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param string An SQL query string
278 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200279 * @return mixed
280 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200281 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 if ($sql == '')
284 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200285 log_message('error', 'Invalid query: '.$sql);
286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if ($this->db_debug)
288 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 return $this->display_error('db_invalid_query');
290 }
291 return FALSE;
292 }
293
294 // Verify table prefix and replace if necessary
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200295 if (($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre))
Derek Jonese7792202010-03-02 17:24:46 -0600296 {
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200297 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Derek Jonese7792202010-03-02 17:24:46 -0600299
Derek Jones37f4b9c2011-07-01 17:56:50 -0500300 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // we will load the caching class and return the previously
302 // cached query if it exists
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200303 if ($this->cache_on == TRUE && stristr($sql, 'SELECT'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 if ($this->_cache_init())
306 {
307 $this->load_rdriver();
308 if (FALSE !== ($cache = $this->CACHE->read($sql)))
309 {
310 return $cache;
311 }
312 }
313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // Compile binds if needed
316 if ($binds !== FALSE)
317 {
318 $sql = $this->compile_binds($sql, $binds);
319 }
320
Derek Jones37f4b9c2011-07-01 17:56:50 -0500321 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 if ($this->save_queries == TRUE)
323 {
324 $this->queries[] = $sql;
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // Start the Query Timer
328 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // Run the Query
331 if (FALSE === ($this->result_id = $this->simple_query($sql)))
332 {
333 if ($this->save_queries == TRUE)
334 {
335 $this->query_times[] = 0;
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // This will trigger a rollback if transactions are being used
339 $this->_trans_status = FALSE;
340
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200341 // Grab the error number and message now, as we might run some
342 // additional queries before displaying the error
343 $error_no = $this->_error_number();
344 $error_msg = $this->_error_message();
345
346 // Log errors
347 log_message('error', 'Query error: '.$error_msg);
348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 if ($this->db_debug)
350 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500352 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200353 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // transactions to remain in limbo.
355 $this->trans_complete();
356
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200357 // Display errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 return $this->display_error(
359 array(
360 'Error Number: '.$error_no,
361 $error_msg,
362 $sql
363 )
364 );
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 return FALSE;
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // Stop and aggregate the query time results
371 $time_end = list($em, $es) = explode(' ', microtime());
372 $this->benchmark += ($em + $es) - ($sm + $ss);
373
374 if ($this->save_queries == TRUE)
375 {
376 $this->query_times[] = ($em + $es) - ($sm + $ss);
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // Increment the query counter
380 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // Was the query a "write" type?
383 // If so we'll simply return true
384 if ($this->is_write_type($sql) === TRUE)
385 {
386 // If caching is enabled we'll auto-cleanup any
387 // existing files related to this particular URI
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200388 if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $this->CACHE->delete();
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 return TRUE;
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // Return TRUE if we don't need to create a result object
397 // Currently only the Oracle driver uses this when stored
398 // procedures are used
399 if ($return_object !== TRUE)
400 {
401 return TRUE;
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
404 // Load and instantiate the result driver
Barry Mienydd671972010-10-04 16:33:58 +0200405 $driver = $this->load_rdriver();
406 $RES = new $driver();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200407 $RES->conn_id = $this->conn_id;
408 $RES->result_id = $this->result_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409
410 if ($this->dbdriver == 'oci8')
411 {
412 $RES->stmt_id = $this->stmt_id;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200413 $RES->curs_id = $this->curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 $RES->limit_used = $this->limit_used;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200415 // Passing the next one by reference to make sure it's updated, if needed:
416 $RES->commit_mode = &$this->_commit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 $this->stmt_id = FALSE;
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Jones37f4b9c2011-07-01 17:56:50 -0500420 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // result object and save it to a cache file.
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200422 if ($this->cache_on == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 // We'll create a new instance of the result object
425 // only without the platform specific driver since
426 // we can't use it with cached data (the query result
427 // resource ID won't be any good once we've cached the
428 // result object, so we'll have to compile the data
429 // and save it)
430 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 $CR->result_object = $RES->result_object();
432 $CR->result_array = $RES->result_array();
Andrey Andreev24abcb92012-01-05 20:40:15 +0200433 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // Reset these since cached objects can not utilize resource IDs.
436 $CR->conn_id = NULL;
437 $CR->result_id = NULL;
438
439 $this->CACHE->write($sql, $CR);
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 return $RES;
443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * Load the result drivers
449 *
Barry Mienydd671972010-10-04 16:33:58 +0200450 * @return string the name of the result class
451 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200452 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 $driver = 'CI_DB_'.$this->dbdriver.'_result';
455
456 if ( ! class_exists($driver))
457 {
Greg Aker3a746652011-04-19 10:59:47 -0500458 include_once(BASEPATH.'database/DB_result.php');
459 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 return $driver;
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // --------------------------------------------------------------------
466
467 /**
468 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500469 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * we only use it when running transaction commands since they do
471 * not require all the features of the main query() function.
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200474 * @return mixed
475 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200476 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
478 if ( ! $this->conn_id)
479 {
480 $this->initialize();
481 }
482
483 return $this->_execute($sql);
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 // --------------------------------------------------------------------
487
488 /**
489 * Disable Transactions
490 * This permits transactions to be disabled at run-time.
491 *
Barry Mienydd671972010-10-04 16:33:58 +0200492 * @return void
493 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200494 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 $this->trans_enabled = FALSE;
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
502 * Enable/disable Transaction Strict Mode
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * When strict mode is enabled, if you are running multiple groups of
505 * transactions, if one group fails all groups will be rolled back.
506 * If strict mode is disabled, each group is treated autonomously, meaning
507 * a failure of one group will not affect any others
508 *
Barry Mienydd671972010-10-04 16:33:58 +0200509 * @return void
510 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200511 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
519 * Start Transaction
520 *
Barry Mienydd671972010-10-04 16:33:58 +0200521 * @return void
522 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200523 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200524 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 if ( ! $this->trans_enabled)
526 {
527 return FALSE;
528 }
529
530 // When transactions are nested we only begin/commit/rollback the outermost ones
531 if ($this->_trans_depth > 0)
532 {
533 $this->_trans_depth += 1;
534 return;
535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500538 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540
541 // --------------------------------------------------------------------
542
543 /**
544 * Complete Transaction
545 *
Barry Mienydd671972010-10-04 16:33:58 +0200546 * @return bool
547 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200548 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
550 if ( ! $this->trans_enabled)
551 {
552 return FALSE;
553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // When transactions are nested we only begin/commit/rollback the outermost ones
556 if ($this->_trans_depth > 1)
557 {
558 $this->_trans_depth -= 1;
559 return TRUE;
560 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500561 else
562 {
563 $this->_trans_depth = 0;
564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 // The query() function will set this flag to FALSE in the event that a query failed
567 if ($this->_trans_status === FALSE)
568 {
569 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // If we are NOT running in strict mode, we will reset
572 // the _trans_status flag so that subsequent groups of transactions
573 // will be permitted.
574 if ($this->trans_strict === FALSE)
575 {
576 $this->_trans_status = TRUE;
577 }
578
579 log_message('debug', 'DB Transaction Failure');
580 return FALSE;
581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 $this->trans_commit();
584 return TRUE;
585 }
586
587 // --------------------------------------------------------------------
588
589 /**
590 * Lets you retrieve the transaction flag to determine if it has failed
591 *
Barry Mienydd671972010-10-04 16:33:58 +0200592 * @return bool
593 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200594 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 return $this->_trans_status;
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Compile Bindings
603 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @param string the sql statement
605 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200606 * @return string
607 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200608 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 if (strpos($sql, $this->bind_marker) === FALSE)
611 {
612 return $sql;
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 if ( ! is_array($binds))
616 {
617 $binds = array($binds);
618 }
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 // Get the sql segments around the bind markers
621 $segments = explode($this->bind_marker, $sql);
622
623 // The count of bind should be 1 less then the count of segments
624 // If there are more bind arguments trim it down
625 if (count($binds) >= count($segments)) {
626 $binds = array_slice($binds, 0, count($segments)-1);
627 }
628
629 // Construct the binded query
630 $result = $segments[0];
631 $i = 0;
632 foreach ($binds as $bind)
633 {
634 $result .= $this->escape($bind);
635 $result .= $segments[++$i];
636 }
637
638 return $result;
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // --------------------------------------------------------------------
642
643 /**
644 * Determines if a query is a "write" type.
645 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 * @param string An SQL query string
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200647 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200648 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200649 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200651 return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 }
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 // --------------------------------------------------------------------
655
656 /**
657 * Calculate the aggregate query elapsed time
658 *
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200659 * @param int The number of decimal places
660 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200661 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200662 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 return number_format($this->benchmark, $decimals);
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Returns the total number of queries
671 *
Barry Mienydd671972010-10-04 16:33:58 +0200672 * @return integer
673 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200674 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
676 return $this->query_count;
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // --------------------------------------------------------------------
680
681 /**
682 * Returns the last query that was executed
683 *
Barry Mienydd671972010-10-04 16:33:58 +0200684 * @return void
685 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200686 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 return end($this->queries);
689 }
690
691 // --------------------------------------------------------------------
692
693 /**
694 * "Smart" Escape String
695 *
696 * Escapes data based on type
697 * Sets boolean and null types
698 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200700 * @return mixed
701 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200702 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200703 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000704 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000706 $str = "'".$this->escape_str($str)."'";
707 }
708 elseif (is_bool($str))
709 {
710 $str = ($str === FALSE) ? 0 : 1;
711 }
712 elseif (is_null($str))
713 {
714 $str = 'NULL';
715 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000716
717 return $str;
718 }
719
720 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600721
Derek Jonese4ed5832009-02-20 21:44:59 +0000722 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000723 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000724 *
725 * Calls the individual driver for platform
726 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200727 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000728 * @param string
729 * @return mixed
730 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200731 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200732 {
733 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000734 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000735
Derek Jonese4ed5832009-02-20 21:44:59 +0000736 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 /**
739 * Primary
740 *
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200741 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * position is the primary key
743 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200745 * @return string
746 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200747 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200748 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 if ( ! is_array($fields))
752 {
753 return FALSE;
754 }
755
756 return current($fields);
757 }
758
759 // --------------------------------------------------------------------
760
761 /**
762 * Returns an array of table names
763 *
Barry Mienydd671972010-10-04 16:33:58 +0200764 * @return array
765 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200766 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
768 // Is there a cached result?
769 if (isset($this->data_cache['table_names']))
770 {
771 return $this->data_cache['table_names'];
772 }
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
775 {
776 if ($this->db_debug)
777 {
778 return $this->display_error('db_unsupported_function');
779 }
780 return FALSE;
781 }
782
783 $retval = array();
784 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 if ($query->num_rows() > 0)
787 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500788 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
790 if (isset($row['TABLE_NAME']))
791 {
792 $retval[] = $row['TABLE_NAME'];
793 }
794 else
795 {
796 $retval[] = array_shift($row);
797 }
798 }
799 }
800
801 $this->data_cache['table_names'] = $retval;
802 return $this->data_cache['table_names'];
803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 // --------------------------------------------------------------------
806
807 /**
808 * Determine if a particular table exists
Andrey Andreev24abcb92012-01-05 20:40:15 +0200809 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 * @return boolean
811 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200812 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200813 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200814 return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 // --------------------------------------------------------------------
818
819 /**
Andrey Andreev7b62bff2012-01-05 20:43:57 +0200820 * Fetch Field Names
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200823 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200825 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 // Is there a cached result?
828 if (isset($this->data_cache['field_names'][$table]))
829 {
830 return $this->data_cache['field_names'][$table];
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 if ($table == '')
834 {
835 if ($this->db_debug)
836 {
837 return $this->display_error('db_field_param_missing');
838 }
839 return FALSE;
840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Greg Aker1edde302010-01-26 00:17:01 +0000842 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 {
844 if ($this->db_debug)
845 {
846 return $this->display_error('db_unsupported_function');
847 }
848 return FALSE;
849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500854 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 {
856 if (isset($row['COLUMN_NAME']))
857 {
858 $retval[] = $row['COLUMN_NAME'];
859 }
860 else
861 {
862 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200863 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 $this->data_cache['field_names'][$table] = $retval;
867 return $this->data_cache['field_names'][$table];
868 }
869
870 // --------------------------------------------------------------------
871
872 /**
873 * Determine if a particular field exists
Andrey Andreev24abcb92012-01-05 20:40:15 +0200874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @param string
876 * @param string
877 * @return boolean
878 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200879 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200880 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200881 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 // --------------------------------------------------------------------
885
886 /**
887 * Returns an object with field data
888 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200890 * @return object
891 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200892 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
894 if ($table == '')
895 {
896 if ($this->db_debug)
897 {
898 return $this->display_error('db_field_param_missing');
899 }
900 return FALSE;
901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200905 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000906
907 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 /**
910 * Generate an insert string
911 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @param string the table upon which the query will be performed
913 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200914 * @return string
915 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200916 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
918 $fields = array();
919 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200920
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500921 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
923 $fields[] = $this->_escape_identifiers($key);
924 $values[] = $this->escape($val);
925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200928 }
929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 // --------------------------------------------------------------------
931
932 /**
933 * Generate an update string
934 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @param string the table upon which the query will be performed
936 * @param array an associative array data of key/values
937 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200938 * @return string
939 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200940 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
942 if ($where == '')
943 {
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200944 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500948 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
951 }
952
953 if ( ! is_array($where))
954 {
955 $dest = array($where);
956 }
957 else
958 {
959 $dest = array();
960 foreach ($where as $key => $val)
961 {
962 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300963 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 if ($val !== '')
966 {
967 if ( ! $this->_has_operator($key))
968 {
969 $key .= ' =';
970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 $val = ' '.$this->escape($val);
973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 $dest[] = $prefix.$key.$val;
976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000978
979 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200980 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000981
982 // --------------------------------------------------------------------
983
984 /**
985 * Tests whether the string has an SQL operator
986 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @param string
988 * @return bool
989 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200990 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200992 return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
994
995 // --------------------------------------------------------------------
996
997 /**
998 * Enables a native PHP function to be run, using a platform agnostic wrapper.
999 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 * @param string the function name
1001 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001002 * @return mixed
1003 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001004 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
1006 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 if (FALSE === strpos($driver, $function))
1009 {
1010 $function = $driver.$function;
1011 }
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 if ( ! function_exists($function))
1014 {
1015 if ($this->db_debug)
1016 {
1017 return $this->display_error('db_unsupported_function');
1018 }
1019 return FALSE;
1020 }
1021 else
1022 {
1023 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1024
Repox71b78092011-12-01 09:19:43 +01001025 if (is_null($args))
1026 {
1027 return call_user_func($function);
1028 }
1029 else
1030 {
1031 return call_user_func_array($function, $args);
1032 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 }
1034 }
1035
1036 // --------------------------------------------------------------------
1037
1038 /**
1039 * Set Cache Directory Path
1040 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * @param string the path to the cache directory
1042 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001043 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001044 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
1046 $this->cachedir = $path;
1047 }
1048
1049 // --------------------------------------------------------------------
1050
1051 /**
1052 * Enable Query Caching
1053 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001055 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001056 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
Andrey Andreev24abcb92012-01-05 20:40:15 +02001058 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 }
1060
1061 // --------------------------------------------------------------------
1062
1063 /**
1064 * Disable Query Caching
1065 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001067 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001068 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
Andrey Andreev24abcb92012-01-05 20:40:15 +02001070 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073
1074 // --------------------------------------------------------------------
1075
1076 /**
1077 * Delete the cache files associated with a particular URI
1078 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001080 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001081 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
1083 if ( ! $this->_cache_init())
1084 {
1085 return FALSE;
1086 }
1087 return $this->CACHE->delete($segment_one, $segment_two);
1088 }
1089
1090 // --------------------------------------------------------------------
1091
1092 /**
1093 * Delete All cache files
1094 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001096 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001097 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
1099 if ( ! $this->_cache_init())
1100 {
1101 return FALSE;
1102 }
1103
1104 return $this->CACHE->delete_all();
1105 }
1106
1107 // --------------------------------------------------------------------
1108
1109 /**
1110 * Initialize the Cache Class
1111 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001113 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001114 private function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001116 if (is_object($this->CACHE) && class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
1118 return TRUE;
1119 }
Derek Allarde37ab382009-02-03 16:13:57 +00001120
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001121 if ( ! class_exists('CI_DB_Cache') && ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
Andrey Andreev24abcb92012-01-05 20:40:15 +02001123 return $this->cache_off();
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 }
Derek Allarde37ab382009-02-03 16:13:57 +00001125
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1127 return TRUE;
1128 }
1129
1130 // --------------------------------------------------------------------
1131
1132 /**
1133 * Close DB Connection
1134 *
Barry Mienydd671972010-10-04 16:33:58 +02001135 * @return void
1136 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001137 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
1139 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1140 {
1141 $this->_close($this->conn_id);
1142 }
1143 $this->conn_id = FALSE;
1144 }
Barry Mienydd671972010-10-04 16:33:58 +02001145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 // --------------------------------------------------------------------
1147
1148 /**
1149 * Display an error message
1150 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 * @param string the error message
1152 * @param string any "swap" values
1153 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001154 * @return string sends the application/error_db.php template
1155 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001156 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Derek Jonese7792202010-03-02 17:24:46 -06001158 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 $LANG->load('db');
1160
1161 $heading = $LANG->line('db_error_heading');
1162
1163 if ($native == TRUE)
1164 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001165 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 }
1167 else
1168 {
1169 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171
Pascal Kriete60f8c392010-08-25 18:03:28 +02001172 // Find the most likely culprit of the error by going through
1173 // the backtrace until the source file is no longer in the
1174 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001175
Pascal Kriete60f8c392010-08-25 18:03:28 +02001176 $trace = debug_backtrace();
1177
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001178 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001179 {
1180 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1181 {
1182 // Found it - use a relative path for safety
1183 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1184 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001185
Pascal Kriete60f8c392010-08-25 18:03:28 +02001186 break;
1187 }
1188 }
Barry Mienydd671972010-10-04 16:33:58 +02001189
Derek Jonese7792202010-03-02 17:24:46 -06001190 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 echo $error->show_error($heading, $message, 'error_db');
1192 exit;
1193 }
1194
1195 // --------------------------------------------------------------------
1196
1197 /**
1198 * Protect Identifiers
1199 *
1200 * This function adds backticks if appropriate based on db type
1201 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 * @param mixed the item to escape
1203 * @return mixed the item with backticks
1204 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001205 public function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
1207 return $this->_protect_identifiers($item, $prefix_single);
1208 }
1209
1210 // --------------------------------------------------------------------
1211
1212 /**
1213 * Protect Identifiers
1214 *
1215 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001216 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001218 * the table prefix onto it. Some logic is necessary in order to deal with
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001219 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 *
1221 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1222 *
1223 * Or a query with aliasing:
1224 *
1225 * SELECT m.member_id, m.member_name FROM members AS m
1226 *
1227 * Since the column name can include up to four segments (host, DB, table, column)
1228 * or also have an alias prefix, we need to do a bit of work to figure this out and
1229 * insert the table prefix (if it exists) in the proper position, and escape only
1230 * the correct identifiers.
1231 *
Andrey Andreev24abcb92012-01-05 20:40:15 +02001232 * @access public (DB Forge needs it to be public!)
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 * @param string
1234 * @param bool
1235 * @param mixed
1236 * @param bool
1237 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001238 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001239 public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 {
1241 if ( ! is_bool($protect_identifiers))
1242 {
1243 $protect_identifiers = $this->_protect_identifiers;
1244 }
Derek Allarde37ab382009-02-03 16:13:57 +00001245
1246 if (is_array($item))
1247 {
1248 $escaped_array = array();
1249
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001250 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001251 {
1252 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1253 }
1254
1255 return $escaped_array;
1256 }
1257
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001259 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // If the item has an alias declaration we remove it and set it aside.
1262 // Basically we remove everything to the right of the first space
1263 $alias = '';
1264 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001265 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 $alias = strstr($item, " ");
1267 $item = substr($item, 0, - strlen($alias));
1268 }
1269
Derek Allard911d3e02008-12-15 14:08:35 +00001270 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001271 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001272 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001273 // way to deal with this, but I'm not thinking of it -- Rick
1274 if (strpos($item, '(') !== FALSE)
1275 {
1276 return $item.$alias;
1277 }
1278
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 // Break the string apart if it contains periods, then insert the table prefix
1280 // in the correct location, assuming the period doesn't indicate that we're dealing
1281 // with an alias. While we're at it, we will escape the components
1282 if (strpos($item, '.') !== FALSE)
1283 {
1284 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 // Does the first segment of the exploded item match
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001287 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 // we have nothing more to do other than escape the item
1289 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001290 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 if ($protect_identifiers === TRUE)
1292 {
1293 foreach ($parts as $key => $val)
1294 {
1295 if ( ! in_array($val, $this->_reserved_identifiers))
1296 {
1297 $parts[$key] = $this->_escape_identifiers($val);
1298 }
1299 }
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001302 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 return $item.$alias;
1304 }
Barry Mienydd671972010-10-04 16:33:58 +02001305
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001306 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 if ($this->dbprefix != '')
1308 {
1309 // We now add the table prefix based on some logic.
1310 // Do we have 4 segments (hostname.database.table.column)?
1311 // If so, we add the table prefix to the column name in the 3rd segment.
1312 if (isset($parts[3]))
1313 {
1314 $i = 2;
1315 }
1316 // Do we have 3 segments (database.table.column)?
1317 // If so, we add the table prefix to the column name in 2nd position
1318 elseif (isset($parts[2]))
1319 {
1320 $i = 1;
1321 }
1322 // Do we have 2 segments (table.column)?
1323 // If so, we add the table prefix to the column name in 1st segment
1324 else
1325 {
1326 $i = 0;
1327 }
Barry Mienydd671972010-10-04 16:33:58 +02001328
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 // This flag is set when the supplied $item does not contain a field name.
1330 // This can happen when this function is being called from a JOIN.
1331 if ($field_exists == FALSE)
1332 {
1333 $i++;
1334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Derek Jones55acc8b2009-07-11 03:38:13 +00001336 // Verify table prefix and replace if necessary
1337 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1338 {
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001339 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001340 }
Barry Mienydd671972010-10-04 16:33:58 +02001341
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 // We only add the table prefix if it does not already exist
1343 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1344 {
1345 $parts[$i] = $this->dbprefix.$parts[$i];
1346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 // Put the parts back together
1349 $item = implode('.', $parts);
1350 }
Barry Mienydd671972010-10-04 16:33:58 +02001351
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 if ($protect_identifiers === TRUE)
1353 {
1354 $item = $this->_escape_identifiers($item);
1355 }
Barry Mienydd671972010-10-04 16:33:58 +02001356
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 return $item.$alias;
1358 }
1359
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001360 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 if ($this->dbprefix != '')
1362 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001363 // Verify table prefix and replace if necessary
1364 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1365 {
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001366 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001367 }
1368
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 // Do we prefix an item with no segments?
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001370 if ($prefix_single == TRUE && substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
1372 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001373 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 }
1375
Andrey Andreev1ab62ae2012-01-20 13:04:10 +02001376 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 {
1378 $item = $this->_escape_identifiers($item);
1379 }
Barry Mienydd671972010-10-04 16:33:58 +02001380
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 return $item.$alias;
1382 }
1383
1384
1385}
1386
Derek Allard2067d1a2008-11-13 22:59:24 +00001387/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001388/* Location: ./system/database/DB_driver.php */