Source code for file /joomla/database/database.php
Documentation is available at database.php
* @version $Id: database.php 12694 2009-09-11 21:03:02Z ian $
* @package Joomla.Framework
* @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
* Database connector class
* @package Joomla.Framework
* The database driver name
* The database error number
* The database error message
* The prefix used on all database tables
* The limit for the query
* The for offset for the limit
* The number of queries performed by the object instance
* The null/zero date string
* Quote for named objects
* The fields that are to be quote
* Database object constructor
* @param array List of options used to configure the connection
// 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.
* 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
if (!isset
( $instances )) {
if (empty($instances[$signature]))
$database =
array_key_exists('database', $options) ?
$options['database'] :
null;
$path =
dirname(__FILE__
).
DS.
'database'.
DS.
$driver.
'.php';
$error =
JError::raiseError( 500, JTEXT::_('Unable to load Database Driver:') .
$driver);
$adapter =
'JDatabase'.
$driver;
$instance =
new $adapter($options);
if ( $error =
$instance->getErrorMsg() )
$error =
JError::raiseError( 500, JTEXT::_('Unable to connect to the database:') .
$error);
$instances[$signature] =
& $instance;
return $instances[$signature];
* Database object destructor
* Get the database connectors
* @return array An array of available session handlers
jimport('joomla.filesystem.folder');
foreach($handlers as $handler)
$class =
'JDatabase'.
ucfirst($name);
require_once(dirname(__FILE__
).
DS.
'database'.
DS.
$name.
'.php');
* Test to see if the MySQLi connector is available
* @return boolean True on success, false otherwise.
* Determines if the connection to the server is active.
* 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
* Splits a string of queries into an array of individual queries
* @param string The queries to split
$current =
substr($queries,$i,1);
if(($current ==
'"' ||
$current ==
'\'')) {
while(substr($queries,$i -
$n +
1, 1) ==
'\\' &&
$n <
$i) {
if($current ==
$open_char) {
if(($current ==
';' &&
!$open)||
$i ==
$end -
1) {
$query_split[] =
substr($queries, $start, ($i -
$start +
1));
* 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
* @param string The string to be escaped
* @param boolean Optional parameter to provide extra escaping
* Get a database error log
* Get the total number of queries made
* Quote an identifier name (field, table, etc)
* @return string The quoted name
// Only quote if the name is not using dot-notation
if (strpos( $s, '.' ) ===
false)
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.
* Update an 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
* Shows the CREATE TABLE statement that creates the given tables
* @param array|string A table name or a list of table names
* @return array A list the create SQL for the tables
* Retrieves information about the given tables
* @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
// ADODB Compatibility Functions
* Get a quoted database escaped string
* @param boolean Default true to escape string, false to leave the string unchanged
function Quote( $text, $escaped =
true )
return '\''.
($escaped ?
$this->getEscaped( $text ) :
$text).
'\'';
* ADODB compatability function
* ADODB compatability function
jimport( 'joomla.database.recordset' );
$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 )