<?php
/**
* This file is part of the easy-CMS project.
* easy-CMS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* easy-CMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with easy-CMS; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
	
include_once('classes/db/DBMSData.class.php');
include_once('classes/db/DBConnection.class.php');
include_once('classes/core/CoreObject.class.php');

/**
* Manage the calls of specific MySQL functions for DBConnection functions.
*
* @access   private
* @author	Cedric Francoys <cedric.francoys@easy-cms.org>
* @version 	$Id: DBConnectionMySQL.class.php,v 1.1.1.1 2006/02/28 12:05:54 vinadelmar Exp $
* @package	DB
*/
class DBConnectionMySQL extends DBConnection {

	var $sql_handler;
	
	/**
	 * Class constructor.
	 *
	 * Initialize the DBMSData object containing data for SQL transactions.
	 *
	 * @access   public
 	 * @param    string    The hostname of the DB server.
	 * @param    string    The name of the DB.
	 * @param    string    The username to use for the connection to the DB.
	 * @param    string    The password to use for the connection to the DB.
 	 * @return   void
	 */
	function DBConnectionMySQL(&$dbData) {
		parent::DBConnection(MYSQL, $dbData);
	}
	
	/**
	 * Open the DBMS connection.
	 *
	 * @return   integer   The status of the connect function call.
	 * @access   public
	 */
	function connect() {
		if($this->sql_handler=mysql_connect($this->dbData->getHostName(), $this->dbData->getUserName(), $this->dbData->getPassword())){
			$result = mysql_select_db($this->dbData->getDBName());
		}
		else {
			die('Unable to establish DB connection : check your parameters.');
		}
		return $result;
	}

	/**
	* Close the DBMS connection.
	*
	* @return   integer   The status of the close function call.
	* @access   public
	*/
	function disconnect() {
		if(!$result = mysql_close($this->sql_handler)) {
			$this->setLastError(mysql_errno()." : ".mysql_error());
		}
		return $result;
	}

	/**
	* Send a SQL query.
	*
	* @param string The query to send to the DBMS.
	* @return resource Returns a resource identifier or -1 if the query was not executed correctly.
	*/
	function sendQuery($query) {
		$result = true;

		if(($result = mysql_query($query)) === false) {
			$this->setErrNo(mysql_errno());
			$this->setErrMsg(mysql_error());
			trigger_error('DBManipulatorMySQL::sendQuery, error : query failure (\''.$query.'\'), '.$this->getErrMsg().'(#'.$this->getErrNo().')', E_USER_ERROR);
		}
		else {
			$this->setAffectedRows(mysql_affected_rows());

			if (is_resource($result)) {
				$this->setNumRows(mysql_num_rows($result));
			}
			else {
			  $this->setNumRows(0);
			}
			$this->setLastId(mysql_insert_id());
		}
		return $result;
	}

	function fetchRow(&$array) {
		if(($result = mysql_fetch_row($array)) === false) {
   		$this->setLastError(mysql_errno()." : ".mysql_error());
		}
		return $result;
	}

	function fetchArray(&$array) {
		if(($result = mysql_fetch_array($array)) === false) {
  		$this->setLastError(mysql_errno()." : ".mysql_error());
    }
		return $result;
	}
}
?>
