blob: eb6ab743063eb74010ba784b187d050b5234ddab [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;
Andrey Andreeva5f2f692012-01-25 21:44:56 +0200398 $RES->curs_id = $this->curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 $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 Andreev12567e82012-01-25 22:50:33 +0200763 // Do we know from which column to get the table name?
764 if ( ! isset($key))
765 {
766 if (array_key_exists('table_name', $row))
767 {
768 $key = 'table_name';
769 }
770 elseif (array_key_exists('TABLE_NAME', $row))
771 {
772 $key = 'TABLE_NAME';
773 }
774 else
775 {
776 /* We have no other choice but to just get the first element's key.
777 * Due to array_shift() accepting it's argument by reference, if
778 * E_STRICT is on, this would trigger a warning. So we'll have to
779 * assign it first.
780 */
781 $key = array_keys($row);
782 $key = array_shift($key);
783 }
784 }
785
786 $this->data_cache['table_names'][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 return $this->data_cache['table_names'];
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 // --------------------------------------------------------------------
793
794 /**
795 * Determine if a particular table exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200796 *
797 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200799 public function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200800 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200801 return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables());
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // --------------------------------------------------------------------
805
806 /**
807 * Fetch MySQL Field Names
808 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200810 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200812 public function list_fields($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
814 // Is there a cached result?
815 if (isset($this->data_cache['field_names'][$table]))
816 {
817 return $this->data_cache['field_names'][$table];
818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 if ($table == '')
821 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200822 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Greg Aker1edde302010-01-26 00:17:01 +0000825 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200827 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 }
Barry Mienydd671972010-10-04 16:33:58 +0200829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 $query = $this->query($sql);
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200831 $this->data_cache['field_names'][$table] = array();
Barry Mienydd671972010-10-04 16:33:58 +0200832
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500833 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 {
Andrey Andreev12567e82012-01-25 22:50:33 +0200835 // Do we know from where to get the column's name?
836 if ( ! isset($key))
837 {
838 if (array_key_exists('column_name', $row))
839 {
840 $key = 'column_name';
841 }
842 elseif (array_key_exists('COLUMN_NAME', $row))
843 {
844 $key = 'COLUMN_NAME';
845 }
846 else
847 {
848 /* We have no other choice but to just get the first element's key.
849 * Due to array_shift() accepting it's argument by reference, if
850 * E_STRICT is on, this would trigger a warning. So we'll have to
851 * assign it first.
852 */
853 $key = array_keys($row);
854 $key = array_shift($key);
855 }
856 }
857
858 $this->data_cache['field_names'][$table][] = $row[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 return $this->data_cache['field_names'][$table];
862 }
863
864 // --------------------------------------------------------------------
865
866 /**
867 * Determine if a particular field exists
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200868 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * @param string
870 * @param string
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200871 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200873 public function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200874 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200875 return in_array($field_name, $this->list_fields($table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // --------------------------------------------------------------------
879
880 /**
881 * Returns an object with field data
882 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @param string the table name
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200884 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200885 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200886 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 if ($table == '')
889 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200890 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200895 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000896
897 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 /**
900 * Generate an insert string
901 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 * @param string the table upon which the query will be performed
903 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200904 * @return string
905 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200906 public function insert_string($table, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200908 $fields = $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200909
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500910 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
912 $fields[] = $this->_escape_identifiers($key);
913 $values[] = $this->escape($val);
914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200917 }
918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // --------------------------------------------------------------------
920
921 /**
922 * Generate an update string
923 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 * @param string the table upon which the query will be performed
925 * @param array an associative array data of key/values
926 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200927 * @return string
928 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200929 public function update_string($table, $data, $where)
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
931 if ($where == '')
932 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200933 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500937 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
940 }
941
942 if ( ! is_array($where))
943 {
944 $dest = array($where);
945 }
946 else
947 {
948 $dest = array();
949 foreach ($where as $key => $val)
950 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200951 $prefix = (count($dest) === 0) ? '' : ' AND ';
Andrey Andreevdc46d992011-09-24 16:25:23 +0300952 $key = $this->_protect_identifiers($key);
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 if ($val !== '')
955 {
956 if ( ! $this->_has_operator($key))
957 {
958 $key .= ' =';
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $val = ' '.$this->escape($val);
962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 $dest[] = $prefix.$key.$val;
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000967
968 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200969 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000970
971 // --------------------------------------------------------------------
972
973 /**
974 * Tests whether the string has an SQL operator
975 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 * @param string
977 * @return bool
978 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200979 protected function _has_operator($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200981 return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
983
984 // --------------------------------------------------------------------
985
986 /**
987 * Enables a native PHP function to be run, using a platform agnostic wrapper.
988 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 * @param string the function name
990 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200991 * @return mixed
992 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +0200993 public function call_function($function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
995 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 if (FALSE === strpos($driver, $function))
998 {
999 $function = $driver.$function;
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 if ( ! function_exists($function))
1003 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001004 return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001006
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001007 return (func_num_args() > 1)
1008 ? call_user_func_array($function, array_splice(func_get_args(), 1))
1009 : call_user_func($function);
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
1011
1012 // --------------------------------------------------------------------
1013
1014 /**
1015 * Set Cache Directory Path
1016 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 * @param string the path to the cache directory
1018 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001019 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001020 public function cache_set_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 {
1022 $this->cachedir = $path;
1023 }
1024
1025 // --------------------------------------------------------------------
1026
1027 /**
1028 * Enable Query Caching
1029 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001030 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001031 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001032 public function cache_on()
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001034 return $this->cache_on = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 }
1036
1037 // --------------------------------------------------------------------
1038
1039 /**
1040 * Disable Query Caching
1041 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001042 * @return bool cache_on value
Barry Mienydd671972010-10-04 16:33:58 +02001043 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001044 public function cache_off()
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001046 return $this->cache_on = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049
1050 // --------------------------------------------------------------------
1051
1052 /**
1053 * Delete the cache files associated with a particular URI
1054 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001055 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001056 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001057 public function cache_delete($segment_one = '', $segment_two = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
1059 if ( ! $this->_cache_init())
1060 {
1061 return FALSE;
1062 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 return $this->CACHE->delete($segment_one, $segment_two);
1065 }
1066
1067 // --------------------------------------------------------------------
1068
1069 /**
1070 * Delete All cache files
1071 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001072 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001073 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001074 public function cache_delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
1076 if ( ! $this->_cache_init())
1077 {
1078 return FALSE;
1079 }
1080
1081 return $this->CACHE->delete_all();
1082 }
1083
1084 // --------------------------------------------------------------------
1085
1086 /**
1087 * Initialize the Cache Class
1088 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001090 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001091 protected function _cache_init()
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001093 if (class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001095 if (is_object($this->CACHE))
Derek Allarde37ab382009-02-03 16:13:57 +00001096 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001097 return TRUE;
Derek Allarde37ab382009-02-03 16:13:57 +00001098 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001100 elseif ( ! @include_once(BASEPATH.'database/DB_cache.php'))
1101 {
1102 return $this->cache_off();
1103 }
Derek Allarde37ab382009-02-03 16:13:57 +00001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1106 return TRUE;
1107 }
1108
1109 // --------------------------------------------------------------------
1110
1111 /**
1112 * Close DB Connection
1113 *
Barry Mienydd671972010-10-04 16:33:58 +02001114 * @return void
1115 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001116 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001118 if ($this->conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
1120 $this->_close($this->conn_id);
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001121 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 // --------------------------------------------------------------------
1126
1127 /**
1128 * Display an error message
1129 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 * @param string the error message
1131 * @param string any "swap" values
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001132 * @param bool whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001133 * @return string sends the application/error_db.php template
1134 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001135 public function display_error($error = '', $swap = '', $native = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Derek Jonese7792202010-03-02 17:24:46 -06001137 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 $LANG->load('db');
1139
1140 $heading = $LANG->line('db_error_heading');
1141
1142 if ($native == TRUE)
1143 {
Andrey Andreev85a99cc2011-09-24 17:17:37 +03001144 $message = (array) $error;
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
1146 else
1147 {
1148 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Pascal Kriete60f8c392010-08-25 18:03:28 +02001151 // Find the most likely culprit of the error by going through
1152 // the backtrace until the source file is no longer in the
1153 // database folder.
Pascal Kriete60f8c392010-08-25 18:03:28 +02001154 $trace = debug_backtrace();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001155 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001156 {
1157 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1158 {
1159 // Found it - use a relative path for safety
1160 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1161 $message[] = 'Line Number: '.$call['line'];
Pascal Kriete60f8c392010-08-25 18:03:28 +02001162 break;
1163 }
1164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Jonese7792202010-03-02 17:24:46 -06001166 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 echo $error->show_error($heading, $message, 'error_db');
1168 exit;
1169 }
1170
1171 // --------------------------------------------------------------------
1172
1173 /**
1174 * Protect Identifiers
1175 *
1176 * This function adds backticks if appropriate based on db type
1177 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 * @param mixed the item to escape
1179 * @return mixed the item with backticks
1180 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001181 public function protect_identifiers($item, $prefix_single = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
1183 return $this->_protect_identifiers($item, $prefix_single);
1184 }
1185
1186 // --------------------------------------------------------------------
1187
1188 /**
1189 * Protect Identifiers
1190 *
1191 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001192 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 * It takes a column or table name (optionally with an alias) and inserts
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001194 * the table prefix onto it. Some logic is necessary in order to deal with
1195 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 *
1197 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1198 *
1199 * Or a query with aliasing:
1200 *
1201 * SELECT m.member_id, m.member_name FROM members AS m
1202 *
1203 * Since the column name can include up to four segments (host, DB, table, column)
1204 * or also have an alias prefix, we need to do a bit of work to figure this out and
1205 * insert the table prefix (if it exists) in the proper position, and escape only
1206 * the correct identifiers.
1207 *
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001208 * NOTE: This is used by DB_forge drivers and therefore needs to be public.
1209 * (until a better solution is implemented)
1210 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 * @param string
1212 * @param bool
1213 * @param mixed
1214 * @param bool
1215 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001216 */
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001217 public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 {
1219 if ( ! is_bool($protect_identifiers))
1220 {
1221 $protect_identifiers = $this->_protect_identifiers;
1222 }
Derek Allarde37ab382009-02-03 16:13:57 +00001223
1224 if (is_array($item))
1225 {
1226 $escaped_array = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001227 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001228 {
1229 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1230 }
1231
1232 return $escaped_array;
1233 }
1234
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001236 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 // If the item has an alias declaration we remove it and set it aside.
1239 // Basically we remove everything to the right of the first space
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001241 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001242 $alias = strstr($item, ' ');
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 $item = substr($item, 0, - strlen($alias));
1244 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001245 else
1246 {
1247 $alias = '';
1248 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001249
Derek Allard911d3e02008-12-15 14:08:35 +00001250 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001251 // If a parenthesis is found we know that we do not need to
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001252 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001253 // way to deal with this, but I'm not thinking of it -- Rick
1254 if (strpos($item, '(') !== FALSE)
1255 {
1256 return $item.$alias;
1257 }
1258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // Break the string apart if it contains periods, then insert the table prefix
1260 // in the correct location, assuming the period doesn't indicate that we're dealing
1261 // with an alias. While we're at it, we will escape the components
1262 if (strpos($item, '.') !== FALSE)
1263 {
1264 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 // Does the first segment of the exploded item match
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001267 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 // we have nothing more to do other than escape the item
1269 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001270 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 if ($protect_identifiers === TRUE)
1272 {
1273 foreach ($parts as $key => $val)
1274 {
1275 if ( ! in_array($val, $this->_reserved_identifiers))
1276 {
1277 $parts[$key] = $this->_escape_identifiers($val);
1278 }
1279 }
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001282 }
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001283
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 return $item.$alias;
1285 }
Barry Mienydd671972010-10-04 16:33:58 +02001286
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001287 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 if ($this->dbprefix != '')
1289 {
1290 // We now add the table prefix based on some logic.
1291 // Do we have 4 segments (hostname.database.table.column)?
1292 // If so, we add the table prefix to the column name in the 3rd segment.
1293 if (isset($parts[3]))
1294 {
1295 $i = 2;
1296 }
1297 // Do we have 3 segments (database.table.column)?
1298 // If so, we add the table prefix to the column name in 2nd position
1299 elseif (isset($parts[2]))
1300 {
1301 $i = 1;
1302 }
1303 // Do we have 2 segments (table.column)?
1304 // If so, we add the table prefix to the column name in 1st segment
1305 else
1306 {
1307 $i = 0;
1308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 // This flag is set when the supplied $item does not contain a field name.
1311 // This can happen when this function is being called from a JOIN.
1312 if ($field_exists == FALSE)
1313 {
1314 $i++;
1315 }
Barry Mienydd671972010-10-04 16:33:58 +02001316
Derek Jones55acc8b2009-07-11 03:38:13 +00001317 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001318 if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001319 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001320 $parts[$i] = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $parts[$i]);
Derek Jones55acc8b2009-07-11 03:38:13 +00001321 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 // We only add the table prefix if it does not already exist
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001323 elseif (strpos($parts[$i], $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
1325 $parts[$i] = $this->dbprefix.$parts[$i];
1326 }
Barry Mienydd671972010-10-04 16:33:58 +02001327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 // Put the parts back together
1329 $item = implode('.', $parts);
1330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 if ($protect_identifiers === TRUE)
1333 {
1334 $item = $this->_escape_identifiers($item);
1335 }
Barry Mienydd671972010-10-04 16:33:58 +02001336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 return $item.$alias;
1338 }
1339
Derek Jones37f4b9c2011-07-01 17:56:50 -05001340 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 if ($this->dbprefix != '')
1342 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001343 // Verify table prefix and replace if necessary
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001344 if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0)
Derek Jones55acc8b2009-07-11 03:38:13 +00001345 {
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001346 $item = preg_replace('/^'.$this->swap_pre.'(\S+?)/', $this->dbprefix.'\\1', $item);
Derek Jones55acc8b2009-07-11 03:38:13 +00001347 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 // Do we prefix an item with no segments?
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001349 elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 {
1351 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001352 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 }
1354
Andrey Andreevaf5d5582012-01-25 21:34:47 +02001355 if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers))
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 {
1357 $item = $this->_escape_identifiers($item);
1358 }
Barry Mienydd671972010-10-04 16:33:58 +02001359
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 return $item.$alias;
1361 }
1362
1363
1364}
1365
Derek Allard2067d1a2008-11-13 22:59:24 +00001366/* End of file DB_driver.php */
Andrey Andreevdc46d992011-09-24 16:25:23 +03001367/* Location: ./system/database/DB_driver.php */