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