blob: 41d17087568041c245e85eebc63803e598b00789 [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
109 * @return void
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
201 * @return resource
202 */
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.
Timothy Warren36fb8de2011-08-24 08:29:05 -0400253 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo');
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
295 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600296 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
298 }
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
303 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
304 {
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
388 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
389 {
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.
422 if ($this->cache_on == TRUE AND $this->_cache_init())
423 {
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 *
492 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200493 * @return void
494 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200495 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
497 $this->trans_enabled = FALSE;
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Enable/disable Transaction Strict Mode
504 * 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 *
509 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200510 * @return void
511 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200512 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 // --------------------------------------------------------------------
518
519 /**
520 * Start Transaction
521 *
Barry Mienydd671972010-10-04 16:33:58 +0200522 * @return void
523 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200524 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200525 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 if ( ! $this->trans_enabled)
527 {
528 return FALSE;
529 }
530
531 // When transactions are nested we only begin/commit/rollback the outermost ones
532 if ($this->_trans_depth > 0)
533 {
534 $this->_trans_depth += 1;
535 return;
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500539 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541
542 // --------------------------------------------------------------------
543
544 /**
545 * Complete Transaction
546 *
Barry Mienydd671972010-10-04 16:33:58 +0200547 * @return bool
548 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200549 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
551 if ( ! $this->trans_enabled)
552 {
553 return FALSE;
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // When transactions are nested we only begin/commit/rollback the outermost ones
557 if ($this->_trans_depth > 1)
558 {
559 $this->_trans_depth -= 1;
560 return TRUE;
561 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500562 else
563 {
564 $this->_trans_depth = 0;
565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 // The query() function will set this flag to FALSE in the event that a query failed
568 if ($this->_trans_status === FALSE)
569 {
570 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // If we are NOT running in strict mode, we will reset
573 // the _trans_status flag so that subsequent groups of transactions
574 // will be permitted.
575 if ($this->trans_strict === FALSE)
576 {
577 $this->_trans_status = TRUE;
578 }
579
580 log_message('debug', 'DB Transaction Failure');
581 return FALSE;
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 $this->trans_commit();
585 return TRUE;
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Lets you retrieve the transaction flag to determine if it has failed
592 *
Barry Mienydd671972010-10-04 16:33:58 +0200593 * @return bool
594 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200595 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 return $this->_trans_status;
598 }
599
600 // --------------------------------------------------------------------
601
602 /**
603 * Compile Bindings
604 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @param string the sql statement
606 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200607 * @return string
608 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200609 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 if (strpos($sql, $this->bind_marker) === FALSE)
612 {
613 return $sql;
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 if ( ! is_array($binds))
617 {
618 $binds = array($binds);
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // Get the sql segments around the bind markers
622 $segments = explode($this->bind_marker, $sql);
623
624 // The count of bind should be 1 less then the count of segments
625 // If there are more bind arguments trim it down
626 if (count($binds) >= count($segments)) {
627 $binds = array_slice($binds, 0, count($segments)-1);
628 }
629
630 // Construct the binded query
631 $result = $segments[0];
632 $i = 0;
633 foreach ($binds as $bind)
634 {
635 $result .= $this->escape($bind);
636 $result .= $segments[++$i];
637 }
638
639 return $result;
640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 // --------------------------------------------------------------------
643
644 /**
645 * Determines if a query is a "write" type.
646 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @param string An SQL query string
Barry Mienydd671972010-10-04 16:33:58 +0200648 * @return boolean
649 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200650 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Derek Allarde37ab382009-02-03 16:13:57 +0000652 if ( ! 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 +0000653 {
654 return FALSE;
655 }
656 return TRUE;
657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // --------------------------------------------------------------------
660
661 /**
662 * Calculate the aggregate query elapsed time
663 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200665 * @return integer
666 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200667 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 return number_format($this->benchmark, $decimals);
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 // --------------------------------------------------------------------
673
674 /**
675 * Returns the total number of queries
676 *
Barry Mienydd671972010-10-04 16:33:58 +0200677 * @return integer
678 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200679 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
681 return $this->query_count;
682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // --------------------------------------------------------------------
685
686 /**
687 * Returns the last query that was executed
688 *
Barry Mienydd671972010-10-04 16:33:58 +0200689 * @return void
690 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200691 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 return end($this->queries);
694 }
695
696 // --------------------------------------------------------------------
697
698 /**
699 * "Smart" Escape String
700 *
701 * Escapes data based on type
702 * Sets boolean and null types
703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200705 * @return mixed
706 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200707 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200708 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000709 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000711 $str = "'".$this->escape_str($str)."'";
712 }
713 elseif (is_bool($str))
714 {
715 $str = ($str === FALSE) ? 0 : 1;
716 }
717 elseif (is_null($str))
718 {
719 $str = 'NULL';
720 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000721
722 return $str;
723 }
724
725 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600726
Derek Jonese4ed5832009-02-20 21:44:59 +0000727 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000728 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000729 *
730 * Calls the individual driver for platform
731 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200732 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000733 * @param string
734 * @return mixed
735 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200736 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200737 {
738 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000739 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000740
Derek Jonese4ed5832009-02-20 21:44:59 +0000741 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 /**
744 * Primary
745 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500746 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 * position is the primary key
748 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200750 * @return string
751 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200752 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200753 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 if ( ! is_array($fields))
757 {
758 return FALSE;
759 }
760
761 return current($fields);
762 }
763
764 // --------------------------------------------------------------------
765
766 /**
767 * Returns an array of table names
768 *
Barry Mienydd671972010-10-04 16:33:58 +0200769 * @return array
770 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200771 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 {
773 // Is there a cached result?
774 if (isset($this->data_cache['table_names']))
775 {
776 return $this->data_cache['table_names'];
777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
780 {
781 if ($this->db_debug)
782 {
783 return $this->display_error('db_unsupported_function');
784 }
785 return FALSE;
786 }
787
788 $retval = array();
789 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 if ($query->num_rows() > 0)
792 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500793 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 if (isset($row['TABLE_NAME']))
796 {
797 $retval[] = $row['TABLE_NAME'];
798 }
799 else
800 {
801 $retval[] = array_shift($row);
802 }
803 }
804 }
805
806 $this->data_cache['table_names'] = $retval;
807 return $this->data_cache['table_names'];
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 // --------------------------------------------------------------------
811
812 /**
813 * Determine if a particular table exists
Andrey Andreev24abcb92012-01-05 20:40:15 +0200814 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 * @return boolean
816 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200817 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200818 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200819 return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 // --------------------------------------------------------------------
823
824 /**
825 * Fetch MySQL Field Names
826 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200828 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200830 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
832 // Is there a cached result?
833 if (isset($this->data_cache['field_names'][$table]))
834 {
835 return $this->data_cache['field_names'][$table];
836 }
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 if ($table == '')
839 {
840 if ($this->db_debug)
841 {
842 return $this->display_error('db_field_param_missing');
843 }
844 return FALSE;
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Greg Aker1edde302010-01-26 00:17:01 +0000847 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 {
849 if ($this->db_debug)
850 {
851 return $this->display_error('db_unsupported_function');
852 }
853 return FALSE;
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500859 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
861 if (isset($row['COLUMN_NAME']))
862 {
863 $retval[] = $row['COLUMN_NAME'];
864 }
865 else
866 {
867 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200868 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 $this->data_cache['field_names'][$table] = $retval;
872 return $this->data_cache['field_names'][$table];
873 }
874
875 // --------------------------------------------------------------------
876
877 /**
878 * Determine if a particular field exists
Andrey Andreev24abcb92012-01-05 20:40:15 +0200879 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 * @param string
881 * @param string
882 * @return boolean
883 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200884 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200885 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200886 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // --------------------------------------------------------------------
890
891 /**
892 * Returns an object with field data
893 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200895 * @return object
896 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200897 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 if ($table == '')
900 {
901 if ($this->db_debug)
902 {
903 return $this->display_error('db_field_param_missing');
904 }
905 return FALSE;
906 }
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200910 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000911
912 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 /**
915 * Generate an insert string
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @param string the table upon which the query will be performed
918 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200919 * @return string
920 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200921 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
923 $fields = array();
924 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200925
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500926 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
928 $fields[] = $this->_escape_identifiers($key);
929 $values[] = $this->escape($val);
930 }
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200933 }
934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 // --------------------------------------------------------------------
936
937 /**
938 * Generate an update string
939 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 * @param string the table upon which the query will be performed
941 * @param array an associative array data of key/values
942 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200943 * @return string
944 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200945 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
947 if ($where == '')
948 {
949 return false;
950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500953 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
955 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
956 }
957
958 if ( ! is_array($where))
959 {
960 $dest = array($where);
961 }
962 else
963 {
964 $dest = array();
965 foreach ($where as $key => $val)
966 {
967 $prefix = (count($dest) == 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300968 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200969
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 if ($val !== '')
971 {
972 if ( ! $this->_has_operator($key))
973 {
974 $key .= ' =';
975 }
Barry Mienydd671972010-10-04 16:33:58 +0200976
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 $val = ' '.$this->escape($val);
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 $dest[] = $prefix.$key.$val;
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000983
984 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200985 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000986
987 // --------------------------------------------------------------------
988
989 /**
990 * Tests whether the string has an SQL operator
991 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 * @param string
993 * @return bool
994 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200995 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200997 return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
999
1000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Enables a native PHP function to be run, using a platform agnostic wrapper.
1004 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 * @param string the function name
1006 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +02001007 * @return mixed
1008 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001009 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
1011 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 if (FALSE === strpos($driver, $function))
1014 {
1015 $function = $driver.$function;
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 if ( ! function_exists($function))
1019 {
1020 if ($this->db_debug)
1021 {
1022 return $this->display_error('db_unsupported_function');
1023 }
1024 return FALSE;
1025 }
1026 else
1027 {
1028 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
1029
Repox71b78092011-12-01 09:19:43 +01001030 if (is_null($args))
1031 {
1032 return call_user_func($function);
1033 }
1034 else
1035 {
1036 return call_user_func_array($function, $args);
1037 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 }
1039 }
1040
1041 // --------------------------------------------------------------------
1042
1043 /**
1044 * Set Cache Directory Path
1045 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 * @param string the path to the cache directory
1047 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001048 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001049 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
1051 $this->cachedir = $path;
1052 }
1053
1054 // --------------------------------------------------------------------
1055
1056 /**
1057 * Enable Query Caching
1058 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001060 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001061 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
Andrey Andreev24abcb92012-01-05 20:40:15 +02001063 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 }
1065
1066 // --------------------------------------------------------------------
1067
1068 /**
1069 * Disable Query Caching
1070 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001072 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001073 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
Andrey Andreev24abcb92012-01-05 20:40:15 +02001075 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 }
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078
1079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Delete the cache files associated with a particular URI
1083 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001085 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001086 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 {
1088 if ( ! $this->_cache_init())
1089 {
1090 return FALSE;
1091 }
1092 return $this->CACHE->delete($segment_one, $segment_two);
1093 }
1094
1095 // --------------------------------------------------------------------
1096
1097 /**
1098 * Delete All cache files
1099 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001101 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001102 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 {
1104 if ( ! $this->_cache_init())
1105 {
1106 return FALSE;
1107 }
1108
1109 return $this->CACHE->delete_all();
1110 }
1111
1112 // --------------------------------------------------------------------
1113
1114 /**
1115 * Initialize the Cache Class
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001118 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001119 private function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
1121 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1122 {
1123 return TRUE;
1124 }
Derek Allarde37ab382009-02-03 16:13:57 +00001125
Andrey Andreev24abcb92012-01-05 20:40:15 +02001126 if ( ! class_exists('CI_DB_Cache') AND ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
Andrey Andreev24abcb92012-01-05 20:40:15 +02001128 return $this->cache_off();
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 }
Derek Allarde37ab382009-02-03 16:13:57 +00001130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1132 return TRUE;
1133 }
1134
1135 // --------------------------------------------------------------------
1136
1137 /**
1138 * Close DB Connection
1139 *
Barry Mienydd671972010-10-04 16:33:58 +02001140 * @return void
1141 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001142 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
1144 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1145 {
1146 $this->_close($this->conn_id);
1147 }
1148 $this->conn_id = FALSE;
1149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 // --------------------------------------------------------------------
1152
1153 /**
1154 * Display an error message
1155 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 * @param string the error message
1157 * @param string any "swap" values
1158 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001159 * @return string sends the application/error_db.php template
1160 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001161 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Derek Jonese7792202010-03-02 17:24:46 -06001163 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 $LANG->load('db');
1165
1166 $heading = $LANG->line('db_error_heading');
1167
1168 if ($native == TRUE)
1169 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001170 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 }
1172 else
1173 {
1174 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1175 }
Barry Mienydd671972010-10-04 16:33:58 +02001176
Pascal Kriete60f8c392010-08-25 18:03:28 +02001177 // Find the most likely culprit of the error by going through
1178 // the backtrace until the source file is no longer in the
1179 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001180
Pascal Kriete60f8c392010-08-25 18:03:28 +02001181 $trace = debug_backtrace();
1182
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001183 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001184 {
1185 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1186 {
1187 // Found it - use a relative path for safety
1188 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1189 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001190
Pascal Kriete60f8c392010-08-25 18:03:28 +02001191 break;
1192 }
1193 }
Barry Mienydd671972010-10-04 16:33:58 +02001194
Derek Jonese7792202010-03-02 17:24:46 -06001195 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 echo $error->show_error($heading, $message, 'error_db');
1197 exit;
1198 }
1199
1200 // --------------------------------------------------------------------
1201
1202 /**
1203 * Protect Identifiers
1204 *
1205 * This function adds backticks if appropriate based on db type
1206 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 * @param mixed the item to escape
1208 * @return mixed the item with backticks
1209 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001210 public function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 {
1212 return $this->_protect_identifiers($item, $prefix_single);
1213 }
1214
1215 // --------------------------------------------------------------------
1216
1217 /**
1218 * Protect Identifiers
1219 *
1220 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001221 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001223 * the table prefix onto it. Some logic is necessary in order to deal with
1224 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 *
1226 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1227 *
1228 * Or a query with aliasing:
1229 *
1230 * SELECT m.member_id, m.member_name FROM members AS m
1231 *
1232 * Since the column name can include up to four segments (host, DB, table, column)
1233 * or also have an alias prefix, we need to do a bit of work to figure this out and
1234 * insert the table prefix (if it exists) in the proper position, and escape only
1235 * the correct identifiers.
1236 *
Andrey Andreev24abcb92012-01-05 20:40:15 +02001237 * @access public (DB Forge needs it to be public!)
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 * @param string
1239 * @param bool
1240 * @param mixed
1241 * @param bool
1242 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001243 */
Andrey Andreev24abcb92012-01-05 20:40:15 +02001244 public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
1246 if ( ! is_bool($protect_identifiers))
1247 {
1248 $protect_identifiers = $this->_protect_identifiers;
1249 }
Derek Allarde37ab382009-02-03 16:13:57 +00001250
1251 if (is_array($item))
1252 {
1253 $escaped_array = array();
1254
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001255 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001256 {
1257 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1258 }
1259
1260 return $escaped_array;
1261 }
1262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001264 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 // If the item has an alias declaration we remove it and set it aside.
1267 // Basically we remove everything to the right of the first space
1268 $alias = '';
1269 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001270 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 $alias = strstr($item, " ");
1272 $item = substr($item, 0, - strlen($alias));
1273 }
1274
Derek Allard911d3e02008-12-15 14:08:35 +00001275 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001276 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001277 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001278 // way to deal with this, but I'm not thinking of it -- Rick
1279 if (strpos($item, '(') !== FALSE)
1280 {
1281 return $item.$alias;
1282 }
1283
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 // Break the string apart if it contains periods, then insert the table prefix
1285 // in the correct location, assuming the period doesn't indicate that we're dealing
1286 // with an alias. While we're at it, we will escape the components
1287 if (strpos($item, '.') !== FALSE)
1288 {
1289 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001292 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 // we have nothing more to do other than escape the item
1294 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001295 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 if ($protect_identifiers === TRUE)
1297 {
1298 foreach ($parts as $key => $val)
1299 {
1300 if ( ! in_array($val, $this->_reserved_identifiers))
1301 {
1302 $parts[$key] = $this->_escape_identifiers($val);
1303 }
1304 }
Barry Mienydd671972010-10-04 16:33:58 +02001305
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001307 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 return $item.$alias;
1309 }
Barry Mienydd671972010-10-04 16:33:58 +02001310
Derek Jones37f4b9c2011-07-01 17:56:50 -05001311 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 if ($this->dbprefix != '')
1313 {
1314 // We now add the table prefix based on some logic.
1315 // Do we have 4 segments (hostname.database.table.column)?
1316 // If so, we add the table prefix to the column name in the 3rd segment.
1317 if (isset($parts[3]))
1318 {
1319 $i = 2;
1320 }
1321 // Do we have 3 segments (database.table.column)?
1322 // If so, we add the table prefix to the column name in 2nd position
1323 elseif (isset($parts[2]))
1324 {
1325 $i = 1;
1326 }
1327 // Do we have 2 segments (table.column)?
1328 // If so, we add the table prefix to the column name in 1st segment
1329 else
1330 {
1331 $i = 0;
1332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 // This flag is set when the supplied $item does not contain a field name.
1335 // This can happen when this function is being called from a JOIN.
1336 if ($field_exists == FALSE)
1337 {
1338 $i++;
1339 }
Barry Mienydd671972010-10-04 16:33:58 +02001340
Derek Jones55acc8b2009-07-11 03:38:13 +00001341 // Verify table prefix and replace if necessary
1342 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1343 {
1344 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1345 }
Barry Mienydd671972010-10-04 16:33:58 +02001346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 // We only add the table prefix if it does not already exist
1348 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1349 {
1350 $parts[$i] = $this->dbprefix.$parts[$i];
1351 }
Barry Mienydd671972010-10-04 16:33:58 +02001352
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 // Put the parts back together
1354 $item = implode('.', $parts);
1355 }
Barry Mienydd671972010-10-04 16:33:58 +02001356
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 if ($protect_identifiers === TRUE)
1358 {
1359 $item = $this->_escape_identifiers($item);
1360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 return $item.$alias;
1363 }
1364
Derek Jones37f4b9c2011-07-01 17:56:50 -05001365 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 if ($this->dbprefix != '')
1367 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001368 // Verify table prefix and replace if necessary
1369 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1370 {
1371 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1372 }
1373
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 // Do we prefix an item with no segments?
1375 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1376 {
1377 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001378 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 }
1380
1381 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1382 {
1383 $item = $this->_escape_identifiers($item);
1384 }
Barry Mienydd671972010-10-04 16:33:58 +02001385
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 return $item.$alias;
1387 }
1388
1389
1390}
1391
Derek Allard2067d1a2008-11-13 22:59:24 +00001392/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001393/* Location: ./system/database/DB_driver.php */