Skip to content

Commit

Permalink
Add Sql prefix to all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Feb 25, 2024
1 parent 5d7dce7 commit 708ef4f
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 220 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions src/RetrySqlConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TConfig, TConnection>
*/
final class RetrySqlConnector implements SqlConnector
Expand All @@ -33,22 +33,22 @@ public function __construct(
}
}

public function connect(SqlConfig $config, ?Cancellation $cancellation = null): Connection
public function connect(SqlConfig $config, ?Cancellation $cancellation = null): SqlConnection
{
$tries = 0;
$exceptions = [];

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)
Expand Down
10 changes: 5 additions & 5 deletions src/CommandResult.php → src/SqlCommandResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TFieldValue>
* @template TResult of SqlResult
* @implements SqlResult<TFieldValue>
* @implements \IteratorAggregate<int, never>
*/
abstract class CommandResult implements Result, \IteratorAggregate
abstract class SqlCommandResult implements SqlResult, \IteratorAggregate
{
use ForbidCloning;
use ForbidSerialization;
Expand Down Expand Up @@ -43,7 +43,7 @@ final public function fetchRow(): ?array
/**
* @return TResult|null
*/
public function getNextResult(): ?Result
public function getNextResult(): ?SqlResult
{
return $this->nextResult->await();
}
Expand Down
68 changes: 34 additions & 34 deletions src/ConnectionPool.php → src/SqlCommonConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResult>
* @template TTransaction of Transaction
* @template TConnection of Connection<TConfig, TResult, TStatement, TTransaction>
* @template TResult of SqlResult
* @template TStatement of SqlStatement<TResult>
* @template TTransaction of SqlTransaction
* @template TConnection of SqlConnection<TConfig, TResult, TStatement, TTransaction>
*
* @implements Pool<TConfig, TResult, TStatement, TTransaction>
* @implements SqlConnectionPool<TConfig, TResult, TStatement, TTransaction>
*/
abstract class ConnectionPool implements Pool
abstract class SqlCommonConnectionPool implements SqlConnectionPool
{
use ForbidCloning;
use ForbidSerialization;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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');

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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(...));
Expand All @@ -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();

Expand All @@ -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();

Expand Down
Loading

0 comments on commit 708ef4f

Please sign in to comment.