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