From 755f6d36c0fab1d8221e71af9c720248c5d2c54c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= <honza.kuchar@grifart.cz> Date: Sun, 8 May 2016 12:33:02 +0200 Subject: [PATCH] CursorDriver: polished --- src/PostgresDriver/CursorDriver.php | 56 +++++++++++++------ .../PostgresDriver/CursorDriverTest.phpt | 30 +++++----- 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/PostgresDriver/CursorDriver.php b/src/PostgresDriver/CursorDriver.php index 1a3d781..65e4e5d 100644 --- a/src/PostgresDriver/CursorDriver.php +++ b/src/PostgresDriver/CursorDriver.php @@ -6,14 +6,14 @@ namespace Grifart\Mappi\Store\PostgresDriver; use Dibi\Connection; +use Dibi\Row; /** * PostgreSQL cursor driver * * Basic envelope over PostgreSQL's cursor. This cursor envelope does not know * cursor position, thus cannot do boundary checks. If you reach end, you - * simply - * get no more values. You can check if you reached the end by + * simply get no more values. You can check if you reached the end by * `isHeadOnRecord()`. * * @package Grifart\Mappi\Store\PostgresDriver @@ -22,10 +22,12 @@ final class CursorDriver implements ICursorDriver { /** @var Connection */ private $connection; + /** @var string */ private $name; + /** @var bool */ - private $headOnRecord = false; + private $headOnRecord = FALSE; /** * Warning: this class itself does not check if cursor is really valid in @@ -41,7 +43,7 @@ final class CursorDriver implements ICursorDriver { $this->connection = $connection; $this->name = $name; - // todo: close; for now automatically closed on transaction end + // todo: close; for now automatically closed on transaction end; maybe in destructor? Ignore errors? } public function getConnection() : Connection @@ -118,40 +120,60 @@ final class CursorDriver implements ICursorDriver if ($rows === 0) { $this->headOnRecord = count($recs) === 1; - } - else { + } else { // todo: test this properly!! $this->headOnRecord = count($recs) === abs($rows); } // todo: DibiRecord to array - return $recs; + return $this->convertRows($recs); } public function fetchOneAt(int $index) : array { - $record = $this->connection->query( + $row = $this->connection->query( "FETCH ABSOLUTE %i FROM %n", $index, $this->getName() )->fetch(); - $this->headOnRecord = $record !== false; - if ($record === false) { - return null; + $this->headOnRecord = $row !== FALSE; + if ($row === FALSE) { + return NULL; } - return $record->toArray(); + return $this->convertRow($row); } public function fetchOneBy(int $rows) : array { - $record = $this->connection->query( + $row = $this->connection->query( "FETCH RELATIVE %i FROM %n", $rows, $this->getName() )->fetch(); - $this->headOnRecord = $record !== false; - if ($record === false) { - return null; + $this->headOnRecord = $row !== FALSE; + if ($row === FALSE) { + return NULL; + } + return $this->convertRow($row); + } + + // ----------- DATA CONVERSION ----------- + + private function convertRow(Row $row) : array + { + return $row->toArray(); + } + + /** + * @param Row[] $rows + * @return array + */ + private function convertRows(array $rows) : array + { + // TODO: TrackedCursor should return correct index values in array indexes + $new = []; + foreach ($rows as $row) { + $new[] = $this->convertRow($row); } - return $record->toArray(); + return $new; } } \ No newline at end of file diff --git a/tests/Store/PostgresDriver/CursorDriverTest.phpt b/tests/Store/PostgresDriver/CursorDriverTest.phpt index f897177..00c50fb 100644 --- a/tests/Store/PostgresDriver/CursorDriverTest.phpt +++ b/tests/Store/PostgresDriver/CursorDriverTest.phpt @@ -87,7 +87,7 @@ class CursorDriverTest extends BaseTest $result = $this->uut->fetchRange(0); // rows backwards + read current Assert::count(1, $result); - Assert::same(3, current($result[0]->toArray())); + Assert::same(3, current($result[0])); } // fetch( x > 0 ) @@ -106,8 +106,8 @@ class CursorDriverTest extends BaseTest // todo: split into fetch decorator for those special fetch* methods Assert::count(2, $result); - Assert::same(4, current($result[0]->toArray())); - Assert::same(5, current($result[1]->toArray())); + Assert::same(4, current($result[0])); + Assert::same(5, current($result[1])); Assert::same(5, $this->helper_fetchCurrentSingle()); } @@ -127,8 +127,8 @@ class CursorDriverTest extends BaseTest // todo: split into fetch decorator for those special fetch* methods Assert::count(2, $result); - Assert::same(2, current($result[0]->toArray())); - Assert::same(1, current($result[1]->toArray())); + Assert::same(2, current($result[0])); + Assert::same(1, current($result[1])); Assert::same(1, $this->helper_fetchCurrentSingle()); } @@ -223,11 +223,11 @@ class CursorDriverTest extends BaseTest $result = $this->uut->fetchRange(ICursorDriver::FETCH_REMAINING); Assert::count(5, $result); - Assert::same(996, current($result[0]->toArray())); - Assert::same(997, current($result[1]->toArray())); - Assert::same(998, current($result[2]->toArray())); - Assert::same(999, current($result[3]->toArray())); - Assert::same(1000, current($result[4]->toArray())); + Assert::same(996, current($result[0])); + Assert::same(997, current($result[1])); + Assert::same(998, current($result[2])); + Assert::same(999, current($result[3])); + Assert::same(1000, current($result[4])); Assert::null($this->helper_fetchCurrentSingle()); Assert::equal(1000, $this->helper_fetchPrevSingle()); @@ -243,11 +243,11 @@ class CursorDriverTest extends BaseTest $result = $this->uut->fetchRange(ICursorDriver::FETCH_FOREGOING); Assert::count(5, $result); - Assert::same(5, current($result[0]->toArray())); - Assert::same(4, current($result[1]->toArray())); - Assert::same(3, current($result[2]->toArray())); - Assert::same(2, current($result[3]->toArray())); - Assert::same(1, current($result[4]->toArray())); + Assert::same(5, current($result[0])); + Assert::same(4, current($result[1])); + Assert::same(3, current($result[2])); + Assert::same(2, current($result[3])); + Assert::same(1, current($result[4])); Assert::null($this->helper_fetchCurrentSingle()); } -- GitLab