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); |
| 525 | } |
| 526 | |
| 527 | // -------------------------------------------------------------------- |
| 528 | |
| 529 | /** |
| 530 | * Complete Transaction |
| 531 | * |
| 532 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 533 | * @return bool |
| 534 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | function trans_complete() |
| 536 | { |
| 537 | if ( ! $this->trans_enabled) |
| 538 | { |
| 539 | return FALSE; |
| 540 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 543 | if ($this->_trans_depth > 1) |
| 544 | { |
| 545 | $this->_trans_depth -= 1; |
| 546 | return TRUE; |
| 547 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 548 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | // The query() function will set this flag to FALSE in the event that a query failed |
| 550 | if ($this->_trans_status === FALSE) |
| 551 | { |
| 552 | $this->trans_rollback(); |
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 | // If we are NOT running in strict mode, we will reset |
| 555 | // the _trans_status flag so that subsequent groups of transactions |
| 556 | // will be permitted. |
| 557 | if ($this->trans_strict === FALSE) |
| 558 | { |
| 559 | $this->_trans_status = TRUE; |
| 560 | } |
| 561 | |
| 562 | log_message('debug', 'DB Transaction Failure'); |
| 563 | return FALSE; |
| 564 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | $this->trans_commit(); |
| 567 | return TRUE; |
| 568 | } |
| 569 | |
| 570 | // -------------------------------------------------------------------- |
| 571 | |
| 572 | /** |
| 573 | * Lets you retrieve the transaction flag to determine if it has failed |
| 574 | * |
| 575 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 576 | * @return bool |
| 577 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | function trans_status() |
| 579 | { |
| 580 | return $this->_trans_status; |
| 581 | } |
| 582 | |
| 583 | // -------------------------------------------------------------------- |
| 584 | |
| 585 | /** |
| 586 | * Compile Bindings |
| 587 | * |
| 588 | * @access public |
| 589 | * @param string the sql statement |
| 590 | * @param array an array of bind data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 591 | * @return string |
| 592 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | function compile_binds($sql, $binds) |
| 594 | { |
| 595 | if (strpos($sql, $this->bind_marker) === FALSE) |
| 596 | { |
| 597 | return $sql; |
| 598 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 599 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 600 | if ( ! is_array($binds)) |
| 601 | { |
| 602 | $binds = array($binds); |
| 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 | // Get the sql segments around the bind markers |
| 606 | $segments = explode($this->bind_marker, $sql); |
| 607 | |
| 608 | // The count of bind should be 1 less then the count of segments |
| 609 | // If there are more bind arguments trim it down |
| 610 | if (count($binds) >= count($segments)) { |
| 611 | $binds = array_slice($binds, 0, count($segments)-1); |
| 612 | } |
| 613 | |
| 614 | // Construct the binded query |
| 615 | $result = $segments[0]; |
| 616 | $i = 0; |
| 617 | foreach ($binds as $bind) |
| 618 | { |
| 619 | $result .= $this->escape($bind); |
| 620 | $result .= $segments[++$i]; |
| 621 | } |
| 622 | |
| 623 | return $result; |
| 624 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 625 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | // -------------------------------------------------------------------- |
| 627 | |
| 628 | /** |
| 629 | * Determines if a query is a "write" type. |
| 630 | * |
| 631 | * @access public |
| 632 | * @param string An SQL query string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 633 | * @return boolean |
| 634 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 635 | function is_write_type($sql) |
| 636 | { |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 637 | 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] | 638 | { |
| 639 | return FALSE; |
| 640 | } |
| 641 | return TRUE; |
| 642 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 643 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | // -------------------------------------------------------------------- |
| 645 | |
| 646 | /** |
| 647 | * Calculate the aggregate query elapsed time |
| 648 | * |
| 649 | * @access public |
| 650 | * @param integer The number of decimal places |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 651 | * @return integer |
| 652 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 653 | function elapsed_time($decimals = 6) |
| 654 | { |
| 655 | return number_format($this->benchmark, $decimals); |
| 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 | * Returns the total number of queries |
| 662 | * |
| 663 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 664 | * @return integer |
| 665 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 666 | function total_queries() |
| 667 | { |
| 668 | return $this->query_count; |
| 669 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 670 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 671 | // -------------------------------------------------------------------- |
| 672 | |
| 673 | /** |
| 674 | * Returns the last query that was executed |
| 675 | * |
| 676 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 677 | * @return void |
| 678 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 679 | function last_query() |
| 680 | { |
| 681 | return end($this->queries); |
| 682 | } |
| 683 | |
| 684 | // -------------------------------------------------------------------- |
| 685 | |
| 686 | /** |
| 687 | * "Smart" Escape String |
| 688 | * |
| 689 | * Escapes data based on type |
| 690 | * Sets boolean and null types |
| 691 | * |
| 692 | * @access public |
| 693 | * @param string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 694 | * @return mixed |
| 695 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 696 | function escape($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 697 | { |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 698 | if (is_string($str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 699 | { |
Derek Jones | a377bdd | 2009-02-11 18:55:24 +0000 | [diff] [blame] | 700 | $str = "'".$this->escape_str($str)."'"; |
| 701 | } |
| 702 | elseif (is_bool($str)) |
| 703 | { |
| 704 | $str = ($str === FALSE) ? 0 : 1; |
| 705 | } |
| 706 | elseif (is_null($str)) |
| 707 | { |
| 708 | $str = 'NULL'; |
| 709 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 710 | |
| 711 | return $str; |
| 712 | } |
| 713 | |
| 714 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 715 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 716 | /** |
Derek Jones | bdc7fb9 | 2009-02-20 21:55:10 +0000 | [diff] [blame] | 717 | * Escape LIKE String |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 718 | * |
| 719 | * Calls the individual driver for platform |
| 720 | * specific escaping for LIKE conditions |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 721 | * |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 722 | * @access public |
| 723 | * @param string |
| 724 | * @return mixed |
| 725 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 726 | function escape_like_str($str) |
| 727 | { |
| 728 | return $this->escape_str($str, TRUE); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 729 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 731 | // -------------------------------------------------------------------- |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 732 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 733 | /** |
| 734 | * Primary |
| 735 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 736 | * Retrieves the primary key. It assumes that the row in the first |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 737 | * position is the primary key |
| 738 | * |
| 739 | * @access public |
| 740 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 741 | * @return string |
| 742 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 743 | function primary($table = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 744 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 745 | $fields = $this->list_fields($table); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 746 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 747 | if ( ! is_array($fields)) |
| 748 | { |
| 749 | return FALSE; |
| 750 | } |
| 751 | |
| 752 | return current($fields); |
| 753 | } |
| 754 | |
| 755 | // -------------------------------------------------------------------- |
| 756 | |
| 757 | /** |
| 758 | * Returns an array of table names |
| 759 | * |
| 760 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 761 | * @return array |
| 762 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 763 | function list_tables($constrain_by_prefix = FALSE) |
| 764 | { |
| 765 | // Is there a cached result? |
| 766 | if (isset($this->data_cache['table_names'])) |
| 767 | { |
| 768 | return $this->data_cache['table_names']; |
| 769 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 770 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 771 | if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) |
| 772 | { |
| 773 | if ($this->db_debug) |
| 774 | { |
| 775 | return $this->display_error('db_unsupported_function'); |
| 776 | } |
| 777 | return FALSE; |
| 778 | } |
| 779 | |
| 780 | $retval = array(); |
| 781 | $query = $this->query($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 782 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | if ($query->num_rows() > 0) |
| 784 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 785 | foreach ($query->result_array() as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 786 | { |
| 787 | if (isset($row['TABLE_NAME'])) |
| 788 | { |
| 789 | $retval[] = $row['TABLE_NAME']; |
| 790 | } |
| 791 | else |
| 792 | { |
| 793 | $retval[] = array_shift($row); |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | $this->data_cache['table_names'] = $retval; |
| 799 | return $this->data_cache['table_names']; |
| 800 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 801 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 802 | // -------------------------------------------------------------------- |
| 803 | |
| 804 | /** |
| 805 | * Determine if a particular table exists |
| 806 | * @access public |
| 807 | * @return boolean |
| 808 | */ |
| 809 | function table_exists($table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 810 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 811 | return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; |
| 812 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 813 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 814 | // -------------------------------------------------------------------- |
| 815 | |
| 816 | /** |
| 817 | * Fetch MySQL Field Names |
| 818 | * |
| 819 | * @access public |
| 820 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 821 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 822 | */ |
| 823 | function list_fields($table = '') |
| 824 | { |
| 825 | // Is there a cached result? |
| 826 | if (isset($this->data_cache['field_names'][$table])) |
| 827 | { |
| 828 | return $this->data_cache['field_names'][$table]; |
| 829 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 830 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 831 | if ($table == '') |
| 832 | { |
| 833 | if ($this->db_debug) |
| 834 | { |
| 835 | return $this->display_error('db_field_param_missing'); |
| 836 | } |
| 837 | return FALSE; |
| 838 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 839 | |
Greg Aker | 1edde30 | 2010-01-26 00:17:01 +0000 | [diff] [blame] | 840 | if (FALSE === ($sql = $this->_list_columns($table))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 841 | { |
| 842 | if ($this->db_debug) |
| 843 | { |
| 844 | return $this->display_error('db_unsupported_function'); |
| 845 | } |
| 846 | return FALSE; |
| 847 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 848 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 849 | $query = $this->query($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 850 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 851 | $retval = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 852 | foreach ($query->result_array() as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 853 | { |
| 854 | if (isset($row['COLUMN_NAME'])) |
| 855 | { |
| 856 | $retval[] = $row['COLUMN_NAME']; |
| 857 | } |
| 858 | else |
| 859 | { |
| 860 | $retval[] = current($row); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 861 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 862 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 863 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 864 | $this->data_cache['field_names'][$table] = $retval; |
| 865 | return $this->data_cache['field_names'][$table]; |
| 866 | } |
| 867 | |
| 868 | // -------------------------------------------------------------------- |
| 869 | |
| 870 | /** |
| 871 | * Determine if a particular field exists |
| 872 | * @access public |
| 873 | * @param string |
| 874 | * @param string |
| 875 | * @return boolean |
| 876 | */ |
| 877 | function field_exists($field_name, $table_name) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 878 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 879 | return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; |
| 880 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 881 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 882 | // -------------------------------------------------------------------- |
| 883 | |
| 884 | /** |
| 885 | * Returns an object with field data |
| 886 | * |
| 887 | * @access public |
| 888 | * @param string the table name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 889 | * @return object |
| 890 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 891 | function field_data($table = '') |
| 892 | { |
| 893 | if ($table == '') |
| 894 | { |
| 895 | if ($this->db_debug) |
| 896 | { |
| 897 | return $this->display_error('db_field_param_missing'); |
| 898 | } |
| 899 | return FALSE; |
| 900 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 901 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 902 | $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); |
| 903 | |
| 904 | return $query->field_data(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 905 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 906 | |
| 907 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 908 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 909 | /** |
| 910 | * Generate an insert string |
| 911 | * |
| 912 | * @access public |
| 913 | * @param string the table upon which the query will be performed |
| 914 | * @param array an associative array data of key/values |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 915 | * @return string |
| 916 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 917 | function insert_string($table, $data) |
| 918 | { |
| 919 | $fields = array(); |
| 920 | $values = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 921 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 922 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 923 | { |
| 924 | $fields[] = $this->_escape_identifiers($key); |
| 925 | $values[] = $this->escape($val); |
| 926 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 927 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 928 | return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 931 | // -------------------------------------------------------------------- |
| 932 | |
| 933 | /** |
| 934 | * Generate an update string |
| 935 | * |
| 936 | * @access public |
| 937 | * @param string the table upon which the query will be performed |
| 938 | * @param array an associative array data of key/values |
| 939 | * @param mixed the "where" statement |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 940 | * @return string |
| 941 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 942 | function update_string($table, $data, $where) |
| 943 | { |
| 944 | if ($where == '') |
| 945 | { |
| 946 | return false; |
| 947 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 948 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 949 | $fields = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 950 | foreach ($data as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 951 | { |
| 952 | $fields[$this->_protect_identifiers($key)] = $this->escape($val); |
| 953 | } |
| 954 | |
| 955 | if ( ! is_array($where)) |
| 956 | { |
| 957 | $dest = array($where); |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | $dest = array(); |
| 962 | foreach ($where as $key => $val) |
| 963 | { |
| 964 | $prefix = (count($dest) == 0) ? '' : ' AND '; |
Andrey Andreev | dc46d99 | 2011-09-24 16:25:23 +0300 | [diff] [blame] | 965 | $key = $this->_protect_identifiers($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 966 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | if ($val !== '') |
| 968 | { |
| 969 | if ( ! $this->_has_operator($key)) |
| 970 | { |
| 971 | $key .= ' ='; |
| 972 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 973 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 974 | $val = ' '.$this->escape($val); |
| 975 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 976 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 977 | $dest[] = $prefix.$key.$val; |
| 978 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 979 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 980 | |
| 981 | return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 982 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 983 | |
| 984 | // -------------------------------------------------------------------- |
| 985 | |
| 986 | /** |
| 987 | * Tests whether the string has an SQL operator |
| 988 | * |
| 989 | * @access private |
| 990 | * @param string |
| 991 | * @return bool |
| 992 | */ |
| 993 | function _has_operator($str) |
| 994 | { |
| 995 | $str = trim($str); |
| 996 | if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) |
| 997 | { |
| 998 | return FALSE; |
| 999 | } |
| 1000 | |
| 1001 | return TRUE; |
| 1002 | } |
| 1003 | |
| 1004 | // -------------------------------------------------------------------- |
| 1005 | |
| 1006 | /** |
| 1007 | * Enables a native PHP function to be run, using a platform agnostic wrapper. |
| 1008 | * |
| 1009 | * @access public |
| 1010 | * @param string the function name |
| 1011 | * @param mixed any parameters needed by the function |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1012 | * @return mixed |
| 1013 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1014 | function call_function($function) |
| 1015 | { |
| 1016 | $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1017 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1018 | if (FALSE === strpos($driver, $function)) |
| 1019 | { |
| 1020 | $function = $driver.$function; |
| 1021 | } |
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 ( ! function_exists($function)) |
| 1024 | { |
| 1025 | if ($this->db_debug) |
| 1026 | { |
| 1027 | return $this->display_error('db_unsupported_function'); |
| 1028 | } |
| 1029 | return FALSE; |
| 1030 | } |
| 1031 | else |
| 1032 | { |
| 1033 | $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; |
| 1034 | |
| 1035 | return call_user_func_array($function, $args); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // -------------------------------------------------------------------- |
| 1040 | |
| 1041 | /** |
| 1042 | * Set Cache Directory Path |
| 1043 | * |
| 1044 | * @access public |
| 1045 | * @param string the path to the cache directory |
| 1046 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1047 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1048 | function cache_set_path($path = '') |
| 1049 | { |
| 1050 | $this->cachedir = $path; |
| 1051 | } |
| 1052 | |
| 1053 | // -------------------------------------------------------------------- |
| 1054 | |
| 1055 | /** |
| 1056 | * Enable Query Caching |
| 1057 | * |
| 1058 | * @access public |
| 1059 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1060 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1061 | function cache_on() |
| 1062 | { |
| 1063 | $this->cache_on = TRUE; |
| 1064 | return TRUE; |
| 1065 | } |
| 1066 | |
| 1067 | // -------------------------------------------------------------------- |
| 1068 | |
| 1069 | /** |
| 1070 | * Disable Query Caching |
| 1071 | * |
| 1072 | * @access public |
| 1073 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1074 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1075 | function cache_off() |
| 1076 | { |
| 1077 | $this->cache_on = FALSE; |
| 1078 | return FALSE; |
| 1079 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1080 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1081 | |
| 1082 | // -------------------------------------------------------------------- |
| 1083 | |
| 1084 | /** |
| 1085 | * Delete the cache files associated with a particular URI |
| 1086 | * |
| 1087 | * @access public |
| 1088 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1089 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1090 | function cache_delete($segment_one = '', $segment_two = '') |
| 1091 | { |
| 1092 | if ( ! $this->_cache_init()) |
| 1093 | { |
| 1094 | return FALSE; |
| 1095 | } |
| 1096 | return $this->CACHE->delete($segment_one, $segment_two); |
| 1097 | } |
| 1098 | |
| 1099 | // -------------------------------------------------------------------- |
| 1100 | |
| 1101 | /** |
| 1102 | * Delete All cache files |
| 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_delete_all() |
| 1108 | { |
| 1109 | if ( ! $this->_cache_init()) |
| 1110 | { |
| 1111 | return FALSE; |
| 1112 | } |
| 1113 | |
| 1114 | return $this->CACHE->delete_all(); |
| 1115 | } |
| 1116 | |
| 1117 | // -------------------------------------------------------------------- |
| 1118 | |
| 1119 | /** |
| 1120 | * Initialize the Cache Class |
| 1121 | * |
| 1122 | * @access private |
| 1123 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1124 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1125 | function _cache_init() |
| 1126 | { |
| 1127 | if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) |
| 1128 | { |
| 1129 | return TRUE; |
| 1130 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1131 | |
| 1132 | if ( ! class_exists('CI_DB_Cache')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1133 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 1134 | if ( ! @include(BASEPATH.'database/DB_cache.php')) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1135 | { |
| 1136 | return $this->cache_off(); |
| 1137 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1138 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1140 | $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects |
| 1141 | return TRUE; |
| 1142 | } |
| 1143 | |
| 1144 | // -------------------------------------------------------------------- |
| 1145 | |
| 1146 | /** |
| 1147 | * Close DB Connection |
| 1148 | * |
| 1149 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1150 | * @return void |
| 1151 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1152 | function close() |
| 1153 | { |
| 1154 | if (is_resource($this->conn_id) OR is_object($this->conn_id)) |
| 1155 | { |
| 1156 | $this->_close($this->conn_id); |
| 1157 | } |
| 1158 | $this->conn_id = FALSE; |
| 1159 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1161 | // -------------------------------------------------------------------- |
| 1162 | |
| 1163 | /** |
| 1164 | * Display an error message |
| 1165 | * |
| 1166 | * @access public |
| 1167 | * @param string the error message |
| 1168 | * @param string any "swap" values |
| 1169 | * @param boolean whether to localize the message |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1170 | * @return string sends the application/error_db.php template |
| 1171 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1172 | function display_error($error = '', $swap = '', $native = FALSE) |
| 1173 | { |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1174 | $LANG =& load_class('Lang', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1175 | $LANG->load('db'); |
| 1176 | |
| 1177 | $heading = $LANG->line('db_error_heading'); |
| 1178 | |
| 1179 | if ($native == TRUE) |
| 1180 | { |
Andrey Andreev | 85a99cc | 2011-09-24 17:17:37 +0300 | [diff] [blame] | 1181 | $message = (array) $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1182 | } |
| 1183 | else |
| 1184 | { |
| 1185 | $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; |
| 1186 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1187 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1188 | // Find the most likely culprit of the error by going through |
| 1189 | // the backtrace until the source file is no longer in the |
| 1190 | // database folder. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1191 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1192 | $trace = debug_backtrace(); |
| 1193 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1194 | foreach ($trace as $call) |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1195 | { |
| 1196 | if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE) |
| 1197 | { |
| 1198 | // Found it - use a relative path for safety |
| 1199 | $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']); |
| 1200 | $message[] = 'Line Number: '.$call['line']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1201 | |
Pascal Kriete | 60f8c39 | 2010-08-25 18:03:28 +0200 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1205 | |
Derek Jones | e779220 | 2010-03-02 17:24:46 -0600 | [diff] [blame] | 1206 | $error =& load_class('Exceptions', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1207 | echo $error->show_error($heading, $message, 'error_db'); |
| 1208 | exit; |
| 1209 | } |
| 1210 | |
| 1211 | // -------------------------------------------------------------------- |
| 1212 | |
| 1213 | /** |
| 1214 | * Protect Identifiers |
| 1215 | * |
| 1216 | * This function adds backticks if appropriate based on db type |
| 1217 | * |
| 1218 | * @access private |
| 1219 | * @param mixed the item to escape |
| 1220 | * @return mixed the item with backticks |
| 1221 | */ |
| 1222 | function protect_identifiers($item, $prefix_single = FALSE) |
| 1223 | { |
| 1224 | return $this->_protect_identifiers($item, $prefix_single); |
| 1225 | } |
| 1226 | |
| 1227 | // -------------------------------------------------------------------- |
| 1228 | |
| 1229 | /** |
| 1230 | * Protect Identifiers |
| 1231 | * |
| 1232 | * This function is used extensively by the Active Record class, and by |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1233 | * a couple functions in this class. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1234 | * 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] | 1235 | * the table prefix onto it. Some logic is necessary in order to deal with |
| 1236 | * column names that include the path. Consider a query like this: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1237 | * |
| 1238 | * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table |
| 1239 | * |
| 1240 | * Or a query with aliasing: |
| 1241 | * |
| 1242 | * SELECT m.member_id, m.member_name FROM members AS m |
| 1243 | * |
| 1244 | * Since the column name can include up to four segments (host, DB, table, column) |
| 1245 | * or also have an alias prefix, we need to do a bit of work to figure this out and |
| 1246 | * insert the table prefix (if it exists) in the proper position, and escape only |
| 1247 | * the correct identifiers. |
| 1248 | * |
| 1249 | * @access private |
| 1250 | * @param string |
| 1251 | * @param bool |
| 1252 | * @param mixed |
| 1253 | * @param bool |
| 1254 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1255 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1256 | function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) |
| 1257 | { |
| 1258 | if ( ! is_bool($protect_identifiers)) |
| 1259 | { |
| 1260 | $protect_identifiers = $this->_protect_identifiers; |
| 1261 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (is_array($item)) |
| 1264 | { |
| 1265 | $escaped_array = array(); |
| 1266 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1267 | foreach ($item as $k => $v) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 1268 | { |
| 1269 | $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v); |
| 1270 | } |
| 1271 | |
| 1272 | return $escaped_array; |
| 1273 | } |
| 1274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1275 | // Convert tabs or multiple spaces into single spaces |
Derek Jones | 7b3b96c | 2009-02-10 21:01:47 +0000 | [diff] [blame] | 1276 | $item = preg_replace('/[\t ]+/', ' ', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | // If the item has an alias declaration we remove it and set it aside. |
| 1279 | // Basically we remove everything to the right of the first space |
| 1280 | $alias = ''; |
| 1281 | if (strpos($item, ' ') !== FALSE) |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1282 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1283 | $alias = strstr($item, " "); |
| 1284 | $item = substr($item, 0, - strlen($alias)); |
| 1285 | } |
| 1286 | |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1287 | // 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] | 1288 | // 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] | 1289 | // 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] | 1290 | // way to deal with this, but I'm not thinking of it -- Rick |
| 1291 | if (strpos($item, '(') !== FALSE) |
| 1292 | { |
| 1293 | return $item.$alias; |
| 1294 | } |
| 1295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1296 | // Break the string apart if it contains periods, then insert the table prefix |
| 1297 | // in the correct location, assuming the period doesn't indicate that we're dealing |
| 1298 | // with an alias. While we're at it, we will escape the components |
| 1299 | if (strpos($item, '.') !== FALSE) |
| 1300 | { |
| 1301 | $parts = explode('.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1302 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1303 | // Does the first segment of the exploded item match |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1304 | // one of the aliases previously identified? If so, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1305 | // we have nothing more to do other than escape the item |
| 1306 | if (in_array($parts[0], $this->ar_aliased_tables)) |
Derek Allard | 911d3e0 | 2008-12-15 14:08:35 +0000 | [diff] [blame] | 1307 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1308 | if ($protect_identifiers === TRUE) |
| 1309 | { |
| 1310 | foreach ($parts as $key => $val) |
| 1311 | { |
| 1312 | if ( ! in_array($val, $this->_reserved_identifiers)) |
| 1313 | { |
| 1314 | $parts[$key] = $this->_escape_identifiers($val); |
| 1315 | } |
| 1316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1318 | $item = implode('.', $parts); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1319 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1320 | return $item.$alias; |
| 1321 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1322 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1323 | // 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] | 1324 | if ($this->dbprefix != '') |
| 1325 | { |
| 1326 | // We now add the table prefix based on some logic. |
| 1327 | // Do we have 4 segments (hostname.database.table.column)? |
| 1328 | // If so, we add the table prefix to the column name in the 3rd segment. |
| 1329 | if (isset($parts[3])) |
| 1330 | { |
| 1331 | $i = 2; |
| 1332 | } |
| 1333 | // Do we have 3 segments (database.table.column)? |
| 1334 | // If so, we add the table prefix to the column name in 2nd position |
| 1335 | elseif (isset($parts[2])) |
| 1336 | { |
| 1337 | $i = 1; |
| 1338 | } |
| 1339 | // Do we have 2 segments (table.column)? |
| 1340 | // If so, we add the table prefix to the column name in 1st segment |
| 1341 | else |
| 1342 | { |
| 1343 | $i = 0; |
| 1344 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1345 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1346 | // This flag is set when the supplied $item does not contain a field name. |
| 1347 | // This can happen when this function is being called from a JOIN. |
| 1348 | if ($field_exists == FALSE) |
| 1349 | { |
| 1350 | $i++; |
| 1351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1352 | |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1353 | // Verify table prefix and replace if necessary |
| 1354 | if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0) |
| 1355 | { |
| 1356 | $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]); |
| 1357 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1358 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1359 | // We only add the table prefix if it does not already exist |
| 1360 | if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) |
| 1361 | { |
| 1362 | $parts[$i] = $this->dbprefix.$parts[$i]; |
| 1363 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1365 | // Put the parts back together |
| 1366 | $item = implode('.', $parts); |
| 1367 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1368 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1369 | if ($protect_identifiers === TRUE) |
| 1370 | { |
| 1371 | $item = $this->_escape_identifiers($item); |
| 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 | return $item.$alias; |
| 1375 | } |
| 1376 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1377 | // Is there a table prefix? If not, no need to insert it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1378 | if ($this->dbprefix != '') |
| 1379 | { |
Derek Jones | 55acc8b | 2009-07-11 03:38:13 +0000 | [diff] [blame] | 1380 | // Verify table prefix and replace if necessary |
| 1381 | if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0) |
| 1382 | { |
| 1383 | $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item); |
| 1384 | } |
| 1385 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1386 | // Do we prefix an item with no segments? |
| 1387 | if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) |
| 1388 | { |
| 1389 | $item = $this->dbprefix.$item; |
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 | } |
| 1392 | |
| 1393 | if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) |
| 1394 | { |
| 1395 | $item = $this->_escape_identifiers($item); |
| 1396 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1397 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1398 | return $item.$alias; |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | /* End of file DB_driver.php */ |
Andrey Andreev | dc46d99 | 2011-09-24 16:25:23 +0300 | [diff] [blame] | 1406 | /* Location: ./system/database/DB_driver.php */ |