2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-02-16 23:23:50 +01:00
|
|
|
use Amp\{ CallableMaker, Producer };
|
2016-09-14 16:27:39 +02:00
|
|
|
use pq;
|
|
|
|
|
|
|
|
class PqUnbufferedResult extends TupleResult implements Operation {
|
2017-02-16 23:23:50 +01:00
|
|
|
use CallableMaker, Internal\Operation {
|
|
|
|
__destruct as private release;
|
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $numCols;
|
|
|
|
|
|
|
|
/**
|
2017-02-07 04:32:17 +01:00
|
|
|
* @param callable(): \AsyncInterop\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;
|
2017-01-18 18:05:05 +01:00
|
|
|
parent::__construct(new Producer(function (callable $emit) use ($result, $fetch) {
|
2016-09-14 16:27:39 +02:00
|
|
|
$count = 0;
|
|
|
|
try {
|
|
|
|
do {
|
2017-02-07 04:32:17 +01:00
|
|
|
$next = $fetch(); // Request next result before current is consumed.
|
2016-09-14 16:27:39 +02:00
|
|
|
++$count;
|
|
|
|
yield $emit($result->fetchRow(pq\Result::FETCH_ASSOC));
|
|
|
|
$result = yield $next;
|
|
|
|
} while ($result instanceof pq\Result);
|
|
|
|
} finally {
|
|
|
|
$this->complete();
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}));
|
|
|
|
}
|
2017-02-16 23:23:50 +01:00
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
parent::__destruct();
|
|
|
|
$this->release();
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
public function numFields(): int {
|
|
|
|
return $this->numCols;
|
|
|
|
}
|
|
|
|
}
|