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