Location: PHPKode > scripts > pdbc > pdbc/ResultSet.class.php
<?php
	/*
	 *	Copyright (C) 2002-2004
	 *	@author chenxi
	 *	@version $Id: ResultSet.class.php,v 0.1 2004/11/02 19:39:38
	 */

	//require_once ('pdbc/interface/ResultSet.php');
	require_once ('pdbc/mysql/ResultSetMetaData.class.php');

	class mysql_ResultSet extends Object {
		var $pstmt  = null;
		var $result = null;
		var $isAvailable = false;

		/* Óαê */
		var $cursor = 0;

		/* ½á¹û¼¯´óС */
		var $size = 0;

		/* ½á¹û¼¯ÔªÊý¾Ý */
		var $metaData = array();

		/* ½á¹û¼¯ÖÐÓÐЧ×ֶεÄÃû³Æ¼¯ */
		var $finfo = array();

		var $absop = false;
		var $isResult = false;
		var $isPstmt  = false;

		/* ±êʶµ±Ç°ResultSet¶ÔÏóÊÇ·ñ¿ÉÓà */
		var $isAvalable = false;

		/* ±êʶµ±Ç°ResultSet¶ÔÏóÊÇ·ñÒÑ¹Ø±Õ */
		var $isClosed = true;

		/* µ±Ç°ResultSet¶ÔÏóµÄÔªÊý¾Ý */
		var $meta = null;

		/* µ±Ç°row */
		var $currentRow = null;

		/**
		 *	¹¹Ô캯Êý
		 *	@param resource
		 */
		function &mysql_ResultSet(&$resource) {
			if (is_resource($resource)) {
				$this->result = $resource;
				$this->isResult = true;
			} else {
				trigger_error('<br/>resource is not available', E_USER_ERROR);
			}
			
			$this->isClosed = false;
			$this->isAvalable = true;
			$this->size = $this->getRow();
		}

		/**
		 *	Îö¹¹º¯Êý
		 */
		function __destruct() {
			$this->cursor = 0;
			$this->size = 0;
			$isClosed = true;
			$rs = null;
			$this->metaData = null;
			$this->result = null;
			$this->currentRow = null;
		}

		/**
		 *	½«µ±Ç°ÓαêÒƶ¯ÖÁÖ¸¶¨µÄλÖÃ
		 *	@param $offset
		 */
		function absolute($offset) {
			$this->checkClosed();

			if (0 >= $offset || $offset > $this->size)
				return false;

			mysql_data_seek($this->result, ($offset-1));
			$row = mysql_fetch_object($this->result);

			if (!is_object($row) || !$row) {
				return false;
			}
			
			$this->cursor = $offset;
			$this->absop = true;
			$this->currentRow = $row;
			return true;
		}

		/**
		 *	¼ì²éµ±Ç°ResultSet¶ÔÏóÊÇ·ñ¿ÉÓÃ
		 *	return void
		 */
		function checkClosed() {
			if ($this->isClosed) {
				require_once ('util/StringBuffer.class.php');
				$msgbuf = new StringBuffer('No operations allowed after resultset closed.');

				$this->throws($msgbuf->toString(), null, EXCEPTION_DIE);
			}
		}

		/**
		 *	¹Ø±Õµ±Ç°ResultSet¶ÔÏ󣬲¢Êͷŵ±Ç°½á¹û¼¯
		 *	return void
		 */
		function close() {
			$this->realClose(true);
		}

		function getFetchSize() {
			//not implement
		}

		/**
		 *	»ñÈ¡µ±Ç°¼Ç¼ָ¶¨ÁеIJ¼¶ûÖµ
		 *	@param $column
		 *	@return boolean
		 *	@access public
		 */
		function getBoolean($column) {
			return $this->getInternalValue($column, 'boolean');
		}

		/**
		 *	»ñÈ¡µ±Ç°¼Ç¼ָ¶¨ÁеÄBlobÖµ
		 *	@param $column
		 */
		function getBlob($column) {
			return $this->getInternalValue($column, 'blob');
		}

		/**
		 *	¼ì²é¸ø¶¨µÄ²ÎÊý(ÁÐ)µÄÖµÊÇ·ñºÏ·¨
		 *	return void
		 */
		function checkInputColumn($column) {
			if ('' == $column || null === $column)
				trigger_error('<br/>mysql_ResultSet->checkInputColumn(): param \'column\' is null', E_USER_ERROR);
		}

		/**
		 *	²é¿´µ±Ç°ResultSet¶ÔÏóÊÇ·ñ¿ÉÓÃ
		 *	return void
		 */
		function checkResource() {
			if (!$this->isAvalable)
				trigger_error('<br/>mysql_ResultSet->getRow(): resource is invalidate', E_USER_ERROR);
		}

		/**
		 *	½«¹â±êÒƶ¯ÖÁµ±Ç°ResultSet¶ÔÏóµÄµÚÒ»Ìõ¼Ç¼
		 *	return boolean
		 */
		function first() {
			return $this->absolute(1);
		}

		/**
		 *	·µ»ØÖ¸¶¨DateTimeÀàÐÍ×ֶεÄÖµ
		 *	@param $column
		 *	@return Datetime
		 *	@access public
		 */
		function getDate($column) {
			return $this->getInternalValue($column, 'datetime');
		}

		/**
		 *	·µ»ØÖ¸¶¨DoubleÀàÐÍ×ֶεÄÖµ
		 *	@param String $column
		 *	@return Double
		 *	@access public
		 */
		function getDouble($column) {
			return $this->getInternalValue($column, 'double');
		}

		/**
		 *	·µ»ØÖ¸¶¨floatÀàÐÍ×ֶεÄÖµ
		 *	@param string $column
		 *	@return float
		 *	@access public
		 */
		function getFloat($column) {
			return $this->getInternalValue($column, 'float');
		}

		/**
		 *	»ñÈ¡µ±Ç°²éѯ½á¹û¼¯µÄÔªÊý¾Ý
		 *	@return mysql_ResultSetMetaData
		 *	@access private
		 */
		function getMetaData() {
			if (!is_resource($this->result))
				return null;
				#trigger_error('<br/>Result is not an mysql_result', E_USER_ERROR);

			return new mysql_ResultSetMetaData($this->result);
		}

		/**
		 *	·µ»ØÖ¸¶¨objectÀàÐÍ×ֶεÄÖµ
		 *	@param string $column
		 *	@return object
		 *	@access public
		 */
		function getObject($column) {
			return $this->getInternalValue($column, 'object');
		}

		/**
		 *	»ñÈ¡µ±Ç°½á¹û¼¯¼Ç¼ÊýÄ¿
		 *	@return int
		 *	@access private
		 */
		function getResultRow() {
			$this->checkResource();

			return mysql_num_rows($this->result);
		}
		
		/**
		 *	·µ»Øµ±Ç°½á¹û¼¯¼Ç¼ÊýÄ¿
		 *	@return int
		 *	@access public
		 */
		function getRow() {
			if ($this->isResult)
				return $this->getResultRow();
			else
				return (int)0;
		}

		/**
		 *	¼ì²é×Ö¶ÎÀàÐÍÊÇ·ñÓë»ùÀàÏà·ûºÏ
		 *	@param	String	$column		×Ö¶ÎÃû
		 *	@param	String	$baseType	»ùÀà
		 *	@return void
		 *	@access private
		 */
		function checkColumnType($column, $baseType) {
			$this->metaData = $this->getMetaData();

			$found = false;
			for ($i = 0; $i < $this->metaData->getColumnCount(); $i++) {
				$name = $this->metaData->getColumnName($i);
				if ($column == $name) {
					$type = $this->metaData->getColumnType($i);
					$found = true;
					break;
				}
			}

			if (!$found)
				$this->throws('mysql_ResultSet->getString(): column('.$column.') not in resultset', null, EXCEPTION_DIE);

			if ($baseType != $type)
				$this->throws('mysql_ResultSet->getString(): type of column ['.$column.'] is not a '.$baseType, null, EXCEPTION_DIE);
		}

		/**
		 *	»ñÈ¡ÔªÊý¾ÝµÄ½á¹¹
		 *	@return String[]
		 *	@access public
		 */
		function getMetaStructure() {
			if (!is_object($this->metaData) && !is_a($this->metaData, 'mysql_ResultSetMetaData'))
				$this->metaData = $this->getMetaData();
			return $this->metaData->getMetadata();
		}

		/**
		 *	·µ»ØÖ¸¶¨stringÀàÐÍ×ֶεÄÖµ
		 *	@param $column
		 *	@return String
		 *	@access public
		 */
		function getString($column) {
			return $this->getInternalValue($column, 'string');
		}

		/**
		 *	·µ»Øµ±Ç°²éѯÉæ¼°µÄ×Ö¶ÎÊý
		 *	@return int
		 *	@access private
		 */
		function getTableFiledNums() {
			return mysql_num_fields($this->result);
		}

		/**
		 *	·µ»Øµ±Ç°²éѯ½á¹ûµÄ×Ö¶ÎÐÅÏ¢
		 *	@param	int	$offset
		 *	@return Object
		 *	@access private
		 */
		function fetchField($offset=0) {
			return mysql_fetch_field($this->result, $offset);
		}

		/**
		 *	»ñÈ¡µ±Ç°²éѯ½á¹û¼¯ÖеÄÉæ¼°µÄ×Ö¶ÎÃûÐÅÏ¢
		 *	@return string[]
		 *	@access public
		 */
		function getFetchFieldInfo() {
			if (!is_resource($this->result))
				return false;
				#trigger_error('<br/>Result is not an mysql_result', E_USER_ERROR);

			$i = 0;
			while ($i < $this->getTableFiledNums()) {
				$fields = $this->fetchField($i);

				if (!$fields) {
					return false;
					#trigger_error("DB_mysql->getTableField(): mysql_fetch_field failed", E_USER_ERROR);
				}

				$finfo[$i++] = $fields->name;
			}
			
			if (!is_array($finfo) && !is_null($finfo[0])) {
				return false;
			}

			return $finfo;
		}

		/**
		 *	´òÓ¡µ±Ç°²éѯ½á¹û¼¯ÖеÄÉæ¼°µÄ×Ö¶ÎÃûÐÅÏ¢
		 *	@return void
		 *	@access public
		 */
		function pGetFetchFieldInfo() {
			print_r($this->getFetchFieldInfo());
		}

		/**
		 *	·µ»ØÖ¸¶¨timeÀàÐÍ×ֶεÄÖµ
		 *	@param string	$column
		 *	@return String
		 *	@access public
		 */
		function getTime($column) {
			//not implement yet
		}

		/**
		 *	»ñÈ¡µ±Ç°¶ÔÏóÖвéѯ·µ»ØµÄ´íÎóÐÅÏ¢
		 *	@access public
		 */
		function getWarnings() {
			//not implement yet
		}

		/**
		 *	·µ»ØÖ¸¶¨intÀàÐÍ×ֶεÄÖµ
		 *	@param string	$column
		 *	@return String
		 *	@access public
		 */
		function getInt($column) {
			return $this->getInternalValue($column, 'int');
		}

		/**
		 *	·µ»ØÖ¸¶¨ÀàÐÍ×ֶεÄÖµ
		 *	@param string	$column
		 *	@param string	$baseType
		 *	@return String
		 *	@access private
		 */
		function getInternalValue($column, $baseType) {
			$this->checkInputColumn($column);

			$this->checkColumnType($column, $baseType);

			if ($this->isResult) {
				return $this->currentRow->$column;
			} else if ($this->isPstmt) {
				return $this->pstmt->$column;
			}
		}

		/**
		 *	·µ»Øµ±Ç°¶ÔÏóÊÇ·ñ¿ÉÓõıêʶ
		 *	@return String
		 *	@access public
		 */
		function isAvailable() {
			return $this->isAvailable;
		}

		/**
		 *	·µ»Øµ±Ç°½á¹û¼¯ÊÇ·ñÒѾ­¹Ø±Õ
		 *	@return boolean
		 ¡Á	@access public
		 */
		function isClosed() {
			return $this->isClosed;
		}

		/**
		 *	·µ»Øµ±Ç°¼Ç¼ÊÇ·ñÊǵÚÒ»Ìõ
		 *	@return boolean
		 */
		function isFirst() {
			return ((int)1 == $this->cursor);
		}

		/**
		 *	·µ»Øµ±Ç°¼Ç¼ÊÇ·ñÊÇ×îºóÒ»Ìõ
		 *	@return boolean
		 */
		function isLast() {
			return ($this->size == $this->cursor);
		}

		/**
		 *	½«ÓαêÖ¸Ïò½á¹û¼¯ÖеÄÏÂÒ»Ìõ¼Ç¼
		 *	@return boolean
		 */
		function next() {
			$this->checkResource();

			if (0 == $this->size)
				return false;
			if ($this->size < ($this->cursor + 1))
				return false;

			if ($this->isResult) {
				$this->currentRow = mysql_fetch_object($this->result);

				if (!$this->currentRow)
					return false;

				if (!is_object($this->currentRow)) {
					trigger_error('<br/>mysql_ResultSet->next(): Result of fetch_object method is not a object', E_USER_ERROR);
				}

				$this->cursor++;
				$this->absop = false;

				if (!$this->metaData)
					$this->getMetaData();

				return true;
			} else {
				trigger_error('<br/>internal error', E_USER_ERROR);
			}
		}

		/**
		 *	½«ÓαêÒƶ¯ÖÁµ±Ç°ResultSet¶ÔÏóµÄ×îºóÒ»Ìõ¼Ç¼
		 *	return boolean
		 */
		function last() {
			$this->cursor = $this->size;
			return $this->absolute(($this->size));
		}

		/**
		 *	½«ÓαêÔÚµ±Ç°ResultSet¶ÔÏóÇ°ÒÆÒ»Ìõ¼Ç¼
		 *	return boolean
		 */
		function previous() {
			if (0 >= $this->cursor) 
				return false;
			
			$cursor = $this->cursor - 1;
			if (0 > $cursor)
				return false;

			return $this->absolute($cursor);
		}

		/**
		 *	¹Ø±Õµ±Ç°¶ÔÏ󣬲¢ÊÍ·Å×ÊÔ´
		 *	@param boolean $calledExplicitly
		 *	@return void
		 *	@access protected
		 */
		function realClose($calledExplicitly=false) {
			if ($this->isResult)
				mysql_free_result($this->result);

			$this->__destruct();
		}

		/**
		 *	Document me
		 */
		function refreshRow() {
			//not implement yet
		}

		/**
		 *	@param $rows
		 */
		function setFetchSize($rows) {
			//not implement yet
		}

		/**
		 *	·µ»Øµ±Ç°µÄÓαê
		 *	@return integer	$this->cursor
		 */
		function getCursor() {
			return $this->cursor;
		}

		/**
		 *	´òÓ¡µ±Ç°µÄÓαê
		 *	@return void
		 */
		function pGetCursor() {
			printf("Current Cursor: (%s)", $this->getCursor());
		}
	}
?>
Return current item: pdbc