From 4b2e04220ba2427921d3d0d1cba0b0298653fc17 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 5 Feb 2007 05:14:48 +0000 Subject: [PATCH] * DibiResult::fetchAssoc() rewritten --- dibi.compact/dibi.compact.php | 12 +++---- dibi/libs/exception.php | 2 +- dibi/libs/resultset.php | 66 +++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/dibi.compact/dibi.compact.php b/dibi.compact/dibi.compact.php index ba8da5c..12ced68 100644 --- a/dibi.compact/dibi.compact.php +++ b/dibi.compact/dibi.compact.php @@ -13,11 +13,11 @@ * @license GNU GENERAL PUBLIC LICENSE v2 * @package dibi * @category Database - * @version 0.7c $Revision: 27 $ $Date: 2007-01-30 22:50:04 +0100 (út, 30 I 2007) $ + * @version 0.7c $Revision: 28 $ $Date: 2007-02-02 04:51:43 +0100 (pá, 02 II 2007) $ */ -define('DIBI','Version 0.7c $Revision: 27 $');if(version_compare(PHP_VERSION,'5.0.3','<'))die('dibi needs PHP 5.0.3 or newer');abstract +define('DIBI','Version 0.7c $Revision: 28 $');if(version_compare(PHP_VERSION,'5.0.3','<'))die('dibi needs PHP 5.0.3 or newer');abstract class DibiDriver{protected$config;public$formats=array('TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);static public @@ -110,11 +110,9 @@ function fetchAll(){@$this->seek(0);$rec=$this->fetch();if(!$rec)return array();$arr=array();if(count($rec)==1){$key=key($rec);do{$arr[]=$rec[$key];}while($rec=$this->fetch());}else{do{$arr[]=$rec;}while($rec=$this->fetch());}return$arr;}final function -fetchAssoc($assocBy){@$this->seek(0);$rec=$this->fetch();if(!$rec)return -array();$assocBy=func_get_args();foreach($assocBy -as$n=>$assoc)if(!array_key_exists($assoc,$rec))unset($assocBy[$n]);$arr=array();do{foreach($assocBy -as$n=>$assoc){$val[$n]=$rec[$assoc];unset($rec[$assoc]);}foreach($assocBy -as$n=>$assoc){if($n==0)$tmp=&$arr[$val[$n]];else$tmp=&$tmp[$assoc][$val[$n]];if($tmp===NULL)$tmp=$rec;}}while($rec=$this->fetch());return$arr;}final +fetchAssoc($assoc){@$this->seek(0);$rec=$this->fetch();if(!$rec)return +array();$arr=NULL;$assoc=explode(',',$assoc);if(count($assoc)===1){$as=$assoc[0];do{$arr[$rec[$as]]=$rec;}while($rec=$this->fetch());return$arr;}$last=count($assoc)-1;if($assoc[$last]==='#')unset($assoc[$last]);do{$x=&$arr;foreach($assoc +as$i=>$as){if($as==='*'){$x=&$x[];}elseif($as==='#'){if($x===NULL){$x=$rec;$x=&$x[$assoc[$i+1]];$x=NULL;}else{$x=&$x[$assoc[$i+1]];}}else{$x=&$x[$rec[$as]];}}if($x===NULL)$x=$rec;}while($rec=$this->fetch());unset($x);return$arr;}final function fetchPairs($key,$value){@$this->seek(0);$rec=$this->fetch();if(!$rec)return array();if(!array_key_exists($key,$rec)||!array_key_exists($value,$rec))return diff --git a/dibi/libs/exception.php b/dibi/libs/exception.php index 9634a8c..088d70d 100644 --- a/dibi/libs/exception.php +++ b/dibi/libs/exception.php @@ -58,7 +58,7 @@ class DibiException extends Exception if ($this->dbError) { $s .= "\nERROR: "; - if (isset($this->dbError['code'])) + if (isset($this->dbError['code'])) $s .= "[" . $this->dbError['code'] . "] "; $s .= $this->dbError['message']; diff --git a/dibi/libs/resultset.php b/dibi/libs/resultset.php index 73e5ec6..c945b6f 100644 --- a/dibi/libs/resultset.php +++ b/dibi/libs/resultset.php @@ -158,7 +158,7 @@ abstract class DibiResult implements IteratorAggregate, Countable /** - * Fetches all records from table. Records , but returns only first field + * Fetches all records from table. * @return array */ final function fetchAll() @@ -188,46 +188,66 @@ abstract class DibiResult implements IteratorAggregate, Countable /** - * Fetches all records from table. Records , but returns only first field - * @param string associative colum [, param, ... ] + * Fetches all records from table and returns associative tree + * Associative descriptor: assoc1,*,assoc2,#,assco3 + * builds a tree: $arr[value1][index][value2]['assoc3'][value3] = {record} + * + * @param string associative descriptor * @return array */ - final function fetchAssoc($assocBy) + final function fetchAssoc($assoc) { @$this->seek(0); $rec = $this->fetch(); - if (!$rec) - return array(); // empty resultset + if (!$rec) return array(); // empty resultset - $assocBy = func_get_args(); + $arr = NULL; + $assoc = explode(',', $assoc); - // check function parameters - !!! ignore or throw error? - foreach ($assocBy as $n => $assoc) // - if (!array_key_exists($assoc, $rec)) unset($assocBy[$n]); + if (count($assoc) === 1) { // speed-up + $as = $assoc[0]; + do { + $arr[ $rec[$as] ] = $rec; + } while ($rec = $this->fetch()); + return $arr; + } - $arr = array(); + $last = count($assoc) - 1; + if ($assoc[$last] === '#') unset($assoc[$last]); - do { // make associative arrays - foreach ($assocBy as $n => $assoc) { - $val[$n] = $rec[$assoc]; - unset($rec[$assoc]); + // make associative tree + do { + $x = & $arr; + + // iterative deepening + foreach ($assoc as $i => $as) { + if ($as === '*') { // indexed-array node + $x = & $x[]; + + } elseif ($as === '#') { // "record" node + if ($x === NULL) { + $x = $rec; + $x = & $x[ $assoc[$i+1] ]; + $x = NULL; // prepare child node + } else { + $x = & $x[ $assoc[$i+1] ]; + } + + } else { // associative-array node + $x = & $x[ $rec[ $as ] ]; + } } - foreach ($assocBy as $n => $assoc) { - if ($n == 0) - $tmp = &$arr[ $val[$n] ]; - else - $tmp = &$tmp[$assoc][ $val[$n] ]; + if ($x === NULL) $x = $rec; // build leaf - if ($tmp === NULL) - $tmp = $rec; - } } while ($rec = $this->fetch()); + unset($x); return $arr; } + /** * Fetches all records from table like $key => $value pairs * @return array