Location: PHPKode > projects > OpenNitro > trunk/Nitro/NitroDB/h8.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: NitroDB :: H8                                                    |
// +---------------------------------------------------------------------------+
// | 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: h8.inc.php 229 2008-04-17 09:20:31Z oli $
//
// Nitro H8 database class
//

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

define ('__H8_SALT', '$1$Hyper8-5$');
srand(time());

/**
 * policies for local operation
 *
 * @todo	testen define juiste type krijgt toegewezen van de waarden uit ini-file; 0 = FALSE, 1 = TRUE
 */
define ('__H8_BATCHED', 			(isset($__NitroConf->CONF['Hyper8']['H8_BATCHED']) 			 ? $__NitroConf->CONF['Hyper8']['H8_BATCHED'] 			: FALSE ));
define ('__H8_TABLE_CACHING', (isset($__NitroConf->CONF['Hyper8']['H8_TABLE_CACHING']) ? $__NitroConf->CONF['Hyper8']['H8_TABLE_CACHING'] : TRUE ));
define ('__H8_TABLE_PERSIST', (isset($__NitroConf->CONF['Hyper8']['H8_TABLE_PERSIST']) ? $__NitroConf->CONF['Hyper8']['H8_TABLE_PERSIST'] : FALSE ));
define ('__H8_TABLE_TTL', 		(isset($__NitroConf->CONF['Hyper8']['H8_TABLE_TTL']) 		 ? $__NitroConf->CONF['Hyper8']['H8_TABLE_TTL'] 		: 300 ));
define ('__H8_INDEX_PERSIST', (isset($__NitroConf->CONF['Hyper8']['H8_INDEX_PERSIST']) ? $__NitroConf->CONF['Hyper8']['H8_INDEX_PERSIST'] : FALSE ));

set_include_path(get_include_path().PATH_SEPARATOR.'/usr/lib/php');
set_include_path(get_include_path().PATH_SEPARATOR.'/usr/local/lib/php'.PATH_SEPARATOR.'/home/janroel/SVNCheckout/Hyper8/HEAD/');
include_once 'XML/RPC.php';
include_once 'H8_Wrapper/H8_Table.inc.php';
include_once 'H8_Wrapper/H8_Index.inc.php';
include_once 'H8_Wrapper/H8_Batch.inc.php';
include_once 'H8_Wrapper/H8_Utils.inc.php';

/**
 * Hyper8 database class definition
 *
 * This is the Hyper8 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_h8 {
	/**
	 * @ignore
	 */
	var $DBHandlerID = 'H8';
	/**
	 * @ignore
	 */
	var $Host;
	/**
	 * @ignore
	 */
	var $Port;
	/**
	 * @ignore
	 */
	var $Username;
	/**
	 * @ignore
	 */
	var $Password;
	/**
	 * @ignore
	 */
	var $Database;
	/**
	 * Hyper8 Table object
	 */
	var $Table;
	/**
	 * @ignore
	 */
	var $ConnectionID;

	/**
	 * @ignore
	 */
	var $FetchMode = H8_ASSOC;
	/**
	 * @ignore
	 */
	var $FetchModes = Array(NITRODB_NUM => H8_FETCHMODE_NUM,
													NITRODB_ASSOC => H8_FETCHMODE_ASSOC,
													NITRODB_BOTH => H8_FETCHMODE_BOTH
													);

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

	/**
	 * @ignore
	 */
	function DB_h8($Host, $Username, $Password, $Database, $Port)
	{
		$this->Host = $Host;
		$this->Port = $Port;
		$this->Username = $Username;
		$this->Password = $Password;
		$this->Database = $Database;
		$this->Table = FALSE;
	}
	
	/**
	 * @ignore
	 *
	 * @return mixed	$ConnectionID or FALSE to indicate an error
	 */
	function Connect($NewLink = FALSE)
	{
		if ($this->Host && $this->Host != 'local') {
			$H8Client = new XML_RPC_Client('/H8_Server.php', $this->Host, $this->Port);
			if (H8_Utils::Ping($H8Client)) {
				$this->ConnectionID = 'Remote';
			} else {
				$this->ConnectionID = FALSE;
			}
		} elseif ($this->Host == 'local') {
			$H8Client = 'local';
			$this->ConnectionID = 'Local';
		} else {
			$H8Client = FALSE;
			$this->ConnectionID = FALSE;
		}
	
		if ($this->ConnectionID) {
			$this->Table = new H8_Table($H8Client, $this->Database);
			if (!$this->Table->isEmpty()) {
				return $this->ConnectionID;
			} else {
				$this->Error = "Failed selecting database: ".$this->Table->GetError();
				return NULL;
			}
		} else {
			$this->Error = "Failed connecting to database server";
			return NULL;
		}
	}
	
	/**
	 * @ignore
	 */
	function query($Query)
	{
		$H8Clause = new H8_Clause($Query);
		$H8Result = $this->Table->Search('', $H8Clause);
		$Result = new NitroDB_h8_Result($H8Result, $this->Table, $this->ConnectionID);
		return $Result;
	}
	
	/**
	 * @ignore
	 */
	function InsertID()
	{
		return FALSE;
	}
	
	/**
	 * @ignore
	 */
	function getOne($Query, $row, $col)
	{
		return FALSE;
	}

	/**
	 * @ignore
	 */
	function getRow($Query, $row, $FetchMode)
	{
		return FALSE;
	}

	/**
	 * @ignore
	 */
	function getCol($Query, $col)
	{
		return FALSE;
	}
	
	function SetFilter($Filter)
	{
		// calculate / get filter result index unit
		$this->Filter = '...filter...??';
	}

	/**
	 * Create new batch
	 */
	function NewBatch()
	{
		$SearchBatch = new NitroDB_H8_Batch($this->Table, $this->Filter);
		return $SearchBatch;
	}
	
	function Search($Clause, $Filter = NULL)
	{
		if ($Filter) {
			// extra filter added
			if ($this->Filter) {
				// join this->Filter with Filter
				// TODO
			}
		} elseif ($this->Filter) {
			$Filter =& $this->Filter;
		}

		$H8Clause = new H8_Clause($Clause);
		$H8Result = $this->Table->Search($Filter, $H8Clause);
		$Result = new NitroDB_h8_Result($H8Result, $this->Table, $this->ConnectionID);
		return $Result;
	}
}

/**
 * Hyper8 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_h8_Result {
	/**
	 * @ignore
	 */
	var $ConnectionID;
	/**
	 * @var	resource	Hyper8 result resource
	 */
	var $Result;
	/**
	 * @var	string	Hyper8 result error
	 */
	var $Error;
	/**
	 * @var	int	Default Hyper8 fetchmode (H8_FETCHMODE_ASSOC)
	 */
	var $FetchMode = H8_FETCHMODE_ASSOC;
	/**
	 * @var	int	Nitro fetchmode translation to Hyper8 fetchmodes
	 */
	var $FetchModes = Array(NITRODB_NUM => H8_FETCHMODE_NUM,
													NITRODB_ASSOC => H8_FETCHMODE_ASSOC,
													NITRODB_BOTH => H8_FETCHMODE_BOTH
													);

	/**
	 * @ignore	constructor
	 */
	function NitroDB_h8_Result(&$Result, &$Table, &$ConnectionID)
	{
		DebugGroup("DB", "Result", "query :".$Result->Message, __FILE__, __LINE__, DEBUG_SQL_OK);
		$this->Result				=& $Result;
		$this->Table				=& $Table;
		$this->ConnectionID =& $ConnectionID;
		if (is_H8_Result($this->Result)) {
			Debug("DB", "Result", "Result: ".$this->Result->ID, __FILE__, __LINE__, DEBUG_SQL_OK);
			$this->Error = FALSE;
		} else {
			Debug("DB", "Result", "Query failed: ".mysql_error(), __FILE__, __LINE__, DEBUG_SQL_ERR);
			$this->Error = '';
			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 (is_H8_Result($this->Result) && !$this->Error) {
			$RV = $this->Result->GetNRows();
			Debug("DB", "Result", "NumRows: ".$RV, __FILE__, __LINE__, DEBUG_SQL_OK);
		} else {
			$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) {
			$RV = $this->Result->GetNext();
		} else {
			$RV = FALSE;
		}
		return $RV;
	}
	
	/**
	 * Fetch 1 row from the result, alias for fetchArray()
	 *
	 * @see	fetchArray()
	 * @access	public
	 */
	function fetchRow($FetchMode = FALSE)
	{
		if ($this->Result && !$this->Error) {
			$RV = $this->fetchArray($FetchMode);
		} else {
			$RV = FALSE;
		}
		return $RV;
	}
	
	/**
	 * Get 1 value from result, alias for fetchResult()
	 *
	 * @see	fetchResult()
	 * @access	public
	 */
	function getResult($Record, $Field)
	{
		return $this->fetchResult($Record, $Field);
	}

	/**
	 * 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)
	{
		// TODO Field!
		if ($this->Result && !$this->Error) {
			$RV = $this->Result->GetRows($Record);
		} else {
			$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 FALSE;
	}
	
	/**
	 * 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 ($this->Result && !$this->Error) {
			Debug("DB", "Result", "Free result: ".$this->Result->ID, __FILE__, __LINE__, DEBUG_SQL_OK);
			$this->Result->free();
			DebugCloseGroup(DEBUG_SQL_OK);
			//destroy(SELF);
			$RV = TRUE;
		} else {
			$RV = FALSE;
		}
		return $RV;
	}

	/**
	 * Get last error message
	 *
	 * This function returns the last error message generated by
	 * the class. This function should always be used instead of
	 * accessing the Error variable directly.
	 *
	 * @return	string	Last error message
	 * @access	public
	 */
	function getMessage()
	{
		// get error message
		return $this->Error;
	}

	/**
	 * Get last insert id
	 *
	 * This function returns the insert id of the last insert query.
	 *
	 * @return	int	Last insert id
	 * @access	public
	 */
	function InsertID()
	{
		return FALSE;
	}
}
?>
Return current item: OpenNitro