Skip to content

Commit 7229efc

Browse files
committed
Update for Sql prefixes in common libs
Also added Postgres prefix to a couple classes which did not already have it.
1 parent da283ad commit 7229efc

38 files changed

+218
-212
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"php": ">=8.1",
2323
"amphp/amp": "^3",
2424
"amphp/pipeline": "^1",
25-
"amphp/sql": "^2-beta.6",
26-
"amphp/sql-common": "^2-beta.9"
25+
"amphp/sql": "2.x-dev",
26+
"amphp/sql-common": "2.x-dev"
2727
},
2828
"require-dev": {
2929
"ext-pgsql": "*",

examples/5-bytea.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

6-
use Amp\Postgres\ByteA;
6+
use Amp\Postgres\PostgresByteA;
77
use Amp\Postgres\PostgresConfig;
88
use Amp\Postgres\PostgresConnectionPool;
99

@@ -18,11 +18,11 @@
1818

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

21-
$statement->execute([new ByteA($a = random_bytes(10))]);
22-
$statement->execute([new ByteA($b = random_bytes(10))]);
23-
$statement->execute([new ByteA($c = random_bytes(10))]);
21+
$statement->execute([new PostgresByteA($a = random_bytes(10))]);
22+
$statement->execute([new PostgresByteA($b = random_bytes(10))]);
23+
$statement->execute([new PostgresByteA($c = random_bytes(10))]);
2424

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

2727
foreach ($result as $row) {
2828
assert($row['value'] === $a);

psalm.xml

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
</errorLevel>
2828
</MissingClosureReturnType>
2929

30+
<RiskyTruthyFalsyComparison>
31+
<errorLevel type="suppress">
32+
<directory name="src"/>
33+
</errorLevel>
34+
</RiskyTruthyFalsyComparison>
35+
3036
<UnsupportedPropertyReferenceUsage>
3137
<errorLevel type="suppress">
3238
<directory name="src"/>

src/Internal/AbstractHandle.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Amp\ForbidCloning;
77
use Amp\ForbidSerialization;
88
use Amp\Pipeline\Queue;
9-
use Amp\Postgres\ByteA;
9+
use Amp\Postgres\PostgresByteA;
1010
use Amp\Postgres\PostgresConfig;
11-
use Amp\Sql\ConnectionException;
11+
use Amp\Sql\SqlConnectionException;
1212
use Revolt\EventLoop;
1313

1414
/**
@@ -75,14 +75,14 @@ protected static function shutdown(
7575
?\Throwable $exception = null,
7676
): void {
7777
if (!empty($listeners)) {
78-
$exception ??= new ConnectionException("The connection was closed");
78+
$exception ??= new SqlConnectionException("The connection was closed");
7979
foreach ($listeners as $listener) {
8080
$listener->error($exception);
8181
}
8282
$listeners = [];
8383
}
8484

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

8888
if (!$onClose->isComplete()) {
@@ -93,7 +93,7 @@ protected static function shutdown(
9393
protected function escapeParams(array $params): array
9494
{
9595
return \array_map(fn (mixed $param) => match (true) {
96-
$param instanceof ByteA => $this->escapeByteA($param->getData()),
96+
$param instanceof PostgresByteA => $this->escapeByteA($param->getData()),
9797
\is_array($param) => $this->escapeParams($param),
9898
default => $param,
9999
}, $params);

src/Internal/ArrayParser.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Amp\ForbidCloning;
66
use Amp\ForbidSerialization;
7-
use Amp\Postgres\ParseException;
7+
use Amp\Postgres\PostgresParseException;
88

99
/**
1010
* @internal
@@ -21,7 +21,7 @@ final class ArrayParser
2121
*
2222
* @return list<mixed> Parsed column data.
2323
*
24-
* @throws ParseException
24+
* @throws PostgresParseException
2525
*/
2626
public static function parse(string $data, \Closure $cast, string $delimiter = ','): array
2727
{
@@ -31,7 +31,7 @@ public static function parse(string $data, \Closure $cast, string $delimiter = '
3131
$data = \iterator_to_array($parser, false);
3232

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

3737
return $data;
@@ -52,23 +52,23 @@ private function __construct(
5252
/**
5353
* Recursive generator parser yielding array values.
5454
*
55-
* @throws ParseException
55+
* @throws PostgresParseException
5656
*/
5757
private function parser(): \Generator
5858
{
5959
if ($this->data === '') {
60-
throw new ParseException("Unexpected end of data");
60+
throw new PostgresParseException("Unexpected end of data");
6161
}
6262

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

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

6969
do {
7070
if ($this->data === '') {
71-
throw new ParseException("Unexpected end of data");
71+
throw new PostgresParseException("Unexpected end of data");
7272
}
7373

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

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

102102
$yield = \stripslashes(\substr($this->data, 1, $position - 1));
@@ -129,20 +129,20 @@ private function parser(): \Generator
129129
*
130130
* @return string First non-whitespace character after given position.
131131
*
132-
* @throws ParseException
132+
* @throws PostgresParseException
133133
*/
134134
private function trim(int $position): string
135135
{
136136
$this->data = \ltrim(\substr($this->data, $position));
137137

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

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

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

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

src/Internal/PgSqlHandle.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use Amp\Postgres\PostgresConfig;
99
use Amp\Postgres\PostgresListener;
1010
use Amp\Postgres\PostgresNotification;
11+
use Amp\Postgres\PostgresQueryError;
1112
use Amp\Postgres\PostgresResult;
1213
use Amp\Postgres\PostgresStatement;
13-
use Amp\Postgres\QueryExecutionError;
14-
use Amp\Sql\ConnectionException;
15-
use Amp\Sql\QueryError;
14+
use Amp\Sql\SqlConnectionException;
1615
use Amp\Sql\SqlException;
16+
use Amp\Sql\SqlQueryError;
1717
use Revolt\EventLoop;
1818
use function Amp\async;
1919

@@ -88,11 +88,11 @@ public function __construct(
8888

8989
try {
9090
if (\pg_connection_status($handle) !== \PGSQL_CONNECTION_OK) {
91-
throw new ConnectionException("The connection closed during the operation");
91+
throw new SqlConnectionException("The connection closed during the operation");
9292
}
9393

9494
if (!\pg_consume_input($handle)) {
95-
throw new ConnectionException(\pg_last_error($handle));
95+
throw new SqlConnectionException(\pg_last_error($handle));
9696
}
9797

9898
while ($result = \pg_get_notify($handle, \PGSQL_ASSOC)) {
@@ -120,7 +120,7 @@ public function __construct(
120120
if (empty($listeners)) {
121121
EventLoop::unreference($watcher);
122122
}
123-
} catch (ConnectionException $exception) {
123+
} catch (SqlConnectionException $exception) {
124124
$handle = null; // Marks connection as dead.
125125
EventLoop::disable($watcher);
126126

@@ -152,9 +152,9 @@ public function __construct(
152152
EventLoop::disable($watcher);
153153

154154
if ($flush === false) {
155-
throw new ConnectionException(\pg_last_error($handle));
155+
throw new SqlConnectionException(\pg_last_error($handle));
156156
}
157-
} catch (ConnectionException $exception) {
157+
} catch (SqlConnectionException $exception) {
158158
$handle = null; // Marks connection as dead.
159159
EventLoop::disable($watcher);
160160

@@ -193,7 +193,7 @@ private static function fetchTypes(\PgSql\Connection $handle): array
193193
private static function getErrorHandler(): \Closure
194194
{
195195
return self::$errorHandler ??= static function (int $code, string $message): never {
196-
throw new ConnectionException($message, $code);
196+
throw new SqlConnectionException($message, $code);
197197
};
198198
}
199199

@@ -227,7 +227,7 @@ private function send(\Closure $function, mixed ...$args): mixed
227227
}
228228

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

233233
while ($result = \pg_get_result($this->handle)) {
@@ -255,7 +255,7 @@ private function send(\Closure $function, mixed ...$args): mixed
255255
* @param string $sql Query SQL.
256256
*
257257
* @throws SqlException
258-
* @throws QueryError
258+
* @throws SqlQueryError
259259
*/
260260
private function createResult(\PgSql\Result $result, string $sql): PostgresResult
261261
{
@@ -265,7 +265,7 @@ private function createResult(\PgSql\Result $result, string $sql): PostgresResul
265265

266266
switch (\pg_result_status($result)) {
267267
case \PGSQL_EMPTY_QUERY:
268-
throw new QueryError("Empty query string");
268+
throw new SqlQueryError("Empty query string");
269269

270270
case \PGSQL_COMMAND_OK:
271271
return new PostgresCommandResult(
@@ -290,7 +290,7 @@ private function createResult(\PgSql\Result $result, string $sql): PostgresResul
290290
}
291291
} finally {
292292
\restore_error_handler();
293-
throw new QueryExecutionError($message, $diagnostics, $sql);
293+
throw new PostgresQueryError($message, $diagnostics, $sql);
294294
}
295295

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

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

471471
$this->listeners[$channel] = $source = new Queue();

src/Internal/PgSqlResultIterator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Amp\ForbidCloning;
66
use Amp\ForbidSerialization;
7-
use Amp\Postgres\ParseException;
7+
use Amp\Postgres\PostgresParseException;
88
use Amp\Postgres\PostgresResult;
99
use Amp\Sql\SqlException;
1010

@@ -71,7 +71,7 @@ private function getIterator(): \Iterator
7171
*
7272
* @return list<mixed>|bool|int|float|string|null
7373
*
74-
* @throws ParseException
74+
* @throws PostgresParseException
7575
*/
7676
private function cast(int $oid, ?string $value): array|bool|int|float|string|null
7777
{

src/Internal/PostgresCommandResult.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Amp\Postgres\Internal;
44

55
use Amp\Postgres\PostgresResult;
6-
use Amp\Sql\Common\CommandResult;
6+
use Amp\Sql\Common\SqlCommandResult;
77

88
/**
99
* @internal
1010
* @psalm-import-type TFieldType from PostgresResult
11-
* @extends CommandResult<TFieldType, PostgresResult>
11+
* @extends SqlCommandResult<TFieldType, PostgresResult>
1212
*/
13-
final class PostgresCommandResult extends CommandResult implements PostgresResult
13+
final class PostgresCommandResult extends SqlCommandResult implements PostgresResult
1414
{
1515
/**
1616
* Changes return type to this library's Result type.

src/Internal/PostgresConnectionStatement.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Amp\DeferredFuture;
66
use Amp\ForbidCloning;
77
use Amp\ForbidSerialization;
8+
use Amp\Postgres\PostgresResult;
89
use Amp\Postgres\PostgresStatement;
9-
use Amp\Sql\Result;
1010
use Amp\Sql\SqlException;
1111

1212
/** @internal */
@@ -67,7 +67,7 @@ public function getLastUsedAt(): int
6767
return $this->lastUsedAt;
6868
}
6969

70-
public function execute(array $params = []): Result
70+
public function execute(array $params = []): PostgresResult
7171
{
7272
if ($this->isClosed()) {
7373
throw new SqlException('The statement has been closed or the connection went away');

src/Internal/PostgresConnectionTransaction.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66
use Amp\Postgres\PostgresResult;
77
use Amp\Postgres\PostgresStatement;
88
use Amp\Postgres\PostgresTransaction;
9-
use Amp\Sql\Common\ConnectionTransaction;
10-
use Amp\Sql\Common\NestableTransactionExecutor;
11-
use Amp\Sql\Transaction;
12-
use Amp\Sql\TransactionIsolation;
9+
use Amp\Sql\Common\SqlConnectionTransaction;
10+
use Amp\Sql\Common\SqlNestableTransactionExecutor;
11+
use Amp\Sql\SqlTransaction;
12+
use Amp\Sql\SqlTransactionIsolation;
1313

1414
/**
1515
* @internal
16-
* @extends ConnectionTransaction<PostgresResult, PostgresStatement, PostgresTransaction, PostgresHandle>
16+
* @extends SqlConnectionTransaction<PostgresResult, PostgresStatement, PostgresTransaction, PostgresHandle>
1717
*/
18-
final class PostgresConnectionTransaction extends ConnectionTransaction implements PostgresTransaction
18+
final class PostgresConnectionTransaction extends SqlConnectionTransaction implements PostgresTransaction
1919
{
2020
use PostgresTransactionDelegate;
2121

2222
public function __construct(
2323
private readonly PostgresHandle $handle,
2424
\Closure $release,
25-
TransactionIsolation $isolation
25+
SqlTransactionIsolation $isolation
2626
) {
2727
parent::__construct($handle, $release, $isolation);
2828
}
2929

3030
protected function createNestedTransaction(
31-
Transaction $transaction,
32-
NestableTransactionExecutor $executor,
31+
SqlTransaction $transaction,
32+
SqlNestableTransactionExecutor $executor,
3333
string $identifier,
3434
\Closure $release,
3535
): PostgresTransaction {

0 commit comments

Comments
 (0)