Skip to content

Commit

Permalink
Result::fetchAssoc() WIP #357
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 28, 2020
1 parent 34e1603 commit 6b34262
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/Dibi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ final public function fetchAssoc(string $assoc): array
}

$this->seek(0);
$row = $this->fetch();
$driver = $this->getResultDriver();
$row = $driver->fetch(true);
if (!$row) {
return []; // empty result set
}
Expand All @@ -246,7 +247,7 @@ final public function fetchAssoc(string $assoc): array
// check columns
foreach ($assoc as $as) {
// 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.");
}
}
Expand All @@ -261,6 +262,7 @@ final public function fetchAssoc(string $assoc): array

// make associative tree
do {
$this->normalize($row);
$x = &$data;

// iterative deepening
Expand All @@ -269,27 +271,33 @@ final public function fetchAssoc(string $assoc): array
$x = &$x[];

} elseif ($as === '=') { // "value" node
$x = $row->{$assoc[$i + 1]};
$x = $row[$assoc[$i + 1]];
continue 2;

} elseif ($as === '->') { // "object" node
if ($x === null) {
$x = clone $row;
$x = new $this->rowClass($row);
$x = &$x->{$assoc[$i + 1]};
$x = null; // prepare child node
} else {
$x = &$x->{$assoc[$i + 1]};
}

} elseif ($as !== '|') { // associative-array node
$x = &$x[$row->$as];
$x = &$x[$row[$as]];
}
}

if ($x === null) { // build leaf
$x = $row;
if ($this->rowFactory) {
$x = ($this->rowFactory)($row);
} elseif ($this->rowClass) {
$x = new $this->rowClass($row);
} else {
$x = $row;
}
}
} while ($row = $this->fetch());
} while ($row = $driver->fetch(true));

unset($x);
return $data;
Expand All @@ -300,7 +308,8 @@ final public function fetchAssoc(string $assoc): array
private function oldFetchAssoc(string $assoc)
{
$this->seek(0);
$row = $this->fetch();
$driver = $this->getResultDriver();
$row = $driver->fetch(true);
if (!$row) {
return []; // empty result set
}
Expand All @@ -323,6 +332,7 @@ private function oldFetchAssoc(string $assoc)
}

do {
$this->normalize($row);
$x = &$data;

foreach ($assoc as $i => $as) {
Expand All @@ -331,7 +341,7 @@ private function oldFetchAssoc(string $assoc)

} elseif ($as === '=') { // "record" node
if ($x === null) {
$x = $row->toArray();
$x = $row;
$x = &$x[$assoc[$i + 1]];
$x = null; // prepare child node
} else {
Expand All @@ -340,26 +350,30 @@ private function oldFetchAssoc(string $assoc)

} elseif ($as === '@') { // "object" node
if ($x === null) {
$x = clone $row;
$x = new $this->rowClass($row);
$x = &$x->{$assoc[$i + 1]};
$x = null; // prepare child node
} else {
$x = &$x->{$assoc[$i + 1]};
}

} else { // associative-array node
$x = &$x[$row->$as];
$x = &$x[$row[$as]];
}
}

if ($x === null) { // build leaf
if ($leaf === '=') {
$x = $row->toArray();
$x = $row;
} elseif ($this->rowFactory) {
$x = ($this->rowFactory)($row);
} elseif ($this->rowClass) {
$x = new $this->rowClass($row);
} else {
$x = $row;
}
}
} while ($row = $this->fetch());
} while ($row = $driver->fetch(true));

unset($x);
return $data;
Expand Down

0 comments on commit 6b34262

Please sign in to comment.