cvs: php4 /pear/Cache DB.php

From: Date: Tue, 14 Aug 2001 19:25:08 +0000
Subject: cvs: php4 /pear/Cache DB.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-516@lists.php.net to get a copy of this message
sebastian Tue Aug 14 15:25:08 2001 EDT Modified files: /php4/pear/Cache DB.php Log: Calculate num_columns/num_rows only once. Add PHPDoc comments. Index: php4/pear/Cache/DB.php diff -u php4/pear/Cache/DB.php:1.3 php4/pear/Cache/DB.php:1.4 --- php4/pear/Cache/DB.php:1.3 Tue Aug 14 14:37:21 2001 +++ php4/pear/Cache/DB.php Tue Aug 14 15:25:08 2001 @@ -15,7 +15,7 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: DB.php,v 1.3 2001/08/14 18:37:21 sebastian Exp $ +// $Id: DB.php,v 1.4 2001/08/14 19:25:08 sebastian Exp $ require_once 'Cache.php'; require_once 'DB.php'; @@ -23,24 +23,50 @@ /** * Cache_DB * -* Purpose: -* -* Caching the result of database queries. -* * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @module Cache_DB * @modulegroup Cache_DB * @package Cache -* @version $Revision: 1.3 $ +* @version $Revision: 1.4 $ * @access public */ class Cache_DB extends Cache { + /** + * PEAR DB Object + * + * @var object + */ var $db; + + /** + * Lifetime of a cached result set (in seconds) + * + * @var integer + */ var $expires = 3600; - var $dsn = ''; + /** + * PEAR DB DSN + * + * @var string + */ + + var $dsn = ''; + + /** + * PEAR DB Options + * + * @var mixed + */ var $options = false; + /** + * Constructor + * + * @param string Name of container class + * @param array Array with container class options + * @param integer Lifetime of a cached result set (in seconds) + */ function Cache_DB($container = 'file', $container_options = array('cache_dir' => '.', 'filename_prefix' => 'query_' @@ -52,6 +78,11 @@ $this->expires = $expires; } + /** + * PEAR-Deconstructor + * Call deconstructor of parent, + * close database connection if open + */ function _Cache_DB() { $this->_Cache(); @@ -60,6 +91,12 @@ } } + /** + * Connect to a database + * + * @param string PEAR DB DSN for the database connection + * @param mixed options + */ function connect($dsn, $options = false) { if (!isset($this->db)) { $this->db = DB::connect($dsn, $options); @@ -69,11 +106,23 @@ } } + /** + * Register a database connection for connect on demand + * + * @param string PEAR DB DSN for the database connection + * @param mixed options + */ function setConnection($dsn, $options = false) { $this->dsn = $dsn; $this->options = $options; } + /** + * Perform an SQL query + * + * @param string SQL Query String + * @return mixed + */ function &query($query) { if (stristr($query, 'SELECT')) { $cache_id = md5($query); @@ -137,20 +186,65 @@ * @module Cache_DB * @modulegroup Cache_DB * @package Cache -* @version $Revision: 1.3 $ +* @version $Revision: 1.4 $ * @access public */ class Cache_DB_Result { - var $rows = array(); - var $columns = array(); - var $cursor = 0; - + /** + * Rows of the result set + * + * @var array + */ + var $rows = array(); + + /** + * Names of the result set's columns + * + * @var array + */ + var $column_names = array(); + + /** + * Cursor + * + * @var integer + */ + var $cursor = 0; + + /** + * Number of columns in the result set + * + * @var integer + */ + var $num_columns = 0; + + /** + * Number of rows in the result set + * + * @var integer + */ + var $num_rows = 0; + + /** + * Constructor + * + * @param array Rows + */ function Cache_DB_Result(&$rows) { - $this->rows = $rows; - $this->columns = array_keys($this->rows[0]); - $this->cursor = 0; + $this->rows = $rows; + $this->column_names = array_keys($this->rows[0]); + $this->cursor = 0; + + $this->num_columns = sizeof($this->column_names); + $this->num_rows = sizeof($this->rows); } + /** + * Fetch and return a row of data + * @param $fetchmode format of fetched row + * @param $rownum the row number to fetch + * @return array a row of data, NULL on no more rows or PEAR_Error on error + */ function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) { if ($this->cursor < sizeof($this->rows)) { if ($fetchmode == DB_FETCHMODE_ASSOC) { @@ -161,8 +255,8 @@ $row = array(); $_row = $this->rows[$this->cursor++]; - foreach ($this->columns as $column) { - $row[] = $_row[$column]; + foreach ($this->column_names as $column_name) { + $row[] = $_row[$column_name]; } return $row; @@ -172,21 +266,39 @@ } } + /** + * Fetch a row of data into an existing variable. + * + * @param $arr reference to data containing the row + * @param $fetchmode format of fetched row + * @param $rownum the row number to fetch + * @return mixed DB_OK on success, NULL on no more rows or + * a DB_Error object on errors + */ function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) { } + /** + * Get the the number of columns in a result set. + * + * @return int the number of columns, or a DB error + */ function numCols() { - if (sizeof($this->rows) > 0) { - return sizeof($this->rows[0]); - } else { - return 0; - } + return $this->num_columns; } + /** + * Get the number of rows in a result set. + * + * @return int the number of rows, or a DB error + */ function numRows() { - return sizeof($this->rows); + return $this->num_rows; } + /** + * Frees the resources allocated for this result set. + */ function free() { }

« previous php.pear.cvs (#516) next »