diff --git a/src/PgSqlResultSet.php b/src/PgSqlResultSet.php index 682f3c2..d2d1997 100644 --- a/src/PgSqlResultSet.php +++ b/src/PgSqlResultSet.php @@ -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; } } diff --git a/src/PqBufferedResultSet.php b/src/PqBufferedResultSet.php index 0b377a9..ae1757e 100644 --- a/src/PqBufferedResultSet.php +++ b/src/PqBufferedResultSet.php @@ -17,16 +17,20 @@ final class PqBufferedResultSet implements Result /** @var int */ private $position = 0; - /** @var Promise */ + /** @var int */ + private $rowCount; + + /** @var Promise */ private $nextResult; /** * @param pq\Result $result PostgreSQL result object. - * @param Promise $nextResult Promise for next result set. + * @param Promise $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; } } diff --git a/src/PqUnbufferedResultSet.php b/src/PqUnbufferedResultSet.php index e94c8ca..ee5a4de 100644 --- a/src/PqUnbufferedResultSet.php +++ b/src/PqUnbufferedResultSet.php @@ -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 $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) {