Source code for file /joomla/database/database.php
Documentation is available at database.php
* @version $Id: database.php 4350 2006-07-28 03:34:45Z eddiea $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2007 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
* Database connector class
* @package Joomla.Framework
/** @var string The database driver name */
/** @var string Internal variable to hold the query sql */
/** @var int Internal variable to hold the database error number */
/** @var string Internal variable to hold the database error message */
/** @var string Internal variable to hold the prefix used on all database tables */
/** @var Internal variable to hold the connector resource */
/** @var Internal variable to hold the last query cursor */
/** @var boolean Debug option */
/** @var int The limit for the query */
/** @var int The for offset for the limit */
/** @var int A counter for the number of queries performed by the object instance */
/** @var array A log of queries */
/** @var string The null/zero date string */
/** @var string Quote for named objects */
* @var boolean UTF-8 support
* @var array The fields that are to be quote
* @var bool Legacy compatibility
* Database object constructor
* @param string Database host
* @param string Database user name
* @param string Database user password
* @param string Database name
* @param string Common prefix for all tables
function __construct( $host=
'localhost', $user, $pass, $db=
'', $table_prefix=
'')
// Determine utf-8 support
//Set charactersets (needed for MySQL 4.1.2+)
// Register faked "destructor" in PHP4 to close all connections we might have made
* Returns a reference to the global Database object, only creating it
* if it doesn't already exist.
* @param string Database driver
* @param string Database host
* @param string Database user name
* @param string Database user password
* @param string Database name
* @param string Common prefix for all tables
* @return JDatabase A database object
function &getInstance( $driver=
'mysql', $host=
'localhost', $user, $pass, $db=
'', $table_prefix=
'' )
if (!isset
( $instances )) {
$signature =
serialize(array($driver, $host, $user, $pass, $db, $table_prefix));
if (empty($instances[$signature])) {
jimport('joomla.database.database.'.
$driver);
$adapter =
'JDatabase'.
$driver;
$instances[$signature] =
new $adapter($host, $user, $pass, $db, $table_prefix);
return $instances[$signature];
* Database object destructor
* Custom settings for UTF support
* Adds a field or array of field names to the list that are to be quoted
* @param mixed Field name or array of names
* Checks if field name needs to be quoted
* @param string The field name
* Sets the debug level on or off
* @param int 0 = off, 1 = on
function debug( $level ) {
* Get the database UTF-8 support
* @return int The error number for the most recent query
* @return string The error message for the most recent query
* Get a database escaped string
* Quote an identifier name (field, table, etc)
* @return string The quoted name
return $q{0} .
$s .
$q{1};
* Get the database table prefix
* @return string The database prefix
* Get the database null date
* @return string Quoted null/zero date string
* 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.
* @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 function replaces a string identifier <var>$prefix</var> with the
* string held is the <var>_table_prefix</var> class variable.
* @param string The SQL query
* @param string The common table prefix
$ip =
strpos($sql, $prefix, $startPos);
$j =
strpos( $sql, "'", $startPos );
$k =
strpos( $sql, '"', $startPos );
if (($k !==
FALSE) &&
(($k <
$j) ||
($j ===
FALSE))) {
// quote comes first, find end of quote
$k =
strpos( $sql, $quoteChar, $j );
while ($l >=
0 &&
$sql{$l} ==
'\\') {
// error in the query - no end quote; ignore it
$literal .=
substr( $sql, $startPos, $k -
$startPos +
1 );
$literal .=
substr( $sql, $startPos, $n -
$startPos );
* @return string The current value of the internal SQL vairable
* @return mixed A database resource if successful, FALSE if not.
* Get the affected rows by the most recent query
* @return int The number of affected rows in the previous operation
* @return mixed A database resource if successful, FALSE if not.
function queryBatch( $abort_on_error=
true, $p_transaction_safe =
false) {
* Get the number of rows returned by the most recent query
* @param object Database resource
* @return int The number of rows
* This method loads the first field of the first row returned by the query.
* @return The value returned in the query or null if the query failed.
* Load an array of single field results into an array
* Fetch a result row as an associative array
* Load a associactive list of database rows
* @param string The field name of a primary key
* @return array If key is empty as sequential list of returned records.
* This global function loads the first row of a query into an object
* Load a list of database objects
* @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.
* Load the first row returned by the query
* @return The first row of the query.
* 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.
* @param string The field name of a primary key
* 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 ) {
* Update ab object in the database
function updateObject( $table, &$object, $keyName, $updateNulls=
true ) {
* 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 ) {
return "DB function failed with error number $this->_errorNum"
.
"<br /><font color=\"red\">$this->_errorMsg</font>"
.
($showSQL ?
"<br />SQL = <pre>$this->_sql</pre>" :
'');
return "DB function reports no errors";
* Get the ID generated from the previous INSERT operation
* Get the database collation
* @return string Collation in use
* Get the version of the database connector
return 'Not available for this connector';
* List tables in a database
* @return array A list of all the tables in the database
* @param array A list of table names
* @return array A list the create SQL for the tables
* List database table fields
* @param array A list of table names
* @return array An array of fields by table
// ADODB Compatibility Functions
* Get a quoted database escaped string
function Quote( $text ) {
* ADODB compatability function
* ADODB compatability function
jimport( 'joomla.database.recordset' );
if (eregi( '^select', $query )) {
$result =
$this->query();
* ADODB compatability function
jimport( 'joomla.database.recordset' );
$this->setQuery( $query, $offset, $count );
* ADODB compatability function
function PageExecute( $sql, $nrows, $page, $inputarr=
false, $secs2cache=
0 )
jimport( 'joomla.database.recordset' );
$this->setQuery( $sql, $page*
$nrows, $nrows );
* ADODB compatability function
* ADODB compatability function
* ADODB compatability function
* ADODB compatability function
* ADODB compatability function
* ADODB compatability function
* ADODB compatability function
* ADODB compatability function
function GenID( $foo1=
null, $foo2=
null ) {