Source code for file /joomla/database/database/mysql.php
Documentation is available at mysql.php
* @version $Id: mysql.php 11316 2008-11-27 03:11:24Z 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
* @package Joomla.Framework
* The database driver name
* The null/zero date string
* Quote for named objects
* Database object constructor
* @param array List of options used to configure the connection
// perform a number of fatality checks, then return gracefully
$this->_errorMsg =
'The MySQL adapter "mysql" is not available.';
$this->_errorMsg =
'Could not connect to MySQL';
// finalize initialization
* Database object destructor
* Test to see if the MySQL connector is available
* @return boolean True on success, false otherwise.
* Determines if the connection to the server is active.
* Select a database for use
* @param string $database
* @return boolean True if the database has been successfully selected
$this->_errorMsg =
'Could not connect to database';
// if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems
$this->setQuery( "SET sql_mode = 'MYSQL40'" );
* @return boolean True - UTF is supported
return ($verParts[0] ==
5 ||
($verParts[0] ==
4 &&
$verParts[1] ==
1 && (int)
$verParts[2] >=
2));
* Custom settings for UTF support
* Get a database escaped string
* @param string The string to be escaped
* @param boolean Optional parameter to provide extra escaping
* @return mixed A database resource if successful, FALSE if not.
// Take a local copy so that we don't modify the original query and cause issues later
* @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)
if ($p_transaction_safe) {
$this->_sql =
'START TRANSACTION;' .
$this->_sql .
'; COMMIT;';
} else if ($m[2] >=
23 &&
$m[3] >=
19) {
$this->_sql =
'BEGIN WORK;' .
$this->_sql .
'; COMMIT;';
} else if ($m[2] >=
23 &&
$m[3] >=
17) {
$this->_sql =
'BEGIN;' .
$this->_sql .
'; COMMIT;';
foreach ($query_split as $command_line) {
$command_line =
trim( $command_line );
if ($command_line !=
'') {
$this->_log[] =
$command_line;
return $error ?
false :
true;
if (!($cur =
$this->query())) {
$buffer =
'<table id="explain-sql">';
$buffer .=
'<thead><tr><td colspan="99">'.
$this->getQuery().
'</td></tr>';
foreach ($row as $k=>
$v) {
$buffer .=
'<th>'.
$k.
'</th>';
$buffer .=
'</thead><tbody><tr>';
foreach ($row as $k=>
$v) {
$buffer .=
'<td>'.
$v.
'</td>';
$buffer .=
'</tbody></table>';
* @return int The number of rows returned from the most recent query.
* 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.
if (!($cur =
$this->query())) {
* Load an array of single field results into an array
if (!($cur =
$this->query())) {
$array[] =
$row[$numinarray];
* Fetch a result row as an associative array
if (!($cur =
$this->query())) {
* Load a assoc list of database rows
* @param string The field name of a primary key
* @return array If <var>key</var> is empty as sequential list of returned records.
if (!($cur =
$this->query())) {
$array[$row[$key]] =
$row;
* This global function loads the first row of a query into an object
if (!($cur =
$this->query())) {
* Load a list of database objects
* 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
* @return array If <var>key</var> is empty as sequential list of returned records.
if (!($cur =
$this->query())) {
$array[$row->$key] =
$row;
* @return The first row of the query.
if (!($cur =
$this->query())) {
* Load a list of database rows (numeric column indexing)
* @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.
if (!($cur =
$this->query())) {
$array[$row[$key]] =
$row;
* 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.
$fmtsql =
'INSERT INTO '.
$this->nameQuote($table).
' ( %s ) VALUES ( %s ) ';
if ($k[0] ==
'_') { // internal field
$values[] =
$this->isQuoted( $k ) ?
$this->Quote( $v ) : (int)
$v;
* @param [type] $updateNulls
function updateObject( $table, &$object, $keyName, $updateNulls=
true )
$fmtsql =
'UPDATE '.
$this->nameQuote($table).
' SET %s WHERE %s';
if( $k ==
$keyName ) { // PK not to be updated
$where =
$keyName .
'=' .
$this->Quote( $v );
$tmp[] =
$this->nameQuote( $k ) .
'=' .
$val;
* Assumes database collation in use by sampling one text field in one table
* @return string Collation in use
$this->setQuery( 'SHOW FULL COLUMNS FROM #__content' );
return $array['4']['Collation'];
return "N/A (mySQL < 4.1.2)";
* @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
settype($tables, 'array'); //force to array
foreach ($tables as $tblval) {
foreach ($rows as $row) {
$result[$tblval] =
$row[1];
* 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
settype($tables, 'array'); //force to array
foreach ($tables as $tblval)
$this->setQuery( 'SHOW FIELDS FROM ' .
$tblval );
foreach ($fields as $field) {
$result[$tblval][$field->Field] =
preg_replace("/[(0-9)]/",'', $field->Type );
foreach ($fields as $field) {
$result[$tblval][$field->Field] =
$field;