Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Database Driver Class |
| 32 | * |
| 33 | * This is the platform-independent base DB implementation class. |
| 34 | * This class will not be called directly. Rather, the adapter |
| 35 | * class for the specific database will extend and instantiate it. |
| 36 | * |
| 37 | * @package CodeIgniter |
| 38 | * @subpackage Drivers |
| 39 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
| 43 | class CI_DB_driver { |
| 44 | |
| 45 | var $username; |
| 46 | var $password; |
| 47 | var $hostname; |
| 48 | var $database; |
| 49 | var $dbdriver = 'mysql'; |
| 50 | var $dbprefix = ''; |
| 51 | var $char_set = 'utf8'; |
| 52 | var $dbcollat = 'utf8_general_ci'; |
| 53 | var $autoinit = TRUE; // Whether to automatically initialize the DB |
| 54 | var $swap_pre = ''; |
| 55 | var $port = ''; |
| 56 | var $pconnect = FALSE; |
| 57 | var $conn_id = FALSE; |
| 58 | var $result_id = FALSE; |
| 59 | var $db_debug = FALSE; |
| 60 | var $benchmark = 0; |
| 61 | var $query_count = 0; |
| 62 | var $bind_marker = '?'; |
| 63 | var $save_queries = TRUE; |
| 64 | var $queries = array(); |
| 65 | var $query_times = array(); |
| 66 | var $data_cache = array(); |
| 67 | var $trans_enabled = TRUE; |
| 68 | var $trans_strict = TRUE; |
| 69 | var $_trans_depth = 0; |
| 70 | var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur |
| 71 | var $cache_on = FALSE; |
| 72 | var $cachedir = ''; |
| 73 | var $cache_autodel = FALSE; |
| 74 | var $CACHE; // The cache class object |
| 75 | |
| 76 | // Private variables |
| 77 | var $_protect_identifiers = TRUE; |
| 78 | var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped |
| 79 | |
| 80 | // These are use with Oracle |
| 81 | var $stmt_id; |
| 82 | var $curs_id; |
| 83 | var $limit_used; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 84 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 87 | * Constructor. Accepts one parameter containing the database |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | * connection settings. |
| 89 | * |
| 90 | * @param array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | */ |
Timothy Warren | a2097a0 | 2011-10-10 10:10:46 -0400 | [diff] [blame] | 92 | function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | { |
| 94 | if (is_array($params)) |
| 95 | { |
| 96 | foreach ($params as $key => $val) |
| 97 | { |
| 98 | $this->$key = $val; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | log_message('debug', 'Database Driver Class Initialized'); |
| 103 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * Initialize Database Settings |
| 109 | * |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 110 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | */ |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 112 | public function initialize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | { |
| 114 | // If an existing connection resource is available |
| 115 | // there is no need to connect and select the database |
| 116 | if (is_resource($this->conn_id) OR is_object($this->conn_id)) |
| 117 | { |
| 118 | return TRUE; |
| 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | // Connect to the database and set the connection ID |
| 124 | $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); |
| 125 | |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 126 | // No connection resource? Check if there is a failover else throw an error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | if ( ! $this->conn_id) |
| 128 | { |
Felix Balfoort | 5d581b6 | 2011-11-29 15:53:01 +0100 | [diff] [blame] | 129 | // Check if there is a failover set |
| 130 | if ( ! empty($this->failover) && is_array($this->failover)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | { |
Felix Balfoort | 5d581b6 | 2011-11-29 15:53:01 +0100 | [diff] [blame] | 132 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | } |
Felix Balfoort | 5d581b6 | 2011-11-29 15:53:01 +0100 | [diff] [blame] | 151 | |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // ---------------------------------------------------------------- |
| 166 | |
| 167 | // Select the DB... assuming a database name is specified in the config file |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 168 | if ($this->database !== '' && ! $this->db_select()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | { |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 170 | log_message('error', 'Unable to select database: '.$this->database); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 172 | if ($this->db_debug) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | { |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 174 | $this->display_error('db_unable_to_select', $this->database); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | } |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 176 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Andrey Andreev | 82e8ac1 | 2012-02-22 19:35:34 +0200 | [diff] [blame] | 179 | // Now we set the character set and that's all |
| 180 | return $this->db_set_charset($this->char_set, $this->dbcollat); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | // -------------------------------------------------------------------- |
| 184 | |
| 185 | /** |
| 186 | * Set client character set |
| 187 | * |
| 188 | * @access public |
| 189 | * @param string |
| 190 | * @param string |
| 191 | * @return resource |
| 192 | */ |
| 193 | function db_set_charset($charset, $collation) |
| 194 | { |
| 195 | if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat)) |
| 196 | { |
| 197 | log_message('error', 'Unable to set database connection charset: '.$this->char_set); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | if ($this->db_debug) |
| 200 | { |
| 201 | $this->display_error('db_unable_to_set_charset', $this->char_set); |
| 202 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | return FALSE; |
| 205 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | return TRUE; |
| 208 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
| 213 | * The name of the platform in use (mysql, mssql, etc...) |
| 214 | * |
| 215 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | * @return string |
| 217 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | function platform() |
| 219 | { |
| 220 | return $this->dbdriver; |
| 221 | } |
| 222 | |
| 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 226 | * Database Version Number. Returns a string containing the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | * version of the database being used |
| 228 | * |
| 229 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | * @return string |
| 231 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | function version() |
| 233 | { |
| 234 | if (FALSE === ($sql = $this->_version())) |
| 235 | { |
| 236 | if ($this->db_debug) |
| 237 | { |
| 238 | return $this->display_error('db_unsupported_function'); |
| 239 | } |
| 240 | return FALSE; |
| 241 | } |
Derek Allard | 3683f77 | 2009-12-16 17:32:33 +0000 | [diff] [blame] | 242 | |
| 243 | // Some DBs have functions that return the version, and don't run special |
| 244 | // SQL queries per se. In these instances, just return the result. |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 245 | $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli'); |
Derek Allard | 3683f77 | 2009-12-16 17:32:33 +0000 | [diff] [blame] | 246 | |
| 247 | if (in_array($this->dbdriver, $driver_version_exceptions)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | { |
| 249 | return $sql; |
| 250 | } |
Derek Allard | 3683f77 | 2009-12-16 17:32:33 +0000 | [diff] [blame] | 251 | else |
| 252 | { |
| 253 | $query = $this->query($sql); |
| 254 | return $query->row('ver'); |
| 255 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
| 261 | * Execute the query |
| 262 | * |
| 263 | * Accepts an SQL string as input and returns a result object upon |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 264 | * successful execution of a "read" type query. Returns boolean TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | * upon successful execution of a "write" type query. Returns boolean |
| 266 | * FALSE upon failure, and if the $db_debug variable is set to TRUE |
| 267 | * will raise an error. |
| 268 | * |
| 269 | * @access public |
| 270 | * @param string An SQL query string |
| 271 | * @param array An array of binding data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | * @return mixed |
| 273 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | function query($sql, $binds = FALSE, $return_object = TRUE) |
| 275 | { |
| 276 | if ($sql == '') |
| 277 | { |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 278 | log_message('error', 'Invalid query: '.$sql); |
| 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | if ($this->db_debug) |
| 281 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | return $this->display_error('db_invalid_query'); |
| 283 | } |
| 284 | return FALSE; |
| 285 | } |
| 286 | |
| 287 | // Verify table prefix and replace if necessary |
| 288 | if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 289 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); |
| 291 | } |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 292 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 293 | // Is query caching enabled? If the query is a "read type" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | // we will load the caching class and return the previously |
| 295 | // cached query if it exists |
| 296 | if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) |
| 297 | { |
| 298 | if ($this->_cache_init()) |
| 299 | { |
| 300 | $this->load_rdriver(); |
| 301 | if (FALSE !== ($cache = $this->CACHE->read($sql))) |
| 302 | { |
| 303 | return $cache; |
| 304 | } |
| 305 | } |
| 306 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | // Compile binds if needed |
| 309 | if ($binds !== FALSE) |
| 310 | { |
| 311 | $sql = $this->compile_binds($sql, $binds); |
| 312 | } |
| 313 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 314 | // Save the query for debugging |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | if ($this->save_queries == TRUE) |
| 316 | { |
| 317 | $this->queries[] = $sql; |
| 318 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 319 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | // Start the Query Timer |
| 321 | $time_start = list($sm, $ss) = explode(' ', microtime()); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | // Run the Query |
| 324 | if (FALSE === ($this->result_id = $this->simple_query($sql))) |
| 325 | { |
| 326 | if ($this->save_queries == TRUE) |
| 327 | { |
| 328 | $this->query_times[] = 0; |
| 329 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | // This will trigger a rollback if transactions are being used |
| 332 | $this->_trans_status = FALSE; |
| 333 | |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 334 | // Grab the error number and message now, as we might run some |
| 335 | // additional queries before displaying the error |
| 336 | $error_no = $this->_error_number(); |
| 337 | $error_msg = $this->_error_message(); |
| 338 | |
| 339 | // Log errors |
| 340 | log_message('error', 'Query error: '.$error_msg); |
| 341 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | if ($this->db_debug) |
| 343 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | // We call this function in order to roll-back queries |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 345 | // if transactions are enabled. If we don't call this here |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 346 | // the error message will trigger an exit, causing the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | // transactions to remain in limbo. |
| 348 | $this->trans_complete(); |
| 349 | |
Niklas Nilsson | d2018ee | 2011-08-30 13:39:42 +0200 | [diff] [blame] | 350 | // Display errors |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | return $this->display_error( |
| 352 | array( |
| 353 | 'Error Number: '.$error_no, |
| 354 | $error_msg, |
| 355 | $sql |
| 356 | ) |
| 357 | ); |
| 358 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | return FALSE; |
| 361 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | // Stop and aggregate the query time results |
| 364 | $time_end = list($em, $es) = explode(' ', microtime()); |
| 365 | $this->benchmark += ($em + $es) - ($sm + $ss); |
| 366 | |
| 367 | if ($this->save_queries == TRUE) |
| 368 | { |
| 369 | $this->query_times[] = ($em + $es) - ($sm + $ss); |
| 370 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 371 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | // Increment the query counter |
| 373 | $this->query_count++; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | // Was the query a "write" type? |
| 376 | // If so we'll simply return true |
| 377 | if ($this->is_write_type($sql) === TRUE) |
| 378 | { |
| 379 | // If caching is enabled we'll auto-cleanup any |
| 380 | // existing files related to this particular URI |
| 381 | if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) |
| 382 | { |
| 383 | $this->CACHE->delete(); |
| 384 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 385 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | return TRUE; |
| 387 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | // Return TRUE if we don't need to create a result object |
| 390 | // Currently only the Oracle driver uses this when stored |
| 391 | // procedures are used |
| 392 | if ($return_object !== TRUE) |
| 393 | { |
| 394 | return TRUE; |
| 395 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 396 | |
| 397 | // Load and instantiate the result driver |
| 398 | |
| 399 | $driver = $this->load_rdriver(); |
| 400 | $RES = new $driver(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | $RES->conn_id = $this->conn_id; |
| 402 | $RES->result_id = $this->result_id; |
| 403 | |
| 404 | if ($this->dbdriver == 'oci8') |
| 405 | { |
| 406 | $RES->stmt_id = $this->stmt_id; |
| 407 | $RES->curs_id = NULL; |
| 408 | $RES->limit_used = $this->limit_used; |
| 409 | $this->stmt_id = FALSE; |
| 410 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | // oci8 vars must be set before calling this |
| 413 | $RES->num_rows = $RES->num_rows(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 415 | // Is query caching enabled? If so, we'll serialize the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | // result object and save it to a cache file. |
| 417 | if ($this->cache_on == TRUE AND $this->_cache_init()) |
| 418 | { |
| 419 | // We'll create a new instance of the result object |
| 420 | // only without the platform specific driver since |
| 421 | // we can't use it with cached data (the query result |
| 422 | // resource ID won't be any good once we've cached the |
| 423 | // result object, so we'll have to compile the data |
| 424 | // and save it) |
| 425 | $CR = new CI_DB_result(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | $CR->num_rows = $RES->num_rows(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | $CR->result_object = $RES->result_object(); |
| 428 | $CR->result_array = $RES->result_array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 429 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | // Reset these since cached objects can not utilize resource IDs. |
| 431 | $CR->conn_id = NULL; |
| 432 | $CR->result_id = NULL; |
| 433 | |
| 434 | $this->CACHE->write($sql, $CR); |
| 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | return $RES; |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------- |
| 441 | |
| 442 | /** |
| 443 | * Load the result drivers |
| 444 | * |
| 445 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 446 | * @return string the name of the result class |
| 447 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | function load_rdriver() |
| 449 | { |
| 450 | $driver = 'CI_DB_'.$this->dbdriver.'_result'; |
| 451 | |
| 452 | if ( ! class_exists($driver)) |
| 453 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 454 | include_once(BASEPATH.'database/DB_result.php'); |
| 455 | include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 457 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | return $driver; |
| 459 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | // -------------------------------------------------------------------- |
| 462 | |
| 463 | /** |
| 464 | * Simple Query |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 465 | * This is a simplified version of the query() function. Internally |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | * we only use it when running transaction commands since they do |
| 467 | * not require all the features of the main query() function. |
| 468 | * |
| 469 | * @access public |
| 470 | * @param string the sql query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | * @return mixed |
| 472 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | function simple_query($sql) |
| 474 | { |
| 475 | if ( ! $this->conn_id) |
| 476 | { |
| 477 | $this->initialize(); |
| 478 | } |
| 479 | |
| 480 | return $this->_execute($sql); |
| 481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | // -------------------------------------------------------------------- |
| 484 | |
| 485 | /** |
| 486 | * Disable Transactions |
| 487 | * This permits transactions to be disabled at run-time. |
| 488 | * |
| 489 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | * @return void |
| 491 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | function trans_off() |
| 493 | { |
| 494 | $this->trans_enabled = FALSE; |
| 495 | } |
| 496 | |
| 497 | // -------------------------------------------------------------------- |
| 498 | |
| 499 | /** |
| 500 | * Enable/disable Transaction Strict Mode |
| 501 | * When strict mode is enabled, if you are running multiple groups of |
| 502 | * transactions, if one group fails all groups will be rolled back. |
| 503 | * If strict mode is disabled, each group is treated autonomously, meaning |
| 504 | * a failure of one group will not affect any others |
| 505 | * |
| 506 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 507 | * @return void |
| 508 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | function trans_strict($mode = TRUE) |
| 510 | { |
| 511 | $this->trans_strict = is_bool($mode) ? $mode : TRUE; |
| 512 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 513 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | // -------------------------------------------------------------------- |
| 515 | |
| 516 | /** |
| 517 | * Start Transaction |
| 518 | * |
| 519 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 520 | * @return void |
| 521 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | function trans_start($test_mode = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | if ( ! $this->trans_enabled) |
| 525 | { |
| 526 | return FALSE; |
| 527 | } |
| 528 | |
| 529 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 530 | if ($this->_trans_depth > 0) |
| 531 | { |
| 532 | $this->_trans_depth += 1; |
| 533 | return; |
| 534 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 535 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | $this->trans_begin($test_mode); |
Jacob Terry | 07fcedf | 2011-11-22 11:21:36 -0500 | [diff] [blame] | 537 | $this->_trans_depth += 1; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // -------------------------------------------------------------------- |
| 541 | |
| 542 | /** |
| 543 | * Complete Transaction |
| 544 | * |
| 545 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 546 | * @return bool |
| 547 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | function trans_complete() |
| 549 | { |
| 550 | if ( ! $this->trans_enabled) |
| 551 | { |
| 552 | return FALSE; |
| 553 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 554 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 556 | if ($this->_trans_depth > 1) |
| 557 | { |
| 558 | $this->_trans_depth -= 1; |
| 559 | return TRUE; |
| 560 | } |
Jacob Terry | 07fcedf | 2011-11-22 11:21:36 -0500 | [diff] [blame] | 561 | else |
| 562 | { |
| 563 | $this->_trans_depth = 0; |
| 564 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | // The query() function will set this flag to FALSE in the event that a query failed |
| 567 | if ($this->_trans_status === FALSE) |
| 568 | { |
| 569 | $this->trans_rollback(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 570 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | // If we are NOT running in strict mode, we will reset |
| 572 | // the _trans_status flag so that subsequent groups of transactions |
| 573 | // will be permitted. |
| 574 | if ($this->trans_strict === FALSE) |
| 575 | { |
| 576 | $this->_trans_status = TRUE; |
| 577 | } |
| 578 | |
| 579 | log_message('debug', 'DB Transaction Failure'); |
| 580 | return FALSE; |
| 581 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 582 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 583 | $this->trans_commit(); |
| 584 | return TRUE; |
| 585 | } |
| 586 | |
| 587 | // -------------------------------------------------------------------- |
| 588 | |
| 589 | /** |
| 590 | * Lets you retrieve the transaction flag to determine if it has failed |
| 591 | * |
| 592 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 593 | * @return bool |
| 594 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | function trans_status() |
| 596 | { |
| 597 | return $this->_trans_status; |
| 598 | } |
| 599 | |
| 600 | // -------------------------------------------------------------------- |
| 601 | |
| 602 | /** |
| 603 | * Compile Bindings |
| 604 | * |
| 605 | * @access public |
| 606 | * @param string the sql statement |
| 607 | * @param array an array of bind data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 608 | * @return string |
| 609 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 610 | function compile_binds($sql, $binds) |
| 611 | { |
| 612 | if (strpos($sql, $this->bind_marker) === FALSE) |
| 613 | { |
| 614 | return $sql; |
| 615 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 616 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 617 | if ( ! is_array($binds)) |
| 618 | { |
| 619 | $binds = array($binds); |
| 620 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 621 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | // Get the sql segments around the bind markers |
| 623 | $segments = explode($this->bind_marker, $sql); |
| 624 | |
| 625 | // The count of bind should be 1 less then the count of segments |
| 626 | // If there are more bind arguments trim it down |
| 627 | if (count($binds) >= count($segments)) { |
| 628 | $binds = array_slice($binds, 0, count($segments)-1); |
| 629 | } |
| 630 | |
| 631 | // Construct the binded query |
| 632 | $result = $segments[0]; |
| 633 | $i = 0; |
| 634 | foreach ($binds as $bind) |
| 635 | { |
| 636 | $result .= $this->escape($bind); |
| 637 | $result .= $segments[++$i]; |
| 638 | } |
| 639 | |
| 640 | return $result; |
| 641 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 642 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 643 | // -------------------------------------------------------------------- |
| 644 | |
| 645 | /** |
| 646 | * Determines if a query is a "write" type. |
| 647 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | * @param string An SQL query string |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame^] | 649 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 650 | */ |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame^] | 651 | public function is_write_type($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | { |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame^] | 653 | return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|OPTIMIZE)\s+/i', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 655 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 656 | // -------------------------------------------------------------------- |
| 657 | |
| 658 | /** |
| 659 | * Calculate the aggregate query elapsed time |
| 660 | * |
| 661 | * @access public |
| 662 | * @param integer The number of decimal places |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 663 | * @return integer |
| 664 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 665 | function elapsed_time($decimals = 6) |
| 666 | { |
| 667 | return number_format($this->benchmark, $decimals); |
| 668 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 669 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 670 | // -------------------------------------------------------------------- |
| 671 | |
| 672 | /** |
| 673 | * Returns the total number of queries |
| 674 | * |
| 675 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 676 | * @return integer |
| 677 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 678 | function total_queries() |
| 679 | { |
| 680 | return $this->query_count; |
| 681 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 682 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 683 | // -------------------------------------------------------------------- |
| 684 | |
| 685 | /** |
| 686 | * Returns the last query that was executed |
| 687 | * |
| 688 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 689 | * @return void |
| 690 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | function last_query() |
| 692 | { |
| 693 | return end($this->queries); |
| 694 | } |
| 695 | |
| 696 | // -------------------------------------------------------------------- |
| 697 | |
| 698 | /** |
| 699 | * "Smart" Escape String |
| 700 | * |
| 701 | * Escapes data based on type |
| 702 | * Sets boolean and null types |
| 703 | * |
| 704 | * @access public |
| 705 | * @param string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 706 | * @return mixed |
| 707 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 708 | function escape($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 709 | { |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 710 | if (is_string($str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 711 | { |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 712 | $str = "'".$this->escape_str($str)."'"; |
| 713 | } |
| 714 | elseif (is_bool($str)) |
| 715 | { |
| 716 | $str = ($str === FALSE) ? 0 : 1; |
| 717 | } |
| 718 | elseif (is_null($str)) |
| 719 | { |
| 720 | $str = 'NULL'; |
| 721 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 722 | |
| 723 | return $str; |
| 724 | } |
| 725 | |
| 726 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 727 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 728 | /** |
Derek Jones | bdc7fb9 | 2009-02-20 21:55:10 +0000 | [diff] [blame] | 729 | * Escape LIKE String |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 730 | * |
| 731 | * Calls the individual driver for platform |
| 732 | * specific escaping for LIKE conditions |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 733 | * |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 734 | * @access public |
| 735 | * @param string |
| 736 | * @return mixed |
| 737 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 738 | function escape_like_str($str) |
| 739 | { |
| 740 | return $this->escape_str($str, TRUE); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 741 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 742 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 743 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 744 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 745 | /** |
| 746 | * Primary |
| 747 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 748 | * Retrieves the primary key. It assumes that the row in the first |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 749 | * position is the primary key |
| 750 | * |
| 751 | * @access public |
| 752 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 753 | * @return string |
| 754 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 755 | function primary($table = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 756 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 757 | $fields = $this->list_fields($table); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 758 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 759 | if ( ! is_array($fields)) |
| 760 | { |
| 761 | return FALSE; |
| 762 | } |
| 763 | |
| 764 | return current($fields); |
| 765 | } |
| 766 | |
| 767 | // -------------------------------------------------------------------- |
| 768 | |
| 769 | /** |
| 770 | * Returns an array of table names |
| 771 | * |
| 772 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 773 | * @return array |
| 774 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 775 | function list_tables($constrain_by_prefix = FALSE) |
| 776 | { |
| 777 | // Is there a cached result? |
| 778 | if (isset($this->data_cache['table_names'])) |
| 779 | { |
| 780 | return $this->data_cache['table_names']; |
| 781 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 782 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) |
| 784 | { |
| 785 | if ($this->db_debug) |
| 786 | { |
| 787 | return $this->display_error('db_unsupported_function'); |
| 788 | } |
| 789 | return FALSE; |
| 790 | } |
| 791 | |
| 792 | $retval = array(); |
| 793 | $query = $this->query($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 794 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 795 | if ($query->num_rows() > 0) |
| 796 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 797 | $table = FALSE; |
| 798 | $rows = $query->result_array(); |
| 799 | $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row)))); |
| 800 | |
| 801 | if ($key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 802 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 803 | $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name'; |
| 804 | } |
| 805 | |
| 806 | foreach ($rows as $row) |
| 807 | { |
| 808 | $retval[] = ( ! $table) ? current($row) : $row[$table]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | $this->data_cache['table_names'] = $retval; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 813 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 814 | return $this->data_cache['table_names']; |
| 815 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 816 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 817 | // -------------------------------------------------------------------- |
| 818 | |
| 819 | /** |
| 820 | * Determine if a particular table exists |
| 821 | * @access public |
| 822 | * @return boolean |
| 823 | */ |
| 824 | function table_exists($table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 825 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 826 | return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; |
| 827 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 828 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 829 | // -------------------------------------------------------------------- |
| 830 | |
| 831 | /** |
| 832 | * Fetch MySQL Field Names |
| 833 | * |
| 834 | * @access public |
| 835 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 836 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 837 | */ |
| 838 | function list_fields($table = '') |
| 839 | { |
| 840 | // Is there a cached result? |
| 841 | if (isset($this->data_cache['field_names'][$table])) |
| 842 | { |
| 843 | return $this->data_cache['field_names'][$table]; |
| 844 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 845 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 846 | if ($table == '') |
| 847 | { |
| 848 | if ($this->db_debug) |
| 849 | { |
| 850 | return $this->display_error('db_field_param_missing'); |
| 851 | } |
| 852 | return FALSE; |
| 853 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 854 | |
Greg Aker | 1edde30 | 2010-01-26 00:17:01 +0000 | [diff] [blame] | 855 | if (FALSE === ($sql = $this->_list_columns($table))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 856 | { |
| 857 | if ($this->db_debug) |
| 858 | { |
| 859 | return $this->display_error('db_unsupported_function'); |
| 860 | } |
| 861 | return FALSE; |
| 862 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 863 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 864 | $query = $this->query($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 865 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 866 | $retval = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 867 | foreach ($query->result_array() as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 868 | { |
| 869 | if (isset($row['COLUMN_NAME'])) |
| 870 | { |
| 871 | $retval[] = $row['COLUMN_NAME']; |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | $retval[] = current($row); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 876 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 877 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 878 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 879 | $this->data_cache['field_names'][$table] = $retval; |
| 880 | return $this->data_cache['field_names'][$table]; |
| 881 | } |
| 882 | |
| 883 | // -------------------------------------------------------------------- |
| 884 | |
| 885 | /** |
| 886 | * Determine if a particular field exists |
| 887 | * @access public |
| 888 | * @param string |
| 889 | * @param string |
| 890 | * @return boolean |
| 891 | */ |
| 892 | function field_exists($field_name, $table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 893 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 894 | return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; |
| 895 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 896 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 897 | // -------------------------------------------------------------------- |
| 898 | |
| 899 | /** |
| 900 | * Returns an object with field data |
| 901 | * |
| 902 | * @access public |
| 903 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 904 | * @return object |
| 905 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 906 | function field_data($table = '') |
| 907 | { |
| 908 | if ($table == '') |
| 909 | { |
| 910 | if ($this->db_debug) |
| 911 | { |
| 912 | return $this->display_error('db_field_param_missing'); |
| 913 | } |
| 914 | return FALSE; |
| 915 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 916 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 917 | $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); |
| 918 | |
| 919 | return $query->field_data(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 920 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 921 | |
| 922 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 923 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 924 | /** |
| 925 | * Generate an insert string |
| 926 | * |
| 927 | * @access public |
| 928 | * @param string the table upon which the query will be performed |
| 929 | * @param array an associative array data of key/values |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 930 | * @return string |
| 931 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 932 | function insert_string($table, $data) |
| 933 | { |
| 934 | $fields = array(); |
| 935 | $values = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 936 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 937 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 938 | { |
| 939 | $fields[] = $this->_escape_identifiers($key); |
| 940 | $values[] = $this->escape($val); |
| 941 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 942 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 943 | return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 946 | // -------------------------------------------------------------------- |
| 947 | |
| 948 | /** |
| 949 | * Generate an update string |
| 950 | * |
| 951 | * @access public |
| 952 | * @param string the table upon which the query will be performed |
| 953 | * @param array an associative array data of key/values |
| 954 | * @param mixed the "where" statement |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 955 | * @return string |
| 956 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 957 | function update_string($table, $data, $where) |
| 958 | { |
| 959 | if ($where == '') |
| 960 | { |
| 961 | return false; |
| 962 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 963 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 964 | $fields = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 965 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 966 | { |
| 967 | $fields[$this->_protect_identifiers($key)] = $this->escape($val); |
| 968 | } |
| 969 | |
| 970 | if ( ! is_array($where)) |
| 971 | { |
| 972 | $dest = array($where); |
| 973 | } |
| 974 | else |
| 975 | { |
| 976 | $dest = array(); |
| 977 | foreach ($where as $key => $val) |
| 978 | { |
| 979 | $prefix = (count($dest) == 0) ? '' : ' AND '; |
Andrey Andreev | dc46d99 | 2011-09-24 16:25:23 +0300 | [diff] [blame] | 980 | $key = $this->_protect_identifiers($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 981 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 982 | if ($val !== '') |
| 983 | { |
| 984 | if ( ! $this->_has_operator($key)) |
| 985 | { |
| 986 | $key .= ' ='; |
| 987 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 988 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 989 | $val = ' '.$this->escape($val); |
| 990 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 991 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 992 | $dest[] = $prefix.$key.$val; |
| 993 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 994 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 995 | |
| 996 | return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 997 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 998 | |
| 999 | // -------------------------------------------------------------------- |
| 1000 | |
| 1001 | /** |
| 1002 | * Tests whether the string has an SQL operator |
| 1003 | * |
| 1004 | * @access private |
| 1005 | * @param string |
| 1006 | * @return bool |
| 1007 | */ |
| 1008 | function _has_operator($str) |
| 1009 | { |
| 1010 | $str = trim($str); |
| 1011 | if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) |
| 1012 | { |
| 1013 | return FALSE; |
| 1014 | } |
| 1015 | |
| 1016 | return TRUE; |
| 1017 | } |
| 1018 | |
| 1019 | // -------------------------------------------------------------------- |
| 1020 | |
| 1021 | /** |
| 1022 | * Enables a native PHP function to be run, using a platform agnostic wrapper. |
| 1023 | * |
| 1024 | * @access public |
| 1025 | * @param string the function name |
| 1026 | * @param mixed any parameters needed by the function |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1027 | * @return mixed |
| 1028 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1029 | function call_function($function) |
| 1030 | { |
| 1031 | $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1032 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1033 | if (FALSE === strpos($driver, $function)) |
| 1034 | { |
| 1035 | $function = $driver.$function; |
| 1036 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1037 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1038 | if ( ! function_exists($function)) |
| 1039 | { |
| 1040 | if ($this->db_debug) |
| 1041 | { |
| 1042 | return $this->display_error('db_unsupported_function'); |
| 1043 | } |
| 1044 | return FALSE; |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; |
| 1049 | |
Repox | 71b7809 | 2011-12-01 09:19:43 +0100 | [diff] [blame] | 1050 | if (is_null($args)) |
| 1051 | { |
| 1052 | return call_user_func($function); |
| 1053 | } |
| 1054 | else |
| 1055 | { |
| 1056 | return call_user_func_array($function, $args); |
| 1057 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | // -------------------------------------------------------------------- |
| 1062 | |
| 1063 | /** |
| 1064 | * Set Cache Directory Path |
| 1065 | * |
| 1066 | * @access public |
| 1067 | * @param string the path to the cache directory |
| 1068 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1069 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1070 | function cache_set_path($path = '') |
| 1071 | { |
| 1072 | $this->cachedir = $path; |
| 1073 | } |
| 1074 | |
| 1075 | // -------------------------------------------------------------------- |
| 1076 | |
| 1077 | /** |
| 1078 | * Enable Query Caching |
| 1079 | * |
| 1080 | * @access public |
| 1081 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1082 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1083 | function cache_on() |
| 1084 | { |
| 1085 | $this->cache_on = TRUE; |
| 1086 | return TRUE; |
| 1087 | } |
| 1088 | |
| 1089 | // -------------------------------------------------------------------- |
| 1090 | |
| 1091 | /** |
| 1092 | * Disable Query Caching |
| 1093 | * |
| 1094 | * @access public |
| 1095 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1096 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1097 | function cache_off() |
| 1098 | { |
| 1099 | $this->cache_on = FALSE; |
| 1100 | return FALSE; |
| 1101 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1102 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1103 | |
| 1104 | // -------------------------------------------------------------------- |
| 1105 | |
| 1106 | /** |
| 1107 | * Delete the cache files associated with a particular URI |
| 1108 | * |
| 1109 | * @access public |
| 1110 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1111 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1112 | function cache_delete($segment_one = '', $segment_two = '') |
| 1113 | { |
| 1114 | if ( ! $this->_cache_init()) |
| 1115 | { |
| 1116 | return FALSE; |
| 1117 | } |
| 1118 | return $this->CACHE->delete($segment_one, $segment_two); |
| 1119 | } |
| 1120 | |
| 1121 | // -------------------------------------------------------------------- |
| 1122 | |
| 1123 | /** |
| 1124 | * Delete All cache files |
| 1125 | * |
| 1126 | * @access public |
| 1127 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1128 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1129 | function cache_delete_all() |
| 1130 | { |
| 1131 | if ( ! $this->_cache_init()) |
| 1132 | { |
| 1133 | return FALSE; |
| 1134 | } |
| 1135 | |
| 1136 | return $this->CACHE->delete_all(); |
| 1137 | } |
| 1138 | |
| 1139 | // -------------------------------------------------------------------- |
| 1140 | |
| 1141 | /** |
| 1142 | * Initialize the Cache Class |
| 1143 | * |
| 1144 | * @access private |
| 1145 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1146 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1147 | function _cache_init() |
| 1148 | { |
| 1149 | if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) |
| 1150 | { |
| 1151 | return TRUE; |
| 1152 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1153 | |
| 1154 | if ( ! class_exists('CI_DB_Cache')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1155 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 1156 | if ( ! @include(BASEPATH.'database/DB_cache.php')) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1157 | { |
| 1158 | return $this->cache_off(); |
| 1159 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1160 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1162 | $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects |
| 1163 | return TRUE; |
| 1164 | } |
| 1165 | |
| 1166 | // -------------------------------------------------------------------- |
| 1167 | |
| 1168 | /** |
| 1169 | * Close DB Connection |
| 1170 | * |
| 1171 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1172 | * @return void |
| 1173 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1174 | function close() |
| 1175 | { |
| 1176 | if (is_resource($this->conn_id) OR is_object($this->conn_id)) |
| 1177 | { |
| 1178 | $this->_close($this->conn_id); |
| 1179 | } |
| 1180 | $this->conn_id = FALSE; |
| 1181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1183 | // -------------------------------------------------------------------- |
| 1184 | |
| 1185 | /** |
| 1186 | * Display an error message |
| 1187 | * |
| 1188 | * @access public |
| 1189 | * @param string the error message |
| 1190 | * @param string any "swap" values |
| 1191 | * @param boolean whether to localize the message |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1192 | * @return string sends the application/error_db.php template |
| 1193 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1194 | function display_error($error = '', $swap = '', $native = FALSE) |
| 1195 | { |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1196 | $LANG =& load_class('Lang', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1197 | $LANG->load('db'); |
| 1198 | |
| 1199 | $heading = $LANG->line('db_error_heading'); |
| 1200 | |
| 1201 | if ($native == TRUE) |
| 1202 | { |
Andrey Andreev | 85a99cc | 2011-09-24 17:17:37 +0300 | [diff] [blame] | 1203 | $message = (array) $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1204 | } |
| 1205 | else |
| 1206 | { |
| 1207 | $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; |
| 1208 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1209 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1210 | // Find the most likely culprit of the error by going through |
| 1211 | // the backtrace until the source file is no longer in the |
| 1212 | // database folder. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1213 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1214 | $trace = debug_backtrace(); |
| 1215 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1216 | foreach ($trace as $call) |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1217 | { |
| 1218 | if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE) |
| 1219 | { |
| 1220 | // Found it - use a relative path for safety |
| 1221 | $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']); |
| 1222 | $message[] = 'Line Number: '.$call['line']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1223 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1224 | break; |
| 1225 | } |
| 1226 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1227 | |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1228 | $error =& load_class('Exceptions', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1229 | echo $error->show_error($heading, $message, 'error_db'); |
| 1230 | exit; |
| 1231 | } |
| 1232 | |
| 1233 | // -------------------------------------------------------------------- |
| 1234 | |
| 1235 | /** |
| 1236 | * Protect Identifiers |
| 1237 | * |
| 1238 | * This function adds backticks if appropriate based on db type |
| 1239 | * |
| 1240 | * @access private |
| 1241 | * @param mixed the item to escape |
| 1242 | * @return mixed the item with backticks |
| 1243 | */ |
| 1244 | function protect_identifiers($item, $prefix_single = FALSE) |
| 1245 | { |
| 1246 | return $this->_protect_identifiers($item, $prefix_single); |
| 1247 | } |
| 1248 | |
| 1249 | // -------------------------------------------------------------------- |
| 1250 | |
| 1251 | /** |
| 1252 | * Protect Identifiers |
| 1253 | * |
| 1254 | * This function is used extensively by the Active Record class, and by |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1255 | * a couple functions in this class. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1256 | * It takes a column or table name (optionally with an alias) and inserts |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1257 | * the table prefix onto it. Some logic is necessary in order to deal with |
| 1258 | * column names that include the path. Consider a query like this: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1259 | * |
| 1260 | * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table |
| 1261 | * |
| 1262 | * Or a query with aliasing: |
| 1263 | * |
| 1264 | * SELECT m.member_id, m.member_name FROM members AS m |
| 1265 | * |
| 1266 | * Since the column name can include up to four segments (host, DB, table, column) |
| 1267 | * or also have an alias prefix, we need to do a bit of work to figure this out and |
| 1268 | * insert the table prefix (if it exists) in the proper position, and escape only |
| 1269 | * the correct identifiers. |
| 1270 | * |
| 1271 | * @access private |
| 1272 | * @param string |
| 1273 | * @param bool |
| 1274 | * @param mixed |
| 1275 | * @param bool |
| 1276 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1277 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) |
| 1279 | { |
| 1280 | if ( ! is_bool($protect_identifiers)) |
| 1281 | { |
| 1282 | $protect_identifiers = $this->_protect_identifiers; |
| 1283 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (is_array($item)) |
| 1286 | { |
| 1287 | $escaped_array = array(); |
| 1288 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1289 | foreach ($item as $k => $v) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1290 | { |
| 1291 | $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v); |
| 1292 | } |
| 1293 | |
| 1294 | return $escaped_array; |
| 1295 | } |
| 1296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1297 | // Convert tabs or multiple spaces into single spaces |
Derek Jones | 7b3b96c | 2009-02-10 21:01:47 +0000 | [diff] [blame] | 1298 | $item = preg_replace('/[\t ]+/', ' ', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1299 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1300 | // If the item has an alias declaration we remove it and set it aside. |
| 1301 | // Basically we remove everything to the right of the first space |
| 1302 | $alias = ''; |
| 1303 | if (strpos($item, ' ') !== FALSE) |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1304 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1305 | $alias = strstr($item, " "); |
| 1306 | $item = substr($item, 0, - strlen($alias)); |
| 1307 | } |
| 1308 | |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1309 | // This is basically a bug fix for queries that use MAX, MIN, etc. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1310 | // If a parenthesis is found we know that we do not need to |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1311 | // escape the data or add a prefix. There's probably a more graceful |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1312 | // way to deal with this, but I'm not thinking of it -- Rick |
| 1313 | if (strpos($item, '(') !== FALSE) |
| 1314 | { |
| 1315 | return $item.$alias; |
| 1316 | } |
| 1317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1318 | // Break the string apart if it contains periods, then insert the table prefix |
| 1319 | // in the correct location, assuming the period doesn't indicate that we're dealing |
| 1320 | // with an alias. While we're at it, we will escape the components |
| 1321 | if (strpos($item, '.') !== FALSE) |
| 1322 | { |
| 1323 | $parts = explode('.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1324 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1325 | // Does the first segment of the exploded item match |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1326 | // one of the aliases previously identified? If so, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1327 | // we have nothing more to do other than escape the item |
| 1328 | if (in_array($parts[0], $this->ar_aliased_tables)) |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1329 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1330 | if ($protect_identifiers === TRUE) |
| 1331 | { |
| 1332 | foreach ($parts as $key => $val) |
| 1333 | { |
| 1334 | if ( ! in_array($val, $this->_reserved_identifiers)) |
| 1335 | { |
| 1336 | $parts[$key] = $this->_escape_identifiers($val); |
| 1337 | } |
| 1338 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1339 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1340 | $item = implode('.', $parts); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1341 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1342 | return $item.$alias; |
| 1343 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1344 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1345 | // Is there a table prefix defined in the config file? If not, no need to do anything |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1346 | if ($this->dbprefix != '') |
| 1347 | { |
| 1348 | // We now add the table prefix based on some logic. |
| 1349 | // Do we have 4 segments (hostname.database.table.column)? |
| 1350 | // If so, we add the table prefix to the column name in the 3rd segment. |
| 1351 | if (isset($parts[3])) |
| 1352 | { |
| 1353 | $i = 2; |
| 1354 | } |
| 1355 | // Do we have 3 segments (database.table.column)? |
| 1356 | // If so, we add the table prefix to the column name in 2nd position |
| 1357 | elseif (isset($parts[2])) |
| 1358 | { |
| 1359 | $i = 1; |
| 1360 | } |
| 1361 | // Do we have 2 segments (table.column)? |
| 1362 | // If so, we add the table prefix to the column name in 1st segment |
| 1363 | else |
| 1364 | { |
| 1365 | $i = 0; |
| 1366 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1367 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1368 | // This flag is set when the supplied $item does not contain a field name. |
| 1369 | // This can happen when this function is being called from a JOIN. |
| 1370 | if ($field_exists == FALSE) |
| 1371 | { |
| 1372 | $i++; |
| 1373 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1374 | |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1375 | // Verify table prefix and replace if necessary |
| 1376 | if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0) |
| 1377 | { |
| 1378 | $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]); |
| 1379 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1380 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1381 | // We only add the table prefix if it does not already exist |
| 1382 | if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) |
| 1383 | { |
| 1384 | $parts[$i] = $this->dbprefix.$parts[$i]; |
| 1385 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1386 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1387 | // Put the parts back together |
| 1388 | $item = implode('.', $parts); |
| 1389 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1390 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1391 | if ($protect_identifiers === TRUE) |
| 1392 | { |
| 1393 | $item = $this->_escape_identifiers($item); |
| 1394 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1395 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1396 | return $item.$alias; |
| 1397 | } |
| 1398 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1399 | // Is there a table prefix? If not, no need to insert it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1400 | if ($this->dbprefix != '') |
| 1401 | { |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1402 | // Verify table prefix and replace if necessary |
| 1403 | if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0) |
| 1404 | { |
| 1405 | $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item); |
| 1406 | } |
| 1407 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1408 | // Do we prefix an item with no segments? |
| 1409 | if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) |
| 1410 | { |
| 1411 | $item = $this->dbprefix.$item; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1412 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) |
| 1416 | { |
| 1417 | $item = $this->_escape_identifiers($item); |
| 1418 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1419 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1420 | return $item.$alias; |
| 1421 | } |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1422 | |
| 1423 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1424 | |
Túbal Martín | 511f225 | 2011-11-24 14:43:45 +0100 | [diff] [blame] | 1425 | /** |
| 1426 | * Dummy method that allows Active Record class to be disabled |
| 1427 | * |
| 1428 | * This function is used extensively by every db driver. |
| 1429 | * |
| 1430 | * @access private |
| 1431 | * @return void |
| 1432 | */ |
| 1433 | protected function _reset_select() |
| 1434 | { |
| 1435 | |
| 1436 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1437 | |
| 1438 | } |
| 1439 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1440 | /* End of file DB_driver.php */ |
Andrey Andreev | 67f71a4 | 2012-03-01 16:18:42 +0200 | [diff] [blame^] | 1441 | /* Location: ./system/database/DB_driver.php */ |