| [ Index ] |
PHP Cross Reference of Joomla 1.5.26 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @version $Id: database.php 14401 2010-01-26 14:10:00Z louis $ 4 * @package Joomla.Framework 5 * @subpackage Database 6 * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved. 7 * @license GNU/GPL, see LICENSE.php 8 * Joomla! is free software. This version may have been modified pursuant 9 * to the GNU General Public License, and as distributed it includes or 10 * is derivative of works licensed under the GNU General Public License or 11 * other free or open source software licenses. 12 * See COPYRIGHT.php for copyright notices and details. 13 */ 14 15 // Check to ensure this file is within the rest of the framework 16 defined('JPATH_BASE') or die(); 17 18 /** 19 * Database connector class 20 * 21 * @abstract 22 * @package Joomla.Framework 23 * @subpackage Database 24 * @since 1.0 25 */ 26 class JDatabase extends JObject 27 { 28 /** 29 * The database driver name 30 * 31 * @var string 32 */ 33 var $name = ''; 34 35 /** 36 * The query sql string 37 * 38 * @var string 39 **/ 40 var $_sql = ''; 41 42 /** 43 * The database error number 44 * 45 * @var int 46 **/ 47 var $_errorNum = 0; 48 49 /** 50 * The database error message 51 * 52 * @var string 53 */ 54 var $_errorMsg = ''; 55 56 /** 57 * The prefix used on all database tables 58 * 59 * @var string 60 */ 61 var $_table_prefix = ''; 62 63 /** 64 * The connector resource 65 * 66 * @var resource 67 */ 68 var $_resource = ''; 69 70 /** 71 * The last query cursor 72 * 73 * @var resource 74 */ 75 var $_cursor = null; 76 77 /** 78 * Debug option 79 * 80 * @var boolean 81 */ 82 var $_debug = 0; 83 84 /** 85 * The limit for the query 86 * 87 * @var int 88 */ 89 var $_limit = 0; 90 91 /** 92 * The for offset for the limit 93 * 94 * @var int 95 */ 96 var $_offset = 0; 97 98 /** 99 * The number of queries performed by the object instance 100 * 101 * @var int 102 */ 103 var $_ticker = 0; 104 105 /** 106 * A log of queries 107 * 108 * @var array 109 */ 110 var $_log = null; 111 112 /** 113 * The null/zero date string 114 * 115 * @var string 116 */ 117 var $_nullDate = null; 118 119 /** 120 * Quote for named objects 121 * 122 * @var string 123 */ 124 var $_nameQuote = null; 125 126 /** 127 * UTF-8 support 128 * 129 * @var boolean 130 * @since 1.5 131 */ 132 var $_utf = 0; 133 134 /** 135 * The fields that are to be quote 136 * 137 * @var array 138 * @since 1.5 139 */ 140 var $_quoted = null; 141 142 /** 143 * Legacy compatibility 144 * 145 * @var bool 146 * @since 1.5 147 */ 148 var $_hasQuoted = null; 149 150 /** 151 * Database object constructor 152 * 153 * @access public 154 * @param array List of options used to configure the connection 155 * @since 1.5 156 */ 157 function __construct( $options ) 158 { 159 $prefix = array_key_exists('prefix', $options) ? $options['prefix'] : 'jos_'; 160 161 // Determine utf-8 support 162 $this->_utf = $this->hasUTF(); 163 164 //Set charactersets (needed for MySQL 4.1.2+) 165 if ($this->_utf){ 166 $this->setUTF(); 167 } 168 169 $this->_table_prefix = $prefix; 170 $this->_ticker = 0; 171 $this->_errorNum = 0; 172 $this->_log = array(); 173 $this->_quoted = array(); 174 $this->_hasQuoted = false; 175 176 // Register faked "destructor" in PHP4 to close all connections we might have made 177 if (version_compare(PHP_VERSION, '5') == -1) { 178 register_shutdown_function(array(&$this, '__destruct')); 179 } 180 } 181 182 /** 183 * Returns a reference to the global Database object, only creating it 184 * if it doesn't already exist. 185 * 186 * The 'driver' entry in the parameters array specifies the database driver 187 * to be used (defaults to 'mysql' if omitted). All other parameters are 188 * database driver dependent. 189 * 190 * @param array Parameters to be passed to the database driver 191 * @return JDatabase A database object 192 * @since 1.5 193 */ 194 function &getInstance( $options = array() ) 195 { 196 static $instances; 197 198 if (!isset( $instances )) { 199 $instances = array(); 200 } 201 202 $signature = serialize( $options ); 203 204 if (empty($instances[$signature])) 205 { 206 $driver = array_key_exists('driver', $options) ? $options['driver'] : 'mysql'; 207 $select = array_key_exists('select', $options) ? $options['select'] : true; 208 $database = array_key_exists('database', $options) ? $options['database'] : null; 209 210 $driver = preg_replace('/[^A-Z0-9_\.-]/i', '', $driver); 211 $path = dirname(__FILE__).DS.'database'.DS.$driver.'.php'; 212 213 if (file_exists($path)) { 214 require_once($path); 215 } else { 216 JError::setErrorHandling(E_ERROR, 'die'); //force error type to die 217 $error = JError::raiseError( 500, JTEXT::_('Unable to load Database Driver:') .$driver); 218 return $error; 219 } 220 221 $adapter = 'JDatabase'.$driver; 222 $instance = new $adapter($options); 223 224 if ( $error = $instance->getErrorMsg() ) 225 { 226 JError::setErrorHandling(E_ERROR, 'ignore'); //force error type to die 227 $error = JError::raiseError( 500, JTEXT::_('Unable to connect to the database:') .$error); 228 return $error; 229 } 230 231 232 $instances[$signature] = & $instance; 233 } 234 235 return $instances[$signature]; 236 } 237 238 /** 239 * Database object destructor 240 * 241 * @abstract 242 * @access private 243 * @return boolean 244 * @since 1.5 245 */ 246 function __destruct() 247 { 248 return true; 249 } 250 251 /** 252 * Get the database connectors 253 * 254 * @access public 255 * @return array An array of available session handlers 256 */ 257 function getConnectors() 258 { 259 jimport('joomla.filesystem.folder'); 260 $handlers = JFolder::files(dirname(__FILE__).DS.'database', '.php$'); 261 262 $names = array(); 263 foreach($handlers as $handler) 264 { 265 $name = substr($handler, 0, strrpos($handler, '.')); 266 $class = 'JDatabase'.ucfirst($name); 267 268 if(!class_exists($class)) { 269 require_once(dirname(__FILE__).DS.'database'.DS.$name.'.php'); 270 } 271 272 if(call_user_func_array( array( trim($class), 'test' ), null)) { 273 $names[] = $name; 274 } 275 } 276 277 return $names; 278 } 279 280 /** 281 * Test to see if the MySQLi connector is available 282 * 283 * @static 284 * @access public 285 * @return boolean True on success, false otherwise. 286 */ 287 function test() 288 { 289 return false; 290 } 291 292 /** 293 * Determines if the connection to the server is active. 294 * 295 * @access public 296 * @return boolean 297 * @since 1.5 298 */ 299 function connected() 300 { 301 return false; 302 } 303 304 /** 305 * Determines UTF support 306 * 307 * @abstract 308 * @access public 309 * @return boolean 310 * @since 1.5 311 */ 312 function hasUTF() { 313 return false; 314 } 315 316 /** 317 * Custom settings for UTF support 318 * 319 * @abstract 320 * @access public 321 * @since 1.5 322 */ 323 function setUTF() { 324 } 325 326 /** 327 * Adds a field or array of field names to the list that are to be quoted 328 * 329 * @access public 330 * @param mixed Field name or array of names 331 * @since 1.5 332 */ 333 function addQuoted( $quoted ) 334 { 335 if (is_string( $quoted )) { 336 $this->_quoted[] = $quoted; 337 } else { 338 $this->_quoted = array_merge( $this->_quoted, (array)$quoted ); 339 } 340 $this->_hasQuoted = true; 341 } 342 343 /** 344 * Splits a string of queries into an array of individual queries 345 * 346 * @access public 347 * @param string The queries to split 348 * @return array queries 349 */ 350 function splitSql( $queries ) 351 { 352 $start = 0; 353 $open = false; 354 $open_char = ''; 355 $end = strlen($queries); 356 $query_split = array(); 357 for($i=0;$i<$end;$i++) { 358 $current = substr($queries,$i,1); 359 if(($current == '"' || $current == '\'')) { 360 $n = 2; 361 while(substr($queries,$i - $n + 1, 1) == '\\' && $n < $i) { 362 $n ++; 363 } 364 if($n%2==0) { 365 if ($open) { 366 if($current == $open_char) { 367 $open = false; 368 $open_char = ''; 369 } 370 } else { 371 $open = true; 372 $open_char = $current; 373 } 374 } 375 } 376 if(($current == ';' && !$open)|| $i == $end - 1) { 377 $query_split[] = substr($queries, $start, ($i - $start + 1)); 378 $start = $i + 1; 379 } 380 } 381 382 return $query_split; 383 } 384 385 386 387 /** 388 * Checks if field name needs to be quoted 389 * 390 * @access public 391 * @param string The field name 392 * @return bool 393 */ 394 function isQuoted( $fieldName ) 395 { 396 if ($this->_hasQuoted) { 397 return in_array( $fieldName, $this->_quoted ); 398 } else { 399 return true; 400 } 401 } 402 403 /** 404 * Sets the debug level on or off 405 * 406 * @access public 407 * @param int 0 = off, 1 = on 408 */ 409 function debug( $level ) { 410 $this->_debug = intval( $level ); 411 } 412 413 /** 414 * Get the database UTF-8 support 415 * 416 * @access public 417 * @return boolean 418 * @since 1.5 419 */ 420 function getUTFSupport() { 421 return $this->_utf; 422 } 423 424 /** 425 * Get the error number 426 * 427 * @access public 428 * @return int The error number for the most recent query 429 */ 430 function getErrorNum() { 431 return $this->_errorNum; 432 } 433 434 435 /** 436 * Get the error message 437 * 438 * @access public 439 * @return string The error message for the most recent query 440 */ 441 function getErrorMsg($escaped = false) 442 { 443 if($escaped) { 444 return addslashes($this->_errorMsg); 445 } else { 446 return $this->_errorMsg; 447 } 448 } 449 450 /** 451 * Get a database escaped string 452 * 453 * @param string The string to be escaped 454 * @param boolean Optional parameter to provide extra escaping 455 * @return string 456 * @access public 457 * @abstract 458 */ 459 function getEscaped( $text, $extra = false ) 460 { 461 return; 462 } 463 464 /** 465 * Get a database error log 466 * 467 * @access public 468 * @return array 469 */ 470 function getLog( ) 471 { 472 return $this->_log; 473 } 474 475 /** 476 * Get the total number of queries made 477 * 478 * @access public 479 * @return array 480 */ 481 function getTicker( ) 482 { 483 return $this->_ticker; 484 } 485 486 /** 487 * Quote an identifier name (field, table, etc) 488 * 489 * @access public 490 * @param string The name 491 * @return string The quoted name 492 */ 493 function nameQuote( $s ) 494 { 495 // Only quote if the name is not using dot-notation 496 if (strpos( $s, '.' ) === false) 497 { 498 $q = $this->_nameQuote; 499 if (strlen( $q ) == 1) { 500 return $q . $s . $q; 501 } else { 502 return $q{0} . $s . $q{1}; 503 } 504 } 505 else { 506 return $s; 507 } 508 } 509 /** 510 * Get the database table prefix 511 * 512 * @access public 513 * @return string The database prefix 514 */ 515 function getPrefix() 516 { 517 return $this->_table_prefix; 518 } 519 520 /** 521 * Get the database null date 522 * 523 * @access public 524 * @return string Quoted null/zero date string 525 */ 526 function getNullDate() 527 { 528 return $this->_nullDate; 529 } 530 531 /** 532 * Sets the SQL query string for later execution. 533 * 534 * This function replaces a string identifier <var>$prefix</var> with the 535 * string held is the <var>_table_prefix</var> class variable. 536 * 537 * @access public 538 * @param string The SQL query 539 * @param string The offset to start selection 540 * @param string The number of results to return 541 * @param string The common table prefix 542 */ 543 function setQuery( $sql, $offset = 0, $limit = 0, $prefix='#__' ) 544 { 545 $this->_sql = $this->replacePrefix( $sql, $prefix ); 546 $this->_limit = (int) $limit; 547 $this->_offset = (int) $offset; 548 } 549 550 /** 551 * This function replaces a string identifier <var>$prefix</var> with the 552 * string held is the <var>_table_prefix</var> class variable. 553 * 554 * @access public 555 * @param string The SQL query 556 * @param string The common table prefix 557 */ 558 function replacePrefix( $sql, $prefix='#__' ) 559 { 560 $sql = trim( $sql ); 561 562 $escaped = false; 563 $quoteChar = ''; 564 565 $n = strlen( $sql ); 566 567 $startPos = 0; 568 $literal = ''; 569 while ($startPos < $n) { 570 $ip = strpos($sql, $prefix, $startPos); 571 if ($ip === false) { 572 break; 573 } 574 575 $j = strpos( $sql, "'", $startPos ); 576 $k = strpos( $sql, '"', $startPos ); 577 if (($k !== FALSE) && (($k < $j) || ($j === FALSE))) { 578 $quoteChar = '"'; 579 $j = $k; 580 } else { 581 $quoteChar = "'"; 582 } 583 584 if ($j === false) { 585 $j = $n; 586 } 587 588 $literal .= str_replace( $prefix, $this->_table_prefix,substr( $sql, $startPos, $j - $startPos ) ); 589 $startPos = $j; 590 591 $j = $startPos + 1; 592 593 if ($j >= $n) { 594 break; 595 } 596 597 // quote comes first, find end of quote 598 while (TRUE) { 599 $k = strpos( $sql, $quoteChar, $j ); 600 $escaped = false; 601 if ($k === false) { 602 break; 603 } 604 $l = $k - 1; 605 while ($l >= 0 && $sql{$l} == '\\') { 606 $l--; 607 $escaped = !$escaped; 608 } 609 if ($escaped) { 610 $j = $k+1; 611 continue; 612 } 613 break; 614 } 615 if ($k === FALSE) { 616 // error in the query - no end quote; ignore it 617 break; 618 } 619 $literal .= substr( $sql, $startPos, $k - $startPos + 1 ); 620 $startPos = $k+1; 621 } 622 if ($startPos < $n) { 623 $literal .= substr( $sql, $startPos, $n - $startPos ); 624 } 625 return $literal; 626 } 627 628 /** 629 * Get the active query 630 * 631 * @access public 632 * @return string The current value of the internal SQL vairable 633 */ 634 function getQuery() 635 { 636 return $this->_sql; 637 } 638 639 /** 640 * Execute the query 641 * 642 * @abstract 643 * @access public 644 * @return mixed A database resource if successful, FALSE if not. 645 */ 646 function query() 647 { 648 return; 649 } 650 651 /** 652 * Get the affected rows by the most recent query 653 * 654 * @abstract 655 * @access public 656 * @return int The number of affected rows in the previous operation 657 * @since 1.0.5 658 */ 659 function getAffectedRows() 660 { 661 return; 662 } 663 664 /** 665 * Execute a batch query 666 * 667 * @abstract 668 * @access public 669 * @return mixed A database resource if successful, FALSE if not. 670 */ 671 function queryBatch( $abort_on_error=true, $p_transaction_safe = false) 672 { 673 return false; 674 } 675 676 /** 677 * Diagnostic function 678 * 679 * @abstract 680 * @access public 681 */ 682 function explain() 683 { 684 return; 685 } 686 687 /** 688 * Get the number of rows returned by the most recent query 689 * 690 * @abstract 691 * @access public 692 * @param object Database resource 693 * @return int The number of rows 694 */ 695 function getNumRows( $cur=null ) 696 { 697 return; 698 } 699 700 /** 701 * This method loads the first field of the first row returned by the query. 702 * 703 * @abstract 704 * @access public 705 * @return The value returned in the query or null if the query failed. 706 */ 707 function loadResult() 708 { 709 return; 710 } 711 712 /** 713 * Load an array of single field results into an array 714 * 715 * @abstract 716 */ 717 function loadResultArray($numinarray = 0) 718 { 719 return; 720 } 721 722 /** 723 * Fetch a result row as an associative array 724 * 725 * @abstract 726 */ 727 function loadAssoc() 728 { 729 return; 730 } 731 732 /** 733 * Load a associactive list of database rows 734 * 735 * @abstract 736 * @access public 737 * @param string The field name of a primary key 738 * @return array If key is empty as sequential list of returned records. 739 */ 740 function loadAssocList( $key='' ) 741 { 742 return; 743 } 744 745 /** 746 * This global function loads the first row of a query into an object 747 * 748 * 749 * @abstract 750 * @access public 751 * @param object 752 */ 753 function loadObject( ) 754 { 755 return; 756 } 757 758 /** 759 * Load a list of database objects 760 * 761 * @abstract 762 * @access public 763 * @param string The field name of a primary key 764 * @return array If <var>key</var> is empty as sequential list of returned records. 765 766 * If <var>key</var> is not empty then the returned array is indexed by the value 767 * the database key. Returns <var>null</var> if the query fails. 768 */ 769 function loadObjectList( $key='' ) 770 { 771 return; 772 } 773 774 /** 775 * Load the first row returned by the query 776 * 777 * @abstract 778 * @access public 779 * @return The first row of the query. 780 */ 781 function loadRow() 782 { 783 return; 784 } 785 786 /** 787 * Load a list of database rows (numeric column indexing) 788 * 789 * If <var>key</var> is not empty then the returned array is indexed by the value 790 * the database key. Returns <var>null</var> if the query fails. 791 * 792 * @abstract 793 * @access public 794 * @param string The field name of a primary key 795 * @return array 796 */ 797 function loadRowList( $key='' ) 798 { 799 return; 800 } 801 802 /** 803 * Inserts a row into a table based on an objects properties 804 * @param string The name of the table 805 * @param object An object whose properties match table fields 806 * @param string The name of the primary key. If provided the object property is updated. 807 */ 808 function insertObject( $table, &$object, $keyName = NULL ) 809 { 810 return; 811 } 812 813 /** 814 * Update an object in the database 815 * 816 * @abstract 817 * @access public 818 * @param string 819 * @param object 820 * @param string 821 * @param boolean 822 */ 823 function updateObject( $table, &$object, $keyName, $updateNulls=true ) 824 { 825 return; 826 } 827 828 /** 829 * Print out an error statement 830 * 831 * @param boolean If TRUE, displays the last SQL statement sent to the database 832 * @return string A standised error message 833 */ 834 function stderr( $showSQL = false ) 835 { 836 if ( $this->_errorNum != 0 ) { 837 return "DB function failed with error number $this->_errorNum" 838 ."<br /><font color=\"red\">$this->_errorMsg</font>" 839 .($showSQL ? "<br />SQL = <pre>$this->_sql</pre>" : ''); 840 } else { 841 return "DB function reports no errors"; 842 } 843 } 844 845 /** 846 * Get the ID generated from the previous INSERT operation 847 * 848 * @abstract 849 * @access public 850 * @return mixed 851 */ 852 function insertid() 853 { 854 return; 855 } 856 857 /** 858 * Get the database collation 859 * 860 * @abstract 861 * @access public 862 * @return string Collation in use 863 */ 864 function getCollation() 865 { 866 return; 867 } 868 869 /** 870 * Get the version of the database connector 871 * 872 * @abstract 873 */ 874 function getVersion() 875 { 876 return 'Not available for this connector'; 877 } 878 879 /** 880 * List tables in a database 881 * 882 * @abstract 883 * @access public 884 * @return array A list of all the tables in the database 885 */ 886 function getTableList() 887 { 888 return; 889 } 890 891 /** 892 * Shows the CREATE TABLE statement that creates the given tables 893 * 894 * @abstract 895 * @access public 896 * @param array|string A table name or a list of table names 897 * @return array A list the create SQL for the tables 898 */ 899 function getTableCreate( $tables ) 900 { 901 return; 902 } 903 904 /** 905 * Retrieves information about the given tables 906 * 907 * @abstract 908 * @access public 909 * @param array|string A table name or a list of table names 910 * @param boolean Only return field types, default true 911 * @return array An array of fields by table 912 */ 913 function getTableFields( $tables, $typeonly = true ) 914 { 915 return; 916 } 917 918 // ---- 919 // ADODB Compatibility Functions 920 // ---- 921 922 /** 923 * Get a quoted database escaped string 924 * 925 * @param string A string 926 * @param boolean Default true to escape string, false to leave the string unchanged 927 * @return string 928 * @access public 929 */ 930 function Quote( $text, $escaped = true ) 931 { 932 return '\''.($escaped ? $this->getEscaped( $text ) : $text).'\''; 933 } 934 935 /** 936 * ADODB compatability function 937 * 938 * @access public 939 * @param string SQL 940 * @since 1.5 941 */ 942 function GetCol( $query ) 943 { 944 $this->setQuery( $query ); 945 return $this->loadResultArray(); 946 } 947 948 /** 949 * ADODB compatability function 950 * 951 * @access public 952 * @param string SQL 953 * @return object 954 * @since 1.5 955 */ 956 function Execute( $query ) 957 { 958 jimport( 'joomla.database.recordset' ); 959 960 $query = trim( $query ); 961 $this->setQuery( $query ); 962 if (preg_match('#^select#i', $query )) { 963 $result = $this->loadRowList(); 964 return new JRecordSet( $result ); 965 } else { 966 $result = $this->query(); 967 if ($result === false) { 968 return false; 969 } else { 970 return new JRecordSet( array() ); 971 } 972 } 973 } 974 975 /** 976 * ADODB compatability function 977 * 978 * @access public 979 * @since 1.5 980 */ 981 function SelectLimit( $query, $count, $offset=0 ) 982 { 983 jimport( 'joomla.database.recordset' ); 984 985 $this->setQuery( $query, $offset, $count ); 986 $result = $this->loadRowList(); 987 return new JRecordSet( $result ); 988 } 989 990 /** 991 * ADODB compatability function 992 * 993 * @access public 994 * @since 1.5 995 */ 996 function PageExecute( $sql, $nrows, $page, $inputarr=false, $secs2cache=0 ) 997 { 998 jimport( 'joomla.database.recordset' ); 999 1000 $this->setQuery( $sql, $page*$nrows, $nrows ); 1001 $result = $this->loadRowList(); 1002 return new JRecordSet( $result ); 1003 } 1004 /** 1005 * ADODB compatability function 1006 * 1007 * @access public 1008 * @param string SQL 1009 * @return array 1010 * @since 1.5 1011 */ 1012 function GetRow( $query ) 1013 { 1014 $this->setQuery( $query ); 1015 $result = $this->loadRowList(); 1016 return $result[0]; 1017 } 1018 1019 /** 1020 * ADODB compatability function 1021 * 1022 * @access public 1023 * @param string SQL 1024 * @return mixed 1025 * @since 1.5 1026 */ 1027 function GetOne( $query ) 1028 { 1029 $this->setQuery( $query ); 1030 $result = $this->loadResult(); 1031 return $result; 1032 } 1033 1034 /** 1035 * ADODB compatability function 1036 * 1037 * @since 1.5 1038 */ 1039 function BeginTrans() 1040 { 1041 } 1042 1043 /** 1044 * ADODB compatability function 1045 * 1046 * @since 1.5 1047 */ 1048 function RollbackTrans() 1049 { 1050 } 1051 1052 /** 1053 * ADODB compatability function 1054 * 1055 * @since 1.5 1056 */ 1057 function CommitTrans() 1058 { 1059 } 1060 1061 /** 1062 * ADODB compatability function 1063 * 1064 * @since 1.5 1065 */ 1066 function ErrorMsg() 1067 { 1068 return $this->getErrorMsg(); 1069 } 1070 1071 /** 1072 * ADODB compatability function 1073 * 1074 * @since 1.5 1075 */ 1076 function ErrorNo() 1077 { 1078 return $this->getErrorNum(); 1079 } 1080 1081 /** 1082 * ADODB compatability function 1083 * 1084 * @since 1.5 1085 */ 1086 function GenID( $foo1=null, $foo2=null ) 1087 { 1088 return '0'; 1089 } 1090 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Mar 28 15:54:07 2012 | Cross-referenced by PHPXref 0.7.1 |