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