1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-11 16:44:30 +02:00

Revert "Result: fetch refactoring" [Close #284]

This reverts commit ab7683a3d2.
This commit is contained in:
David Grudl
2018-05-01 13:05:43 +02:00
parent 3c9a3da83a
commit 9bcca8feb0

View File

@@ -166,10 +166,13 @@ class Result implements IDataSource
*/ */
final public function fetch() final public function fetch()
{ {
$row = $this->fetchArray(); $row = $this->getResultDriver()->fetch(true);
if ($row === null) { if ($row === null) {
return null; return null;
} elseif ($this->rowFactory) { }
$this->fetched = true;
$this->normalize($row);
if ($this->rowFactory) {
return ($this->rowFactory)($row); return ($this->rowFactory)($row);
} elseif ($this->rowClass) { } elseif ($this->rowClass) {
return new $this->rowClass($row); return new $this->rowClass($row);
@@ -183,14 +186,6 @@ class Result implements IDataSource
* @return mixed value on success, null if no next record * @return mixed value on success, null if no next record
*/ */
final public function fetchSingle() final public function fetchSingle()
{
if ($row = $this->fetchArray()) {
return reset($row);
}
}
private function fetchArray(): ?array
{ {
$row = $this->getResultDriver()->fetch(true); $row = $this->getResultDriver()->fetch(true);
if ($row === null) { if ($row === null) {
@@ -198,7 +193,7 @@ class Result implements IDataSource
} }
$this->fetched = true; $this->fetched = true;
$this->normalize($row); $this->normalize($row);
return $row; return reset($row);
} }
@@ -244,7 +239,7 @@ class Result implements IDataSource
} }
$this->seek(0); $this->seek(0);
$row = $this->fetchArray(); $row = $this->fetch();
if (!$row) { if (!$row) {
return []; // empty result set return []; // empty result set
} }
@@ -255,7 +250,7 @@ class Result implements IDataSource
// check columns // check columns
foreach ($assoc as $as) { foreach ($assoc as $as) {
// offsetExists ignores null in PHP 5.2.1, isset() surprisingly null accepts // offsetExists ignores null in PHP 5.2.1, isset() surprisingly null accepts
if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !array_key_exists($as, $row)) { if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !property_exists($row, $as)) {
throw new \InvalidArgumentException("Unknown column '$as' in associative descriptor."); throw new \InvalidArgumentException("Unknown column '$as' in associative descriptor.");
} }
} }
@@ -270,7 +265,6 @@ class Result implements IDataSource
// make associative tree // make associative tree
do { do {
$row = new Row($row);
$x = &$data; $x = &$data;
// iterative deepening // iterative deepening
@@ -299,7 +293,7 @@ class Result implements IDataSource
if ($x === null) { // build leaf if ($x === null) { // build leaf
$x = $row; $x = $row;
} }
} while ($row = $this->fetchArray()); } while ($row = $this->fetch());
unset($x); unset($x);
return $data; return $data;
@@ -312,7 +306,7 @@ class Result implements IDataSource
private function oldFetchAssoc(string $assoc) private function oldFetchAssoc(string $assoc)
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetchArray(); $row = $this->fetch();
if (!$row) { if (!$row) {
return []; // empty result set return []; // empty result set
} }
@@ -335,7 +329,6 @@ class Result implements IDataSource
} }
do { do {
$row = new Row($row);
$x = &$data; $x = &$data;
foreach ($assoc as $i => $as) { foreach ($assoc as $i => $as) {
@@ -372,7 +365,7 @@ class Result implements IDataSource
$x = $row; $x = $row;
} }
} }
} while ($row = $this->fetchArray()); } while ($row = $this->fetch());
unset($x); unset($x);
return $data; return $data;
@@ -386,7 +379,7 @@ class Result implements IDataSource
final public function fetchPairs(string $key = null, string $value = null): array final public function fetchPairs(string $key = null, string $value = null): array
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetchArray(); $row = $this->fetch();
if (!$row) { if (!$row) {
return []; // empty result set return []; // empty result set
} }
@@ -399,37 +392,37 @@ class Result implements IDataSource
} }
// autodetect // autodetect
$keys = array_keys($row); $tmp = array_keys($row->toArray());
$key = $keys[0]; $key = $tmp[0];
if (count($row) < 2) { // indexed-array if (count($row) < 2) { // indexed-array
do { do {
$data[] = $row[$key]; $data[] = $row[$key];
} while ($row = $this->fetchArray()); } while ($row = $this->fetch());
return $data; return $data;
} }
$value = $keys[1]; $value = $tmp[1];
} else { } else {
if (!array_key_exists($value, $row)) { if (!property_exists($row, $value)) {
throw new \InvalidArgumentException("Unknown value column '$value'."); throw new \InvalidArgumentException("Unknown value column '$value'.");
} }
if ($key === null) { // indexed-array if ($key === null) { // indexed-array
do { do {
$data[] = $row[$value]; $data[] = $row[$value];
} while ($row = $this->fetchArray()); } while ($row = $this->fetch());
return $data; return $data;
} }
if (!array_key_exists($key, $row)) { if (!property_exists($row, $key)) {
throw new \InvalidArgumentException("Unknown key column '$key'."); throw new \InvalidArgumentException("Unknown key column '$key'.");
} }
} }
do { do {
$data[(string) $row[$key]] = $row[$value]; $data[(string) $row[$key]] = $row[$value];
} while ($row = $this->fetchArray()); } while ($row = $this->fetch());
return $data; return $data;
} }