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