Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/database/database/mysql.php

Documentation is available at mysql.php

  1. <?php
  2. /**
  3. @version        $Id: mysql.php 6757 2007-03-03 00:47:05Z CoolAcid $
  4. @package        Joomla.Framework
  5. @subpackage    Database
  6. @copyright    Copyright (C) 2005 - 2007 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.  * MySQL database driver
  20.  *
  21.  * @package        Joomla.Framework
  22.  * @subpackage    Database
  23.  * @since        1.0
  24.  */
  25. class JDatabaseMySQL extends JDatabase
  26. {
  27.     /** @var string The database driver name */
  28.     var $name            = 'mysql';
  29.     /** @var string The null/zero date string */
  30.     var $_nullDate        = '0000-00-00 00:00:00';
  31.     /** @var string Quote for named objects */
  32.     var $_nameQuote        = '`';
  33.  
  34.     /**
  35.     * Database object constructor
  36.     * @param string Database host
  37.     * @param string Database user name
  38.     * @param string Database user password
  39.     * @param string Database name
  40.     * @param string Common prefix for all tables
  41.     */
  42.     function __construct$host='localhost'$user$pass$db=''$table_prefix='')
  43.     {
  44.         // perform a number of fatality checks, then die gracefully
  45.         if (!function_exists'mysql_connect' )) {
  46.             $this->_errorNum = 1;
  47.             $this->_errorMsg = 'The MySQL adapter "mysql" is not available.';
  48.             return;
  49.         }
  50.  
  51.         if (!($this->_resource = @mysql_connect$host$user$passtrue ))) {
  52.             $this->_errorNum = 2;
  53.             $this->_errorMsg = 'Could not connect to MySQL';
  54.             return;
  55.         }
  56.  
  57.         if ($db != '' && !mysql_select_db$db$this->_resource )) {
  58.             $this->_errorNum = 3;
  59.             $this->_errorMsg = 'Could not connect to database';
  60.             return;
  61.         }
  62.  
  63.         // if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems
  64.         if strpos$this->getVersion()'5' === {
  65.             $this->setQuery"SET sql_mode = 'MYSQL40'" );
  66.             $this->query();
  67.         }
  68.  
  69.         parent::__construct($host$user$pass$db$table_prefix);
  70.     }
  71.  
  72.     /**
  73.      * Database object destructor
  74.      *
  75.      * @return boolean 
  76.      * @since 1.5
  77.      */
  78.     function __destruct()
  79.     {
  80.         $return false;
  81.         if (is_resource($this->_resource)) {
  82.             $return mysql_close($this->_resource);
  83.         }
  84.         return $return;
  85.     }
  86.  
  87.     /**
  88.      * Determines UTF support
  89.      */
  90.     function hasUTF()
  91.     {
  92.         $verParts explode'.'$this->getVersion() );
  93.         return ($verParts[0== || ($verParts[0== && $verParts[1== && (int)$verParts[2>= 2));
  94.     }
  95.  
  96.     /**
  97.      * Custom settings for UTF support
  98.      */
  99.     function setUTF()
  100.     {
  101.         //mysql_query("SET CHARACTER SET utf8",$this->_resource);
  102.         mysql_query"SET NAMES 'utf8'"$this->_resource );
  103.     }
  104.  
  105.     /**
  106.     * Get a database escaped string
  107.     * @return string 
  108.     */
  109.     function getEscaped$text )
  110.     {
  111.         return mysql_real_escape_string$text$this->_resource );
  112.     }
  113.  
  114.     /**
  115.     * Execute the query
  116.     * @return mixed A database resource if successful, FALSE if not.
  117.     */
  118.     function query()
  119.     {
  120.         if (!is_resource($this->_resource)) {
  121.             return false;
  122.         }
  123.  
  124.         if ($this->_limit > || $this->_offset > 0{
  125.             $this->_sql .= ' LIMIT '.$this->_offset.', '.$this->_limit;
  126.         }
  127.         if ($this->_debug{
  128.             $this->_ticker++;
  129.             $this->_log[$this->_sql;
  130.         }
  131.         $this->_errorNum = 0;
  132.         $this->_errorMsg = '';
  133.         $this->_cursor = mysql_query$this->_sql$this->_resource );
  134.  
  135.         if (!$this->_cursor)
  136.         {
  137.             $this->_errorNum = mysql_errno$this->_resource );
  138.             $this->_errorMsg = mysql_error$this->_resource )." SQL=$this->_sql";
  139.  
  140.             if ($this->_debug{
  141.                 JError::raiseError('joomla.database:'.$this->_errorNum'JDatabaseMySQL::query: '.$this->_errorMsg );
  142.             }
  143.             return false;
  144.         }
  145.         return $this->_cursor;
  146.     }
  147.  
  148.     /**
  149.      * @return int The number of affected rows in the previous operation
  150.      * @since 1.0.5
  151.      */
  152.     function getAffectedRows()
  153.     {
  154.         return mysql_affected_rows$this->_resource );
  155.     }
  156.  
  157.     /**
  158.     * Execute a batch query
  159.     * @return mixed A database resource if successful, FALSE if not.
  160.     */
  161.     function queryBatch$abort_on_error=true$p_transaction_safe false)
  162.     {
  163.         $this->_errorNum = 0;
  164.         $this->_errorMsg = '';
  165.         if ($p_transaction_safe{
  166.             $si mysql_get_server_info$this->_resource );
  167.             preg_match_all"/(\d+)\.(\d+)\.(\d+)/i"$si$m );
  168.             if ($m[1>= 4{
  169.                 $this->_sql = 'START TRANSACTION;' $this->_sql . '; COMMIT;';
  170.             else if ($m[2>= 23 && $m[3>= 19{
  171.                 $this->_sql = 'BEGIN WORK;' $this->_sql . '; COMMIT;';
  172.             else if ($m[2>= 23 && $m[3>= 17{
  173.                 $this->_sql = 'BEGIN;' $this->_sql . '; COMMIT;';
  174.             }
  175.         }
  176.         $query_split preg_split ("/[;]+/"$this->_sql);
  177.         $error 0;
  178.         foreach ($query_split as $command_line{
  179.             $command_line trim$command_line );
  180.             if ($command_line != ''{
  181.                 $this->_cursor = mysql_query$command_line$this->_resource );
  182.                 if (!$this->_cursor{
  183.                     $error 1;
  184.                     $this->_errorNum .= mysql_errno$this->_resource ' ';
  185.                     $this->_errorMsg .= mysql_error$this->_resource )." SQL=$command_line <br />";
  186.                     if ($abort_on_error{
  187.                         return $this->_cursor;
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.         return $error false true;
  193.     }
  194.  
  195.     /**
  196.     * Diagnostic function
  197.     */
  198.     function explain()
  199.     {
  200.         $temp $this->_sql;
  201.         $this->_sql = "EXPLAIN $this->_sql";
  202.         $this->query();
  203.  
  204.         if (!($cur $this->query())) {
  205.             return null;
  206.         }
  207.         $first true;
  208.  
  209.         $buffer '<table id="explain-sql">';
  210.         $buffer .= '<thead><tr><td colspan="99">'.$this->getQuery().'</td></tr>';
  211.         while ($row mysql_fetch_assoc$cur )) {
  212.             if ($first{
  213.                 $buffer .= '<tr>';
  214.                 foreach ($row as $k=>$v{
  215.                     $buffer .= '<th>'.$k.'</th>';
  216.                 }
  217.                 $buffer .= '</tr>';
  218.                 $first false;
  219.             }
  220.             $buffer .= '</thead><tbody><tr>';
  221.             foreach ($row as $k=>$v{
  222.                 $buffer .= '<td>'.$v.'</td>';
  223.             }
  224.             $buffer .= '</tr>';
  225.         }
  226.         $buffer .= '</tbody></table>';
  227.         mysql_free_result$cur );
  228.  
  229.         $this->_sql = $temp;
  230.  
  231.         return $buffer;
  232.     }
  233.     /**
  234.     * @return int The number of rows returned from the most recent query.
  235.     */
  236.     function getNumRows$cur=null )
  237.     {
  238.         return mysql_num_rows$cur $cur $this->_cursor );
  239.     }
  240.  
  241.     /**
  242.     * This method loads the first field of the first row returned by the query.
  243.     *
  244.     * @return The value returned in the query or null if the query failed.
  245.     */
  246.     function loadResult()
  247.     {
  248.         if (!($cur $this->query())) {
  249.             return null;
  250.         }
  251.         $ret null;
  252.         if ($row mysql_fetch_row$cur )) {
  253.             $ret $row[0];
  254.         }
  255.         mysql_free_result$cur );
  256.         return $ret;
  257.     }
  258.     /**
  259.     * Load an array of single field results into an array
  260.     */
  261.     function loadResultArray($numinarray 0)
  262.     {
  263.         if (!($cur $this->query())) {
  264.             return null;
  265.         }
  266.         $array array();
  267.         while ($row mysql_fetch_row$cur )) {
  268.             $array[$row[$numinarray];
  269.         }
  270.         mysql_free_result$cur );
  271.         return $array;
  272.     }
  273.  
  274.     /**
  275.     * Fetch a result row as an associative array
  276.     *
  277.     * return array
  278.     */
  279.     function loadAssoc()
  280.     {
  281.         if (!($cur $this->query())) {
  282.             return null;
  283.         }
  284.         $ret null;
  285.         if ($array mysql_fetch_assoc$cur )) {
  286.             $ret $array;
  287.         }
  288.         mysql_free_result$cur );
  289.         return $ret;
  290.     }
  291.  
  292.     /**
  293.     * Load a assoc list of database rows
  294.     * @param string The field name of a primary key
  295.     * @return array If <var>key</var> is empty as sequential list of returned records.
  296.     */
  297.     function loadAssocList$key='' )
  298.     {
  299.         if (!($cur $this->query())) {
  300.             return null;
  301.         }
  302.         $array array();
  303.         while ($row mysql_fetch_assoc$cur )) {
  304.             if ($key{
  305.                 $array[$row[$key]] $row;
  306.             else {
  307.                 $array[$row;
  308.             }
  309.         }
  310.         mysql_free_result$cur );
  311.         return $array;
  312.     }
  313.     /**
  314.     * This global function loads the first row of a query into an object
  315.     *
  316.     * return object
  317.     */
  318.     function loadObject)
  319.     {
  320.         if (!($cur $this->query())) {
  321.             return null;
  322.         }
  323.         $ret null;
  324.         if ($object mysql_fetch_object$cur )) {
  325.             $ret $object;
  326.         }
  327.         mysql_free_result$cur );
  328.         return $ret;
  329.     }
  330.     /**
  331.     * Load a list of database objects
  332.     * @param string The field name of a primary key
  333.     * @return array If <var>key</var> is empty as sequential list of returned records.
  334.     *  If <var>key</var> is not empty then the returned array is indexed by the value
  335.     *  the database key.  Returns <var>null</var> if the query fails.
  336.     */
  337.     function loadObjectList$key='' )
  338.     {
  339.         if (!($cur $this->query())) {
  340.             return null;
  341.         }
  342.         $array array();
  343.         while ($row mysql_fetch_object$cur )) {
  344.             if ($key{
  345.                 $array[$row->$key$row;
  346.             else {
  347.                 $array[$row;
  348.             }
  349.         }
  350.         mysql_free_result$cur );
  351.         return $array;
  352.     }
  353.     /**
  354.     * @return The first row of the query.
  355.     */
  356.     function loadRow()
  357.     {
  358.         if (!($cur $this->query())) {
  359.             return null;
  360.         }
  361.         $ret null;
  362.         if ($row mysql_fetch_row$cur )) {
  363.             $ret $row;
  364.         }
  365.         mysql_free_result$cur );
  366.         return $ret;
  367.     }
  368.     /**
  369.     * Load a list of database rows (numeric column indexing)
  370.     * @param string The field name of a primary key
  371.     * @return array If <var>key</var> is empty as sequential list of returned records.
  372.     *  If <var>key</var> is not empty then the returned array is indexed by the value
  373.     *  the database key.  Returns <var>null</var> if the query fails.
  374.     */
  375.     function loadRowList$key=null )
  376.     {
  377.         if (!($cur $this->query())) {
  378.             return null;
  379.         }
  380.         $array array();
  381.         while ($row mysql_fetch_row$cur )) {
  382.             if ($key !== null{
  383.                 $array[$row[$key]] $row;
  384.             else {
  385.                 $array[$row;
  386.             }
  387.         }
  388.         mysql_free_result$cur );
  389.         return $array;
  390.     }
  391.     /**
  392.      * Inserts a row into a table based on an objects properties
  393.      * @param    string    The name of the table
  394.      * @param    object    An object whose properties match table fields
  395.      * @param    string    The name of the primary key. If provided the object property is updated.
  396.      */
  397.     function insertObject$table&$object$keyName NULL )
  398.     {
  399.         $fmtsql "INSERT INTO $table ( %s ) VALUES ( %s ) ";
  400.         $fields array();
  401.         foreach (get_object_vars$object as $k => $v{
  402.             if (is_array($vor is_object($vor $v === NULL{
  403.                 continue;
  404.             }
  405.             if ($k[0== '_'// internal field
  406.                 continue;
  407.             }
  408.             $fields[$this->nameQuote$k );;
  409.             $values[$this->isQuoted$k $this->Quote$v $v;
  410.         }
  411.         $this->setQuerysprintf$fmtsqlimplode","$fields ,  implode","$values ) ) );
  412.         if (!$this->query()) {
  413.             return false;
  414.         }
  415.         $id $this->insertid();
  416.         if ($keyName && $id{
  417.             $object->$keyName $id;
  418.         }
  419.         return true;
  420.     }
  421.  
  422.     /**
  423.      * Document::db_updateObject()
  424.      * @param [type] $updateNulls 
  425.      */
  426.     function updateObject$table&$object$keyName$updateNulls=true )
  427.     {
  428.         $fmtsql "UPDATE $table SET %s WHERE %s";
  429.         $tmp array();
  430.         foreach (get_object_vars$object as $k => $v)
  431.         {
  432.             ifis_array($vor is_object($vor $k[0== '_' // internal or NA field
  433.                 continue;
  434.             }
  435.             if$k == $keyName // PK not to be updated
  436.                 $where $keyName '=' $this->Quote$v );
  437.                 continue;
  438.             }
  439.             if ($v === null)
  440.             {
  441.                 if ($updateNulls{
  442.                     $val 'NULL';
  443.                 else {
  444.                     continue;
  445.                 }
  446.             else {
  447.                 $val $this->isQuoted$k $this->Quote$v $v;
  448.             }
  449.             $tmp[$this->nameQuote$k '=' $val;
  450.         }
  451.         $this->setQuerysprintf$fmtsqlimplode","$tmp $where ) );
  452.         return $this->query();
  453.     }
  454.  
  455.     function insertid()
  456.     {
  457.         return mysql_insert_id$this->_resource );
  458.     }
  459.  
  460.     function getVersion()
  461.     {
  462.         return mysql_get_server_info$this->_resource );
  463.     }
  464.     /**
  465.      * Assumes database collation in use by sampling one text field in one table
  466.      * @return string Collation in use
  467.      */
  468.     function getCollation ()
  469.     {
  470.         if $this->hasUTF() ) {
  471.             $this->setQuery'SHOW FULL COLUMNS FROM #__content' );
  472.             $array $this->loadAssocList();
  473.             return $array['4']['Collation'];
  474.         else {
  475.             return "N/A (mySQL < 4.1.2)";
  476.         }
  477.     }
  478.  
  479.     /**
  480.      * @return array A list of all the tables in the database
  481.      */
  482.     function getTableList()
  483.     {
  484.         $this->setQuery'SHOW TABLES' );
  485.         return $this->loadResultArray();
  486.     }
  487.     /**
  488.      * @param array A list of table names
  489.      * @return array A list the create SQL for the tables
  490.      */
  491.     function getTableCreate$tables )
  492.     {
  493.         $result array();
  494.  
  495.         foreach ($tables as $tblval{
  496.             $this->setQuery'SHOW CREATE table ' $this->getEscaped$tblval ) );
  497.             $rows $this->loadRowList();
  498.             foreach ($rows as $row{
  499.                 $result[$tblval$row[1];
  500.             }
  501.         }
  502.  
  503.         return $result;
  504.     }
  505.     /**
  506.      * @param array A list of table names
  507.      * @return array An array of fields by table
  508.      */
  509.     function getTableFields$tables )
  510.     {
  511.         $result array();
  512.  
  513.         foreach ($tables as $tblval{
  514.             $this->setQuery'SHOW FIELDS FROM ' $tblval );
  515.             $fields $this->loadObjectList();
  516.             foreach ($fields as $field{
  517.                 $result[$tblval][$field->Fieldpreg_replace("/[(0-9)]/",''$field->Type );
  518.             }
  519.         }
  520.  
  521.         return $result;
  522.     }
  523. }

Documentation generated on Mon, 05 Mar 2007 21:11:35 +0000 by phpDocumentor 1.3.1