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