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