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