|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Laudis Neo4j package. |
| 7 | + * |
| 8 | + * (c) Laudis technologies <http://laudis.tech> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Laudis\Neo4j\Network\Bolt; |
| 15 | + |
| 16 | +use Bolt\Bolt; |
| 17 | +use Ds\Vector; |
| 18 | +use Laudis\Neo4j\Contracts\SessionInterface; |
| 19 | +use Laudis\Neo4j\Contracts\TransactionInterface; |
| 20 | +use Laudis\Neo4j\Formatter\BoltCypherFormatter; |
| 21 | + |
| 22 | +class CasualClusterSession implements SessionInterface |
| 23 | +{ |
| 24 | + private RoutingTable $routingTable; |
| 25 | + private array $parsedUrl; |
| 26 | + private Bolt $bolt; |
| 27 | + private BoltCypherFormatter $formatter; |
| 28 | + private BoltInjections $injections; |
| 29 | + |
| 30 | + public function __construct(RoutingTable $routingTable, array $parsedUrl, Bolt $bolt, BoltCypherFormatter $formatter, BoltInjections $injections) |
| 31 | + { |
| 32 | + $this->routingTable = $routingTable; |
| 33 | + $this->parsedUrl = $parsedUrl; |
| 34 | + $this->bolt = $bolt; |
| 35 | + $this->formatter = $formatter; |
| 36 | + $this->injections = $injections; |
| 37 | + } |
| 38 | + |
| 39 | + public function run(iterable $statements): Vector |
| 40 | + { |
| 41 | + return $this->preparedSession($statements)->run($statements); |
| 42 | + } |
| 43 | + |
| 44 | + public function runOverTransaction(TransactionInterface $transaction, iterable $statements): Vector |
| 45 | + { |
| 46 | + return $this->preparedSession($statements)->runOverTransaction($transaction, $statements); |
| 47 | + } |
| 48 | + |
| 49 | + public function rollbackTransaction(TransactionInterface $transaction): void |
| 50 | + { |
| 51 | + $this->preparedSession()->rollbackTransaction($transaction); |
| 52 | + } |
| 53 | + |
| 54 | + public function commitTransaction(TransactionInterface $transaction, iterable $statements): Vector |
| 55 | + { |
| 56 | + return $this->preparedSession($statements)->commitTransaction($transaction, $statements); |
| 57 | + } |
| 58 | + |
| 59 | + public function openTransaction(?iterable $statements = null): TransactionInterface |
| 60 | + { |
| 61 | + return $this->preparedSession($statements)->openTransaction($statements); |
| 62 | + } |
| 63 | + |
| 64 | + private function needsWriter(array $statements): bool |
| 65 | + { |
| 66 | + return count(preg_grep( |
| 67 | + '/(CREATE|SET|MERGE|DELETE)/m', |
| 68 | + array_map(function ($statement) { |
| 69 | + return $statement->getText(); |
| 70 | + }, $statements) |
| 71 | + )) > 0; |
| 72 | + } |
| 73 | + |
| 74 | + private function preparedSession(?iterable $statements = []) |
| 75 | + { |
| 76 | + if (!is_null($statements) && $this->needsWriter($this->coerce_statements($statements))) { |
| 77 | + $leaders = $this->routingTable->getLeaders(); |
| 78 | + |
| 79 | + $parsedUrl = $this->parsedUrl; |
| 80 | + $parsedUrl['host'] = $leaders->getHost(); |
| 81 | + return new BoltSession($parsedUrl, $this->bolt, $this->formatter, $this->injections); |
| 82 | + } |
| 83 | + |
| 84 | + return new BoltSession($this->parsedUrl, $this->bolt, $this->formatter, $this->injections); |
| 85 | + } |
| 86 | + |
| 87 | + private function coerce_statements(iterable $statements): array |
| 88 | + { |
| 89 | + if (is_array($statements)) return $statements; |
| 90 | + $s = []; |
| 91 | + array_push($s, ...$statements); |
| 92 | + return $s; |
| 93 | + } |
| 94 | +} |
0 commit comments