Skip to content
Snippets Groups Projects
Commit 755f6d36 authored by Jan Kuchař's avatar Jan Kuchař
Browse files

CursorDriver: polished

parent 2c76cd15
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment