cvs: pear /Structures_DataGrid/DataGrid/DataSource Array.php DB.php

From: Date: Wed, 15 Feb 2006 20:49:07 +0000
Subject: cvs: pear /Structures_DataGrid/DataGrid/DataSource Array.php DB.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-38272@lists.php.net to get a copy of this message
wiesemann Wed Feb 15 20:49:07 2006 UTC Modified files: /pear/Structures_DataGrid/DataGrid/DataSource Array.php DB.php Log: - DB DataSource driver now extends Array DataSource driver (unneeded code removed) -- usage of this driver is deprecated for performance reasons - Array DataSource driver reworked (integrated staticFetch() into fetch(), removed possibility to call sort() statically) (changes related to bugs #6582 and #6779)

http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/DataSource/Array.php?r1=1.17&r2=1.18&diff_format=u Index: pear/Structures_DataGrid/DataGrid/DataSource/Array.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.17 pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.18 --- pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.17 Sun Jan 29 10:05:03 2006 +++ pear/Structures_DataGrid/DataGrid/DataSource/Array.php Wed Feb 15 20:49:07 2006 @@ -17,7 +17,7 @@ // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: Array.php,v 1.17 2006/01/29 10:05:03 wiesemann Exp $ +// $Id: Array.php,v 1.18 2006/02/15 20:49:07 wiesemann Exp $ require_once 'Structures/DataGrid/DataSource/Common.php'; @@ -26,7 +26,7 @@ * * This class is a data source driver for a 2D Array * - * @version $Revision: 1.17 $ + * @version $Revision: 1.18 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @author Andrew Nagy <asnagy@webitecture.org> * @access public @@ -96,38 +96,17 @@ if ($this->_ar && !$this->_options['fields']) { $this->setOptions(array('fields' => array_keys($this->_ar[0]))); } - $records =& $this->staticFetch($this->_ar, $this->_options['fields'], - $offset, $len, $sortField, $sortDir); - return $records; - } - - /** - * Reusable static fetch method - * - * Since many drivers end up needing this array driver's features, - * the following method is provided in order to avoid subclassing - * this class. - * - * @param integer $offset Limit offset (starting from 0) - * @param integer $len Limit length - * @param string $sortField Field to sort by - * @param string $sortDir Sort direction : 'ASC' or 'DESC' - * @static - */ - function &staticFetch($ar, $fieldList, $offset=0, $len=null, - $sortField=null, $sortDir='ASC') - { + // sorting if ($sortField) { - Structures_DataGrid_DataSource_Array::sort($sortField, $sortDir, - $ar); + $this->sort($sortField, $sortDir); } // slicing if (is_null($len)) { - $slice = array_slice($ar, $offset); + $slice = array_slice($this->_ar, $offset); } else { - $slice = array_slice($ar, $offset, $len); + $slice = array_slice($this->_ar, $offset, $len); } // Filter out fields that are to not be rendered @@ -139,38 +118,34 @@ foreach ($slice as $rec) { $buf = array(); foreach ($rec as $key => $val) { - if (in_array($key, $fieldList)) { + if (in_array($key, $this->_options['fields'])) { $buf[$key] = $val; } } $records[] = $buf; } - + return $records; } - + /** - * Sorts the array. Can be called statically if the ar parameter is - * specified + * Sorts the array. * * @access public * @param string $sortField Field to sort by * @param string $sortDir Sort direction : 'ASC' or 'DESC' - * @param array $ar The array to sort (Used for static calls) */ - function sort($sortField, $sortDir, $ar = array()) + function sort($sortField, $sortDir) { - if (!($numRows = count($ar))) { - $ar = $this->_ar; - $numRows = count($ar); - } - $sortAr = array(); + $numRows = count($this->_ar); + for ($i = 0; $i < $numRows; $i++) { - $sortAr[$i] = $ar[$i][$sortField]; + $sortAr[$i] = $this->_ar[$i][$sortField]; } + $sortDir = strtoupper($sortDir) == 'ASC' ? SORT_ASC : SORT_DESC; - array_multisort($sortAr, $sortDir, $ar); + array_multisort($sortAr, $sortDir, $this->_ar); } } http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/DataSource/DB.php?r1=1.14&r2=1.15&diff_format=u Index: pear/Structures_DataGrid/DataGrid/DataSource/DB.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/DB.php:1.14 pear/Structures_DataGrid/DataGrid/DataSource/DB.php:1.15 --- pear/Structures_DataGrid/DataGrid/DataSource/DB.php:1.14 Wed Dec 21 11:10:13 2005 +++ pear/Structures_DataGrid/DataGrid/DataSource/DB.php Wed Feb 15 20:49:07 2006 @@ -17,16 +17,16 @@ // | Olivier Guilyardi <olivier@samalyse.com> | // +----------------------------------------------------------------------+ // -// $Id $ +// $Id: DB.php,v 1.15 2006/02/15 20:49:07 wiesemann Exp $ -require_once 'Structures/DataGrid/DataSource/Common.php'; +require_once 'Structures/DataGrid/DataSource/Array.php'; /** * PEAR::DB Data Source Driver * * This class is a data source driver for the PEAR::DB::DB_Result object * - * @version $Revision: 1.14 $ + * @version $Revision: 1.15 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @access public @@ -34,7 +34,7 @@ * @category Structures */ class Structures_DataGrid_DataSource_DB - extends Structures_DataGrid_DataSource_Common + extends Structures_DataGrid_DataSource_Array { /** * Reference to the DB_Result object @@ -51,7 +51,7 @@ */ function Structures_DataGrid_DataSource_DB() { - parent::Structures_DataGrid_DataSource_Common(); + parent::Structures_DataGrid_DataSource_Array(); } /** @@ -68,71 +68,14 @@ } if (strtolower(get_class($result)) == 'db_result') { - $this->_result =& $result; + while ($record = $result->fetchRow(DB_FETCHMODE_ASSOC)) { + $this->_ar[] = $record; + } return true; } else { return new PEAR_Error('The provided source must be a DB_Result'); } } - /** - * Fetch - * - * @param integer $offset Offset (starting from 0) - * @param integer $limit Limit - * @param string $sortField Field to sort by - * @param string $sortDir Sort direction : 'ASC' or 'DES - * @access public - * @return array The 2D Array of the records - */ - function &fetch($offset=0, $limit=null, $sortField=null, $sortDir='ASC') - { - $recordSet = array(); - - // Fetch the Data - if ($numRows = $this->_result->numRows()) { - while ($record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC)) { - $recordSet[] = $record; - } - - // Determine fields to render - if (!$this->_options['fields']) { - $this->setOptions(array('fields' => array_keys($recordSet[0]))); - } - - // Limit and Sort the Data - $recordSet =& Structures_DataGrid_DataSource_Array::staticFetch( - $recordSet, $this->_options['fields'], $offset, - $limit, $sortField, $sortDir); - } - - return $recordSet; - } - - /** - * Count - * - * @access public - * @return int The number or records - */ - function count() - { - return $this->_result->numRows(); - } - - /** - * This should not be used due to performance issues, but is available for - * compatability. - * - * @access public - * @param string $sortField Field to sort by - * @param string $sortDir Sort direction : 'ASC' or 'DESC' - */ - function sort($sortField, $sortDir) - { - return new PEAR_Error('Cannot sort a DB_Result Object'); - } - - } ?> \ No newline at end of file
« previous php.pear.cvs (#38272) next »