diff --git a/composer.json b/composer.json index f8d3f27..4538b66 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ } ], "require": { + "php": "^7.0", "amphp/amp": "^2", "amphp/sql": "dev-master" }, diff --git a/src/Connection.php b/src/Connection.php index f8cf662..f1b4b76 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -55,13 +55,13 @@ final public function isAlive(): bool * * @throws FailureException */ - final public function lastUsedAt(): int + final public function getLastUsedAt(): int { if (! $this->handle) { throw new FailureException('Not connected'); } - return $this->handle->lastUsedAt(); + return $this->handle->getLastUsedAt(); } /** @@ -183,7 +183,7 @@ final public function listen(string $channel): Promise * * @throws FailureException */ - final public function transaction(int $isolation = Transaction::ISOLATION_COMMITTED): Promise + final public function beginTransaction(int $isolation = Transaction::ISOLATION_COMMITTED): Promise { if (! $this->handle) { throw new FailureException('Not connected'); @@ -213,7 +213,7 @@ final public function transaction(int $isolation = Transaction::ISOLATION_COMMIT $this->busy = new Deferred; - return new ConnectionTransaction($this->handle, $isolation, $this->release); + return new ConnectionTransaction($this->handle, $this->release, $isolation); }); } diff --git a/src/ConnectionTransaction.php b/src/ConnectionTransaction.php index 3171ebc..1558e1b 100644 --- a/src/ConnectionTransaction.php +++ b/src/ConnectionTransaction.php @@ -23,12 +23,12 @@ final class ConnectionTransaction implements Transaction /** * @param Handle $handle - * @param int $isolation * @param callable $release + * @param int $isolation * * @throws \Error If the isolation level is invalid. */ - public function __construct(Handle $handle, int $isolation = SqlTransaction::ISOLATION_COMMITTED, callable $release) + public function __construct(Handle $handle, callable $release, int $isolation = SqlTransaction::ISOLATION_COMMITTED) { switch ($isolation) { case SqlTransaction::ISOLATION_UNCOMMITTED: @@ -62,9 +62,9 @@ public function __destruct() /** * {@inheritdoc} */ - public function lastUsedAt(): int + public function getLastUsedAt(): int { - return $this->handle->lastUsedAt(); + return $this->handle->getLastUsedAt(); } /** diff --git a/src/PgSqlCommandResult.php b/src/PgSqlCommandResult.php index 909c852..5b23c08 100644 --- a/src/PgSqlCommandResult.php +++ b/src/PgSqlCommandResult.php @@ -28,7 +28,7 @@ public function __destruct() /** * @return int Number of rows affected by the INSERT, UPDATE, or DELETE query. */ - public function affectedRows(): int + public function getAffectedRowCount(): int { return \pg_affected_rows($this->handle); } @@ -36,7 +36,7 @@ public function affectedRows(): int /** * @return string */ - public function lastOid(): string + public function getLastOid(): string { return (string) \pg_last_oid($this->handle); } diff --git a/src/PgSqlHandle.php b/src/PgSqlHandle.php index 9ae779d..32e8f5d 100644 --- a/src/PgSqlHandle.php +++ b/src/PgSqlHandle.php @@ -195,7 +195,7 @@ public function isAlive(): bool /** * {@inheritdoc} */ - public function lastUsedAt(): int + public function getLastUsedAt(): int { return $this->lastUsedAt; } diff --git a/src/PgSqlResultSet.php b/src/PgSqlResultSet.php index 594a398..46944ec 100644 --- a/src/PgSqlResultSet.php +++ b/src/PgSqlResultSet.php @@ -17,9 +17,6 @@ final class PgSqlResultSet implements ResultSet /** @var mixed Last row emitted. */ private $currentRow; - /** @var int Next row fetch type. */ - private $type = self::FETCH_ASSOC; - /** @var int[] */ private $fieldTypes = []; @@ -56,10 +53,9 @@ public function __destruct() /** * {@inheritdoc} */ - public function advance(int $type = self::FETCH_ASSOC): Promise + public function advance(): Promise { $this->currentRow = null; - $this->type = $type; if (++$this->position > \pg_num_rows($this->handle)) { return new Success(false); @@ -71,7 +67,7 @@ public function advance(int $type = self::FETCH_ASSOC): Promise /** * {@inheritdoc} */ - public function getCurrent() + public function getCurrent(int $type = self::FETCH_ASSOC) { if ($this->currentRow !== null) { return $this->currentRow; @@ -98,7 +94,7 @@ public function getCurrent() $result[$column] = $this->cast($column, $result[$column]); } - if ($this->type === self::FETCH_ARRAY) { + if ($type === self::FETCH_ARRAY) { return $this->currentRow = $result; } @@ -107,11 +103,11 @@ public function getCurrent() $assoc[$name] = $result[$index]; } - if ($this->type === self::FETCH_ASSOC) { + if ($type === self::FETCH_ASSOC) { return $this->currentRow = $assoc; } - if ($this->type === self::FETCH_OBJECT) { + if ($type === self::FETCH_OBJECT) { return $this->currentRow = (object) $assoc; } @@ -243,7 +239,7 @@ public function numRows(): int /** * @return int Number of fields in each row. */ - public function numFields(): int + public function getFieldCount(): int { return \pg_num_fields($this->handle); } @@ -255,9 +251,9 @@ public function numFields(): int * * @throws \Error If the field number does not exist in the result. */ - public function fieldName(int $fieldNum): string + public function getFieldName(int $fieldNum): string { - if (0 > $fieldNum || $this->numFields() <= $fieldNum) { + if (0 > $fieldNum || $this->getFieldCount() <= $fieldNum) { throw new \Error(\sprintf('No field with index %d in result', $fieldNum)); } @@ -271,7 +267,7 @@ public function fieldName(int $fieldNum): string * * @throws \Error If the field name does not exist in the result. */ - public function fieldNum(string $fieldName): int + public function getFieldIndex(string $fieldName): int { $result = \pg_field_num($this->handle, $fieldName); diff --git a/src/PgSqlStatement.php b/src/PgSqlStatement.php index 3d6790e..b440c6f 100644 --- a/src/PgSqlStatement.php +++ b/src/PgSqlStatement.php @@ -16,9 +16,6 @@ final class PgSqlStatement implements Statement /** @var string */ private $sql; - /** @var Internal\ReferenceQueue */ - private $queue; - /** @var string[] */ private $params; @@ -58,7 +55,7 @@ public function getQuery(): string } /** {@inheritdoc} */ - public function lastUsedAt(): int + public function getLastUsedAt(): int { return $this->lastUsedAt; } diff --git a/src/PooledResultSet.php b/src/PooledResultSet.php index 4c04853..fcf88f3 100644 --- a/src/PooledResultSet.php +++ b/src/PooledResultSet.php @@ -19,8 +19,8 @@ public function __construct(ResultSet $result, callable $release) $this->result = $result; } - public function numFields(): int + public function getFieldCount(): int { - return $this->result->numFields(); + return $this->result->getFieldCount(); } } diff --git a/src/PqBufferedResultSet.php b/src/PqBufferedResultSet.php index 968b879..e0dc232 100644 --- a/src/PqBufferedResultSet.php +++ b/src/PqBufferedResultSet.php @@ -17,9 +17,6 @@ final class PqBufferedResultSet implements ResultSet /** @var mixed Last row emitted. */ private $currentRow; - /** @var int Next row fetch type. */ - private $type = self::FETCH_ASSOC; - /** * @param pq\Result $result PostgreSQL result object. */ @@ -32,10 +29,9 @@ public function __construct(pq\Result $result) /** * {@inheritdoc} */ - public function advance(int $type = self::FETCH_ASSOC): Promise + public function advance(): Promise { $this->currentRow = null; - $this->type = $type; if (++$this->position > $this->result->numRows) { return new Success(false); @@ -47,7 +43,7 @@ public function advance(int $type = self::FETCH_ASSOC): Promise /** * {@inheritdoc} */ - public function getCurrent() + public function getCurrent(int $type = self::FETCH_ASSOC) { if ($this->currentRow !== null) { return $this->currentRow; @@ -57,7 +53,7 @@ public function getCurrent() throw new \Error("No more rows remain in the result set"); } - switch ($this->type) { + switch ($type) { case self::FETCH_ASSOC: return $this->currentRow = $this->result->fetchRow(pq\Result::FETCH_ASSOC); case self::FETCH_ARRAY: @@ -69,12 +65,12 @@ public function getCurrent() } } - public function numRows(): int + public function getNumRows(): int { return $this->result->numRows; } - public function numFields(): int + public function getFieldCount(): int { return $this->result->numCols; } diff --git a/src/PqCommandResult.php b/src/PqCommandResult.php index 43af02d..4972321 100644 --- a/src/PqCommandResult.php +++ b/src/PqCommandResult.php @@ -21,7 +21,7 @@ public function __construct(pq\Result $result) /** * @return int Number of rows affected by the INSERT, UPDATE, or DELETE query. */ - public function affectedRows(): int + public function getAffectedRowCount(): int { return $this->result->affectedRows; } diff --git a/src/PqHandle.php b/src/PqHandle.php index 73f4cee..121eaa8 100644 --- a/src/PqHandle.php +++ b/src/PqHandle.php @@ -150,7 +150,7 @@ public function isAlive(): bool /** * {@inheritdoc} */ - public function lastUsedAt(): int + public function getLastUsedAt(): int { return $this->lastUsedAt; } diff --git a/src/PqStatement.php b/src/PqStatement.php index 82c410c..d7fd012 100644 --- a/src/PqStatement.php +++ b/src/PqStatement.php @@ -55,7 +55,7 @@ public function getQuery(): string } /** {@inheritdoc} */ - public function lastUsedAt(): int + public function getLastUsedAt(): int { return $this->lastUsedAt; } diff --git a/src/PqUnbufferedResultSet.php b/src/PqUnbufferedResultSet.php index d6e0fa3..0f1c9cc 100644 --- a/src/PqUnbufferedResultSet.php +++ b/src/PqUnbufferedResultSet.php @@ -17,9 +17,6 @@ final class PqUnbufferedResultSet implements ResultSet /** @var array|object Last row emitted. */ private $currentRow; - /** @var int Next row fetch type. */ - private $type = self::FETCH_ASSOC; - /** * @param callable(): $fetch Function to fetch next result row. * @param \pq\Result $result PostgreSQL result object. @@ -44,10 +41,9 @@ public function __construct(callable $fetch, pq\Result $result, callable $releas /** * {@inheritdoc} */ - public function advance(int $type = self::FETCH_ASSOC): Promise + public function advance(): Promise { $this->currentRow = null; - $this->type = $type; return $this->producer->advance(); } @@ -55,7 +51,7 @@ public function advance(int $type = self::FETCH_ASSOC): Promise /** * {@inheritdoc} */ - public function getCurrent() + public function getCurrent(int $type = self::FETCH_ASSOC) { if ($this->currentRow !== null) { return $this->currentRow; @@ -64,7 +60,7 @@ public function getCurrent() /** @var \pq\Result $result */ $result = $this->producer->getCurrent(); - switch ($this->type) { + switch ($type) { case self::FETCH_ASSOC: return $this->currentRow = $result->fetchRow(pq\Result::FETCH_ASSOC); case self::FETCH_ARRAY: @@ -79,7 +75,7 @@ public function getCurrent() /** * @return int Number of fields (columns) in each result set. */ - public function numFields(): int + public function getFieldCount(): int { return $this->numCols; } diff --git a/src/ResultSet.php b/src/ResultSet.php index a7ac24c..3fc03c5 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -11,5 +11,5 @@ interface ResultSet extends SqlResultSet * * @return int */ - public function numFields(): int; + public function getFieldCount(): int; } diff --git a/test/AbstractLinkTest.php b/test/AbstractLinkTest.php index a89361c..2939091 100644 --- a/test/AbstractLinkTest.php +++ b/test/AbstractLinkTest.php @@ -56,7 +56,7 @@ public function testQueryWithTupleResult() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); $data = $this->getData(); @@ -83,8 +83,8 @@ public function testQueryWithUnconsumedTupleResult() $data = $this->getData(); - for ($i = 0; yield $result->advance(SqlResultSet::FETCH_OBJECT); ++$i) { - $row = $result->getCurrent(); + for ($i = 0; yield $result->advance(); ++$i) { + $row = $result->getCurrent(SqlResultSet::FETCH_OBJECT); $this->assertSame($data[$i][0], $row->domain); $this->assertSame($data[$i][1], $row->tld); } @@ -98,7 +98,7 @@ public function testQueryWithCommandResult() $result = yield $this->connection->query("INSERT INTO test VALUES ('canon', 'jp')"); $this->assertInstanceOf(CommandResult::class, $result); - $this->assertSame(1, $result->affectedRows()); + $this->assertSame(1, $result->getAffectedRowCount()); }); } @@ -144,10 +144,10 @@ public function testPrepare() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); - while (yield $result->advance(SqlResultSet::FETCH_ARRAY)) { - $row = $result->getCurrent(); + while (yield $result->advance()) { + $row = $result->getCurrent(SqlResultSet::FETCH_ARRAY); $this->assertSame($data[0], $row[0]); $this->assertSame($data[1], $row[1]); } @@ -174,10 +174,10 @@ public function testPrepareWithNamedParams() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); - while (yield $result->advance(SqlResultSet::FETCH_ARRAY)) { - $row = $result->getCurrent(); + while (yield $result->advance()) { + $row = $result->getCurrent(SqlResultSet::FETCH_ARRAY); $this->assertSame($data[0], $row[0]); $this->assertSame($data[1], $row[1]); } @@ -204,10 +204,10 @@ public function testPrepareWithUnnamedParams() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); - while (yield $result->advance(SqlResultSet::FETCH_ARRAY)) { - $row = $result->getCurrent(); + while (yield $result->advance()) { + $row = $result->getCurrent(SqlResultSet::FETCH_ARRAY); $this->assertSame($data[0], $row[0]); $this->assertSame($data[1], $row[1]); } @@ -234,10 +234,10 @@ public function testPrepareWithNamedParamsWithDataAppearingAsNamedParam() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); - while (yield $result->advance(SqlResultSet::FETCH_ARRAY)) { - $row = $result->getCurrent(); + while (yield $result->advance()) { + $row = $result->getCurrent(SqlResultSet::FETCH_ARRAY); $this->assertSame($data[0], $row[0]); $this->assertSame($data[1], $row[1]); } @@ -285,7 +285,7 @@ public function testPrepareSameQuery() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); while (yield $result->advance()) { $row = $result->getCurrent(); @@ -322,7 +322,7 @@ public function testSimultaneousPrepareSameQuery() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); while (yield $result->advance()) { $row = $result->getCurrent(); @@ -337,7 +337,7 @@ public function testSimultaneousPrepareSameQuery() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); while (yield $result->advance()) { $row = $result->getCurrent(); @@ -365,8 +365,8 @@ public function testPrepareThenExecuteWithUnconsumedTupleResult() $data = $this->getData(); - for ($i = 0; yield $result->advance(SqlResultSet::FETCH_OBJECT); ++$i) { - $row = $result->getCurrent(); + for ($i = 0; yield $result->advance(); ++$i) { + $row = $result->getCurrent(SqlResultSet::FETCH_OBJECT); $this->assertSame($data[$i][0], $row->domain); $this->assertSame($data[$i][1], $row->tld); } @@ -383,7 +383,7 @@ public function testExecute() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); while (yield $result->advance()) { $row = $result->getCurrent(); @@ -409,7 +409,7 @@ public function testExecuteWithNamedParams() $this->assertInstanceOf(ResultSet::class, $result); - $this->assertSame(2, $result->numFields()); + $this->assertSame(2, $result->getFieldCount()); while (yield $result->advance()) { $row = $result->getCurrent(); @@ -581,7 +581,7 @@ public function testTransaction() $isolation = SqlTransaction::ISOLATION_COMMITTED; /** @var \Amp\Postgres\Transaction $transaction */ - $transaction = yield $this->connection->transaction($isolation); + $transaction = yield $this->connection->beginTransaction($isolation); $this->assertInstanceOf(Transaction::class, $transaction);