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