mirror of
https://github.com/danog/postgres.git
synced 2024-12-02 09:27:54 +01:00
Free result on disposal
Rather than on destruction.
This commit is contained in:
parent
e742c25c3e
commit
13f89b854b
@ -17,6 +17,9 @@ final class PgSqlResultSet implements Result
|
||||
/** @var int */
|
||||
private $position = 0;
|
||||
|
||||
/** @var int */
|
||||
private $rowCount;
|
||||
|
||||
/** @var int[] */
|
||||
private $fieldTypes = [];
|
||||
|
||||
@ -36,6 +39,7 @@ final class PgSqlResultSet implements Result
|
||||
public function __construct($handle, Promise $nextResult)
|
||||
{
|
||||
$this->handle = $handle;
|
||||
$this->rowCount = \pg_num_rows($this->handle);
|
||||
$this->nextResult = $nextResult;
|
||||
|
||||
$numFields = \pg_num_fields($this->handle);
|
||||
@ -66,7 +70,7 @@ final class PgSqlResultSet implements Result
|
||||
return new Failure(new DisposedException);
|
||||
}
|
||||
|
||||
if (++$this->position > \pg_num_rows($this->handle)) {
|
||||
if (++$this->position > $this->rowCount) {
|
||||
return new Success(null);
|
||||
}
|
||||
|
||||
@ -74,21 +78,23 @@ final class PgSqlResultSet implements Result
|
||||
|
||||
if ($result === false) {
|
||||
$message = \pg_result_error($this->handle);
|
||||
\pg_free_result($this->handle);
|
||||
return new Failure(new FailureException($message));
|
||||
}
|
||||
|
||||
try {
|
||||
return new Success($this->processRow($result));
|
||||
} catch (\Throwable $exception) {
|
||||
\pg_free_result($this->handle);
|
||||
$this->handle = null;
|
||||
return new Failure($exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function dispose(): void
|
||||
{
|
||||
if ($this->handle === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
\pg_free_result($this->handle);
|
||||
$this->handle = null;
|
||||
}
|
||||
|
||||
@ -237,6 +243,6 @@ final class PgSqlResultSet implements Result
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
return \pg_num_rows($this->handle);
|
||||
return $this->rowCount;
|
||||
}
|
||||
}
|
||||
|
@ -17,16 +17,20 @@ final class PqBufferedResultSet implements Result
|
||||
/** @var int */
|
||||
private $position = 0;
|
||||
|
||||
/** @var Promise<ResultSet|null> */
|
||||
/** @var int */
|
||||
private $rowCount;
|
||||
|
||||
/** @var Promise<Result|null> */
|
||||
private $nextResult;
|
||||
|
||||
/**
|
||||
* @param pq\Result $result PostgreSQL result object.
|
||||
* @param Promise<ResultSet|null> $nextResult Promise for next result set.
|
||||
* @param Promise<Result|null> $nextResult Promise for next result set.
|
||||
*/
|
||||
public function __construct(pq\Result $result, Promise $nextResult)
|
||||
{
|
||||
$this->result = $result;
|
||||
$this->rowCount = $result->numRows;
|
||||
$this->result->autoConvert = pq\Result::CONV_SCALAR | pq\Result::CONV_ARRAY;
|
||||
$this->nextResult = $nextResult;
|
||||
}
|
||||
@ -68,6 +72,6 @@ final class PqBufferedResultSet implements Result
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
return $this->result->numRows;
|
||||
return $this->rowCount;
|
||||
}
|
||||
}
|
||||
|
@ -17,19 +17,19 @@ final class PqUnbufferedResultSet implements Result
|
||||
private $nextResult;
|
||||
|
||||
/**
|
||||
* @param callable():Promise<\pq\Result|Result|null> $fetch Function to fetch next result row.
|
||||
* @param \pq\Result $result Initial PostgreSQL result object.
|
||||
* @param callable():Promise<\pq\Result|null> $fetch Function to fetch next result row.
|
||||
* @param \pq\Result $result Initial pq\Result result object.
|
||||
* @param Promise<Result|null> $nextResult
|
||||
*/
|
||||
public function __construct(callable $fetch, pq\Result $result, Promise $nextResult)
|
||||
{
|
||||
$this->nextResult = $nextResult;
|
||||
$this->generator = new AsyncGenerator(static function (callable $yield) use ($result, $fetch): \Generator {
|
||||
$this->generator = new AsyncGenerator(static function (callable $emit) use ($result, $fetch): \Generator {
|
||||
try {
|
||||
do {
|
||||
$promise = $fetch();
|
||||
$result->autoConvert = pq\Result::CONV_SCALAR | pq\Result::CONV_ARRAY;
|
||||
yield $yield($result->fetchRow(pq\Result::FETCH_ASSOC));
|
||||
yield $emit($result->fetchRow(pq\Result::FETCH_ASSOC));
|
||||
$result = yield $promise;
|
||||
} while ($result instanceof pq\Result);
|
||||
} catch (DisposedException $exception) {
|
||||
|
Loading…
Reference in New Issue
Block a user