Skip to content

Commit be59ba0

Browse files
committed
Refactor transactions
Nested transactions now are supported directly on Transaction by moving beginTransaction() to Executor instead of Link. Added onCommit() and onRollback() event hooks to transactions.
1 parent 2cd168c commit be59ba0

3 files changed

Lines changed: 26 additions & 31 deletions

File tree

src/Executor.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/**
66
* @template TResult of Result
77
* @template TStatement of Statement
8+
* @template TTransaction of Transaction
89
*/
910
interface Executor extends TransientResource
1011
{
@@ -32,7 +33,7 @@ public function prepare(string $sql): Statement;
3233

3334
/**
3435
* @param string $sql SQL query to prepare and execute.
35-
* @param mixed[] $params Query parameters.
36+
* @param array<int, mixed>|array<string, mixed> $params Query parameters.
3637
*
3738
* @return TResult
3839
*
@@ -43,7 +44,9 @@ public function prepare(string $sql): Statement;
4344
public function execute(string $sql, array $params = []): Result;
4445

4546
/**
46-
* Closes the executor. No further queries may be performed.
47+
* Starts a transaction, returning an object where all queries are executed on a single connection.
48+
*
49+
* @return TTransaction
4750
*/
48-
public function close(): void;
51+
public function beginTransaction(): Transaction;
4952
}

src/Link.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77
* @template TStatement of Statement
88
* @template TTransaction of Transaction
99
*
10-
* @extends Executor<TResult, TStatement>
10+
* @extends Executor<TResult, TStatement, TTransaction>
1111
*/
1212
interface Link extends Executor
1313
{
1414
/**
15-
* Starts a transaction on a single connection.
15+
* Sets the transaction isolation level for transactions began on this link.
1616
*
17-
* @param TransactionIsolation $isolation Transaction isolation level.'
18-
*
19-
* @return TTransaction
17+
* @see Executor::beginTransaction()
2018
*/
21-
public function beginTransaction(
22-
TransactionIsolation $isolation = TransactionIsolationLevel::Committed,
23-
): Transaction;
19+
public function setTransactionIsolation(TransactionIsolation $isolation): void;
2420
}

src/Transaction.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/**
66
* @template TResult of Result
77
* @template TStatement of Statement
8-
* @extends Executor<TResult, TStatement>
8+
* @template TTransaction of Transaction
9+
* @extends Executor<TResult, TStatement, TTransaction>
910
*/
1011
interface Transaction extends Executor
1112
{
@@ -16,6 +17,11 @@ public function getIsolationLevel(): TransactionIsolation;
1617
*/
1718
public function isActive(): bool;
1819

20+
/**
21+
* @return bool True if this transaction was created inside another transaction using a savepoint.
22+
*/
23+
public function isNestedTransaction(): bool;
24+
1925
/**
2026
* Commits the transaction and makes it inactive.
2127
*
@@ -31,29 +37,19 @@ public function commit(): void;
3137
public function rollback(): void;
3238

3339
/**
34-
* Creates a savepoint with the given identifier.
35-
*
36-
* @param non-empty-string $identifier Savepoint identifier.
40+
* Attaches a callback which is invoked when the entire transaction is committed. If this transaction
41+
* is a nested transaction, the callback will not be invoked until the top-level transaction is committed.
3742
*
38-
* @throws TransactionError If the transaction has been committed or rolled back.
43+
* @param \Closure():void $onCommit
3944
*/
40-
public function createSavepoint(string $identifier): void;
45+
public function onCommit(\Closure $onCommit): void;
4146

4247
/**
43-
* Rolls back to the savepoint with the given identifier.
44-
*
45-
* @param non-empty-string $identifier Savepoint identifier.
48+
* Attaches a callback which is invoked when the transaction is rolled back. If in a nested transaction, the
49+
* callbacks may be invoked when rolling back to a savepoint or if the entire transaction is rolled back,
50+
* regardless of if the savepoint was released prior.
4651
*
47-
* @throws TransactionError If the transaction has been committed or rolled back.
48-
*/
49-
public function rollbackTo(string $identifier): void;
50-
51-
/**
52-
* Releases the savepoint with the given identifier.
53-
*
54-
* @param non-empty-string $identifier Savepoint identifier.
55-
*
56-
* @throws TransactionError If the transaction has been committed or rolled back.
52+
* @param \Closure():void $onRollback
5753
*/
58-
public function releaseSavepoint(string $identifier): void;
54+
public function onRollback(\Closure $onRollback): void;
5955
}

0 commit comments

Comments
 (0)