mirror of
https://github.com/dg/dibi.git
synced 2025-07-13 18:46:23 +02:00
Result: fetch refactoring
This commit is contained in:
@ -166,13 +166,10 @@ class Result implements IDataSource
|
|||||||
*/
|
*/
|
||||||
final public function fetch()
|
final public function fetch()
|
||||||
{
|
{
|
||||||
$row = $this->getResultDriver()->fetch(true);
|
$row = $this->fetchArray();
|
||||||
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);
|
||||||
@ -186,6 +183,14 @@ 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) {
|
||||||
@ -193,7 +198,7 @@ class Result implements IDataSource
|
|||||||
}
|
}
|
||||||
$this->fetched = true;
|
$this->fetched = true;
|
||||||
$this->normalize($row);
|
$this->normalize($row);
|
||||||
return reset($row);
|
return $row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -239,7 +244,7 @@ class Result implements IDataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->seek(0);
|
$this->seek(0);
|
||||||
$row = $this->fetch();
|
$row = $this->fetchArray();
|
||||||
if (!$row) {
|
if (!$row) {
|
||||||
return []; // empty result set
|
return []; // empty result set
|
||||||
}
|
}
|
||||||
@ -250,7 +255,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 !== '|' && !property_exists($row, $as)) {
|
if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !array_key_exists($as, $row)) {
|
||||||
throw new \InvalidArgumentException("Unknown column '$as' in associative descriptor.");
|
throw new \InvalidArgumentException("Unknown column '$as' in associative descriptor.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -265,6 +270,7 @@ class Result implements IDataSource
|
|||||||
|
|
||||||
// make associative tree
|
// make associative tree
|
||||||
do {
|
do {
|
||||||
|
$row = new Row($row);
|
||||||
$x = &$data;
|
$x = &$data;
|
||||||
|
|
||||||
// iterative deepening
|
// iterative deepening
|
||||||
@ -293,7 +299,7 @@ class Result implements IDataSource
|
|||||||
if ($x === null) { // build leaf
|
if ($x === null) { // build leaf
|
||||||
$x = $row;
|
$x = $row;
|
||||||
}
|
}
|
||||||
} while ($row = $this->fetch());
|
} while ($row = $this->fetchArray());
|
||||||
|
|
||||||
unset($x);
|
unset($x);
|
||||||
return $data;
|
return $data;
|
||||||
@ -306,7 +312,7 @@ class Result implements IDataSource
|
|||||||
private function oldFetchAssoc(string $assoc)
|
private function oldFetchAssoc(string $assoc)
|
||||||
{
|
{
|
||||||
$this->seek(0);
|
$this->seek(0);
|
||||||
$row = $this->fetch();
|
$row = $this->fetchArray();
|
||||||
if (!$row) {
|
if (!$row) {
|
||||||
return []; // empty result set
|
return []; // empty result set
|
||||||
}
|
}
|
||||||
@ -329,6 +335,7 @@ class Result implements IDataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
$row = new Row($row);
|
||||||
$x = &$data;
|
$x = &$data;
|
||||||
|
|
||||||
foreach ($assoc as $i => $as) {
|
foreach ($assoc as $i => $as) {
|
||||||
@ -365,7 +372,7 @@ class Result implements IDataSource
|
|||||||
$x = $row;
|
$x = $row;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ($row = $this->fetch());
|
} while ($row = $this->fetchArray());
|
||||||
|
|
||||||
unset($x);
|
unset($x);
|
||||||
return $data;
|
return $data;
|
||||||
@ -379,7 +386,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->fetch();
|
$row = $this->fetchArray();
|
||||||
if (!$row) {
|
if (!$row) {
|
||||||
return []; // empty result set
|
return []; // empty result set
|
||||||
}
|
}
|
||||||
@ -392,37 +399,37 @@ class Result implements IDataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
// autodetect
|
// autodetect
|
||||||
$tmp = array_keys($row->toArray());
|
$keys = array_keys($row);
|
||||||
$key = $tmp[0];
|
$key = $keys[0];
|
||||||
if (count($row) < 2) { // indexed-array
|
if (count($row) < 2) { // indexed-array
|
||||||
do {
|
do {
|
||||||
$data[] = $row[$key];
|
$data[] = $row[$key];
|
||||||
} while ($row = $this->fetch());
|
} while ($row = $this->fetchArray());
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $tmp[1];
|
$value = $keys[1];
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (!property_exists($row, $value)) {
|
if (!array_key_exists($value, $row)) {
|
||||||
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->fetch());
|
} while ($row = $this->fetchArray());
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!property_exists($row, $key)) {
|
if (!array_key_exists($key, $row)) {
|
||||||
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->fetch());
|
} while ($row = $this->fetchArray());
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user