Skip to content

Commit 089d933

Browse files
committed
added run, commit, rollback and pull to connection
1 parent 6e95c74 commit 089d933

File tree

4 files changed

+90
-45
lines changed

4 files changed

+90
-45
lines changed

src/Bolt/BoltResult.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Laudis\Neo4j\Bolt;
1515

16-
use function array_key_exists;
1716
use function array_splice;
1817
use Bolt\protocol\V4;
1918
use function call_user_func;
@@ -109,24 +108,9 @@ public function consume(): array
109108
return $this->meta ?? [];
110109
}
111110

112-
/**
113-
* @return non-empty-list<list>
114-
*/
115-
private function pull(): array
116-
{
117-
$protocol = $this->connection->getImplementation();
118-
if (!$protocol instanceof V4) {
119-
/** @var non-empty-list<list> */
120-
return $protocol->pullAll(['qid' => $this->qid]);
121-
}
122-
123-
/** @var non-empty-list<list> */
124-
return $protocol->pull(['n' => $this->fetchSize, 'qid' => $this->qid]);
125-
}
126-
127111
private function fetchResults(): void
128112
{
129-
$meta = $this->pull();
113+
$meta = $this->connection->pull($this->qid, $this->fetchSize);
130114

131115
/** @var list<list> $rows */
132116
$rows = array_splice($meta, 0, count($meta) - 1);

src/Bolt/BoltUnmanagedTransaction.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class BoltUnmanagedTransaction implements UnmanagedTransactionInterface
5050
/** @psalm-readonly */
5151
private BoltConnection $connection;
5252
/** @psalm-readonly */
53-
private string $database;
53+
private ?string $database;
5454

5555
private bool $isRolledBack = false;
5656

@@ -61,7 +61,7 @@ final class BoltUnmanagedTransaction implements UnmanagedTransactionInterface
6161
/**
6262
* @param FormatterInterface<T> $formatter
6363
*/
64-
public function __construct(string $database, FormatterInterface $formatter, BoltConnection $connection, SessionConfiguration $config, TransactionConfiguration $tsxConfig)
64+
public function __construct(?string $database, FormatterInterface $formatter, BoltConnection $connection, SessionConfiguration $config, TransactionConfiguration $tsxConfig)
6565
{
6666
$this->formatter = $formatter;
6767
$this->connection = $connection;
@@ -82,7 +82,7 @@ public function commit(iterable $statements = []): CypherList
8282
});
8383

8484
try {
85-
$this->getBolt()->commit();
85+
$this->connection->commit();
8686
$this->isCommitted = true;
8787
} catch (MessageException $e) {
8888
$this->handleMessageException($e);
@@ -96,7 +96,7 @@ public function commit(iterable $statements = []): CypherList
9696
public function rollback(): void
9797
{
9898
try {
99-
$this->connection->getImplementation()->rollback();
99+
$this->connection->rollback();
100100
$this->isRolledBack = true;
101101
} catch (MessageException $e) {
102102
$this->handleMessageException($e);
@@ -118,13 +118,16 @@ public function run(string $statement, iterable $parameters = [])
118118
*/
119119
public function runStatement(Statement $statement)
120120
{
121-
$extra = ['db' => $this->database, 'tx_timeout' => (int) ($this->tsxConfig->getTimeout() * 1000)];
122121
$parameters = ParameterHelper::formatParameters($statement->getParameters(), true);
123122
$start = microtime(true);
124123

125124
try {
126-
/** @var BoltMeta $meta */
127-
$meta = $this->getBolt()->run($statement->getText(), $parameters->toArray(), $extra);
125+
$meta = $this->connection->run(
126+
$statement->getText(),
127+
$parameters->toArray(),
128+
$this->database,
129+
$this->tsxConfig->getTimeout()
130+
);
128131
$run = microtime(true);
129132
} catch (MessageException $e) {
130133
$this->handleMessageException($e);
@@ -156,14 +159,6 @@ public function runStatements(iterable $statements): CypherList
156159
return new CypherList($tbr);
157160
}
158161

159-
/**
160-
* @psalm-immutable
161-
*/
162-
private function getBolt(): V3
163-
{
164-
return $this->connection->getImplementation();
165-
}
166-
167162
/**
168163
* @throws Neo4jException
169164
*

src/Bolt/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private function startTransaction(TransactionConfiguration $config, SessionConfi
170170
try {
171171
$connection = $this->acquireConnection($config, $sessionConfig);
172172

173-
$connection->getImplementation()->begin(['db' => $this->config->getDatabase(), 'tx_timeout' => (int) ($config->getTimeout() * 1000)]);
173+
$connection->begin($this->config->getDatabase(), $config->getTimeout());
174174
} catch (MessageException $e) {
175175
if (isset($connection)) {
176176
$connection->reset();

src/Common/BoltConnection.php

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Laudis\Neo4j\Common;
1515

1616
use Bolt\protocol\V3;
17+
use Bolt\protocol\V4;
1718
use Laudis\Neo4j\BoltFactory;
1819
use Laudis\Neo4j\Contracts\ConnectionInterface;
1920
use Laudis\Neo4j\Databags\DatabaseInfo;
@@ -25,6 +26,8 @@
2526

2627
/**
2728
* @implements ConnectionInterface<V3>
29+
*
30+
* @psalm-import-type BoltMeta from \Laudis\Neo4j\Contracts\FormatterInterface
2831
*/
2932
final class BoltConnection implements ConnectionInterface
3033
{
@@ -144,9 +147,7 @@ public function isOpen(): bool
144147

145148
public function open(): void
146149
{
147-
if ($this->boltProtocol === null) {
148-
$this->boltProtocol = $this->factory->build()[0];
149-
}
150+
$this->boltProtocol = $this->factory->build()[0];
150151
}
151152

152153
public function setTimeout(float $timeout): void
@@ -156,15 +157,15 @@ public function setTimeout(float $timeout): void
156157

157158
public function close(): void
158159
{
159-
if ($this->ownerCount === 0 && $this->boltProtocol !== null) {
160+
if ($this->ownerCount === 0) {
160161
$this->boltProtocol = null;
161162
$this->hasBeenReset = false;
162163
}
163164
}
164165

165166
public function reset(): void
166167
{
167-
if ($this->boltProtocol !== null) {
168+
if ($this->boltProtocol) {
168169
$this->boltProtocol->reset();
169170
$this->boltProtocol = $this->factory->build()[0];
170171
if ($this->ownerCount > 0) {
@@ -179,17 +180,69 @@ public function reset(): void
179180
*/
180181
public function begin(?string $database, ?float $timeout): void
181182
{
182-
$params = [];
183-
if ($database) {
184-
$params['db'] = $database;
183+
if ($this->boltProtocol === null) {
184+
throw new RuntimeException('Cannot begin on a closed connection');
185185
}
186-
if ($timeout) {
187-
$params['tx_timeout'] = $timeout * 1000;
186+
187+
$this->boltProtocol->begin($this->buildExtra($database, $timeout));
188+
}
189+
190+
/**
191+
* @return BoltMeta
192+
*/
193+
public function run(string $text, array $parameters, ?string $database, ?float $timeout): array
194+
{
195+
if ($this->boltProtocol === null) {
196+
throw new RuntimeException('Cannot run on a closed connection');
188197
}
189198

190-
if ($this->boltProtocol) {
191-
$this->boltProtocol->begin($params);
199+
/** @var BoltMeta */
200+
return $this->boltProtocol->run($text, $parameters, $this->buildExtra($database, $timeout));
201+
}
202+
203+
public function commit(): void
204+
{
205+
if ($this->boltProtocol === null) {
206+
throw new RuntimeException('Cannot commit on a closed connection');
192207
}
208+
209+
$this->boltProtocol->commit();
210+
}
211+
212+
public function rollback(): void
213+
{
214+
if ($this->boltProtocol === null) {
215+
throw new RuntimeException('Cannot commit on a closed connection');
216+
}
217+
218+
$this->boltProtocol->rollback();
219+
}
220+
221+
/**
222+
* @return non-empty-list<list>
223+
*/
224+
public function pull(?int $qid, ?int $fetchSize): array
225+
{
226+
if ($this->boltProtocol === null) {
227+
throw new RuntimeException('Cannot pull on a closed connection');
228+
}
229+
230+
$extra = [];
231+
if ($fetchSize) {
232+
$extra['n'] = $fetchSize;
233+
}
234+
235+
if ($qid) {
236+
$extra['qid'] = $qid;
237+
}
238+
239+
if (!$this->boltProtocol instanceof V4) {
240+
/** @var non-empty-list<list> */
241+
return $this->boltProtocol->pullAll($extra);
242+
}
243+
244+
/** @var non-empty-list<list> */
245+
return $this->boltProtocol->pull($extra);
193246
}
194247

195248
/**
@@ -215,4 +268,17 @@ public function decrementOwner(): void
215268
{
216269
--$this->ownerCount;
217270
}
271+
272+
private function buildExtra(?string $database, ?float $timeout): array
273+
{
274+
$extra = [];
275+
if ($database) {
276+
$extra['db'] = $database;
277+
}
278+
if ($timeout) {
279+
$extra['tx_timeout'] = $timeout * 1000;
280+
}
281+
282+
return $extra;
283+
}
218284
}

0 commit comments

Comments
 (0)