1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-15 10:57:22 +01:00
postgres/lib/PqUnbufferedResultSet.php

105 lines
3.0 KiB
PHP
Raw Normal View History

2016-12-30 06:21:17 +01:00
<?php
2016-09-14 16:27:39 +02:00
namespace Amp\Postgres;
use Amp\Coroutine;
2017-05-16 06:14:02 +02:00
use Amp\Producer;
use Amp\Promise;
2016-09-14 16:27:39 +02:00
use pq;
class PqUnbufferedResultSet implements ResultSet, Operation {
2016-09-14 16:27:39 +02:00
/** @var int */
private $numCols;
2017-05-16 06:28:37 +02:00
/** @var \Amp\Producer */
private $producer;
2017-11-16 16:43:18 +01:00
/** @var array|object Last row emitted. */
private $currentRow;
/** @var int Next row fetch type. */
private $type = self::FETCH_ASSOC;
/** @var \Amp\Postgres\Internal\ReferenceQueue */
private $queue;
2016-09-14 16:27:39 +02:00
/**
* @param callable(): \Amp\Promise $fetch Function to fetch next result row.
2016-09-14 16:27:39 +02:00
* @param \pq\Result $result PostgreSQL result object.
*/
public function __construct(callable $fetch, pq\Result $result) {
$this->numCols = $result->numCols;
$this->queue = $queue = new Internal\ReferenceQueue;
2017-11-16 16:43:18 +01:00
$this->producer = new Producer(static function (callable $emit) use ($queue, $result, $fetch) {
2016-09-14 16:27:39 +02:00
try {
do {
2017-02-07 04:32:17 +01:00
$next = $fetch(); // Request next result before current is consumed.
2017-11-16 16:43:18 +01:00
yield $emit($result);
2016-09-14 16:27:39 +02:00
$result = yield $next;
} while ($result instanceof pq\Result);
} finally {
$queue->unreference();
2016-09-14 16:27:39 +02:00
}
2017-11-16 16:43:18 +01:00
});
2016-09-14 16:27:39 +02:00
}
2017-02-16 23:23:50 +01:00
public function __destruct() {
if (!$this->queue->isReferenced()) { // Producer above did not complete, so consume remaining results.
Promise\rethrow(new Coroutine($this->dispose()));
}
}
2017-11-16 16:43:18 +01:00
/**
* {@inheritdoc}
*/
public function advance(int $type = self::FETCH_ASSOC): Promise {
$this->currentRow = null;
$this->type = $type;
return $this->producer->advance();
}
/**
* {@inheritdoc}
*/
public function getCurrent() {
if ($this->currentRow !== null) {
return $this->currentRow;
}
switch ($this->type) {
case self::FETCH_ASSOC:
return $this->currentRow = $this->producer->getCurrent()->fetchRow(pq\Result::FETCH_ASSOC);
case self::FETCH_ARRAY:
return $this->currentRow = $this->producer->getCurrent()->fetchRow(pq\Result::FETCH_ARRAY);
case self::FETCH_OBJECT:
return $this->currentRow = $this->producer->getCurrent()->fetchRow(pq\Result::FETCH_OBJECT);
default:
throw new \Error("Invalid result fetch type");
}
}
private function dispose(): \Generator {
try {
while (yield $this->producer->advance()); // Discard unused result rows.
} catch (\Throwable $exception) {
// Ignore failure while discarding results.
}
}
/**
* @return int Number of fields (columns) in each result set.
*/
2016-09-14 16:27:39 +02:00
public function numFields(): int {
return $this->numCols;
}
/**
* {@inheritdoc}
*/
public function onDestruct(callable $onComplete) {
$this->queue->onDestruct($onComplete);
}
2017-05-16 06:28:37 +02:00
}