/system/database/DB_result.php 数据库结果类

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Database Result Class
 * 数据库结果类
 * This is the platform-independent result class.
 * This class will not be called directly. Rather, the adapter
 * class for the specific database will extend and instantiate it.
 * 这是一个平台独立的结果类。
 * 这个类不会被直接调用。相反,该适配器
 * 类的特定数据库将扩展并创建实例。
 * @category	Database
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/database/
 */
class CI_DB_result {

	var $conn_id				= NULL; //数据库链接id
	var $result_id				= NULL; //返回资源id
	var $result_array			= array(); //返回数组
	var $result_object			= array(); //返回的对象
	var $custom_result_object	= array(); //
	var $current_row			= 0;       //当前的行
	var $num_rows				= 0;       //总记录数
	var $row_data				= NULL;    //


	/**
	 * Query result.  Acts as a wrapper function for the following functions.
	 * 查询结果。作为一个包装函数用于以下功能。
	 * @access	public
	 * @param	string	can be "object" or "array"  字符串可以是“对象”或“阵列”
	 * @return	mixed	either a result object or array  无论是结果对象或数组
	 */
	public function result($type = 'object')
	{
		if ($type == 'array') 
			return $this->result_array();
		else if ($type == 'object') 
			return $this->result_object();
		else 
			return $this->custom_result_object($type);
	}

	// --------------------------------------------------------------------

	/**
	 * Custom query result.
	 * 自定义的查询结果。
	 * @param class_name A string that represents the type of object you want back
	 * CLASS_NAME一个字符串,表示对象的类型,你想回来
	 * @return array of objects
	 */
	public function custom_result_object($class_name)
	{
		//在$custom_result_object是否存在$class_name存在这个键值
		if (array_key_exists($class_name, $this->custom_result_object))
		{
			//返回
			return $this->custom_result_object[$class_name];
		}

        //如果返回资源的值为false或者返回数值等于0
		if ($this->result_id === FALSE OR $this->num_rows() == 0)
		{
			return array();
		}

		// add the data to the object
		// 将数据添加到该对象
		$this->_data_seek(0); //$this->_data_seek(0)是表示什么呢?这里定义只返回一个true
		$result_object = array();

		while ($row = $this->_fetch_object())
		{
			$object = new $class_name();  //实例化这个$class_name();对象

			foreach ($row as $key => $value)
			{
				$object->$key = $value; //将这个对象中的key设置为$value值
			}

            //然后将这个对象好像就是一条一条记录样,当做一个对象来处理,
            //好比以前一条记录是一个数组,但现在的一条记录是一个对象了
			$result_object[] = $object;
		}

		// return the array
		// 返回一个数组
		return $this->custom_result_object[$class_name] = $result_object;
	}

	// --------------------------------------------------------------------

	/**
	 * Query result.  "object" version.
	 * 查询结果。 “对象”的版本。
	 * @access	public
	 * @return	object
	 */
	public function result_object()
	{
		//如果返回对象大于0时,直接返回
		if (count($this->result_object) > 0)
		{
			return $this->result_object;
		}

		// In the event that query caching is on the result_id variable
		// will return FALSE since there isn't a valid SQL resource so
		// we'll simply return an empty array.

		// 在事件查询缓存上result_id变量,
		// 将返回FALSE,因为没有一个有效的SQL资源
		// 我们将简单地返回一个空数组。
		if ($this->result_id === FALSE OR $this->num_rows() == 0)
		{
			return array();
		}

		$this->_data_seek(0);
		//循环该返回对象
		while ($row = $this->_fetch_object())
		{
			$this->result_object[] = $row; //返回result_object() 
		}
        //返回对象数组
		return $this->result_object;
	}

	// --------------------------------------------------------------------

	/**
	 * Query result.  "array" version.
	 * 查询结果。“阵列”的版本。
	 *
	 * @access	public
	 * @return	array
	 */
	public function result_array()
	{
		if (count($this->result_array) > 0)
		{
			return $this->result_array;
		}

		// In the event that query caching is on the result_id variable
		// will return FALSE since there isn't a valid SQL resource so
		// we'll simply return an empty array.
		// 在事件查询缓存上result_id变量,
		// 将返回FALSE,因为没有一个有效的SQL资源
		// 我们将简单地返回一个空数组。
		if ($this->result_id === FALSE OR $this->num_rows() == 0)
		{
			return array();
		}

		$this->_data_seek(0);
		//$this->_fetch_assoc() 返回一个关联数组
		while ($row = $this->_fetch_assoc())
		{
			$this->result_array[] = $row;
		}

		return $this->result_array;
	}

	// --------------------------------------------------------------------

	/**
	 * Query result.  Acts as a wrapper function for the following functions.
	 * 查询结果。作为一个包装函数用于以下功能。
	 *
	 * @access	public
	 * @param	string
	 * @param	string	can be "object" or "array"      可以是“对象”或“阵列”
	 * @return	mixed	either a result object or array 无论是结果对象或数组
	 */
	public function row($n = 0, $type = 'object')
	{
		//如果$n不为数值型
		if ( ! is_numeric($n))
		{
			// We cache the row data for subsequent uses 缓存的行数据的后续使用
			if ( ! is_array($this->row_data))
			{
				$this->row_data = $this->row_array(0);
			}

			// array_key_exists() instead of isset() to allow for MySQL NULL values
			//array_key_exists()isset()的,而不是让MySQL的NULL值
			if (array_key_exists($n, $this->row_data))
			{
				return $this->row_data[$n];
			}
			// reset the $n variable if the result was not achieved
			// 如果结果没有达到复位的$ n变量
			$n = 0;
		}

		if ($type == 'object') 
			return $this->row_object($n);
		else if ($type == 'array') 
			return $this->row_array($n);
		else 
			return $this->custom_row_object($n, $type);
	}

	// --------------------------------------------------------------------

	/**
	 * Assigns an item into a particular column slot
	 * 分配一个项目到一个特定的列插槽
	 * @access	public
	 * @return	object
	 */
	public function set_row($key, $value = NULL)
	{
		// We cache the row data for subsequent uses
		// 缓存的行数据的后续使用
		if ( ! is_array($this->row_data))
		{
			$this->row_data = $this->row_array(0);
		}

		if (is_array($key))
		{
			foreach ($key as $k => $v)
			{
				$this->row_data[$k] = $v;
			}

			return;
		}

		if ($key != '' AND ! is_null($value))
		{
			$this->row_data[$key] = $value;
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Returns a single result row - custom object version
	 * 返回一个结果行 - 自定义对象的版本
	 * @access	public
	 * @return	object
	 */
	public function custom_row_object($n, $type)
	{
		//$this->custom_result_object($type) //自定义查结果
		$result = $this->custom_result_object($type);

        //如果为空值
		if (count($result) == 0)
		{
			return $result;
		}

        //
		if ($n != $this->current_row AND isset($result[$n]))
		{
			$this->current_row = $n; //更新当前使用的记录
		}
        //返回$result中当前使用的记录集
		return $result[$this->current_row];
	}


	/**
	 * Returns a single result row - object version
	 * 返回一个结果行 - 对象版本
	 * @access	public
	 * @return	object
	 */
	public function row_object($n = 0)
	{
		$result = $this->result_object();

		if (count($result) == 0)
		{
			return $result;
		}

		if ($n != $this->current_row AND isset($result[$n]))
		{
			$this->current_row = $n;
		}

		return $result[$this->current_row];
	}

	// --------------------------------------------------------------------

	/**
	 * Returns a single result row - array version
	 * 返回一个结果行 - 数组版本
	 * @access	public
	 * @return	array
	 */
	public function row_array($n = 0)
	{
		$result = $this->result_array();

		if (count($result) == 0)
		{
			return $result;
		}

		if ($n != $this->current_row AND isset($result[$n]))
		{
			$this->current_row = $n;
		}

		return $result[$this->current_row];
	}


	// --------------------------------------------------------------------

	/**
	 * Returns the "first" row
	 * 返回“第一”行
	 * @access	public
	 * @return	object
	 */
	public function first_row($type = 'object')
	{
		$result = $this->result($type);

		if (count($result) == 0)
		{
			return $result;
		}
		return $result[0];
	}

	// --------------------------------------------------------------------

	/**
	 * Returns the "last" row
	 * 返回最后一行
	 * @access	public
	 * @return	object
	 */
	public function last_row($type = 'object')
	{
		$result = $this->result($type);

		if (count($result) == 0)
		{
			return $result;
		}
		return $result[count($result) -1];
	}

	// --------------------------------------------------------------------

	/**
	 * Returns the "next" row
	 * 返回下一条数据
	 * @access	public
	 * @return	object
	 */
	public function next_row($type = 'object')
	{
		$result = $this->result($type);

		if (count($result) == 0)
		{
			return $result;
		}

        //如果当前存在当前值的下一个元素
		if (isset($result[$this->current_row + 1]))
		{
			++$this->current_row;
		}

		return $result[$this->current_row];
	}

	// --------------------------------------------------------------------

	/**
	 * Returns the "previous" row
	 * 返回“先前”行
	 * @access	public
	 * @return	object
	 */
	public function previous_row($type = 'object')
	{
		$result = $this->result($type);

		if (count($result) == 0)
		{
			return $result;
		}

		if (isset($result[$this->current_row - 1]))
		{
			--$this->current_row;
		}
		return $result[$this->current_row];
	}

	// --------------------------------------------------------------------

	/**
	 * The following functions are normally overloaded by the identically named
	 * methods in the platform-specific driver -- except when query caching
	 * is used.  When caching is enabled we do not load the other driver.
	 * These functions are primarily here to prevent undefined function errors
	 * when a cached result object is in use.  They are not otherwise fully
	 * operational due to the unavailability of the database resource IDs with
	 * cached results.
	 * 以下功能相同的命名通常超载
	 * 特定于平台的驱动程序的方法 - 除了当查询缓存
	 * 被使用。当缓存被启用,我们不加载其他驱动程序。
	 * 这些功能主要是防止函数未定义错误
	 * 当对象是使用一个缓存的结果。他们不完全
	 * 经营由于数据库资源ID无法与
	 * 高速缓存的结果。
	 */
	public function num_rows() { return $this->num_rows; }
	public function num_fields() { return 0; }
	public function list_fields() { return array(); }
	public function field_data() { return array(); }
	public function free_result() { return TRUE; }
	protected function _data_seek() { return TRUE; }
	protected function _fetch_assoc() { return array(); }
	protected function _fetch_object() { return array(); }

}
// END DB_result class

/* End of file DB_result.php */
/* Location: ./system/database/DB_result.php */

  

posted @ 2013-05-24 16:59  简单--生活  阅读(421)  评论(0)    收藏  举报
简单--生活(CSDN)