1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-15 18:44:27 +02:00

* DibiResult::fetchAssoc() rewritten

This commit is contained in:
David Grudl
2007-02-05 05:14:48 +00:00
parent 0c86515076
commit 4b2e04220b
3 changed files with 49 additions and 31 deletions

View File

@@ -13,11 +13,11 @@
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @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 class
DibiDriver{protected$config;public$formats=array('TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);static DibiDriver{protected$config;public$formats=array('TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);static
public public
@@ -110,11 +110,9 @@ function
fetchAll(){@$this->seek(0);$rec=$this->fetch();if(!$rec)return 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 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 function
fetchAssoc($assocBy){@$this->seek(0);$rec=$this->fetch();if(!$rec)return fetchAssoc($assoc){@$this->seek(0);$rec=$this->fetch();if(!$rec)return
array();$assocBy=func_get_args();foreach($assocBy 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$n=>$assoc)if(!array_key_exists($assoc,$rec))unset($assocBy[$n]);$arr=array();do{foreach($assocBy 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
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
function function
fetchPairs($key,$value){@$this->seek(0);$rec=$this->fetch();if(!$rec)return 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 array();if(!array_key_exists($key,$rec)||!array_key_exists($value,$rec))return

View File

@@ -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 * @return array
*/ */
final function fetchAll() final function fetchAll()
@@ -188,46 +188,66 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Fetches all records from table. Records , but returns only first field * Fetches all records from table and returns associative tree
* @param string associative colum [, param, ... ] * Associative descriptor: assoc1,*,assoc2,#,assco3
* builds a tree: $arr[value1][index][value2]['assoc3'][value3] = {record}
*
* @param string associative descriptor
* @return array * @return array
*/ */
final function fetchAssoc($assocBy) final function fetchAssoc($assoc)
{ {
@$this->seek(0); @$this->seek(0);
$rec = $this->fetch(); $rec = $this->fetch();
if (!$rec) if (!$rec) return array(); // empty resultset
return array(); // empty resultset
$assocBy = func_get_args(); $arr = NULL;
$assoc = explode(',', $assoc);
// check function parameters - !!! ignore or throw error? if (count($assoc) === 1) { // speed-up
foreach ($assocBy as $n => $assoc) // $as = $assoc[0];
if (!array_key_exists($assoc, $rec)) unset($assocBy[$n]); 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 // make associative tree
foreach ($assocBy as $n => $assoc) { do {
$val[$n] = $rec[$assoc]; $x = & $arr;
unset($rec[$assoc]);
// 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 ($x === NULL) $x = $rec; // build leaf
if ($n == 0)
$tmp = &$arr[ $val[$n] ];
else
$tmp = &$tmp[$assoc][ $val[$n] ];
if ($tmp === NULL)
$tmp = $rec;
}
} while ($rec = $this->fetch()); } while ($rec = $this->fetch());
unset($x);
return $arr; return $arr;
} }
/** /**
* Fetches all records from table like $key => $value pairs * Fetches all records from table like $key => $value pairs
* @return array * @return array