18
18
use Ds \Vector ;
19
19
use Exception ;
20
20
use Laudis \Neo4j \Common \TransactionHelper ;
21
+ use Laudis \Neo4j \Contracts \ConnectionInterface ;
21
22
use Laudis \Neo4j \Contracts \FormatterInterface ;
22
23
use Laudis \Neo4j \Contracts \UnmanagedTransactionInterface ;
23
24
use Laudis \Neo4j \Databags \Neo4jError ;
26
27
use Laudis \Neo4j \ParameterHelper ;
27
28
use Laudis \Neo4j \Types \CypherList ;
28
29
use Throwable ;
30
+ use function microtime ;
29
31
30
32
/**
31
33
* @template T
37
39
final class BoltUnmanagedTransaction implements UnmanagedTransactionInterface
38
40
{
39
41
private FormatterInterface $ formatter ;
40
- private Bolt $ bolt ;
42
+ /** @var ConnectionInterface<Bolt> */
43
+ private ConnectionInterface $ connection ;
41
44
private string $ database ;
42
45
private bool $ finished = false ;
43
46
44
47
/**
45
- * @param FormatterInterface<T> $formatter
48
+ * @param FormatterInterface<T> $formatter
49
+ * @param ConnectionInterface<Bolt> $connection
46
50
*/
47
- public function __construct (string $ database , FormatterInterface $ formatter , Bolt $ bolt )
51
+ public function __construct (string $ database , FormatterInterface $ formatter , ConnectionInterface $ connection )
48
52
{
49
53
$ this ->formatter = $ formatter ;
50
- $ this ->bolt = $ bolt ;
54
+ $ this ->connection = $ connection ;
51
55
$ this ->database = $ database ;
52
56
}
53
57
@@ -60,7 +64,7 @@ public function commit(iterable $statements = []): CypherList
60
64
}
61
65
62
66
try {
63
- $ this ->bolt ->commit ();
67
+ $ this ->getBolt () ->commit ();
64
68
$ this ->finished = true ;
65
69
} catch (Exception $ e ) {
66
70
$ code = TransactionHelper::extractCode ($ e );
@@ -77,7 +81,7 @@ public function rollback(): void
77
81
}
78
82
79
83
try {
80
- $ this ->bolt ->rollback ();
84
+ $ this ->connection -> getImplementation () ->rollback ();
81
85
$ this ->finished = true ;
82
86
} catch (Exception $ e ) {
83
87
$ code = TransactionHelper::extractCode ($ e ) ?? '' ;
@@ -112,20 +116,38 @@ public function runStatements(iterable $statements): CypherList
112
116
$ extra = ['db ' => $ this ->database ];
113
117
$ parameters = ParameterHelper::formatParameters ($ statement ->getParameters ());
114
118
try {
119
+ $ start = microtime (true );
115
120
/** @var BoltMeta $meta */
116
- $ meta = $ this ->bolt ->run ($ statement ->getText (), $ parameters ->toArray (), $ extra );
121
+ $ meta = $ this ->getBolt ()->run ($ statement ->getText (), $ parameters ->toArray (), $ extra );
122
+ $ run = microtime (true );
117
123
/** @var array<array> $results */
118
- $ results = $ this ->bolt ->pullAll ();
124
+ $ results = $ this ->getBolt ()->pullAll ();
125
+ $ end = microtime (true );
119
126
} catch (Throwable $ e ) {
120
127
if ($ e instanceof MessageException) {
121
128
$ code = TransactionHelper::extractCode ($ e ) ?? '' ;
122
129
throw new Neo4jException (new Vector ([new Neo4jError ($ code , $ e ->getMessage ())]), $ e );
123
130
}
124
131
throw $ e ;
125
132
}
126
- $ tbr ->push ($ this ->formatter ->formatBoltResult ($ meta , $ results , $ this ->bolt ));
133
+ $ tbr ->push ($ this ->formatter ->formatBoltResult (
134
+ $ meta ,
135
+ $ results ,
136
+ $ this ->connection ,
137
+ $ run - $ start ,
138
+ $ end - $ start ,
139
+ $ statement
140
+ ));
127
141
}
128
142
129
143
return new CypherList ($ tbr );
130
144
}
145
+
146
+ /**
147
+ * @return Bolt|mixed
148
+ */
149
+ private function getBolt ()
150
+ {
151
+ return $ this ->connection ->getImplementation ();
152
+ }
131
153
}
0 commit comments