admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Database Driver Class |
| 20 | * |
| 21 | * This is the platform-independent base DB implementation class. |
| 22 | * This class will not be called directly. Rather, the adapter |
| 23 | * class for the specific database will extend and instantiate it. |
| 24 | * |
| 25 | * @package CodeIgniter |
| 26 | * @subpackage Drivers |
| 27 | * @category Database |
| 28 | * @author Rick Ellis |
| 29 | * @link http://www.codeigniter.com/user_guide/database/ |
| 30 | */ |
| 31 | class CI_DB_driver { |
| 32 | |
| 33 | var $username; |
| 34 | var $password; |
| 35 | var $hostname; |
| 36 | var $database; |
| 37 | var $dbdriver = 'mysql'; |
| 38 | var $dbprefix = ''; |
| 39 | var $port = ''; |
| 40 | var $pconnect = FALSE; |
| 41 | var $conn_id = FALSE; |
| 42 | var $result_id = FALSE; |
| 43 | var $db_debug = FALSE; |
| 44 | var $benchmark = 0; |
| 45 | var $query_count = 0; |
| 46 | var $bind_marker = '?'; |
| 47 | var $queries = array(); |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 48 | var $data_cache = array(); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 49 | var $trans_enabled = TRUE; |
| 50 | var $_trans_depth = 0; |
| 51 | var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 52 | var $cache_on = FALSE; |
| 53 | var $cachedir = ''; |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 54 | var $cache; // The cache class object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 55 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 56 | |
| 57 | // These are use with Oracle |
| 58 | var $stmt_id; |
| 59 | var $curs_id; |
| 60 | var $limit_used; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 61 | |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 62 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Constructor. Accepts one parameter containing the database |
| 66 | * connection settings. |
| 67 | * |
| 68 | * Database settings can be passed as discreet |
| 69 | * parameters or as a data source name in the first |
| 70 | * parameter. DSNs must have this prototype: |
| 71 | * $dsn = 'driver://username:password@hostname/database'; |
| 72 | * |
| 73 | * @param mixed. Can be an array or a DSN string |
| 74 | */ |
| 75 | function CI_DB_driver($params) |
| 76 | { |
| 77 | $this->initialize($params); |
| 78 | log_message('debug', 'Database Driver Class Initialized'); |
| 79 | } |
| 80 | |
| 81 | // -------------------------------------------------------------------- |
| 82 | |
| 83 | /** |
| 84 | * Initialize Database Settings |
| 85 | * |
| 86 | * @access private Called by the constructor |
| 87 | * @param mixed |
| 88 | * @return void |
| 89 | */ |
| 90 | function initialize($params = '') |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 91 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 92 | if (is_array($params)) |
| 93 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 94 | $defaults = array( |
| 95 | 'hostname' => '', |
| 96 | 'username' => '', |
| 97 | 'password' => '', |
| 98 | 'database' => '', |
| 99 | 'dbdriver' => 'mysql', |
| 100 | 'dbprefix' => '', |
| 101 | 'port' => '', |
| 102 | 'pconnect' => FALSE, |
| 103 | 'db_debug' => FALSE, |
| 104 | 'cachedir' => '', |
| 105 | 'cache_on' => FALSE |
| 106 | ); |
| 107 | |
| 108 | foreach ($defaults as $key => $val) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 109 | { |
| 110 | $this->$key = ( ! isset($params[$key])) ? $val : $params[$key]; |
| 111 | } |
| 112 | } |
| 113 | elseif (strpos($params, '://')) |
| 114 | { |
| 115 | if (FALSE === ($dsn = @parse_url($params))) |
| 116 | { |
| 117 | log_message('error', 'Invalid DB Connection String'); |
| 118 | |
| 119 | if ($this->db_debug) |
| 120 | { |
| 121 | return $this->display_error('db_invalid_connection_str'); |
| 122 | } |
| 123 | return FALSE; |
| 124 | } |
| 125 | |
| 126 | $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']); |
| 127 | $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']); |
| 128 | $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); |
| 129 | $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); |
| 130 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 131 | |
| 132 | // Connect to the database |
| 133 | $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); |
| 134 | |
| 135 | if ( ! $this->conn_id) |
| 136 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 137 | log_message('error', 'Unable to connect to the database'); |
| 138 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 139 | if ($this->db_debug) |
| 140 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 141 | $this->display_error('db_unable_to_connect'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 144 | else |
| 145 | { |
| 146 | if ( ! $this->db_select()) |
| 147 | { |
| 148 | log_message('error', 'Unable to select database: '.$this->database); |
| 149 | |
| 150 | if ($this->db_debug) |
| 151 | { |
| 152 | $this->display_error('db_unable_to_select', $this->database); |
| 153 | } |
| 154 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 155 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | // -------------------------------------------------------------------- |
| 160 | |
| 161 | /** |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 162 | * The name of the platform in use (mysql, mssql, etc...) |
| 163 | * |
| 164 | * @access public |
| 165 | * @return string |
| 166 | */ |
| 167 | function platform() |
| 168 | { |
| 169 | return $this->dbdriver; |
| 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Database Version Number. Returns a string containing the |
| 176 | * version of the database being used |
| 177 | * |
| 178 | * @access public |
| 179 | * @return string |
| 180 | */ |
| 181 | function version() |
| 182 | { |
| 183 | if (FALSE === ($sql = $this->_version())) |
| 184 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 185 | if ($this->db_debug) |
| 186 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 187 | return $this->display_error('db_unsupported_function'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 188 | } |
| 189 | return FALSE; |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 190 | } |
| 191 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 192 | if ($this->dbdriver == 'oci8') |
| 193 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 194 | return $sql; |
| 195 | } |
| 196 | |
| 197 | $query = $this->query($sql); |
| 198 | $row = $query->row(); |
| 199 | return $row->ver; |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 205 | * Execute the query |
| 206 | * |
| 207 | * Accepts an SQL string as input and returns a result object upon |
| 208 | * successful execution of a "read" type query. Returns boolean TRUE |
| 209 | * upon successful execution of a "write" type query. Returns boolean |
| 210 | * FALSE upon failure, and if the $db_debug variable is set to TRUE |
| 211 | * will raise an error. |
| 212 | * |
| 213 | * @access public |
| 214 | * @param string An SQL query string |
| 215 | * @param array An array of binding data |
| 216 | * @return mixed |
| 217 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 218 | function query($sql, $binds = FALSE, $return_object = TRUE) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 219 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 220 | if ($sql == '') |
| 221 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 222 | if ($this->db_debug) |
| 223 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 224 | log_message('error', 'Invalid query: '.$sql); |
| 225 | return $this->display_error('db_invalid_query'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 226 | } |
| 227 | return FALSE; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 228 | } |
| 229 | |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 230 | // Is query caching enabled? If the query is a "read type" |
| 231 | // we will load the caching class and return the previously |
| 232 | // cached query if it exists |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 233 | if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 234 | { |
| 235 | if ($this->_cache_init()) |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 236 | { |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 237 | if (FALSE !== ($cache = $this->cache->read($sql))) |
| 238 | { |
| 239 | return $cache; |
| 240 | } |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 244 | // Compile binds if needed |
| 245 | if ($binds !== FALSE) |
| 246 | { |
| 247 | $sql = $this->compile_binds($sql, $binds); |
| 248 | } |
| 249 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 250 | // Save the query for debugging |
| 251 | $this->queries[] = $sql; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 252 | |
| 253 | // Start the Query Timer |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 254 | $time_start = list($sm, $ss) = explode(' ', microtime()); |
| 255 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 256 | // Run the Query |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 257 | if (FALSE === ($this->result_id = $this->simple_query($sql))) |
| 258 | { |
| 259 | // This will trigger a rollback if transactions are being used |
| 260 | $this->_trans_failure = TRUE; |
| 261 | |
| 262 | if ($this->db_debug) |
| 263 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 264 | log_message('error', 'Query error: '.$this->_error_message()); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 265 | return $this->display_error( |
| 266 | array( |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 267 | 'Error Number: '.$this->_error_number(), |
| 268 | $this->_error_message(), |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 269 | $sql |
| 270 | ) |
| 271 | ); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return FALSE; |
| 275 | } |
| 276 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 277 | // Stop and aggregate the query time results |
| 278 | $time_end = list($em, $es) = explode(' ', microtime()); |
| 279 | $this->benchmark += ($em + $es) - ($sm + $ss); |
| 280 | |
| 281 | // Increment the query counter |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 282 | $this->query_count++; |
| 283 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 284 | // Was the query a "write" type? |
| 285 | // If so we'll simply return true |
| 286 | if ($this->is_write_type($sql) === TRUE) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 287 | { |
| 288 | // If caching is enabled we'll auto-cleanup any |
| 289 | // existing files related to this particular URI |
| 290 | |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 291 | if ($this->cache_on == TRUE AND $this->_cache_init()) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 292 | { |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 293 | $this->cache->delete(); |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 294 | } |
| 295 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 296 | return TRUE; |
| 297 | } |
| 298 | |
| 299 | // Return TRUE if we don't need to create a result object |
| 300 | // Currently only the Oracle driver uses this when stored |
| 301 | // procedures are used |
| 302 | if ($return_object !== TRUE) |
| 303 | { |
| 304 | return TRUE; |
| 305 | } |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 306 | |
| 307 | // Load and instantiate the result driver |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 308 | |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 309 | $driver = $this->load_rdriver(); |
| 310 | |
| 311 | $RES = new $driver(); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 312 | $RES->conn_id = $this->conn_id; |
| 313 | $RES->db_debug = $this->db_debug; |
| 314 | $RES->result_id = $this->result_id; |
| 315 | |
| 316 | if ($this->dbdriver == 'oci8') |
| 317 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 318 | $RES->stmt_id = $this->stmt_id; |
| 319 | $RES->curs_id = NULL; |
| 320 | $RES->limit_used = $this->limit_used; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 321 | } |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 322 | |
| 323 | // Is query caching enabled? If so, we'll serialize the |
| 324 | // result object and save it to a cache file |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 325 | if ($this->cache_on == TRUE AND $this->_cache_init()) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 326 | { |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 327 | $this->cache->write($sql, $RES); |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 328 | } |
| 329 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 330 | return $RES; |
| 331 | } |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 332 | |
| 333 | // -------------------------------------------------------------------- |
| 334 | |
| 335 | /** |
| 336 | * Load the result drivers |
| 337 | * |
| 338 | * @access public |
| 339 | * @return string the name of the result class |
| 340 | */ |
| 341 | function load_rdriver() |
| 342 | { |
| 343 | $driver = 'CI_DB_'.$this->dbdriver.'_result'; |
| 344 | |
| 345 | if ( ! class_exists($driver)) |
| 346 | { |
| 347 | include_once(BASEPATH.'database/DB_result'.EXT); |
| 348 | include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); |
| 349 | } |
| 350 | |
| 351 | return $driver; |
| 352 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 353 | |
| 354 | // -------------------------------------------------------------------- |
| 355 | |
| 356 | /** |
| 357 | * Simple Query |
| 358 | * This is a simiplified version of the query() function. Internally |
| 359 | * we only use it when running transaction commands since they do |
| 360 | * not require all the features of the main query() function. |
| 361 | * |
| 362 | * @access public |
| 363 | * @param string the sql query |
| 364 | * @return mixed |
| 365 | */ |
| 366 | function simple_query($sql) |
| 367 | { |
| 368 | if ( ! $this->conn_id) |
| 369 | { |
| 370 | $this->initialize(); |
| 371 | } |
| 372 | |
| 373 | return $this->_execute($sql, $this->conn_id); |
| 374 | } |
| 375 | |
| 376 | // -------------------------------------------------------------------- |
| 377 | |
| 378 | /** |
| 379 | * Disable Transactions |
| 380 | * This permits transactions to be disabled at run-time. |
| 381 | * |
| 382 | * @access public |
| 383 | * @return void |
| 384 | */ |
| 385 | function trans_off() |
| 386 | { |
| 387 | $this->trans_enabled = FALSE; |
| 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
| 393 | * Start Transaction |
| 394 | * |
| 395 | * @access public |
| 396 | * @return void |
| 397 | */ |
| 398 | function trans_start($test_mode = FALSE) |
| 399 | { |
| 400 | if ( ! $this->trans_enabled) |
| 401 | { |
| 402 | return FALSE; |
| 403 | } |
| 404 | |
| 405 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 406 | if ($this->_trans_depth > 0) |
| 407 | { |
| 408 | $this->_trans_depth += 1; |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | $this->trans_begin($test_mode); |
| 413 | } |
| 414 | |
| 415 | // -------------------------------------------------------------------- |
| 416 | |
| 417 | /** |
| 418 | * Complete Transaction |
| 419 | * |
| 420 | * @access public |
| 421 | * @return bool |
| 422 | */ |
| 423 | function trans_complete() |
| 424 | { |
| 425 | if ( ! $this->trans_enabled) |
| 426 | { |
| 427 | return FALSE; |
| 428 | } |
| 429 | |
| 430 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 431 | if ($this->_trans_depth > 1) |
| 432 | { |
| 433 | $this->_trans_depth -= 1; |
| 434 | return TRUE; |
| 435 | } |
| 436 | |
| 437 | // The query() function will set this flag to TRUE in the event that a query failed |
| 438 | if ($this->_trans_failure === TRUE) |
| 439 | { |
| 440 | $this->trans_rollback(); |
| 441 | |
| 442 | if ($this->db_debug) |
| 443 | { |
| 444 | return $this->display_error('db_transaction_failure'); |
| 445 | } |
| 446 | return FALSE; |
| 447 | } |
| 448 | |
| 449 | $this->trans_commit(); |
| 450 | return TRUE; |
| 451 | } |
| 452 | |
| 453 | // -------------------------------------------------------------------- |
| 454 | |
| 455 | /** |
| 456 | * Lets you retrieve the transaction flag to determine if it has failed |
| 457 | * |
| 458 | * @access public |
| 459 | * @return bool |
| 460 | */ |
| 461 | function trans_status() |
| 462 | { |
| 463 | return $this->_trans_failure; |
| 464 | } |
| 465 | |
| 466 | // -------------------------------------------------------------------- |
| 467 | |
| 468 | /** |
| 469 | * Compile Bindings |
| 470 | * |
| 471 | * @access public |
| 472 | * @param string the sql statement |
| 473 | * @param array an array of bind data |
| 474 | * @return string |
| 475 | */ |
| 476 | function compile_binds($sql, $binds) |
| 477 | { |
| 478 | if (FALSE === strpos($sql, $this->bind_marker)) |
| 479 | { |
| 480 | return $sql; |
| 481 | } |
| 482 | |
| 483 | if ( ! is_array($binds)) |
| 484 | { |
| 485 | $binds = array($binds); |
| 486 | } |
| 487 | |
| 488 | foreach ($binds as $val) |
| 489 | { |
| 490 | $val = $this->escape($val); |
| 491 | |
| 492 | // Just in case the replacement string contains the bind |
| 493 | // character we'll temporarily replace it with a marker |
| 494 | $val = str_replace($this->bind_marker, '{%bind_marker%}', $val); |
| 495 | $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1); |
| 496 | } |
| 497 | |
| 498 | return str_replace('{%bind_marker%}', $this->bind_marker, $sql); |
| 499 | } |
| 500 | |
| 501 | // -------------------------------------------------------------------- |
| 502 | |
| 503 | /** |
| 504 | * Determines if a query is a "write" type. |
| 505 | * |
| 506 | * @access public |
| 507 | * @param string An SQL query string |
| 508 | * @return boolean |
| 509 | */ |
| 510 | function is_write_type($sql) |
| 511 | { |
| 512 | if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) |
| 513 | { |
| 514 | return FALSE; |
| 515 | } |
| 516 | return TRUE; |
| 517 | } |
| 518 | |
| 519 | // -------------------------------------------------------------------- |
| 520 | |
| 521 | /** |
| 522 | * Calculate the aggregate query elapsed time |
| 523 | * |
| 524 | * @access public |
| 525 | * @param intiger The number of decimal places |
| 526 | * @return integer |
| 527 | */ |
| 528 | function elapsed_time($decimals = 6) |
| 529 | { |
| 530 | return number_format($this->benchmark, $decimals); |
| 531 | } |
| 532 | |
| 533 | // -------------------------------------------------------------------- |
| 534 | |
| 535 | /** |
| 536 | * Returns the total number of queries |
| 537 | * |
| 538 | * @access public |
| 539 | * @return integer |
| 540 | */ |
| 541 | function total_queries() |
| 542 | { |
| 543 | return $this->query_count; |
| 544 | } |
| 545 | |
| 546 | // -------------------------------------------------------------------- |
| 547 | |
| 548 | /** |
| 549 | * Returns the last query that was executed |
| 550 | * |
| 551 | * @access public |
| 552 | * @return void |
| 553 | */ |
| 554 | function last_query() |
| 555 | { |
| 556 | return end($this->queries); |
| 557 | } |
| 558 | |
| 559 | // -------------------------------------------------------------------- |
| 560 | |
| 561 | /** |
| 562 | * "Smart" Escape String |
| 563 | * |
| 564 | * Escapes data based on type |
| 565 | * Sets boolean and null types |
| 566 | * |
| 567 | * @access public |
| 568 | * @param string |
| 569 | * @return integer |
| 570 | */ |
| 571 | function escape($str) |
| 572 | { |
| 573 | switch (gettype($str)) |
| 574 | { |
| 575 | case 'string' : $str = "'".$this->escape_str($str)."'"; |
| 576 | break; |
| 577 | case 'boolean' : $str = ($str === FALSE) ? 0 : 1; |
| 578 | break; |
| 579 | default : $str = ($str === NULL) ? 'NULL' : $str; |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | return $str; |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 584 | } |
| 585 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 586 | |
| 587 | |
| 588 | // -------------------------------------------------------------------- |
| 589 | |
| 590 | /** |
| 591 | * Primary |
| 592 | * |
| 593 | * Retrieves the primary key. It assumes that the row in the first |
| 594 | * position is the primary key |
| 595 | * |
| 596 | * @access public |
| 597 | * @param string the table name |
| 598 | * @return string |
| 599 | */ |
| 600 | function primary($table = '') |
| 601 | { |
| 602 | $fields = $this->field_names($table); |
| 603 | |
| 604 | if ( ! is_array($fields)) |
| 605 | { |
| 606 | return FALSE; |
| 607 | } |
| 608 | |
| 609 | return current($fields); |
| 610 | } |
| 611 | |
| 612 | // -------------------------------------------------------------------- |
| 613 | |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 614 | /** |
| 615 | * Returns an array of table names |
| 616 | * |
| 617 | * @access public |
| 618 | * @return array |
| 619 | */ |
| 620 | function list_tables() |
| 621 | { |
| 622 | // Is there a cached result? |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 623 | if (isset($this->data_cache['table_names'])) |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 624 | { |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 625 | return $this->data_cache['table_names']; |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (FALSE === ($sql = $this->_list_tables())) |
| 629 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 630 | if ($this->db_debug) |
| 631 | { |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 632 | return $this->display_error('db_unsupported_function'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 633 | } |
| 634 | return FALSE; |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | $retval = array(); |
| 638 | $query = $this->query($sql); |
| 639 | |
| 640 | if ($query->num_rows() > 0) |
| 641 | { |
| 642 | foreach($query->result_array() as $row) |
| 643 | { |
| 644 | if (isset($row['TABLE_NAME'])) |
| 645 | { |
| 646 | $retval[] = $row['TABLE_NAME']; |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | $retval[] = array_shift($row); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 655 | return $this->data_cache['table_names'] =& $retval; |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | // -------------------------------------------------------------------- |
| 659 | |
| 660 | /** |
| 661 | * Determine if a particular table exists |
| 662 | * @access public |
| 663 | * @return boolean |
| 664 | */ |
| 665 | function table_exists($table_name) |
| 666 | { |
| 667 | return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; |
| 668 | } |
| 669 | |
| 670 | // -------------------------------------------------------------------- |
| 671 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 672 | /** |
| 673 | * Fetch MySQL Field Names |
| 674 | * |
| 675 | * @access public |
| 676 | * @param string the table name |
| 677 | * @return array |
| 678 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 679 | function list_fields($table = '') |
| 680 | { |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 681 | // Is there a cached result? |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 682 | if (isset($this->data_cache['field_names'][$table])) |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 683 | { |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 684 | return $this->data_cache['field_names'][$table]; |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 685 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 686 | |
| 687 | if ($table == '') |
| 688 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 689 | if ($this->db_debug) |
| 690 | { |
| 691 | return $this->display_error('db_field_param_missing'); |
| 692 | } |
| 693 | return FALSE; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 694 | } |
| 695 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 696 | if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table))) |
| 697 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 698 | if ($this->db_debug) |
| 699 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 700 | return $this->display_error('db_unsupported_function'); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 701 | } |
| 702 | return FALSE; |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 703 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 704 | |
| 705 | $query = $this->query($sql); |
| 706 | |
| 707 | $retval = array(); |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 708 | foreach($query->result_array() as $row) |
| 709 | { |
| 710 | if (isset($row['COLUMN_NAME'])) |
| 711 | { |
| 712 | $retval[] = $row['COLUMN_NAME']; |
| 713 | } |
| 714 | else |
| 715 | { |
| 716 | $retval[] = current($row); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 717 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 718 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 719 | |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 720 | return $this->data_cache['field_names'][$table] =& $retval; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 721 | } |
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 722 | |
| 723 | // -------------------------------------------------------------------- |
| 724 | |
| 725 | /** |
| 726 | * Determine if a particular field exists |
| 727 | * @access public |
| 728 | * @param string |
| 729 | * @param string |
| 730 | * @return boolean |
| 731 | */ |
| 732 | function field_exists($field_name, $table_name) |
| 733 | { |
| 734 | return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; |
| 735 | } |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 736 | |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 737 | // -------------------------------------------------------------------- |
| 738 | |
| 739 | /** |
| 740 | * DEPRECATED - use list_fields() |
| 741 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 742 | function field_names($table = '') |
| 743 | { |
| 744 | return $this->list_fields($table); |
| 745 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 746 | |
| 747 | // -------------------------------------------------------------------- |
| 748 | |
| 749 | /** |
| 750 | * Returns an object with field data |
| 751 | * |
| 752 | * @access public |
| 753 | * @param string the table name |
| 754 | * @return object |
| 755 | */ |
| 756 | function field_data($table = '') |
| 757 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 758 | if ($table == '') |
| 759 | { |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 760 | if ($this->db_debug) |
| 761 | { |
| 762 | return $this->display_error('db_field_param_missing'); |
| 763 | } |
| 764 | return FALSE; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 765 | } |
| 766 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 767 | $query = $this->query($this->_field_data($this->dbprefix.$table)); |
| 768 | return $query->field_data(); |
| 769 | } |
| 770 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 771 | // -------------------------------------------------------------------- |
| 772 | |
| 773 | /** |
| 774 | * Generate an insert string |
| 775 | * |
| 776 | * @access public |
| 777 | * @param string the table upon which the query will be performed |
| 778 | * @param array an associative array data of key/values |
| 779 | * @return string |
| 780 | */ |
| 781 | function insert_string($table, $data) |
| 782 | { |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 783 | $fields = array(); |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 784 | $values = array(); |
| 785 | |
| 786 | foreach($data as $key => $val) |
| 787 | { |
| 788 | $fields[] = $key; |
| 789 | $values[] = $this->escape($val); |
| 790 | } |
| 791 | |
| 792 | return $this->_insert($this->dbprefix.$table, $fields, $values); |
| 793 | } |
| 794 | |
| 795 | // -------------------------------------------------------------------- |
| 796 | |
| 797 | /** |
| 798 | * Generate an update string |
| 799 | * |
| 800 | * @access public |
| 801 | * @param string the table upon which the query will be performed |
| 802 | * @param array an associative array data of key/values |
| 803 | * @param mixed the "where" statement |
| 804 | * @return string |
| 805 | */ |
| 806 | function update_string($table, $data, $where) |
| 807 | { |
| 808 | if ($where == '') |
| 809 | return false; |
| 810 | |
| 811 | $fields = array(); |
| 812 | foreach($data as $key => $val) |
| 813 | { |
| 814 | $fields[$key] = $this->escape($val); |
| 815 | } |
| 816 | |
| 817 | if ( ! is_array($where)) |
| 818 | { |
| 819 | $dest = array($where); |
| 820 | } |
| 821 | else |
| 822 | { |
| 823 | $dest = array(); |
| 824 | foreach ($where as $key => $val) |
| 825 | { |
| 826 | $prefix = (count($dest) == 0) ? '' : ' AND '; |
| 827 | |
| 828 | if ($val != '') |
| 829 | { |
| 830 | if ( ! $this->_has_operator($key)) |
| 831 | { |
| 832 | $key .= ' ='; |
| 833 | } |
| 834 | |
| 835 | $val = ' '.$this->escape($val); |
| 836 | } |
| 837 | |
| 838 | $dest[] = $prefix.$key.$val; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | return $this->_update($this->dbprefix.$table, $fields, $dest); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 843 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 844 | |
| 845 | // -------------------------------------------------------------------- |
| 846 | |
| 847 | /** |
| 848 | * Enables a native PHP function to be run, using a platform agnostic wrapper. |
| 849 | * |
| 850 | * @access public |
| 851 | * @param string the function name |
| 852 | * @param mixed any parameters needed by the function |
| 853 | * @return mixed |
| 854 | */ |
| 855 | function call_function($function) |
| 856 | { |
| 857 | $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; |
| 858 | |
| 859 | if (FALSE === strpos($driver, $function)) |
| 860 | { |
| 861 | $function = $driver.$function; |
| 862 | } |
| 863 | |
| 864 | if ( ! function_exists($function)) |
| 865 | { |
| 866 | if ($this->db_debug) |
| 867 | { |
| 868 | return $this->display_error('db_unsupported_function'); |
| 869 | } |
| 870 | return FALSE; |
| 871 | } |
| 872 | else |
| 873 | { |
admin | ee54c11 | 2006-09-28 17:13:38 +0000 | [diff] [blame] | 874 | $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; |
| 875 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 876 | return call_user_func_array($function, $args); |
| 877 | } |
| 878 | } |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 879 | |
| 880 | // -------------------------------------------------------------------- |
| 881 | |
| 882 | /** |
| 883 | * Enable Query Caching |
| 884 | * |
| 885 | * @access public |
| 886 | * @return void |
| 887 | */ |
| 888 | function cache_on() |
| 889 | { |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 890 | return $this->query_caching = TRUE; |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | // -------------------------------------------------------------------- |
| 894 | |
| 895 | /** |
| 896 | * Disable Query Caching |
| 897 | * |
| 898 | * @access public |
| 899 | * @return void |
| 900 | */ |
| 901 | function cache_off() |
| 902 | { |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 903 | return $this->query_caching = FALSE; |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 904 | } |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame^] | 905 | |
| 906 | // -------------------------------------------------------------------- |
| 907 | |
| 908 | /** |
| 909 | * Set Cache Directory Path |
| 910 | * |
| 911 | * @access public |
| 912 | * @param string the path to the cache directory |
| 913 | * @return void |
| 914 | */ |
| 915 | function cache_set_path($path = '') |
| 916 | { |
| 917 | $this->cachedir = $path; |
| 918 | } |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 919 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 920 | // -------------------------------------------------------------------- |
| 921 | |
| 922 | /** |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 923 | * Initialize the Cache Class |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 924 | * |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 925 | * @access private |
| 926 | * @return void |
| 927 | */ |
| 928 | function _cache_init() |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 929 | { |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame^] | 930 | if (is_object($this->cache) AND class_exists('CI_DB_Cache')) |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 931 | { |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 932 | return TRUE; |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 933 | } |
| 934 | |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame^] | 935 | if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT)) |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 936 | { |
admin | fa70817 | 2006-10-04 18:42:42 +0000 | [diff] [blame^] | 937 | return $this->cache_off(); |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 938 | } |
| 939 | |
admin | 0c40565 | 2006-10-04 18:37:57 +0000 | [diff] [blame] | 940 | $this->cache = new CI_DB_Cache; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 941 | return TRUE; |
| 942 | } |
| 943 | |
admin | db8a374 | 2006-10-04 07:43:35 +0000 | [diff] [blame] | 944 | |
| 945 | // -------------------------------------------------------------------- |
| 946 | |
| 947 | /** |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 948 | * Close DB Connection |
| 949 | * |
| 950 | * @access public |
| 951 | * @return void |
| 952 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 953 | function close() |
| 954 | { |
| 955 | if (is_resource($this->conn_id)) |
| 956 | { |
| 957 | $this->_close($this->conn_id); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 958 | } |
| 959 | $this->conn_id = FALSE; |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 960 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 961 | |
| 962 | // -------------------------------------------------------------------- |
| 963 | |
| 964 | /** |
| 965 | * Display an error message |
| 966 | * |
| 967 | * @access public |
| 968 | * @param string the error message |
| 969 | * @param string any "swap" values |
| 970 | * @param boolean whether to localize the message |
| 971 | * @return string sends the application/errror_db.php template |
| 972 | */ |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 973 | function display_error($error = '', $swap = '', $native = FALSE) |
| 974 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 975 | $LANG = new CI_Language(); |
| 976 | $LANG->load('db'); |
| 977 | |
| 978 | $heading = 'MySQL Error'; |
| 979 | |
| 980 | if ($native == TRUE) |
| 981 | { |
| 982 | $message = $error; |
| 983 | } |
| 984 | else |
| 985 | { |
| 986 | $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; |
| 987 | } |
| 988 | |
| 989 | if ( ! class_exists('CI_Exceptions')) |
| 990 | { |
| 991 | include_once(BASEPATH.'libraries/Exceptions'.EXT); |
| 992 | } |
| 993 | |
| 994 | $error = new CI_Exceptions(); |
| 995 | echo $error->show_error('An Error Was Encountered', $message, 'error_db'); |
| 996 | exit; |
| 997 | |
admin | 6871d95 | 2006-10-04 00:36:18 +0000 | [diff] [blame] | 998 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 999 | |
| 1000 | } |
| 1001 | |
| 1002 | ?> |