diff --git a/composer.json b/composer.json index 5727a4a..5835d6f 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": ">=8.1", "amphp/amp": "^3", - "amphp/sql": "^2-beta.6" + "amphp/sql": "2.x-dev" }, "require-dev": { "amphp/php-cs-fixer-config": "^2", diff --git a/src/RetrySqlConnector.php b/src/RetrySqlConnector.php index c59fd04..8915bfa 100644 --- a/src/RetrySqlConnector.php +++ b/src/RetrySqlConnector.php @@ -6,14 +6,14 @@ use Amp\CompositeException; use Amp\ForbidCloning; use Amp\ForbidSerialization; -use Amp\Sql\Connection; -use Amp\Sql\ConnectionException; use Amp\Sql\SqlConfig; +use Amp\Sql\SqlConnection; +use Amp\Sql\SqlConnectionException; use Amp\Sql\SqlConnector; /** * @template TConfig of SqlConfig - * @template TConnection of Connection + * @template TConnection of SqlConnection * @implements SqlConnector */ final class RetrySqlConnector implements SqlConnector @@ -33,7 +33,7 @@ public function __construct( } } - public function connect(SqlConfig $config, ?Cancellation $cancellation = null): Connection + public function connect(SqlConfig $config, ?Cancellation $cancellation = null): SqlConnection { $tries = 0; $exceptions = []; @@ -41,14 +41,14 @@ public function connect(SqlConfig $config, ?Cancellation $cancellation = null): do { try { return $this->connector->connect($config, $cancellation); - } catch (ConnectionException $exception) { + } catch (SqlConnectionException $exception) { $exceptions[] = $exception; } } while (++$tries < $this->maxTries); $name = $config->getHost() . ':' . $config->getPort(); - throw new ConnectionException( + throw new SqlConnectionException( "Could not connect to database server at {$name} after {$tries} tries", 0, new CompositeException($exceptions) diff --git a/src/CommandResult.php b/src/SqlCommandResult.php similarity index 84% rename from src/CommandResult.php rename to src/SqlCommandResult.php index ae1a4a3..c55625a 100644 --- a/src/CommandResult.php +++ b/src/SqlCommandResult.php @@ -5,15 +5,15 @@ use Amp\ForbidCloning; use Amp\ForbidSerialization; use Amp\Future; -use Amp\Sql\Result; +use Amp\Sql\SqlResult; /** * @template TFieldValue - * @template TResult of Result - * @implements Result + * @template TResult of SqlResult + * @implements SqlResult * @implements \IteratorAggregate */ -abstract class CommandResult implements Result, \IteratorAggregate +abstract class SqlCommandResult implements SqlResult, \IteratorAggregate { use ForbidCloning; use ForbidSerialization; @@ -43,7 +43,7 @@ final public function fetchRow(): ?array /** * @return TResult|null */ - public function getNextResult(): ?Result + public function getNextResult(): ?SqlResult { return $this->nextResult->await(); } diff --git a/src/ConnectionPool.php b/src/SqlCommonConnectionPool.php similarity index 84% rename from src/ConnectionPool.php rename to src/SqlCommonConnectionPool.php index 0b0aa08..2bc77bf 100644 --- a/src/ConnectionPool.php +++ b/src/SqlCommonConnectionPool.php @@ -6,30 +6,30 @@ use Amp\ForbidCloning; use Amp\ForbidSerialization; use Amp\Future; -use Amp\Sql\Connection; -use Amp\Sql\Link; -use Amp\Sql\Pool; -use Amp\Sql\Result; use Amp\Sql\SqlConfig; +use Amp\Sql\SqlConnection; +use Amp\Sql\SqlConnectionPool; use Amp\Sql\SqlConnector; use Amp\Sql\SqlException; -use Amp\Sql\Statement; -use Amp\Sql\Transaction; -use Amp\Sql\TransactionIsolation; -use Amp\Sql\TransactionIsolationLevel; +use Amp\Sql\SqlLink; +use Amp\Sql\SqlResult; +use Amp\Sql\SqlStatement; +use Amp\Sql\SqlTransaction; +use Amp\Sql\SqlTransactionIsolation; +use Amp\Sql\SqlTransactionIsolationLevel; use Revolt\EventLoop; use function Amp\async; /** * @template TConfig of SqlConfig - * @template TResult of Result - * @template TStatement of Statement - * @template TTransaction of Transaction - * @template TConnection of Connection + * @template TResult of SqlResult + * @template TStatement of SqlStatement + * @template TTransaction of SqlTransaction + * @template TConnection of SqlConnection * - * @implements Pool + * @implements SqlConnectionPool */ -abstract class ConnectionPool implements Pool +abstract class SqlCommonConnectionPool implements SqlConnectionPool { use ForbidCloning; use ForbidSerialization; @@ -60,7 +60,7 @@ abstract class ConnectionPool implements Pool * * @return TStatement */ - abstract protected function createStatement(Statement $statement, \Closure $release): Statement; + abstract protected function createStatement(SqlStatement $statement, \Closure $release): SqlStatement; /** * Creates a Result of the appropriate type using the Result object returned by the Link object and the @@ -70,14 +70,14 @@ abstract protected function createStatement(Statement $statement, \Closure $rele * @param \Closure():void $release * @return TResult */ - abstract protected function createResult(Result $result, \Closure $release): Result; + abstract protected function createResult(SqlResult $result, \Closure $release): SqlResult; /** * @param \Closure(string):TStatement $prepare * * @return TStatement */ - abstract protected function createStatementPool(string $sql, \Closure $prepare): Statement; + abstract protected function createStatementPool(string $sql, \Closure $prepare): SqlStatement; /** * Creates a Transaction of the appropriate type using the Transaction object returned by the Link object and the @@ -88,7 +88,7 @@ abstract protected function createStatementPool(string $sql, \Closure $prepare): * * @return TTransaction */ - abstract protected function createTransaction(Transaction $transaction, \Closure $release): Transaction; + abstract protected function createTransaction(SqlTransaction $transaction, \Closure $release): SqlTransaction; /** * @param TConfig $config @@ -101,7 +101,7 @@ public function __construct( private readonly SqlConnector $connector, private readonly int $maxConnections = self::DEFAULT_MAX_CONNECTIONS, private int $idleTimeout = self::DEFAULT_IDLE_TIMEOUT, - private TransactionIsolation $transactionIsolation = TransactionIsolationLevel::Committed, + private SqlTransactionIsolation $transactionIsolation = SqlTransactionIsolationLevel::Committed, ) { /** @psalm-suppress TypeDoesNotContainType */ if ($this->idleTimeout < 1) { @@ -123,7 +123,7 @@ public function __construct( $now = \time(); while (!$idle->isEmpty()) { $connection = $idle->bottom(); - \assert($connection instanceof Link); + \assert($connection instanceof SqlLink); if ($connection->getLastUsedAt() + $idleTimeout > $now) { return; @@ -146,12 +146,12 @@ public function __destruct() $this->close(); } - public function getTransactionIsolation(): TransactionIsolation + public function getTransactionIsolation(): SqlTransactionIsolation { return $this->transactionIsolation; } - public function setTransactionIsolation(TransactionIsolation $isolation): void + public function setTransactionIsolation(SqlTransactionIsolation $isolation): void { $this->transactionIsolation = $isolation; } @@ -173,7 +173,7 @@ public function getLastUsedAt(): int $time = 0; foreach ($this->connections as $connection) { - \assert($connection instanceof Link); + \assert($connection instanceof SqlLink); if (($lastUsedAt = $connection->getLastUsedAt()) > $time) { $time = $lastUsedAt; } @@ -218,7 +218,7 @@ public function close(): void * * @throws SqlException */ - public function extractConnection(): Connection + public function extractConnection(): SqlConnection { $connection = $this->pop(); $this->connections->detach($connection); @@ -246,7 +246,7 @@ public function getConnectionLimit(): int * @throws SqlException If creating a new connection fails. * @throws \Error If the pool has been closed. */ - protected function pop(): Connection + protected function pop(): SqlConnection { if ($this->isClosed()) { throw new \Error("The pool has been closed"); @@ -266,11 +266,11 @@ protected function pop(): Connection $this->future = async(fn () => $this->connector->connect($this->config)) )->await(); /** @psalm-suppress DocblockTypeContradiction */ - if (!$connection instanceof Link) { + if (!$connection instanceof SqlLink) { throw new \Error(\sprintf( "%s::connect() must resolve to an instance of %s", \get_class($this->connector), - Link::class + SqlLink::class )); } } finally { @@ -298,7 +298,7 @@ protected function pop(): Connection } $connection = $this->idle->dequeue(); - \assert($connection instanceof Link); + \assert($connection instanceof SqlLink); if (!$connection->isClosed()) { return $connection; @@ -315,7 +315,7 @@ protected function pop(): Connection * * @throws \Error If the connection is not part of this pool. */ - protected function push(Connection $connection): void + protected function push(SqlConnection $connection): void { \assert(isset($this->connections[$connection]), 'Connection is not part of this pool'); @@ -329,7 +329,7 @@ protected function push(Connection $connection): void $this->awaitingConnection = null; } - public function query(string $sql): Result + public function query(string $sql): SqlResult { $connection = $this->pop(); @@ -343,7 +343,7 @@ public function query(string $sql): Result return $this->createResult($result, fn () => $this->push($connection)); } - public function execute(string $sql, array $params = []): Result + public function execute(string $sql, array $params = []): SqlResult { $connection = $this->pop(); @@ -360,7 +360,7 @@ public function execute(string $sql, array $params = []): Result /** * Prepared statements returned by this method will stay alive as long as the pool remains open. */ - public function prepare(string $sql): Statement + public function prepare(string $sql): SqlStatement { /** @psalm-suppress InvalidArgument Psalm is not properly detecting the templated return type. */ return $this->createStatementPool($sql, $this->prepareStatement(...)); @@ -373,7 +373,7 @@ public function prepare(string $sql): Statement * * @throws SqlException */ - private function prepareStatement(string $sql): Statement + private function prepareStatement(string $sql): SqlStatement { $connection = $this->pop(); @@ -387,7 +387,7 @@ private function prepareStatement(string $sql): Statement return $this->createStatement($statement, fn () => $this->push($connection)); } - public function beginTransaction(): Transaction + public function beginTransaction(): SqlTransaction { $connection = $this->pop(); diff --git a/src/ConnectionTransaction.php b/src/SqlConnectionTransaction.php similarity index 81% rename from src/ConnectionTransaction.php rename to src/SqlConnectionTransaction.php index 0badadb..5d274d0 100644 --- a/src/ConnectionTransaction.php +++ b/src/SqlConnectionTransaction.php @@ -5,23 +5,23 @@ use Amp\DeferredFuture; use Amp\ForbidCloning; use Amp\ForbidSerialization; -use Amp\Sql\Result; use Amp\Sql\SqlException; -use Amp\Sql\Statement; -use Amp\Sql\Transaction; -use Amp\Sql\TransactionError; -use Amp\Sql\TransactionIsolation; +use Amp\Sql\SqlResult; +use Amp\Sql\SqlStatement; +use Amp\Sql\SqlTransaction; +use Amp\Sql\SqlTransactionError; +use Amp\Sql\SqlTransactionIsolation; use Revolt\EventLoop; /** - * @template TResult of Result - * @template TStatement of Statement - * @template TTransaction of Transaction - * @template TNestedExecutor of NestableTransactionExecutor + * @template TResult of SqlResult + * @template TStatement of SqlStatement + * @template TTransaction of SqlTransaction + * @template TNestedExecutor of SqlNestableTransactionExecutor * - * @implements Transaction + * @implements SqlTransaction */ -abstract class ConnectionTransaction implements Transaction +abstract class SqlConnectionTransaction implements SqlTransaction { use ForbidCloning; use ForbidSerialization; @@ -48,7 +48,7 @@ abstract class ConnectionTransaction implements Transaction * * @return TResult */ - abstract protected function createResult(Result $result, \Closure $release): Result; + abstract protected function createResult(SqlResult $result, \Closure $release): SqlResult; /** * Creates a Statement of the appropriate type using the Statement object returned by the Transaction object and @@ -61,10 +61,10 @@ abstract protected function createResult(Result $result, \Closure $release): Res * @return TStatement */ abstract protected function createStatement( - Statement $statement, + SqlStatement $statement, \Closure $release, \Closure $awaitBusyResource, - ): Statement; + ): SqlStatement; /** * @param TTransaction $transaction @@ -75,20 +75,20 @@ abstract protected function createStatement( * @return TTransaction */ abstract protected function createNestedTransaction( - Transaction $transaction, - NestableTransactionExecutor $executor, + SqlTransaction $transaction, + SqlNestableTransactionExecutor $executor, string $identifier, \Closure $release, - ): Transaction; + ): SqlTransaction; /** * @param TNestedExecutor $executor * @param \Closure():void $release */ public function __construct( - private readonly NestableTransactionExecutor $executor, + private readonly SqlNestableTransactionExecutor $executor, \Closure $release, - private readonly TransactionIsolation $isolation, + private readonly SqlTransactionIsolation $isolation, ) { $busy = &$this->busy; $refCount = &$this->refCount; @@ -181,15 +181,15 @@ public function isActive(): bool return $this->active && !$this->executor->isClosed(); } - public function getIsolation(): TransactionIsolation + public function getIsolation(): SqlTransactionIsolation { return $this->isolation; } /** - * @throws TransactionError If the transaction has been committed or rolled back. + * @throws SqlTransactionError If the transaction has been committed or rolled back. */ - public function query(string $sql): Result + public function query(string $sql): SqlResult { $this->awaitPendingNestedTransaction(); @@ -205,11 +205,11 @@ public function query(string $sql): Result } /** - * @throws TransactionError If the transaction has been committed or rolled back. + * @throws SqlTransactionError If the transaction has been committed or rolled back. * * @psalm-suppress InvalidReturnStatement, InvalidReturnType */ - public function prepare(string $sql): Statement + public function prepare(string $sql): SqlStatement { $this->awaitPendingNestedTransaction(); @@ -230,9 +230,9 @@ public function prepare(string $sql): Statement } /** - * @throws TransactionError If the transaction has been committed or rolled back. + * @throws SqlTransactionError If the transaction has been committed or rolled back. */ - public function execute(string $sql, array $params = []): Result + public function execute(string $sql, array $params = []): SqlResult { $this->awaitPendingNestedTransaction(); @@ -247,7 +247,7 @@ public function execute(string $sql, array $params = []): Result return $this->createResult($result, $this->release); } - public function beginTransaction(): Transaction + public function beginTransaction(): SqlTransaction { $this->awaitPendingNestedTransaction(); @@ -268,7 +268,7 @@ public function beginTransaction(): Transaction /** * Commits the transaction and makes it inactive. * - * @throws TransactionError If the transaction has been committed or rolled back. + * @throws SqlTransactionError If the transaction has been committed or rolled back. */ public function commit(): void { @@ -286,7 +286,7 @@ public function commit(): void /** * Rolls back the transaction and makes it inactive. * - * @throws TransactionError If the transaction has been committed or rolled back. + * @throws SqlTransactionError If the transaction has been committed or rolled back. */ public function rollback(): void { @@ -318,7 +318,7 @@ private function awaitPendingNestedTransaction(): void } if ($this->isClosed()) { - throw new TransactionError("The transaction has been committed or rolled back"); + throw new SqlTransactionError("The transaction has been committed or rolled back"); } } } diff --git a/src/NestableTransactionExecutor.php b/src/SqlNestableTransactionExecutor.php similarity index 77% rename from src/NestableTransactionExecutor.php rename to src/SqlNestableTransactionExecutor.php index 5b25ea1..78fecd7 100644 --- a/src/NestableTransactionExecutor.php +++ b/src/SqlNestableTransactionExecutor.php @@ -2,17 +2,17 @@ namespace Amp\Sql\Common; -use Amp\Sql\Executor; -use Amp\Sql\Result; -use Amp\Sql\Statement; +use Amp\Sql\SqlExecutor; +use Amp\Sql\SqlResult; +use Amp\Sql\SqlStatement; /** - * @template TResult of Result - * @template TStatement of Statement + * @template TResult of SqlResult + * @template TStatement of SqlStatement * - * @extends Executor + * @extends SqlExecutor */ -interface NestableTransactionExecutor extends Executor +interface SqlNestableTransactionExecutor extends SqlExecutor { /** * Commits the current transaction. diff --git a/src/NestedTransaction.php b/src/SqlNestedTransaction.php similarity index 85% rename from src/NestedTransaction.php rename to src/SqlNestedTransaction.php index c498a14..772ec49 100644 --- a/src/NestedTransaction.php +++ b/src/SqlNestedTransaction.php @@ -5,23 +5,23 @@ use Amp\DeferredFuture; use Amp\ForbidCloning; use Amp\ForbidSerialization; -use Amp\Sql\Result; use Amp\Sql\SqlException; -use Amp\Sql\Statement; -use Amp\Sql\Transaction; -use Amp\Sql\TransactionError; -use Amp\Sql\TransactionIsolation; +use Amp\Sql\SqlResult; +use Amp\Sql\SqlStatement; +use Amp\Sql\SqlTransaction; +use Amp\Sql\SqlTransactionError; +use Amp\Sql\SqlTransactionIsolation; use Revolt\EventLoop; /** - * @template TResult of Result - * @template TStatement of Statement - * @template TTransaction of Transaction - * @template TNestedExecutor of NestableTransactionExecutor + * @template TResult of SqlResult + * @template TStatement of SqlStatement + * @template TTransaction of SqlTransaction + * @template TNestedExecutor of SqlNestableTransactionExecutor * - * @implements Transaction + * @implements SqlTransaction */ -abstract class NestedTransaction implements Transaction +abstract class SqlNestedTransaction implements SqlTransaction { use ForbidCloning; use ForbidSerialization; @@ -50,7 +50,7 @@ abstract class NestedTransaction implements Transaction * * @return TResult */ - abstract protected function createResult(Result $result, \Closure $release): Result; + abstract protected function createResult(SqlResult $result, \Closure $release): SqlResult; /** * @param TTransaction $transaction @@ -61,11 +61,11 @@ abstract protected function createResult(Result $result, \Closure $release): Res * @return TTransaction */ abstract protected function createNestedTransaction( - Transaction $transaction, - NestableTransactionExecutor $executor, + SqlTransaction $transaction, + SqlNestableTransactionExecutor $executor, string $identifier, \Closure $release, - ): Transaction; + ): SqlTransaction; /** * @param TTransaction $transaction Transaction object created by connection. @@ -74,8 +74,8 @@ abstract protected function createNestedTransaction( * @param \Closure():void $release Callable to be invoked when the transaction completes or is destroyed. */ public function __construct( - private readonly Transaction $transaction, - private readonly NestableTransactionExecutor $executor, + private readonly SqlTransaction $transaction, + private readonly SqlNestableTransactionExecutor $executor, private readonly string $identifier, \Closure $release, ) { @@ -145,7 +145,7 @@ public function __destruct() }); } - public function query(string $sql): Result + public function query(string $sql): SqlResult { $this->awaitPendingNestedTransaction(); ++$this->refCount; @@ -159,14 +159,14 @@ public function query(string $sql): Result } } - public function prepare(string $sql): Statement + public function prepare(string $sql): SqlStatement { $this->awaitPendingNestedTransaction(); return $this->executor->prepare($sql); } - public function execute(string $sql, array $params = []): Result + public function execute(string $sql, array $params = []): SqlResult { $this->awaitPendingNestedTransaction(); ++$this->refCount; @@ -180,7 +180,7 @@ public function execute(string $sql, array $params = []): Result } } - public function beginTransaction(): Transaction + public function beginTransaction(): SqlTransaction { $this->awaitPendingNestedTransaction(); ++$this->refCount; @@ -270,7 +270,7 @@ public function getSavepointIdentifier(): string return $this->identifier; } - public function getIsolation(): TransactionIsolation + public function getIsolation(): SqlTransactionIsolation { return $this->transaction->getIsolation(); } @@ -287,7 +287,7 @@ private function awaitPendingNestedTransaction(): void } if ($this->isClosed()) { - throw new TransactionError('The transaction has already been committed or rolled back'); + throw new SqlTransactionError('The transaction has already been committed or rolled back'); } } } diff --git a/src/PooledResult.php b/src/SqlPooledResult.php similarity index 83% rename from src/PooledResult.php rename to src/SqlPooledResult.php index 59cbb9e..0c14f82 100644 --- a/src/PooledResult.php +++ b/src/SqlPooledResult.php @@ -5,17 +5,17 @@ use Amp\ForbidCloning; use Amp\ForbidSerialization; use Amp\Future; -use Amp\Sql\Result; +use Amp\Sql\SqlResult; use Revolt\EventLoop; use function Amp\async; /** * @template TFieldValue - * @template TResult of Result - * @implements Result + * @template TResult of SqlResult + * @implements SqlResult * @implements \IteratorAggregate> */ -abstract class PooledResult implements Result, \IteratorAggregate +abstract class SqlPooledResult implements SqlResult, \IteratorAggregate { use ForbidCloning; use ForbidSerialization; @@ -27,22 +27,22 @@ abstract class PooledResult implements Result, \IteratorAggregate private readonly \Iterator $iterator; /** - * @template Tr of Result + * @template Tr of SqlResult * * @param Tr $result * @param \Closure():void $release * * @return Tr */ - abstract protected static function newInstanceFrom(Result $result, \Closure $release): Result; + abstract protected static function newInstanceFrom(SqlResult $result, \Closure $release): SqlResult; /** * @param TResult $result Result object created by pooled connection or statement. * @param \Closure():void $release Callable to be invoked when the result set is destroyed. */ - public function __construct(private readonly Result $result, private readonly \Closure $release) + public function __construct(private readonly SqlResult $result, private readonly \Closure $release) { - if ($this->result instanceof CommandResult) { + if ($this->result instanceof SqlCommandResult) { $this->iterator = $this->result->getIterator(); $this->next = self::fetchNextResult($this->result, $this->release); return; @@ -113,23 +113,23 @@ public function getColumnCount(): ?int /** * @return TResult|null */ - public function getNextResult(): ?Result + public function getNextResult(): ?SqlResult { $this->next ??= self::fetchNextResult($this->result, $this->release); return $this->next->await(); } /** - * @template Tr of Result + * @template Tr of SqlResult * * @param Tr $result * @param \Closure():void $release * * @return Future */ - private static function fetchNextResult(Result $result, \Closure $release): Future + private static function fetchNextResult(SqlResult $result, \Closure $release): Future { - return async(static function () use ($result, $release): ?Result { + return async(static function () use ($result, $release): ?SqlResult { /** @var Tr|null $result */ $result = $result->getNextResult(); diff --git a/src/PooledStatement.php b/src/SqlPooledStatement.php similarity index 85% rename from src/PooledStatement.php rename to src/SqlPooledStatement.php index cfde48c..b8053d2 100644 --- a/src/PooledStatement.php +++ b/src/SqlPooledStatement.php @@ -4,18 +4,18 @@ use Amp\ForbidCloning; use Amp\ForbidSerialization; -use Amp\Sql\Result; use Amp\Sql\SqlException; -use Amp\Sql\Statement; +use Amp\Sql\SqlResult; +use Amp\Sql\SqlStatement; use Revolt\EventLoop; /** - * @template TResult of Result - * @template TStatement of Statement + * @template TResult of SqlResult + * @template TStatement of SqlStatement * - * @implements Statement + * @implements SqlStatement */ -abstract class PooledStatement implements Statement +abstract class SqlPooledStatement implements SqlStatement { use ForbidCloning; use ForbidSerialization; @@ -34,7 +34,7 @@ abstract class PooledStatement implements Statement * * @return TResult */ - abstract protected function createResult(Result $result, \Closure $release): Result; + abstract protected function createResult(SqlResult $result, \Closure $release): SqlResult; /** * @param TStatement $statement Statement object created by pooled connection. @@ -44,7 +44,7 @@ abstract protected function createResult(Result $result, \Closure $release): Res * wait if the parent resource is busy with another action (e.g., a nested transaction). */ public function __construct( - private readonly Statement $statement, + private readonly SqlStatement $statement, \Closure $release, private readonly ?\Closure $awaitBusyResource = null, ) { @@ -64,7 +64,7 @@ public function __destruct() /** * @return TResult */ - public function execute(array $params = []): Result + public function execute(array $params = []): SqlResult { if (!$this->release) { throw new SqlException('The statement has been closed'); diff --git a/src/PooledTransaction.php b/src/SqlPooledTransaction.php similarity index 78% rename from src/PooledTransaction.php rename to src/SqlPooledTransaction.php index d2b28ec..a0ecdce 100644 --- a/src/PooledTransaction.php +++ b/src/SqlPooledTransaction.php @@ -4,20 +4,20 @@ use Amp\ForbidCloning; use Amp\ForbidSerialization; -use Amp\Sql\Result; -use Amp\Sql\Statement; -use Amp\Sql\Transaction; -use Amp\Sql\TransactionIsolation; +use Amp\Sql\SqlResult; +use Amp\Sql\SqlStatement; +use Amp\Sql\SqlTransaction; +use Amp\Sql\SqlTransactionIsolation; use Revolt\EventLoop; /** - * @template TResult of Result - * @template TStatement of Statement - * @template TTransaction of Transaction + * @template TResult of SqlResult + * @template TStatement of SqlStatement + * @template TTransaction of SqlTransaction * - * @implements Transaction + * @implements SqlTransaction */ -abstract class PooledTransaction implements Transaction +abstract class SqlPooledTransaction implements SqlTransaction { use ForbidCloning; use ForbidSerialization; @@ -36,7 +36,7 @@ abstract class PooledTransaction implements Transaction * * @return TStatement */ - abstract protected function createStatement(Statement $statement, \Closure $release): Statement; + abstract protected function createStatement(SqlStatement $statement, \Closure $release): SqlStatement; /** * Creates a Result of the appropriate type using the Result object returned by the Link object and the @@ -47,7 +47,7 @@ abstract protected function createStatement(Statement $statement, \Closure $rele * * @return TResult */ - abstract protected function createResult(Result $result, \Closure $release): Result; + abstract protected function createResult(SqlResult $result, \Closure $release): SqlResult; /** * @param TTransaction $transaction @@ -55,13 +55,13 @@ abstract protected function createResult(Result $result, \Closure $release): Res * * @return TTransaction */ - abstract protected function createTransaction(Transaction $transaction, \Closure $release): Transaction; + abstract protected function createTransaction(SqlTransaction $transaction, \Closure $release): SqlTransaction; /** * @param TTransaction $transaction Transaction object created by pooled connection. * @param \Closure():void $release Callable to be invoked when the transaction completes or is destroyed. */ - public function __construct(private readonly Transaction $transaction, \Closure $release) + public function __construct(private readonly SqlTransaction $transaction, \Closure $release) { $refCount = &$this->refCount; $this->release = static function () use (&$refCount, $release): void { @@ -77,7 +77,7 @@ public function __construct(private readonly Transaction $transaction, \Closure } } - public function query(string $sql): Result + public function query(string $sql): SqlResult { ++$this->refCount; @@ -90,7 +90,7 @@ public function query(string $sql): Result } } - public function prepare(string $sql): Statement + public function prepare(string $sql): SqlStatement { ++$this->refCount; @@ -103,7 +103,7 @@ public function prepare(string $sql): Statement } } - public function execute(string $sql, array $params = []): Result + public function execute(string $sql, array $params = []): SqlResult { ++$this->refCount; @@ -116,7 +116,7 @@ public function execute(string $sql, array $params = []): Result } } - public function beginTransaction(): Transaction + public function beginTransaction(): SqlTransaction { ++$this->refCount; @@ -177,7 +177,7 @@ public function getSavepointIdentifier(): ?string return $this->transaction->getSavepointIdentifier(); } - public function getIsolation(): TransactionIsolation + public function getIsolation(): SqlTransactionIsolation { return $this->transaction->getIsolation(); } diff --git a/src/StatementPool.php b/src/SqlStatementPool.php similarity index 80% rename from src/StatementPool.php rename to src/SqlStatementPool.php index 13fba19..25d5eb0 100644 --- a/src/StatementPool.php +++ b/src/SqlStatementPool.php @@ -5,27 +5,27 @@ use Amp\DeferredFuture; use Amp\ForbidCloning; use Amp\ForbidSerialization; -use Amp\Sql\Pool; -use Amp\Sql\Result; use Amp\Sql\SqlConfig; +use Amp\Sql\SqlConnectionPool; use Amp\Sql\SqlException; -use Amp\Sql\Statement; -use Amp\Sql\Transaction; +use Amp\Sql\SqlResult; +use Amp\Sql\SqlStatement; +use Amp\Sql\SqlTransaction; use Revolt\EventLoop; /** * @template TConfig of SqlConfig - * @template TResult of Result - * @template TStatement of Statement - * @template TTransaction of Transaction - * @implements Statement + * @template TResult of SqlResult + * @template TStatement of SqlStatement + * @template TTransaction of SqlTransaction + * @implements SqlStatement */ -abstract class StatementPool implements Statement +abstract class SqlStatementPool implements SqlStatement { use ForbidCloning; use ForbidSerialization; - private readonly Pool $pool; + private readonly SqlConnectionPool $pool; /** @var \SplQueue */ private readonly \SplQueue $statements; @@ -45,14 +45,14 @@ abstract class StatementPool implements Statement * * @return TResult */ - abstract protected function createResult(Result $result, \Closure $release): Result; + abstract protected function createResult(SqlResult $result, \Closure $release): SqlResult; /** - * @param Pool $pool Pool used to prepare statements for execution. + * @param SqlConnectionPool $pool Pool used to prepare statements for execution. * @param string $sql SQL statement to prepare * @param \Closure(string):TStatement $prepare Callable that returns a new prepared statement. */ - public function __construct(Pool $pool, string $sql, \Closure $prepare) + public function __construct(SqlConnectionPool $pool, string $sql, \Closure $prepare) { $this->lastUsedAt = \time(); $this->statements = $statements = new \SplQueue; @@ -67,7 +67,7 @@ public function __construct(Pool $pool, string $sql, \Closure $prepare) while (!$statements->isEmpty()) { $statement = $statements->bottom(); - \assert($statement instanceof Statement); + \assert($statement instanceof SqlStatement); if ($statement->getLastUsedAt() + $idleTimeout > $now) { return; @@ -93,7 +93,7 @@ public function __destruct() * * @return TResult */ - public function execute(array $params = []): Result + public function execute(array $params = []): SqlResult { if ($this->isClosed()) { throw new SqlException('The statement has been closed or the connection pool has been closed'); @@ -119,7 +119,7 @@ public function execute(array $params = []): Result * * @param TStatement $statement */ - protected function push(Statement $statement): void + protected function push(SqlStatement $statement): void { $maxConnections = $this->pool->getConnectionLimit(); @@ -137,11 +137,11 @@ protected function push(Statement $statement): void /** * @return TStatement */ - protected function pop(): Statement + protected function pop(): SqlStatement { while (!$this->statements->isEmpty()) { $statement = $this->statements->dequeue(); - \assert($statement instanceof Statement); + \assert($statement instanceof SqlStatement); if (!$statement->isClosed()) { return $statement; diff --git a/test/RetrySqlConnectorTest.php b/test/RetrySqlConnectorTest.php index 68920ae..0f26622 100644 --- a/test/RetrySqlConnectorTest.php +++ b/test/RetrySqlConnectorTest.php @@ -4,9 +4,9 @@ use Amp\PHPUnit\AsyncTestCase; use Amp\Sql\Common\RetrySqlConnector; -use Amp\Sql\Connection; -use Amp\Sql\ConnectionException; use Amp\Sql\SqlConfig; +use Amp\Sql\SqlConnection; +use Amp\Sql\SqlConnectionException; use Amp\Sql\SqlConnector; class RetrySqlConnectorTest extends AsyncTestCase @@ -16,7 +16,7 @@ public function testSuccessfulConnect() $connector = $this->createMock(SqlConnector::class); $connector->expects($this->once()) ->method('connect') - ->willReturn($this->createMock(Connection::class)); + ->willReturn($this->createMock(SqlConnection::class)); $retry = new RetrySqlConnector($connector); @@ -26,7 +26,7 @@ public function testSuccessfulConnect() $connection = $retry->connect($config); - $this->assertInstanceOf(Connection::class, $connection); + $this->assertInstanceOf(SqlConnection::class, $connection); } public function testFirstTryFailConnect() @@ -34,15 +34,15 @@ public function testFirstTryFailConnect() $connector = $this->createMock(SqlConnector::class); $connector->expects($this->exactly(2)) ->method('connect') - ->willReturnCallback(function (): Connection { + ->willReturnCallback(function (): SqlConnection { static $initial = true; if ($initial) { $initial = false; - throw new ConnectionException; + throw new SqlConnectionException; } - return $this->createMock(Connection::class); + return $this->createMock(SqlConnection::class); }); $retry = new RetrySqlConnector($connector); @@ -53,7 +53,7 @@ public function testFirstTryFailConnect() $connection = $retry->connect($config); - $this->assertInstanceOf(Connection::class, $connection); + $this->assertInstanceOf(SqlConnection::class, $connection); } public function testFailingConnect() @@ -63,7 +63,7 @@ public function testFailingConnect() $connector = $this->createMock(SqlConnector::class); $connector->expects($this->exactly($tries)) ->method('connect') - ->willThrowException(new ConnectionException); + ->willThrowException(new SqlConnectionException); $retry = new RetrySqlConnector($connector, $tries); @@ -71,7 +71,7 @@ public function testFailingConnect() ->setConstructorArgs(['localhost', 5432]) ->getMockForAbstractClass(); - $this->expectException(ConnectionException::class); + $this->expectException(SqlConnectionException::class); $this->expectExceptionMessage('Could not connect to database server'); $connection = $retry->connect($config); diff --git a/test/ConnectionPoolTest.php b/test/SqlCommonConnectionPoolTest.php similarity index 80% rename from test/ConnectionPoolTest.php rename to test/SqlCommonConnectionPoolTest.php index 394f5aa..6bcee9a 100644 --- a/test/ConnectionPoolTest.php +++ b/test/SqlCommonConnectionPoolTest.php @@ -4,23 +4,23 @@ use Amp\Future; use Amp\PHPUnit\AsyncTestCase; -use Amp\Sql\Common\ConnectionPool; -use Amp\Sql\Common\Test\Stub\StubPooledResult; -use Amp\Sql\Connection; -use Amp\Sql\Result; +use Amp\Sql\Common\SqlCommonConnectionPool; +use Amp\Sql\Common\Test\Stub\StubSqlPooledResult; use Amp\Sql\SqlConfig; +use Amp\Sql\SqlConnection; use Amp\Sql\SqlConnector; +use Amp\Sql\SqlResult; use function Amp\async; use function Amp\delay; -class ConnectionPoolTest extends AsyncTestCase +class SqlCommonConnectionPoolTest extends AsyncTestCase { public function testInvalidMaxConnections() { $this->expectException(\Error::class); $this->expectExceptionMessage('Pool must contain at least one connection'); - $this->getMockBuilder(ConnectionPool::class) + $this->getMockBuilder(SqlCommonConnectionPool::class) ->setConstructorArgs([ $this->createMock(SqlConfig::class), $this->createMock(SqlConnector::class), @@ -35,8 +35,8 @@ private function createConnector(): SqlConnector $connector = $this->createMock(SqlConnector::class); $connector->method('connect') - ->willReturnCallback(function () use ($now): Connection { - $connection = $this->createMock(Connection::class); + ->willReturnCallback(function () use ($now): SqlConnection { + $connection = $this->createMock(SqlConnection::class); $connection->method('getLastUsedAt') ->willReturn($now); @@ -46,7 +46,7 @@ private function createConnector(): SqlConnector $connection->method('query') ->willReturnCallback(function () { delay(0.1); - return $this->createMock(Result::class); + return $this->createMock(SqlResult::class); }); return $connection; @@ -55,9 +55,9 @@ private function createConnector(): SqlConnector return $connector; } - private function createPool(SqlConnector $connector, int $maxConnections = 100, int $idleTimeout = 10): ConnectionPool + private function createPool(SqlConnector $connector, int $maxConnections = 100, int $idleTimeout = 10): SqlCommonConnectionPool { - $pool = $this->getMockBuilder(ConnectionPool::class) + $pool = $this->getMockBuilder(SqlCommonConnectionPool::class) ->setConstructorArgs([ $this->createMock(SqlConfig::class), $connector, @@ -67,7 +67,7 @@ private function createPool(SqlConnector $connector, int $maxConnections = 100, ->getMockForAbstractClass(); $pool->method('createResult') - ->willReturnCallback(fn (Result $result, \Closure $release) => new StubPooledResult($result, $release)); + ->willReturnCallback(fn (SqlResult $result, \Closure $release) => new StubSqlPooledResult($result, $release)); return $pool; } diff --git a/test/PooledResultTest.php b/test/SqlPooledResultTest.php similarity index 75% rename from test/PooledResultTest.php rename to test/SqlPooledResultTest.php index d76cbcc..9145336 100644 --- a/test/PooledResultTest.php +++ b/test/SqlPooledResultTest.php @@ -3,11 +3,11 @@ namespace Amp\Sql\Common\Test; use Amp\PHPUnit\AsyncTestCase; -use Amp\Sql\Common\Test\Stub\StubPooledResult; -use Amp\Sql\Common\Test\Stub\StubResult; +use Amp\Sql\Common\Test\Stub\StubSqlPooledResult; +use Amp\Sql\Common\Test\Stub\StubSqlResult; use function Amp\delay; -class PooledResultTest extends AsyncTestCase +class SqlPooledResultTest extends AsyncTestCase { public function testIdleConnectionsRemovedAfterTimeout() { @@ -19,9 +19,9 @@ public function testIdleConnectionsRemovedAfterTimeout() $expectedRow = ['column' => 'value']; - $secondResult = new StubResult([$expectedRow]); - $firstResult = new StubResult([$expectedRow], $secondResult); - $pooledResult = new StubPooledResult(new StubResult([$expectedRow], $firstResult), $release); + $secondResult = new StubSqlResult([$expectedRow]); + $firstResult = new StubSqlResult([$expectedRow], $secondResult); + $pooledResult = new StubSqlPooledResult(new StubSqlResult([$expectedRow], $firstResult), $release); $iterator = $pooledResult->getIterator(); diff --git a/test/StatementPoolTest.php b/test/SqlStatementPoolTest.php similarity index 79% rename from test/StatementPoolTest.php rename to test/SqlStatementPoolTest.php index 090ba69..bf0df0c 100644 --- a/test/StatementPoolTest.php +++ b/test/SqlStatementPoolTest.php @@ -3,22 +3,22 @@ namespace Amp\Sql\Common\Test; use Amp\PHPUnit\AsyncTestCase; -use Amp\Sql\Common\StatementPool; -use Amp\Sql\Pool; -use Amp\Sql\Statement; +use Amp\Sql\Common\SqlStatementPool; +use Amp\Sql\SqlConnectionPool; +use Amp\Sql\SqlStatement; use function Amp\delay; -class StatementPoolTest extends AsyncTestCase +class SqlStatementPoolTest extends AsyncTestCase { public function testActiveStatementsRemainAfterTimeout() { - $pool = $this->createMock(Pool::class); + $pool = $this->createMock(SqlConnectionPool::class); $pool->method('isClosed') ->willReturn(false); $pool->method('getIdleTimeout') ->willReturn(60); - $statement = $this->createMock(Statement::class); + $statement = $this->createMock(SqlStatement::class); $statement->method('isClosed') ->willReturn(false); $statement->method('getQuery') @@ -28,7 +28,7 @@ public function testActiveStatementsRemainAfterTimeout() $statement->expects($this->once()) ->method('execute'); - $statementPool = $this->getMockBuilder(StatementPool::class) + $statementPool = $this->getMockBuilder(SqlStatementPool::class) ->setConstructorArgs([$pool, 'SELECT 1', $this->createCallback(1, fn () => $statement)]) ->getMockForAbstractClass(); @@ -45,14 +45,14 @@ public function testActiveStatementsRemainAfterTimeout() public function testIdleStatementsRemovedAfterTimeout() { - $pool = $this->createMock(Pool::class); + $pool = $this->createMock(SqlConnectionPool::class); $pool->method('isClosed') ->willReturn(false); $pool->method('getIdleTimeout') ->willReturn(1); - $createStatement = function (): Statement { - $statement = $this->createMock(Statement::class); + $createStatement = function (): SqlStatement { + $statement = $this->createMock(SqlStatement::class); $statement->method('isClosed') ->willReturn(false); $statement->method('getQuery') @@ -65,7 +65,7 @@ public function testIdleStatementsRemovedAfterTimeout() return $statement; }; - $statementPool = $this->getMockBuilder(StatementPool::class) + $statementPool = $this->getMockBuilder(SqlStatementPool::class) ->setConstructorArgs([$pool, 'SELECT 1', $this->createCallback(2, $createStatement)]) ->getMockForAbstractClass(); diff --git a/test/Stub/StubPooledResult.php b/test/Stub/StubPooledResult.php deleted file mode 100644 index 1a62100..0000000 --- a/test/Stub/StubPooledResult.php +++ /dev/null @@ -1,14 +0,0 @@ -rows = \array_values($rows); } @@ -25,7 +25,7 @@ public function fetchRow(): ?array return $this->rows[$this->current++] ?? null; } - public function getNextResult(): ?Result + public function getNextResult(): ?SqlResult { return $this->next; }