blob: d9d83d55def03a8def4ca53a863cb37c73b0cd21 [file] [log] [blame]
Andrey Andreevaf5d5582012-01-25 21:34:47 +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 Andreevaf5d5582012-01-25 21:34:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevaf5d5582012-01-25 21:34:47 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Driver Class
30 *
31 * This is the platform-independent base DB implementation class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_driver {
42
Andrey Andreevaf5d5582012-01-25 21:34:47 +020043 public $username;
44 public $password;
45 public $hostname;
46 public $database;
47 public $dbdriver = 'mysql';
48 public $dbprefix = '';
49 public $char_set = 'utf8';
50 public $dbcollat = 'utf8_general_ci';
51 public $autoinit = TRUE; // Whether to automatically initialize the DB
52 public $swap_pre = '';
53 public $port = '';
54 public $pconnect = FALSE;
55 public $conn_id = FALSE;
56 public $result_id = FALSE;
57 public $db_debug = FALSE;
58 public $benchmark = 0;
59 public $query_count = 0;
60 public $bind_marker = '?';
61 public $save_queries = TRUE;
62 public $queries = array();
63 public $query_times = array();
64 public $data_cache = array();
65 public $trans_enabled = TRUE;
66 public $trans_strict = TRUE;
67 protected $_trans_depth = 0;
68 protected $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
69 public $cache_on = FALSE;
70 public $cachedir = '';
71 public $cache_autodel = FALSE;
72 public $CACHE; // The cache class object
Derek Allard2067d1a2008-11-13 22:59:24 +000073
Andrey Andreevaf5d5582012-01-25 21:34:47 +020074 protected $_protect_identifiers = TRUE;
75 protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
Derek Allard2067d1a2008-11-13 22:59:24 +000076
77 // These are use with Oracle
Andrey Andreevaf5d5582012-01-25 21:34:47 +020078 public $stmt_id;
79 public $curs_id;
80 public $limit_used;
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 /**
Andrey Andreevaf5d5582012-01-25 21:34:47 +020083 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * connection settings.
85 *
86 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020087 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +020088 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
90 if (is_array($params))
91 {
92 foreach ($params as $key => $val)
93 {
94 $this->$key = $val;
95 }
96 }
97
98 log_message('debug', 'Database Driver Class Initialized');
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 // --------------------------------------------------------------------
102
103 /**
104 * Initialize Database Settings
105 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200106 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200107 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200108 public function initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200110 /* If an established connection is available, then there's
111 * no need to connect and select the database.
112 *
113 * Depending on the database driver, conn_id can be either
114 * boolean TRUE, a resource or an object.
115 */
116 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
118 return TRUE;
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // Connect to the database and set the connection ID
124 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
125
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200126 // No connection resource? Check if there is a failover else throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if ( ! $this->conn_id)
128 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100129 // Check if there is a failover set
130 if ( ! empty($this->failover) && is_array($this->failover))
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Felix Balfoort5d581b62011-11-29 15:53:01 +0100132 // Go over all the failovers
133 foreach ($this->failover as $failover)
134 {
135 // Replace the current settings with those of the failover
136 foreach ($failover as $key => $val)
137 {
138 $this->$key = $val;
139 }
140
141 // Try to connect
142 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
143
144 // If a connection is made break the foreach loop
145 if ($this->conn_id)
146 {
147 break;
148 }
149 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Felix Balfoort5d581b62011-11-29 15:53:01 +0100151
152 // We still don't have a connection?
153 if ( ! $this->conn_id)
154 {
155 log_message('error', 'Unable to connect to the database');
156
157 if ($this->db_debug)
158 {
159 $this->display_error('db_unable_to_connect');
160 }
161 return FALSE;
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 // ----------------------------------------------------------------
166
167 // Select the DB... assuming a database name is specified in the config file
168 if ($this->database != '')
169 {
170 if ( ! $this->db_select())
171 {
172 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 if ($this->db_debug)
175 {
176 $this->display_error('db_unable_to_select', $this->database);
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180 else
181 {
182 // We've selected the DB. Now we set the character set
183 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
184 {
185 return FALSE;
186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 return TRUE;
189 }
190 }
191
192 return TRUE;
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // --------------------------------------------------------------------
196
197 /**
198 * Set client character set
199 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * @param string
201 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200202 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200204 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
207 {
208 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ($this->db_debug)
211 {
212 $this->display_error('db_unable_to_set_charset', $this->char_set);
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 return FALSE;
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 return TRUE;
219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // --------------------------------------------------------------------
222
223 /**
224 * The name of the platform in use (mysql, mssql, etc...)
225 *
Barry Mienydd671972010-10-04 16:33:58 +0200226 * @return string
227 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200228 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 return $this->dbdriver;
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200236 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * version of the database being used
238 *
Barry Mienydd671972010-10-04 16:33:58 +0200239 * @return string
240 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200241 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 if (FALSE === ($sql = $this->_version()))
244 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200245 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Derek Allard3683f772009-12-16 17:32:33 +0000247
248 // Some DBs have functions that return the version, and don't run special
249 // SQL queries per se. In these instances, just return the result.
Timothy Warren36fb8de2011-08-24 08:29:05 -0400250 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo');
Derek Allard3683f772009-12-16 17:32:33 +0000251
252 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 return $sql;
255 }
Derek Allard3683f772009-12-16 17:32:33 +0000256 else
257 {
258 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200259 $query = $query->row();
260 return $query->ver;
Derek Allard3683f772009-12-16 17:32:33 +0000261 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // --------------------------------------------------------------------
265
266 /**
267 * Execute the query
268 *
269 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500270 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * upon successful execution of a "write" type query. Returns boolean
272 * FALSE upon failure, and if the $db_debug variable is set to TRUE
273 * will raise an error.
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string An SQL query string
276 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200277 * @return mixed
278 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200279 public function query($sql, $binds = FALSE, $return_object = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 if ($sql == '')
282 {
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200283 log_message('error', 'Invalid query: '.$sql);
284
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200285 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200289 if ( ($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600290 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200291 $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
Derek Jonese7792202010-03-02 17:24:46 -0600293
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200294 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // we will load the caching class and return the previously
296 // cached query if it exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200297 if ($this->cache_on == TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200299 $this->load_rdriver();
300 if (FALSE !== ($cache = $this->CACHE->read($sql)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200302 return $cache;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // Compile binds if needed
307 if ($binds !== FALSE)
308 {
309 $sql = $this->compile_binds($sql, $binds);
310 }
311
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200312 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 if ($this->save_queries == TRUE)
314 {
315 $this->queries[] = $sql;
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // Start the Query Timer
319 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // Run the Query
322 if (FALSE === ($this->result_id = $this->simple_query($sql)))
323 {
324 if ($this->save_queries == TRUE)
325 {
326 $this->query_times[] = 0;
327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // This will trigger a rollback if transactions are being used
330 $this->_trans_status = FALSE;
331
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200332 // Grab the error number and message now, as we might run some
333 // additional queries before displaying the error
334 $error_no = $this->_error_number();
335 $error_msg = $this->_error_message();
336
337 // Log errors
338 log_message('error', 'Query error: '.$error_msg);
339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 if ($this->db_debug)
341 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // We call this function in order to roll-back queries
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200343 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200344 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // transactions to remain in limbo.
346 $this->trans_complete();
347
Niklas Nilssond2018ee2011-08-30 13:39:42 +0200348 // Display errors
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200349 return $this->display_error(array('Error Number: '.$error_no, $error_msg, $sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 return FALSE;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Stop and aggregate the query time results
356 $time_end = list($em, $es) = explode(' ', microtime());
357 $this->benchmark += ($em + $es) - ($sm + $ss);
358
359 if ($this->save_queries == TRUE)
360 {
361 $this->query_times[] = ($em + $es) - ($sm + $ss);
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // Increment the query counter
365 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // Was the query a "write" type?
368 // If so we'll simply return true
369 if ($this->is_write_type($sql) === TRUE)
370 {
371 // If caching is enabled we'll auto-cleanup any
372 // existing files related to this particular URI
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200373 if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 $this->CACHE->delete();
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 return TRUE;
379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // Return TRUE if we don't need to create a result object
382 // Currently only the Oracle driver uses this when stored
383 // procedures are used
384 if ($return_object !== TRUE)
385 {
386 return TRUE;
387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
389 // Load and instantiate the result driver
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200390 $driver = $this->load_rdriver();
391 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $RES->conn_id = $this->conn_id;
393 $RES->result_id = $this->result_id;
394
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200395 if ($this->dbdriver === 'oci8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 $RES->stmt_id = $this->stmt_id;
398 $RES->curs_id = NULL;
399 $RES->limit_used = $this->limit_used;
400 $this->stmt_id = FALSE;
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // oci8 vars must be set before calling this
404 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200405
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200406 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // result object and save it to a cache file.
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200408 if ($this->cache_on == TRUE && $this->_cache_init())
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 // We'll create a new instance of the result object
411 // only without the platform specific driver since
412 // we can't use it with cached data (the query result
413 // resource ID won't be any good once we've cached the
414 // result object, so we'll have to compile the data
415 // and save it)
416 $CR = new CI_DB_result();
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 $CR->result_object = $RES->result_object();
418 $CR->result_array = $RES->result_array();
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200419 $CR->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // Reset these since cached objects can not utilize resource IDs.
422 $CR->conn_id = NULL;
423 $CR->result_id = NULL;
424
425 $this->CACHE->write($sql, $CR);
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 return $RES;
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Load the result drivers
435 *
Barry Mienydd671972010-10-04 16:33:58 +0200436 * @return string the name of the result class
437 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200438 public function load_rdriver()
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
440 $driver = 'CI_DB_'.$this->dbdriver.'_result';
441
442 if ( ! class_exists($driver))
443 {
Greg Aker3a746652011-04-19 10:59:47 -0500444 include_once(BASEPATH.'database/DB_result.php');
445 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 return $driver;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // --------------------------------------------------------------------
452
453 /**
454 * Simple Query
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200455 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 * we only use it when running transaction commands since they do
457 * not require all the features of the main query() function.
458 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200460 * @return mixed
461 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200462 public function simple_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 if ( ! $this->conn_id)
465 {
466 $this->initialize();
467 }
468
469 return $this->_execute($sql);
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // --------------------------------------------------------------------
473
474 /**
475 * Disable Transactions
476 * This permits transactions to be disabled at run-time.
477 *
Barry Mienydd671972010-10-04 16:33:58 +0200478 * @return void
479 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200480 public function trans_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 $this->trans_enabled = FALSE;
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * Enable/disable Transaction Strict Mode
489 * When strict mode is enabled, if you are running multiple groups of
490 * transactions, if one group fails all groups will be rolled back.
491 * If strict mode is disabled, each group is treated autonomously, meaning
492 * a failure of one group will not affect any others
493 *
Barry Mienydd671972010-10-04 16:33:58 +0200494 * @return void
495 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200496 public function trans_strict($mode = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
498 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 // --------------------------------------------------------------------
502
503 /**
504 * Start Transaction
505 *
Barry Mienydd671972010-10-04 16:33:58 +0200506 * @return void
507 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200508 public function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200509 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 if ( ! $this->trans_enabled)
511 {
512 return FALSE;
513 }
514
515 // When transactions are nested we only begin/commit/rollback the outermost ones
516 if ($this->_trans_depth > 0)
517 {
518 $this->_trans_depth += 1;
519 return;
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 $this->trans_begin($test_mode);
Jacob Terry07fcedf2011-11-22 11:21:36 -0500523 $this->_trans_depth += 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525
526 // --------------------------------------------------------------------
527
528 /**
529 * Complete Transaction
530 *
Barry Mienydd671972010-10-04 16:33:58 +0200531 * @return bool
532 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200533 public function trans_complete()
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
535 if ( ! $this->trans_enabled)
536 {
537 return FALSE;
538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 // When transactions are nested we only begin/commit/rollback the outermost ones
541 if ($this->_trans_depth > 1)
542 {
543 $this->_trans_depth -= 1;
544 return TRUE;
545 }
Jacob Terry07fcedf2011-11-22 11:21:36 -0500546 else
547 {
548 $this->_trans_depth = 0;
549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 // The query() function will set this flag to FALSE in the event that a query failed
552 if ($this->_trans_status === FALSE)
553 {
554 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // If we are NOT running in strict mode, we will reset
557 // the _trans_status flag so that subsequent groups of transactions
558 // will be permitted.
559 if ($this->trans_strict === FALSE)
560 {
561 $this->_trans_status = TRUE;
562 }
563
564 log_message('debug', 'DB Transaction Failure');
565 return FALSE;
566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 $this->trans_commit();
569 return TRUE;
570 }
571
572 // --------------------------------------------------------------------
573
574 /**
575 * Lets you retrieve the transaction flag to determine if it has failed
576 *
Barry Mienydd671972010-10-04 16:33:58 +0200577 * @return bool
578 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200579 public function trans_status()
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 return $this->_trans_status;
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
587 * Compile Bindings
588 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @param string the sql statement
590 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200591 * @return string
592 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200593 public function compile_binds($sql, $binds)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 if (strpos($sql, $this->bind_marker) === FALSE)
596 {
597 return $sql;
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 if ( ! is_array($binds))
601 {
602 $binds = array($binds);
603 }
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // Get the sql segments around the bind markers
606 $segments = explode($this->bind_marker, $sql);
607
608 // The count of bind should be 1 less then the count of segments
609 // If there are more bind arguments trim it down
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200610 if (count($binds) >= count($segments))
611 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 $binds = array_slice($binds, 0, count($segments)-1);
613 }
614
615 // Construct the binded query
616 $result = $segments[0];
617 $i = 0;
618 foreach ($binds as $bind)
619 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200620 $result .= $this->escape($bind).$segments[++$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
622
623 return $result;
624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // --------------------------------------------------------------------
627
628 /**
629 * Determines if a query is a "write" type.
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @param string An SQL query string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200632 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200633 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200634 public function is_write_type($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200636 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 +0000637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 // --------------------------------------------------------------------
640
641 /**
642 * Calculate the aggregate query elapsed time
643 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200644 * @param int The number of decimal places
645 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200646 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200647 public function elapsed_time($decimals = 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 return number_format($this->benchmark, $decimals);
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // --------------------------------------------------------------------
653
654 /**
655 * Returns the total number of queries
656 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200657 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200658 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200659 public function total_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 return $this->query_count;
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 // --------------------------------------------------------------------
665
666 /**
667 * Returns the last query that was executed
668 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200669 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200670 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200671 public function last_query()
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 return end($this->queries);
674 }
675
676 // --------------------------------------------------------------------
677
678 /**
679 * "Smart" Escape String
680 *
681 * Escapes data based on type
682 * Sets boolean and null types
683 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200685 * @return mixed
686 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200687 public function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200688 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000689 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200691 return "'".$this->escape_str($str)."'";
Derek Jonesa377bdd2009-02-11 18:55:24 +0000692 }
693 elseif (is_bool($str))
694 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200695 return ($str === FALSE) ? 0 : 1;
Derek Jonesa377bdd2009-02-11 18:55:24 +0000696 }
697 elseif (is_null($str))
698 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200699 return 'NULL';
Derek Jonesa377bdd2009-02-11 18:55:24 +0000700 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000701
702 return $str;
703 }
704
705 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600706
Derek Jonese4ed5832009-02-20 21:44:59 +0000707 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000708 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000709 *
710 * Calls the individual driver for platform
711 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200712 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000713 * @param string
714 * @return mixed
715 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200716 public function escape_like_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200717 {
718 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000719 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000720
Derek Jonese4ed5832009-02-20 21:44:59 +0000721 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 /**
724 * Primary
725 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200726 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 * position is the primary key
728 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200730 * @return string
731 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200732 public function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200733 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 $fields = $this->list_fields($table);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200735 return is_array($fields) ? current($fields) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737
738 // --------------------------------------------------------------------
739
740 /**
741 * Returns an array of table names
742 *
Barry Mienydd671972010-10-04 16:33:58 +0200743 * @return array
744 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200745 public function list_tables($constrain_by_prefix = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
747 // Is there a cached result?
748 if (isset($this->data_cache['table_names']))
749 {
750 return $this->data_cache['table_names'];
751 }
Barry Mienydd671972010-10-04 16:33:58 +0200752
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
754 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200755 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
757
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200758 $this->data_cache['table_names'] = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200760
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200761 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200763 $this->data_cache['table_names'][] = isset($row['TABLE_NAME']) ? $row['TABLE_NAME'] : array_shift($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 return $this->data_cache['table_names'];
767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 // --------------------------------------------------------------------
770
771 /**
772 * Determine if a particular table exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200773 *
774 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200776 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200777 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200778 return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // --------------------------------------------------------------------
782
783 /**
784 * Fetch MySQL Field Names
785 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200787 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200789 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
791 // Is there a cached result?
792 if (isset($this->data_cache['field_names'][$table]))
793 {
794 return $this->data_cache['field_names'][$table];
795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 if ($table == '')
798 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200799 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 }
Barry Mienydd671972010-10-04 16:33:58 +0200801
Greg Aker1edde302010-01-26 00:17:01 +0000802 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200804 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200808 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200809
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500810 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200812 $this->data_cache['field_names'][$table][] = isset($row['COLUMN_NAME']) ? $row['COLUMN_NAME'] : current($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 return $this->data_cache['field_names'][$table];
816 }
817
818 // --------------------------------------------------------------------
819
820 /**
821 * Determine if a particular field exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200822 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 * @param string
824 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200825 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200827 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200828 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200829 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 // --------------------------------------------------------------------
833
834 /**
835 * Returns an object with field data
836 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 * @param string the table name
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200838 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200839 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200840 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
842 if ($table == '')
843 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200844 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200849 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000850
851 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 /**
854 * Generate an insert string
855 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 * @param string the table upon which the query will be performed
857 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200858 * @return string
859 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200860 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200862 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200863
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500864 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
866 $fields[] = $this->_escape_identifiers($key);
867 $values[] = $this->escape($val);
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200871 }
872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 // --------------------------------------------------------------------
874
875 /**
876 * Generate an update string
877 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * @param string the table upon which the query will be performed
879 * @param array an associative array data of key/values
880 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200881 * @return string
882 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200883 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 {
885 if ($where == '')
886 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200887 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500891 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
893 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
894 }
895
896 if ( ! is_array($where))
897 {
898 $dest = array($where);
899 }
900 else
901 {
902 $dest = array();
903 foreach ($where as $key => $val)
904 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200905 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300906 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 if ($val !== '')
909 {
910 if ( ! $this->_has_operator($key))
911 {
912 $key .= ' =';
913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 $val = ' '.$this->escape($val);
916 }
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 $dest[] = $prefix.$key.$val;
919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000921
922 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200923 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000924
925 // --------------------------------------------------------------------
926
927 /**
928 * Tests whether the string has an SQL operator
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @param string
931 * @return bool
932 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200933 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200935 return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 }
937
938 // --------------------------------------------------------------------
939
940 /**
941 * Enables a native PHP function to be run, using a platform agnostic wrapper.
942 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 * @param string the function name
944 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200945 * @return mixed
946 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200947 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 if (FALSE === strpos($driver, $function))
952 {
953 $function = $driver.$function;
954 }
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 if ( ! function_exists($function))
957 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200958 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200961 return (func_num_args() > 1)
962 ? call_user_func_array($function, array_splice(func_get_args(), 1))
963 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
965
966 // --------------------------------------------------------------------
967
968 /**
969 * Set Cache Directory Path
970 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 * @param string the path to the cache directory
972 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200973 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200974 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
976 $this->cachedir = $path;
977 }
978
979 // --------------------------------------------------------------------
980
981 /**
982 * Enable Query Caching
983 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200984 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +0200985 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200986 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200988 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
990
991 // --------------------------------------------------------------------
992
993 /**
994 * Disable Query Caching
995 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200996 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +0200997 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200998 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001000 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
Barry Mienydd671972010-10-04 16:33:58 +02001002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003
1004 // --------------------------------------------------------------------
1005
1006 /**
1007 * Delete the cache files associated with a particular URI
1008 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001009 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001010 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001011 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
1013 if ( ! $this->_cache_init())
1014 {
1015 return FALSE;
1016 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 return $this->CACHE->delete($segment_one, $segment_two);
1019 }
1020
1021 // --------------------------------------------------------------------
1022
1023 /**
1024 * Delete All cache files
1025 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001026 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001027 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001028 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 {
1030 if ( ! $this->_cache_init())
1031 {
1032 return FALSE;
1033 }
1034
1035 return $this->CACHE->delete_all();
1036 }
1037
1038 // --------------------------------------------------------------------
1039
1040 /**
1041 * Initialize the Cache Class
1042 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001044 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001045 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001047 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001049 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001050 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001051 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001052 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001054 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1055 {
1056 return $this->cache_off();
1057 }
Derek Allarde37ab382009-02-03 16:13:57 +00001058
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1060 return TRUE;
1061 }
1062
1063 // --------------------------------------------------------------------
1064
1065 /**
1066 * Close DB Connection
1067 *
Barry Mienydd671972010-10-04 16:33:58 +02001068 * @return void
1069 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001070 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001072 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 {
1074 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001075 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 }
Barry Mienydd671972010-10-04 16:33:58 +02001078
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Display an error message
1083 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 * @param string the error message
1085 * @param string any "swap" values
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001086 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001087 * @return string sends the application/error_db.php template
1088 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001089 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
Derek Jonese7792202010-03-02 17:24:46 -06001091 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 $LANG->load('db');
1093
1094 $heading = $LANG->line('db_error_heading');
1095
1096 if ($native == TRUE)
1097 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001098 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 }
1100 else
1101 {
1102 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104
Pascal Kriete60f8c392010-08-25 18:03:28 +02001105 // Find the most likely culprit of the error by going through
1106 // the backtrace until the source file is no longer in the
1107 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001108 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001109 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001110 {
1111 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1112 {
1113 // Found it - use a relative path for safety
1114 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1115 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001116 break;
1117 }
1118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Jonese7792202010-03-02 17:24:46 -06001120 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 echo $error->show_error($heading, $message, 'error_db');
1122 exit;
1123 }
1124
1125 // --------------------------------------------------------------------
1126
1127 /**
1128 * Protect Identifiers
1129 *
1130 * This function adds backticks if appropriate based on db type
1131 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 * @param mixed the item to escape
1133 * @return mixed the item with backticks
1134 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001135 public function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
1137 return $this->_protect_identifiers($item, $prefix_single);
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Protect Identifiers
1144 *
1145 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001146 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001148 * the table prefix onto it. Some logic is necessary in order to deal with
1149 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 *
1151 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1152 *
1153 * Or a query with aliasing:
1154 *
1155 * SELECT m.member_id, m.member_name FROM members AS m
1156 *
1157 * Since the column name can include up to four segments (host, DB, table, column)
1158 * or also have an alias prefix, we need to do a bit of work to figure this out and
1159 * insert the table prefix (if it exists) in the proper position, and escape only
1160 * the correct identifiers.
1161 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001162 * NOTE: This is used by DB_forge drivers and therefore needs to be public.
1163 * (until a better solution is implemented)
1164 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 * @param string
1166 * @param bool
1167 * @param mixed
1168 * @param bool
1169 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001170 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001171 public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
1173 if ( ! is_bool($protect_identifiers))
1174 {
1175 $protect_identifiers = $this->_protect_identifiers;
1176 }
Derek Allarde37ab382009-02-03 16:13:57 +00001177
1178 if (is_array($item))
1179 {
1180 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001181 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001182 {
1183 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1184 }
1185
1186 return $escaped_array;
1187 }
1188
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001190 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 // If the item has an alias declaration we remove it and set it aside.
1193 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001195 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001196 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 $item = substr($item, 0, - strlen($alias));
1198 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001199 else
1200 {
1201 $alias = '';
1202 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001203
Derek Allard911d3e02008-12-15 14:08:35 +00001204 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001205 // If a parenthesis is found we know that we do not need to
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001206 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001207 // way to deal with this, but I'm not thinking of it -- Rick
1208 if (strpos($item, '(') !== FALSE)
1209 {
1210 return $item.$alias;
1211 }
1212
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 // Break the string apart if it contains periods, then insert the table prefix
1214 // in the correct location, assuming the period doesn't indicate that we're dealing
1215 // with an alias. While we're at it, we will escape the components
1216 if (strpos($item, '.') !== FALSE)
1217 {
1218 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001219
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 // Does the first segment of the exploded item match
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001221 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 // we have nothing more to do other than escape the item
1223 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001224 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 if ($protect_identifiers === TRUE)
1226 {
1227 foreach ($parts as $key => $val)
1228 {
1229 if ( ! in_array($val, $this->_reserved_identifiers))
1230 {
1231 $parts[$key] = $this->_escape_identifiers($val);
1232 }
1233 }
Barry Mienydd671972010-10-04 16:33:58 +02001234
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001236 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 return $item.$alias;
1239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001241 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 if ($this->dbprefix != '')
1243 {
1244 // We now add the table prefix based on some logic.
1245 // Do we have 4 segments (hostname.database.table.column)?
1246 // If so, we add the table prefix to the column name in the 3rd segment.
1247 if (isset($parts[3]))
1248 {
1249 $i = 2;
1250 }
1251 // Do we have 3 segments (database.table.column)?
1252 // If so, we add the table prefix to the column name in 2nd position
1253 elseif (isset($parts[2]))
1254 {
1255 $i = 1;
1256 }
1257 // Do we have 2 segments (table.column)?
1258 // If so, we add the table prefix to the column name in 1st segment
1259 else
1260 {
1261 $i = 0;
1262 }
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 // This flag is set when the supplied $item does not contain a field name.
1265 // This can happen when this function is being called from a JOIN.
1266 if ($field_exists == FALSE)
1267 {
1268 $i++;
1269 }
Barry Mienydd671972010-10-04 16:33:58 +02001270
Derek Jones55acc8b2009-07-11 03:38:13 +00001271 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001272 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001273 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001274 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001275 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001277 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 {
1279 $parts[$i] = $this->dbprefix.$parts[$i];
1280 }
Barry Mienydd671972010-10-04 16:33:58 +02001281
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 // Put the parts back together
1283 $item = implode('.', $parts);
1284 }
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 if ($protect_identifiers === TRUE)
1287 {
1288 $item = $this->_escape_identifiers($item);
1289 }
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 return $item.$alias;
1292 }
1293
Derek Jones37f4b9c2011-07-01 17:56:50 -05001294 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 if ($this->dbprefix != '')
1296 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001297 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001298 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001299 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001300 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001301 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001303 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 {
1305 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001306 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
1308
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001309 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 {
1311 $item = $this->_escape_identifiers($item);
1312 }
Barry Mienydd671972010-10-04 16:33:58 +02001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 return $item.$alias;
1315 }
1316
1317
1318}
1319
Derek Allard2067d1a2008-11-13 22:59:24 +00001320/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001321/* Location: ./system/database/DB_driver.php */