Location: PHPKode > projects > OpenNitro > trunk/Nitro/NitroDB/mysql.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: NitroDB :: MySQL                                                 |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library 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 Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// +---------------------------------------------------------------------------+
//
// $Id: mysql.inc.php 229 2008-04-17 09:20:31Z oli $
//
// Nitro MySQL database class
//

/**
 * This file contains the MySQL implementation of the Nitro database layer
 *
 * @package	Nitro
 * @subpackage	DB
 * @author 		Siggi Oskarsson
 * @version 	$Revision: 1.25 $
 * @copyright	2004 June Systems BV
 */

/**
 * MySQL database class definition
 *
 * This is the MySQL handler implementation for the database layer
 * class. The functions in this class should not be called directly,
 * but should always be called through the main Nitro database class.
 *
 * @package	Nitro
 * @subpackage	DB
 * @see DB
 */
class DB_mysql extends DB_Nitro {
	/**
	 * @ignore
	 */
	var $DBHandlerID = 'MySQL';
	/**
	 * @ignore
	 */
	var $Host;
	/**
	 * @ignore
	 */
	var $Port;
	/**
	 * @ignore
	 */
	var $Username;
	/**
	 * @ignore
	 */
	var $Password;
	/**
	 * @ignore
	 */
	var $Database;
	/**
	 * @ignore
	 */
	var $ConnectionID;

	/**
	 * @ignore
	 */
	var $FetchMode = MYSQL_ASSOC;
	/**
	 * @ignore
	 */
	var $FetchModes = Array(NITRODB_NUM => MYSQL_NUM,
													NITRODB_ASSOC => MYSQL_ASSOC,
													NITRODB_BOTH => MYSQL_BOTH
													);

	/**
	 * @ignore
	 */
	var $Error;

	/**
	 * @ignore
	 */
	function DB_mysql($Host, $Username, $Password, $Database, $Port)
	{
		$this->Host = $Host;
		$this->Port = $Port;
		$this->Username = $Username;
		$this->Password = $Password;
		$this->Database = $Database;
	}
	
	/**
	 * @ignore
	 */
	function Connect($NewLink = FALSE)
	{
		if ($NewLink) {
			$this->ConnectionID = @mysql_connect($this->Host, $this->Username, $this->Password, TRUE);
		} else {
			$this->ConnectionID = @mysql_connect($this->Host, $this->Username, $this->Password);
		}
		if ($this->ConnectionID) {
			if(@mysql_select_db($this->Database, $this->ConnectionID)) {
				return $this->ConnectionID;
			} else {
				$this->Error = "Failed selecting database";
				return NULL;
			}
		} else {
			$this->Error = "Failed connecting to dabase server";
			return NULL;
		}
	}
	
	/**
	 * @ignore
	 */
	function query($Query, $Limit = 0, $Offset = 0)
	{
		@mysql_select_db($this->Database, $this->ConnectionID);
		$Result = new NitroDB_mysql_Result($Query, $this->ConnectionID);
		if (DB::isError($Result)) {
			$this->Error = __CLASS__."::".__FUNCTION__.": ".$Result->Error;
		} else {
			$this->Error = FALSE;
		}

		return $Result;
	}
	
	/**
	 * @ignore
	 */
	function listTables()
	{
		$RV = Array();

		$Result = $this->query("SHOW TABLES FROM ".$this->Database);

		if (DB::isError($Result)) {
			$this->Error = __CLASS__."::".__FUNCTION__.": ".$Result->Error;
		}

		while ($row = $Result->fetchArray(NITRODB_NUM)) {
			$RV[] = $row[0];
		}

		$Result->free();

		return $RV;
	}
	
	/**
	 * @ignore
	 */
	function listFields($Table)
	{
		$RV = Array();

		$Result = $this->query("SHOW COLUMNS FROM ".addslashes($Table));

		if (DB::isError($Result)) {
			$this->Error = __CLASS__."::".__FUNCTION__.": ".$Result->Error;
		}

		while ($row = $Result->fetchArray()) {
			$RV[$row['Field']] = $row;
		}

		$Result->free();

		return $RV;
	}
	
	/**
	 * @ignore
	 */
	function escapeString($String)
	{
		return mysql_real_escape_string($String);
	}

	/**
	 * @ignore
	 */
	function getDateTimeFunction()
	{
		return "CURRENT_TIMESTAMP";
	}

	/**
	 * @ignore
	 */
	function InsertID()
	{
		return mysql_insert_id($this->ConnectionID);
	}
	
	/**
	 * @ignore
	 */
	function PrepareQuery($Query)
	{
		return $Query;
	}

	/**
	 * @ignore
	 */
	function Limit($Query, $Limit, $Offset = 0)
	{
		$Query = $Query." LIMIT $Limit".($Offset > 0 ? ", $Offset" : "");
		
		return $Query;
	}
	
	/**
	 * @ignore
	 */
	function CheckSlave()
	{
		// TODO: make this better! This doesn't really work....
		//return @mysql_query('SHOW SLAVE STATUS', $this->ConnectionID);
		return TRUE;	
	}
}

/**
 * MySQL Result object implementation
 *
 * <CODE>
 * $Query         = 'SELECT * FROM User';
 * $Result        = $DBConnection->query($Query);
 * $AffectedRows  = $Result->affectedRows();
 * $Row           = $Result->fetchArray();
 * $Value         = $Result->fetchResult(0,0);
 * $Row           = $Result->fetchRow();
 * $FreeResult    = $Result->free();
 * $Error         = $Result->getMessage();
 * $Value         = $Result->getResult(0,0);
 * $InsertID      = $Result->insertID();
 * $NumRows       = $Result->numRows();
 * </CODE>
 *
 * @package	Nitro
 * @subpackage	DB
 * @access	public
 */
class NitroDB_mysql_Result extends NitroDB_Nitro_Result {
	/**
	 * @ignore
	 */
	var $ConnectionID;
	/**
	 * @var	string	Query
	 */
	var $Query;
	/**
	 * @var	resource	MySQL result resource
	 */
	var $Result;
	/**
	 * @var	string	MySQL result error
	 */
	var $Error;
	/**
	 * @var	int	MySQL insert id
	 */
	var $InsertID = NULL;
	/**
	 * @var	int	MySQL affected rows
	 */
	var $AffectedRows = NULL;
	/**
	 * @var	int	Default MySQL fetchmode (MYSQL_ASSOC)
	 */
	var $FetchMode = MYSQL_ASSOC;
	/**
	 * @var	int	Nitro fetchmode translation to MySQL fetchmodes
	 */
	var $FetchModes = Array(NITRODB_NUM => MYSQL_NUM,
													NITRODB_ASSOC => MYSQL_ASSOC,
													NITRODB_BOTH => MYSQL_BOTH
													);

	/**
	 * @ignore	constructor
	 */
	function NitroDB_mysql_Result($Query, $ConnectionID)
	{
		DebugGroup("DB", "Result", "query :".$Query, __FILE__, __LINE__, DEBUG_SQL_OK);
		$this->ConnectionID = $ConnectionID;
		$this->Query = $Query;

		if ($this->Result = @mysql_query($Query, $ConnectionID)) {
			Debug("DB", "Result", "Result: ".$this->Result, __FILE__, __LINE__, DEBUG_SQL_OK);

			$this->InsertID = (eregi('^[:space:]*[INSERT|REPLACE]', $Query) ? @mysql_insert_id($this->ConnectionID) : NULL);
			Debug(__CLASS__, __FUNCTION__, "Insert id {$this->InsertID}", __FILE__, __LINE__, DEBUG_SQL_OK);

			$this->AffectedRows = @mysql_affected_Rows($this->ConnectionID);
			Debug(__CLASS__, __FUNCTION__, "Affected rows {$this->AffectedRows}", __FILE__, __LINE__, DEBUG_SQL_OK);

			$this->Error = FALSE;
		} else {
			Debug("DB", "Result", "Query failed: ".mysql_error(), __FILE__, __LINE__, DEBUG_SQL_ERR);
			$this->Error = mysql_error()."\nQUERY:\n".$Query;
		}
		DebugCloseGroup(DEBUG_SQL_OK);
	}
	
	/**
	 * Get the number of rows in the result
	 *
	 * This function returns the number of rows in the result set
	 * for the query that was run or FALSE on error.
	 *
	 * @return	mixed	Number of rows or FALSE on error
	 * @access	public
	 */
	function numRows()
	{
		if ($this->Result && !$this->Error) {
			$RV = mysql_num_rows($this->Result);
			Debug(__CLASS__, __FUNCTION__, "NumRows: ".$RV, __FILE__, __LINE__, DEBUG_SQL_OK);
		} else {
			Debug(__CLASS__, __FUNCTION__, "Numrows failed, not a result or Error present", __FILE__, __LINE__, DEBUG_SQL_ERR);
			$RV = FALSE;
		}
		return $RV;
	}
	
	/**
	 * Fetch 1 row from the result
	 *
	 * This function fetches exactly one row from the result and returns
	 * it as an array. By using the FetchMode parameter the user can choose
	 * whether to return an associative or indexed array or both.
	 *
	 * @param	mixed	$FetchMode	Fetchmode to use in return array
	 * @return	mixed	Return array or FALSE on error
	 * @access	public
	 */
	function fetchArray($FetchMode = FALSE)
	{
		if ($this->Result && !$this->Error) {
			Debug(__CLASS__, __FUNCTION__, "fetching array", __FILE__, __LINE__, DEBUG_SQL_OKV);
			$RV = mysql_fetch_array($this->Result, ($FetchMode ? $this->FetchModes[$FetchMode] : $this->FetchMode));
		} else {
			Debug(__CLASS__, __FUNCTION__, "fetchArray failed, not a result or Error present", __FILE__, __LINE__, DEBUG_SQL_ERR);
			$RV = FALSE;
		}
		return $RV;
	}
	
	/**
	 * Get 1 value from result
	 *
	 * Returns exactly 1 value from the result found in the row given
	 * by $Record and column given by $Field.
	 *
	 * @param	int	$Record	Row number in result
	 * @param	int	$Field	Column number in result
	 * @return	mixed	Value from result or FALSE on error
	 * @access	public
	 */
	function fetchResult($Record, $Field)
	{
		if ($this->Result && !$this->Error) {
			Debug(__CLASS__, __FUNCTION__, "fetching result ($Record, $Field)", __FILE__, __LINE__, DEBUG_SQL_OKV);
			$RV = mysql_result($this->Result, $Record, $Field);
		} else {
			Debug(__CLASS__, __FUNCTION__, "fetchResult failed, not a result or Error present", __FILE__, __LINE__, DEBUG_SQL_ERR);
			$RV = FALSE;
		}
		return $RV;
	}
	
	/**
	 * Return number of affected rows from last query
	 *
	 * This function returns the number of rows that were affected by
	 * the last query run on the database.
	 *
	 * @return	mixed	Number of rows affected or FALSE on error
	 * @access	public
	 */
	function affectedRows()
	{
		return $this->AffectedRows;
	}
	
	/**
	 * Free result set
	 *
	 * This function should always be used when the result set
	 * created is not used anymore. This will free the result from
	 * the database memory and destroy the object itself.
	 *
	 * @return	boolean	Success or failure of freeing result
	 * @access	public
	 */
	function free()
	{
		if (is_resource($this->Result) && !$this->Error) {
			Debug("DB", "Result", "Free result: ".$this->Result, __FILE__, __LINE__, DEBUG_SQL_OK);
			@mysql_free_result($this->Result);
			//DebugCloseGroup(DEBUG_SQL_OK);
			//destroy(SELF);
			$RV = TRUE;
		} else {
			$RV = FALSE;
		}
		return $RV;
	}

	/**
	 * Get last insert id
	 *
	 * This function returns the insert id of the last insert query.
	 *
	 * @return	int	Last insert id
	 * @access	public
	 */
	function InsertID()
	{
		if (!isset($this->InsertID)) {
			$this->InsertID = @mysql_insert_id($this->ConnectionID);
		}
		
		return $this->InsertID;
	}
}
?>
Return current item: OpenNitro