Skip to content

Commit 7186750

Browse files
committed
added functionality for binding transactions to client
1 parent 8a83f4f commit 7186750

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

src/Basic/Client.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,19 @@ public function verifyConnectivity(?string $driver = null): bool
7777
{
7878
return $this->client->verifyConnectivity($driver);
7979
}
80+
81+
public function bindTransaction(?string $alias = null, ?TransactionConfiguration $config = null): void
82+
{
83+
$this->client->bindTransaction($alias, $config);
84+
}
85+
86+
public function commitBoundTransaction(?string $alias = null, int $depth = 1): void
87+
{
88+
$this->client->commitBoundTransaction($alias, $depth);
89+
}
90+
91+
public function rollbackBoundTransaction(?string $alias = null, int $depth = 1): void
92+
{
93+
$this->client->rollbackBoundTransaction($alias, $depth);
94+
}
8095
}

src/Client.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
*/
3434
final class Client implements ClientInterface
3535
{
36+
/**
37+
* @var array<string, list<UnmanagedTransactionInterface<ResultFormat>>>
38+
*/
39+
private array $boundTransactions = [];
40+
3641
/**
3742
* @psalm-mutation-free
3843
*
@@ -108,6 +113,42 @@ public function verifyConnectivity(?string $driver = null): bool
108113
return $this->driverSetups->verifyConnectivity($this->defaultSessionConfiguration, $driver);
109114
}
110115

116+
public function bindTransaction(?string $alias = null, ?TransactionConfiguration $config = null): void
117+
{
118+
$alias ??= $this->driverSetups->getDefaultAlias();
119+
120+
$this->boundTransactions[$alias] ??= [];
121+
$this->boundTransactions[$alias][] = $this->beginTransaction(null, $alias, $config);
122+
}
123+
124+
public function rollbackBoundTransaction(?string $alias = null, int $depth = 1): void
125+
{
126+
$this->popTransactions(static fn (UnmanagedTransactionInterface $tsx) => $tsx->rollback(), $alias, $depth);
127+
}
128+
129+
/**
130+
* @param callable(UnmanagedTransactionInterface<ResultFormat>): void $handler
131+
*/
132+
private function popTransactions(callable $handler, ?string $alias = null, int $depth = 1): void
133+
{
134+
$alias ??= $this->driverSetups->getDefaultAlias();
135+
136+
if (!array_key_exists($alias, $this->boundTransactions)) {
137+
return;
138+
}
139+
140+
while (count($this->boundTransactions[$alias]) !== 0 && $depth !== 0) {
141+
$tsx = array_pop($this->boundTransactions[$alias]);
142+
$handler($tsx);
143+
--$depth;
144+
}
145+
}
146+
147+
public function commitBoundTransaction(?string $alias = null, int $depth = 1): void
148+
{
149+
$this->popTransactions(static fn (UnmanagedTransactionInterface $tsx) => $tsx->commit(), $alias, $depth);
150+
}
151+
111152
private function getTsxConfig(?TransactionConfiguration $config): TransactionConfiguration
112153
{
113154
if ($config !== null) {

src/Common/DriverSetupManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ private function decideAlias(?string $alias): string
137137
return $alias ?? $this->default ?? array_key_first($this->driverSetups) ?? 'default';
138138
}
139139

140+
public function getDefaultAlias(): string
141+
{
142+
return $this->decideAlias(null);
143+
}
144+
140145
/**
141146
* @psalm-mutation-free
142147
*/

src/Contracts/ClientInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,19 @@ public function transaction(callable $tsxHandler, ?string $alias = null, ?Transa
109109
* Checks to see if the driver can make a valid connection to the configured neo4j server.
110110
*/
111111
public function verifyConnectivity(?string $driver = null): bool;
112+
113+
/**
114+
* Binds a transaction to the client, so it runs all subsequent queries on the latest transaction instead of a session or the previously bound transaction.
115+
*/
116+
public function bindTransaction(?string $alias = null, ?TransactionConfiguration $config = null): void;
117+
118+
/**
119+
* Release a transaction from the client by committing it, so it runs all subsequent queries on a session or the previously bound transaction instead of the latest transaction. You can control the amount of transactions to be released by the depth parameter, with -1 being all transactions.
120+
*/
121+
public function commitBoundTransaction(?string $alias = null, int $depth = 1): void;
122+
123+
/**
124+
* Release a transaction from the client by rolling it back, so it runs all subsequent queries on a session or the previously bound transaction instead of the latest transaction. You can control the amount of transactions to be released by the depth parameter, with -1 being all transactions.
125+
*/
126+
public function rollbackBoundTransaction(?string $alias = null, int $depth = 1): void;
112127
}

0 commit comments

Comments
 (0)