1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-14 02:17:27 +01:00
postgres/lib/PqBufferedResultSet.php

76 lines
1.9 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;
2017-11-16 16:43:18 +01:00
use Amp\Promise;
use Amp\Success;
2016-09-14 16:27:39 +02:00
use pq;
class PqBufferedResultSet implements ResultSet {
2016-09-14 16:27:39 +02:00
/** @var \pq\Result */
private $result;
2017-11-16 16:43:18 +01:00
/** @var int */
private $position = 0;
/** @var mixed Last row emitted. */
private $currentRow;
/** @var int Next row fetch type. */
private $type = self::FETCH_ASSOC;
2016-09-14 16:27:39 +02:00
/**
* @param pq\Result $result PostgreSQL result object.
*/
public function __construct(pq\Result $result) {
$this->result = $result;
2017-12-17 22:51:40 +01:00
$this->result->autoConvert = pq\Result::CONV_SCALAR | pq\Result::CONV_ARRAY;
2017-11-16 16:43:18 +01:00
}
/**
* {@inheritdoc}
*/
public function advance(int $type = self::FETCH_ASSOC): Promise {
$this->currentRow = null;
$this->type = $type;
if (++$this->position > $this->result->numRows) {
return new Success(false);
}
return new Success(true);
}
/**
* {@inheritdoc}
*/
public function getCurrent() {
if ($this->currentRow !== null) {
return $this->currentRow;
}
if ($this->position > $this->result->numRows) {
throw new \Error("No more rows remain in the result set");
}
switch ($this->type) {
case self::FETCH_ASSOC:
return $this->currentRow = $this->result->fetchRow(pq\Result::FETCH_ASSOC);
case self::FETCH_ARRAY:
return $this->currentRow = $this->result->fetchRow(pq\Result::FETCH_ARRAY);
case self::FETCH_OBJECT:
return $this->currentRow = $this->result->fetchRow(pq\Result::FETCH_OBJECT);
default:
throw new \Error("Invalid result fetch type");
}
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
public function numRows(): int {
return $this->result->numRows;
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
public function numFields(): int {
return $this->result->numCols;
}
}