1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 18:02:25 +01: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
* @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 (, 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

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
*/
final function fetchAll()
@ -188,45 +188,65 @@ 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]);
$arr = array();
do { // make associative arrays
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;
}
if (count($assoc) === 1) { // speed-up
$as = $assoc[0];
do {
$arr[ $rec[$as] ] = $rec;
} while ($rec = $this->fetch());
return $arr;
}
$last = count($assoc) - 1;
if ($assoc[$last] === '#') unset($assoc[$last]);
// 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 ] ];
}
}
if ($x === NULL) $x = $rec; // build leaf
} while ($rec = $this->fetch());
unset($x);
return $arr;
}
/**
* Fetches all records from table like $key => $value pairs