Skip to content

Commit aab9597

Browse files
committed
Nested transactions
1 parent 542e09a commit aab9597

5 files changed

+153
-64
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.x-dev",
26-
"amphp/sql-common": "2.x-dev"
25+
"amphp/sql": "^2",
26+
"amphp/sql-common": "^2"
2727
},
2828
"require-dev": {
2929
"ext-pgsql": "*",
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Postgres\Internal;
4+
5+
use Amp\Postgres\PostgresResult;
6+
use Amp\Postgres\PostgresStatement;
7+
use Amp\Postgres\PostgresTransaction;
8+
use Amp\Sql\Common\NestedTransaction;
9+
10+
/**
11+
* @internal
12+
* @extends NestedTransaction<PostgresResult, PostgresStatement, PostgresTransaction>
13+
*/
14+
final class PostgresNestedTransaction extends NestedTransaction implements PostgresTransaction
15+
{
16+
use PostgresTransactionDelegate;
17+
18+
protected function getTransaction(): PostgresTransaction
19+
{
20+
return $this->transaction;
21+
}
22+
}

src/Internal/PostgresPooledTransaction.php

+3-62
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,17 @@
66
use Amp\Postgres\PostgresStatement;
77
use Amp\Postgres\PostgresTransaction;
88
use Amp\Sql\Common\PooledTransaction;
9-
use Amp\Sql\Result;
10-
use Amp\Sql\Statement;
119

1210
/**
1311
* @internal
1412
* @extends PooledTransaction<PostgresResult, PostgresStatement, PostgresTransaction>
1513
*/
1614
final class PostgresPooledTransaction extends PooledTransaction implements PostgresTransaction
1715
{
18-
protected function createStatement(Statement $statement, \Closure $release): PostgresStatement
19-
{
20-
\assert($statement instanceof PostgresStatement);
21-
return new PostgresPooledStatement($statement, $release);
22-
}
23-
24-
protected function createResult(Result $result, \Closure $release): PostgresResult
25-
{
26-
\assert($result instanceof PostgresResult);
27-
return new PostgresPooledResult($result, $release);
28-
}
29-
30-
/**
31-
* @param \Closure():void $release
32-
*/
33-
public function __construct(private readonly PostgresTransaction $transaction, \Closure $release)
34-
{
35-
parent::__construct($transaction, $release);
36-
}
37-
38-
/**
39-
* Changes return type to this library's Result type.
40-
*/
41-
public function query(string $sql): PostgresResult
42-
{
43-
return parent::query($sql);
44-
}
45-
46-
/**
47-
* Changes return type to this library's Statement type.
48-
*/
49-
public function prepare(string $sql): PostgresStatement
50-
{
51-
return parent::prepare($sql);
52-
}
53-
54-
/**
55-
* Changes return type to this library's Result type.
56-
*/
57-
public function execute(string $sql, array $params = []): PostgresResult
58-
{
59-
return parent::execute($sql, $params);
60-
}
61-
62-
public function notify(string $channel, string $payload = ""): PostgresResult
63-
{
64-
return $this->transaction->notify($channel, $payload);
65-
}
66-
67-
public function quoteString(string $data): string
68-
{
69-
return $this->transaction->quoteString($data);
70-
}
71-
72-
public function quoteName(string $name): string
73-
{
74-
return $this->transaction->quoteName($name);
75-
}
16+
use PostgresTransactionDelegate;
7617

77-
public function escapeByteA(string $data): string
18+
protected function getTransaction(): PostgresTransaction
7819
{
79-
return $this->transaction->escapeByteA($data);
20+
return $this->transaction;
8021
}
8122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Postgres\Internal;
4+
5+
use Amp\Postgres\PostgresResult;
6+
use Amp\Postgres\PostgresStatement;
7+
use Amp\Postgres\PostgresTransaction;
8+
use Amp\Sql\Result;
9+
use Amp\Sql\Statement;
10+
11+
/** @internal */
12+
trait PostgresTransactionDelegate
13+
{
14+
abstract protected function getTransaction(): PostgresTransaction;
15+
16+
/**
17+
* @param \Closure():void $release
18+
*/
19+
protected function createStatement(Statement $statement, \Closure $release): PostgresStatement
20+
{
21+
\assert($statement instanceof PostgresStatement);
22+
return new PostgresPooledStatement($statement, $release);
23+
}
24+
25+
/**
26+
* @param \Closure():void $release
27+
*/
28+
protected function createResult(Result $result, \Closure $release): PostgresResult
29+
{
30+
\assert($result instanceof PostgresResult);
31+
return new PostgresPooledResult($result, $release);
32+
}
33+
34+
/**
35+
* Changes return type to this library's Result type.
36+
*/
37+
public function query(string $sql): PostgresResult
38+
{
39+
return parent::query($sql);
40+
}
41+
42+
/**
43+
* Changes return type to this library's Statement type.
44+
*/
45+
public function prepare(string $sql): PostgresStatement
46+
{
47+
return parent::prepare($sql);
48+
}
49+
50+
/**
51+
* Changes return type to this library's Result type.
52+
*/
53+
public function execute(string $sql, array $params = []): PostgresResult
54+
{
55+
return parent::execute($sql, $params);
56+
}
57+
58+
/**
59+
* @param non-empty-string $channel
60+
*/
61+
public function notify(string $channel, string $payload = ""): PostgresResult
62+
{
63+
return $this->getTransaction()->notify($channel, $payload);
64+
}
65+
66+
public function quoteString(string $data): string
67+
{
68+
return $this->getTransaction()->quoteString($data);
69+
}
70+
71+
public function quoteName(string $name): string
72+
{
73+
return $this->getTransaction()->quoteName($name);
74+
}
75+
76+
public function escapeByteA(string $data): string
77+
{
78+
return $this->transaction->escapeByteA($data);
79+
}
80+
}

src/PostgresNestableTransaction.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Postgres;
4+
5+
use Amp\Sql\Common\NestableTransaction;
6+
use Amp\Sql\Transaction;
7+
use Amp\Sql\TransactionIsolation;
8+
use Amp\Sql\TransactionIsolationLevel;
9+
10+
/**
11+
* @extends NestableTransaction<PostgresResult, PostgresStatement, PostgresTransaction>
12+
*/
13+
final class PostgresNestableTransaction extends NestableTransaction implements PostgresLink
14+
{
15+
protected function createNestedTransaction(
16+
Transaction $transaction,
17+
\Closure $release,
18+
string $identifier,
19+
): Transaction {
20+
return new Internal\PostgresNestedTransaction($transaction, $release, $identifier);
21+
}
22+
23+
/**
24+
* Changes return type to this library's Transaction type.
25+
*/
26+
public function beginTransaction(
27+
TransactionIsolation $isolation = TransactionIsolationLevel::Committed
28+
): PostgresTransaction {
29+
return parent::beginTransaction($isolation);
30+
}
31+
32+
public function quoteString(string $data): string
33+
{
34+
return $this->transaction->quoteString($data);
35+
}
36+
37+
public function quoteName(string $name): string
38+
{
39+
return $this->transaction->quoteName($name);
40+
}
41+
42+
public function escapeByteA(string $data): string
43+
{
44+
return $this->transaction->escapeByteA($data);
45+
}
46+
}

0 commit comments

Comments
 (0)