1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-13 01:24:06 +02:00

+ DibiResult::fetchAll()

* changed year in headers (2007)
This commit is contained in:
David Grudl
2007-01-08 00:55:11 +00:00
parent 3b8766d376
commit ac980fe8c9
14 changed files with 143 additions and 49 deletions

View File

@@ -8,7 +8,7 @@
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database

View File

@@ -8,7 +8,7 @@
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database

View File

@@ -8,7 +8,7 @@
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database
@@ -38,8 +38,8 @@ if (!interface_exists('Countable', false)) {
* $result = dibi::query('SELECT * FROM [table]');
* $value = $result->fetchSingle();
* $all = $result->fetchAll();
* $assoc = $result->fetchAll('id');
* $assoc = $result->fetchAll('active', 'id');
* $assoc = $result->fetchAssoc('id');
* $assoc = $result->fetchAssoc('active', 'id');
* unset($result);
* </code>
*/
@@ -52,6 +52,15 @@ abstract class DibiResult implements IteratorAggregate, Countable
protected $convert;
static private $meta = array(
dibi::FIELD_TEXT => 'string',
dibi::FIELD_BINARY => 'string',
dibi::FIELD_BOOL => 'bool',
dibi::FIELD_INTEGER => 'int',
dibi::FIELD_FLOAT => 'float',
dibi::FIELD_COUNTER => 'int',
);
/**
@@ -150,7 +159,6 @@ abstract class DibiResult implements IteratorAggregate, Countable
/**
* Fetches all records from table. Records , but returns only first field
* @param string associative colum [, param, ... ]
* @return array
*/
final function fetchAll()
@@ -160,18 +168,40 @@ abstract class DibiResult implements IteratorAggregate, Countable
if (!$rec)
return array(); // empty resultset
$assocBy = func_get_args();
$arr = array();
if (!$assocBy) { // no associative array
$value = count($rec) == 1 ? key($rec) : NULL;
if (count($rec) == 1) {
$key = key($rec);
do {
$arr[] = $value === NULL ? $rec : $rec[$value];
$arr[] = $rec[$key];
} while ($rec = $this->fetch());
return $arr;
} else {
do {
$arr[] = $rec;
} while ($rec = $this->fetch());
}
return $arr;
}
/**
* Fetches all records from table. Records , but returns only first field
* @param string associative colum [, param, ... ]
* @return array
*/
final function fetchAssoc($assocBy)
{
@$this->seek(0);
$rec = $this->fetch();
if (!$rec)
return array(); // empty resultset
$assocBy = func_get_args();
$arr = array();
do { // make associative arrays
foreach ($assocBy as $n => $assoc) {
$val[$n] = $rec[$assoc];
@@ -193,7 +223,6 @@ abstract class DibiResult implements IteratorAggregate, Countable
}
/**
* Fetches all records from table like $key => $value pairs
* @return array
@@ -246,23 +275,13 @@ abstract class DibiResult implements IteratorAggregate, Countable
}
public function convert($value, $type)
{
if ($value === NULL || $value === FALSE)
return $value;
static $conv = array(
dibi::FIELD_TEXT => 'string',
dibi::FIELD_BINARY => 'string',
dibi::FIELD_BOOL => 'bool',
dibi::FIELD_INTEGER => 'int',
dibi::FIELD_FLOAT => 'float',
dibi::FIELD_COUNTER => 'int',
);
if (isset($conv[$type])) {
settype($value, $conv[$type]);
if (isset(self::$meta[$type])) {
settype($value, self::$meta[$type]);
return $value;
}

View File

@@ -8,7 +8,7 @@
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database