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