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