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