Skip to content

Commit

Permalink
Update for Sql prefixes in common libs
Browse files Browse the repository at this point in the history
Also added Postgres prefix to a couple classes which did not already have it.
  • Loading branch information
trowski committed Feb 25, 2024
1 parent da283ad commit 7229efc
Show file tree
Hide file tree
Showing 38 changed files with 218 additions and 212 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"php": ">=8.1",
"amphp/amp": "^3",
"amphp/pipeline": "^1",
"amphp/sql": "^2-beta.6",
"amphp/sql-common": "^2-beta.9"
"amphp/sql": "2.x-dev",
"amphp/sql-common": "2.x-dev"
},
"require-dev": {
"ext-pgsql": "*",
Expand Down
10 changes: 5 additions & 5 deletions examples/5-bytea.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require dirname(__DIR__) . '/vendor/autoload.php';

use Amp\Postgres\ByteA;
use Amp\Postgres\PostgresByteA;
use Amp\Postgres\PostgresConfig;
use Amp\Postgres\PostgresConnectionPool;

Expand All @@ -18,11 +18,11 @@

$statement = $transaction->prepare('INSERT INTO test VALUES (?)');

$statement->execute([new ByteA($a = random_bytes(10))]);
$statement->execute([new ByteA($b = random_bytes(10))]);
$statement->execute([new ByteA($c = random_bytes(10))]);
$statement->execute([new PostgresByteA($a = random_bytes(10))]);
$statement->execute([new PostgresByteA($b = random_bytes(10))]);
$statement->execute([new PostgresByteA($c = random_bytes(10))]);

$result = $transaction->execute('SELECT * FROM test WHERE value = :value', ['value' => new ByteA($a)]);
$result = $transaction->execute('SELECT * FROM test WHERE value = :value', ['value' => new PostgresByteA($a)]);

foreach ($result as $row) {
assert($row['value'] === $a);
Expand Down
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
</errorLevel>
</MissingClosureReturnType>

<RiskyTruthyFalsyComparison>
<errorLevel type="suppress">
<directory name="src"/>
</errorLevel>
</RiskyTruthyFalsyComparison>

<UnsupportedPropertyReferenceUsage>
<errorLevel type="suppress">
<directory name="src"/>
Expand Down
10 changes: 5 additions & 5 deletions src/Internal/AbstractHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Pipeline\Queue;
use Amp\Postgres\ByteA;
use Amp\Postgres\PostgresByteA;
use Amp\Postgres\PostgresConfig;
use Amp\Sql\ConnectionException;
use Amp\Sql\SqlConnectionException;
use Revolt\EventLoop;

/**
Expand Down Expand Up @@ -75,14 +75,14 @@ protected static function shutdown(
?\Throwable $exception = null,
): void {
if (!empty($listeners)) {
$exception ??= new ConnectionException("The connection was closed");
$exception ??= new SqlConnectionException("The connection was closed");
foreach ($listeners as $listener) {
$listener->error($exception);
}
$listeners = [];
}

$pendingOperation?->error($exception ?? new ConnectionException("The connection was closed"));
$pendingOperation?->error($exception ?? new SqlConnectionException("The connection was closed"));
$pendingOperation = null;

if (!$onClose->isComplete()) {
Expand All @@ -93,7 +93,7 @@ protected static function shutdown(
protected function escapeParams(array $params): array
{
return \array_map(fn (mixed $param) => match (true) {
$param instanceof ByteA => $this->escapeByteA($param->getData()),
$param instanceof PostgresByteA => $this->escapeByteA($param->getData()),
\is_array($param) => $this->escapeParams($param),
default => $param,
}, $params);
Expand Down
22 changes: 11 additions & 11 deletions src/Internal/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Postgres\ParseException;
use Amp\Postgres\PostgresParseException;

/**
* @internal
Expand All @@ -21,7 +21,7 @@ final class ArrayParser
*
* @return list<mixed> Parsed column data.
*
* @throws ParseException
* @throws PostgresParseException
*/
public static function parse(string $data, \Closure $cast, string $delimiter = ','): array
{
Expand All @@ -31,7 +31,7 @@ public static function parse(string $data, \Closure $cast, string $delimiter = '
$data = \iterator_to_array($parser, false);

if ($parser->getReturn() !== '') {
throw new ParseException("Data left in buffer after parsing");
throw new PostgresParseException("Data left in buffer after parsing");
}

return $data;
Expand All @@ -52,23 +52,23 @@ private function __construct(
/**
* Recursive generator parser yielding array values.
*
* @throws ParseException
* @throws PostgresParseException
*/
private function parser(): \Generator
{
if ($this->data === '') {
throw new ParseException("Unexpected end of data");
throw new PostgresParseException("Unexpected end of data");
}

if ($this->data[0] !== '{') {
throw new ParseException("Missing opening bracket");
throw new PostgresParseException("Missing opening bracket");
}

$this->data = \ltrim(\substr($this->data, 1));

do {
if ($this->data === '') {
throw new ParseException("Unexpected end of data");
throw new PostgresParseException("Unexpected end of data");
}

if ($this->data[0] === '}') { // Empty array
Expand Down Expand Up @@ -96,7 +96,7 @@ private function parser(): \Generator
}

if (!isset($this->data[$position])) {
throw new ParseException("Could not find matching quote in quoted value");
throw new PostgresParseException("Could not find matching quote in quoted value");
}

$yield = \stripslashes(\substr($this->data, 1, $position - 1));
Expand Down Expand Up @@ -129,20 +129,20 @@ private function parser(): \Generator
*
* @return string First non-whitespace character after given position.
*
* @throws ParseException
* @throws PostgresParseException
*/
private function trim(int $position): string
{
$this->data = \ltrim(\substr($this->data, $position));

if ($this->data === '') {
throw new ParseException("Unexpected end of data");
throw new PostgresParseException("Unexpected end of data");
}

$end = $this->data[0];

if ($end !== $this->delimiter && $end !== '}') {
throw new ParseException("Invalid delimiter");
throw new PostgresParseException("Invalid delimiter");
}

$this->data = \ltrim(\substr($this->data, 1));
Expand Down
30 changes: 15 additions & 15 deletions src/Internal/PgSqlHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use Amp\Postgres\PostgresConfig;
use Amp\Postgres\PostgresListener;
use Amp\Postgres\PostgresNotification;
use Amp\Postgres\PostgresQueryError;
use Amp\Postgres\PostgresResult;
use Amp\Postgres\PostgresStatement;
use Amp\Postgres\QueryExecutionError;
use Amp\Sql\ConnectionException;
use Amp\Sql\QueryError;
use Amp\Sql\SqlConnectionException;
use Amp\Sql\SqlException;
use Amp\Sql\SqlQueryError;
use Revolt\EventLoop;
use function Amp\async;

Expand Down Expand Up @@ -88,11 +88,11 @@ public function __construct(

try {
if (\pg_connection_status($handle) !== \PGSQL_CONNECTION_OK) {
throw new ConnectionException("The connection closed during the operation");
throw new SqlConnectionException("The connection closed during the operation");
}

if (!\pg_consume_input($handle)) {
throw new ConnectionException(\pg_last_error($handle));
throw new SqlConnectionException(\pg_last_error($handle));
}

while ($result = \pg_get_notify($handle, \PGSQL_ASSOC)) {
Expand Down Expand Up @@ -120,7 +120,7 @@ public function __construct(
if (empty($listeners)) {
EventLoop::unreference($watcher);
}
} catch (ConnectionException $exception) {
} catch (SqlConnectionException $exception) {
$handle = null; // Marks connection as dead.
EventLoop::disable($watcher);

Expand Down Expand Up @@ -152,9 +152,9 @@ public function __construct(
EventLoop::disable($watcher);

if ($flush === false) {
throw new ConnectionException(\pg_last_error($handle));
throw new SqlConnectionException(\pg_last_error($handle));
}
} catch (ConnectionException $exception) {
} catch (SqlConnectionException $exception) {
$handle = null; // Marks connection as dead.
EventLoop::disable($watcher);

Expand Down Expand Up @@ -193,7 +193,7 @@ private static function fetchTypes(\PgSql\Connection $handle): array
private static function getErrorHandler(): \Closure
{
return self::$errorHandler ??= static function (int $code, string $message): never {
throw new ConnectionException($message, $code);
throw new SqlConnectionException($message, $code);
};
}

Expand Down Expand Up @@ -227,7 +227,7 @@ private function send(\Closure $function, mixed ...$args): mixed
}

if ($this->handle === null) {
throw new ConnectionException("The connection to the database has been closed");
throw new SqlConnectionException("The connection to the database has been closed");
}

while ($result = \pg_get_result($this->handle)) {
Expand Down Expand Up @@ -255,7 +255,7 @@ private function send(\Closure $function, mixed ...$args): mixed
* @param string $sql Query SQL.
*
* @throws SqlException
* @throws QueryError
* @throws SqlQueryError
*/
private function createResult(\PgSql\Result $result, string $sql): PostgresResult
{
Expand All @@ -265,7 +265,7 @@ private function createResult(\PgSql\Result $result, string $sql): PostgresResul

switch (\pg_result_status($result)) {
case \PGSQL_EMPTY_QUERY:
throw new QueryError("Empty query string");
throw new SqlQueryError("Empty query string");

case \PGSQL_COMMAND_OK:
return new PostgresCommandResult(
Expand All @@ -290,7 +290,7 @@ private function createResult(\PgSql\Result $result, string $sql): PostgresResul
}
} finally {
\restore_error_handler();
throw new QueryExecutionError($message, $diagnostics, $sql);
throw new PostgresQueryError($message, $diagnostics, $sql);
}

case \PGSQL_BAD_RESPONSE:
Expand Down Expand Up @@ -428,7 +428,7 @@ public function prepare(string $sql): PostgresStatement
foreach (self::DIAGNOSTIC_CODES as $fieldCode => $description) {
$diagnostics[$description] = \pg_result_error_field($result, $fieldCode);
}
throw new QueryExecutionError(\pg_result_error($result), $diagnostics, $sql);
throw new PostgresQueryError(\pg_result_error($result), $diagnostics, $sql);

case \PGSQL_BAD_RESPONSE:
throw new SqlException(\pg_result_error($result));
Expand Down Expand Up @@ -465,7 +465,7 @@ public function notify(string $channel, string $payload = ""): PostgresResult
public function listen(string $channel): PostgresListener
{
if (isset($this->listeners[$channel])) {
throw new QueryError(\sprintf("Already listening on channel '%s'", $channel));
throw new SqlQueryError(\sprintf("Already listening on channel '%s'", $channel));
}

$this->listeners[$channel] = $source = new Queue();
Expand Down
4 changes: 2 additions & 2 deletions src/Internal/PgSqlResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Postgres\ParseException;
use Amp\Postgres\PostgresParseException;
use Amp\Postgres\PostgresResult;
use Amp\Sql\SqlException;

Expand Down Expand Up @@ -71,7 +71,7 @@ private function getIterator(): \Iterator
*
* @return list<mixed>|bool|int|float|string|null
*
* @throws ParseException
* @throws PostgresParseException
*/
private function cast(int $oid, ?string $value): array|bool|int|float|string|null
{
Expand Down
6 changes: 3 additions & 3 deletions src/Internal/PostgresCommandResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Amp\Postgres\Internal;

use Amp\Postgres\PostgresResult;
use Amp\Sql\Common\CommandResult;
use Amp\Sql\Common\SqlCommandResult;

/**
* @internal
* @psalm-import-type TFieldType from PostgresResult
* @extends CommandResult<TFieldType, PostgresResult>
* @extends SqlCommandResult<TFieldType, PostgresResult>
*/
final class PostgresCommandResult extends CommandResult implements PostgresResult
final class PostgresCommandResult extends SqlCommandResult implements PostgresResult
{
/**
* Changes return type to this library's Result type.
Expand Down
4 changes: 2 additions & 2 deletions src/Internal/PostgresConnectionStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Amp\DeferredFuture;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Postgres\PostgresResult;
use Amp\Postgres\PostgresStatement;
use Amp\Sql\Result;
use Amp\Sql\SqlException;

/** @internal */
Expand Down Expand Up @@ -67,7 +67,7 @@ public function getLastUsedAt(): int
return $this->lastUsedAt;
}

public function execute(array $params = []): Result
public function execute(array $params = []): PostgresResult
{
if ($this->isClosed()) {
throw new SqlException('The statement has been closed or the connection went away');
Expand Down
18 changes: 9 additions & 9 deletions src/Internal/PostgresConnectionTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
use Amp\Postgres\PostgresResult;
use Amp\Postgres\PostgresStatement;
use Amp\Postgres\PostgresTransaction;
use Amp\Sql\Common\ConnectionTransaction;
use Amp\Sql\Common\NestableTransactionExecutor;
use Amp\Sql\Transaction;
use Amp\Sql\TransactionIsolation;
use Amp\Sql\Common\SqlConnectionTransaction;
use Amp\Sql\Common\SqlNestableTransactionExecutor;
use Amp\Sql\SqlTransaction;
use Amp\Sql\SqlTransactionIsolation;

/**
* @internal
* @extends ConnectionTransaction<PostgresResult, PostgresStatement, PostgresTransaction, PostgresHandle>
* @extends SqlConnectionTransaction<PostgresResult, PostgresStatement, PostgresTransaction, PostgresHandle>
*/
final class PostgresConnectionTransaction extends ConnectionTransaction implements PostgresTransaction
final class PostgresConnectionTransaction extends SqlConnectionTransaction implements PostgresTransaction
{
use PostgresTransactionDelegate;

public function __construct(
private readonly PostgresHandle $handle,
\Closure $release,
TransactionIsolation $isolation
SqlTransactionIsolation $isolation
) {
parent::__construct($handle, $release, $isolation);
}

protected function createNestedTransaction(
Transaction $transaction,
NestableTransactionExecutor $executor,
SqlTransaction $transaction,
SqlNestableTransactionExecutor $executor,
string $identifier,
\Closure $release,
): PostgresTransaction {
Expand Down
Loading

0 comments on commit 7229efc

Please sign in to comment.