16
16
use Psr \Http \Client \RequestExceptionInterface ;
17
17
use RuntimeException ;
18
18
use stdClass ;
19
+ use Neo4j \QueryAPI \Objects \Bookmarks ;
19
20
20
- /**
21
- * @method parseChildren(mixed $children)
22
- */
23
21
class Neo4jQueryAPI
24
22
{
23
+
25
24
private Client $ client ;
26
25
27
26
public function __construct (Client $ client )
@@ -48,26 +47,26 @@ public static function login(string $address, string $username, string $password
48
47
* @throws Neo4jException
49
48
* @throws RequestExceptionInterface
50
49
*/
51
- public function run (string $ cypher , array $ parameters = [], string $ database = 'neo4j ' ): ResultSet
50
+ public function run (string $ cypher , array $ parameters = [], string $ database = 'neo4j ' , Bookmarks $ bookmark = null ): ResultSet
52
51
{
53
52
try {
54
- // Prepare the payload for the request
55
53
$ payload = [
56
54
'statement ' => $ cypher ,
57
55
'parameters ' => empty ($ parameters ) ? new stdClass () : $ parameters ,
58
- 'includeCounters ' => true
56
+ 'includeCounters ' => true ,
59
57
];
60
58
61
- // Execute the request to the Neo4j server
59
+ if ($ bookmark !== null ) {
60
+ $ payload ['bookmarks ' ] = $ bookmark ->getBookmarks ();
61
+ }
62
+
62
63
$ response = $ this ->client ->post ('/db/ ' . $ database . '/query/v2 ' , [
63
64
'json ' => $ payload ,
64
65
]);
65
66
66
- // Decode the response body
67
67
$ data = json_decode ($ response ->getBody ()->getContents (), true );
68
68
$ ogm = new OGM ();
69
69
70
- // Extract result rows
71
70
$ keys = $ data ['data ' ]['fields ' ];
72
71
$ values = $ data ['data ' ]['values ' ];
73
72
$ rows = array_map (function ($ resultRow ) use ($ ogm , $ keys ) {
@@ -79,31 +78,31 @@ public function run(string $cypher, array $parameters = [], string $database = '
79
78
return new ResultRow ($ data );
80
79
}, $ values );
81
80
82
-
83
81
if (isset ($ data ['profiledQueryPlan ' ])) {
84
82
$ profile = $ this ->createProfileData ($ data ['profiledQueryPlan ' ]);
85
83
}
86
84
85
+ $ resultCounters = new ResultCounters (
86
+ containsUpdates: $ data ['counters ' ]['containsUpdates ' ] ?? false ,
87
+ nodesCreated: $ data ['counters ' ]['nodesCreated ' ] ?? 0 ,
88
+ nodesDeleted: $ data ['counters ' ]['nodesDeleted ' ] ?? 0 ,
89
+ propertiesSet: $ data ['counters ' ]['propertiesSet ' ] ?? 0 ,
90
+ relationshipsCreated: $ data ['counters ' ]['relationshipsCreated ' ] ?? 0 ,
91
+ relationshipsDeleted: $ data ['counters ' ]['relationshipsDeleted ' ] ?? 0 ,
92
+ labelsAdded: $ data ['counters ' ]['labelsAdded ' ] ?? 0 ,
93
+ labelsRemoved: $ data ['counters ' ]['labelsRemoved ' ] ?? 0 ,
94
+ indexesAdded: $ data ['counters ' ]['indexesAdded ' ] ?? 0 ,
95
+ indexesRemoved: $ data ['counters ' ]['indexesRemoved ' ] ?? 0 ,
96
+ constraintsAdded: $ data ['counters ' ]['constraintsAdded ' ] ?? 0 ,
97
+ constraintsRemoved: $ data ['counters ' ]['constraintsRemoved ' ] ?? 0 ,
98
+ containsSystemUpdates: $ data ['counters ' ]['containsSystemUpdates ' ] ?? false ,
99
+ systemUpdates: $ data ['counters ' ]['systemUpdates ' ] ?? 0
100
+ );
87
101
88
- // Return a ResultSet containing rows, counters, and the profiled query plan
89
102
return new ResultSet (
90
103
$ rows ,
91
- new ResultCounters (
92
- containsUpdates: $ data ['counters ' ]['containsUpdates ' ],
93
- nodesCreated: $ data ['counters ' ]['nodesCreated ' ],
94
- nodesDeleted: $ data ['counters ' ]['nodesDeleted ' ],
95
- propertiesSet: $ data ['counters ' ]['propertiesSet ' ],
96
- relationshipsCreated: $ data ['counters ' ]['relationshipsCreated ' ],
97
- relationshipsDeleted: $ data ['counters ' ]['relationshipsDeleted ' ],
98
- labelsAdded: $ data ['counters ' ]['labelsAdded ' ],
99
- labelsRemoved: $ data ['counters ' ]['labelsRemoved ' ],
100
- indexesAdded: $ data ['counters ' ]['indexesAdded ' ],
101
- indexesRemoved: $ data ['counters ' ]['indexesRemoved ' ],
102
- constraintsAdded: $ data ['counters ' ]['constraintsAdded ' ],
103
- constraintsRemoved: $ data ['counters ' ]['constraintsRemoved ' ],
104
- containsSystemUpdates: $ data ['counters ' ]['containsSystemUpdates ' ],
105
- systemUpdates: $ data ['counters ' ]['systemUpdates ' ]
106
- ),
104
+ $ resultCounters ,
105
+ new Bookmarks ($ data ['bookmarks ' ] ?? []),
107
106
$ profile
108
107
);
109
108
} catch (RequestExceptionInterface $ e ) {
@@ -112,7 +111,7 @@ public function run(string $cypher, array $parameters = [], string $database = '
112
111
$ contents = $ response ->getBody ()->getContents ();
113
112
$ errorResponse = json_decode ($ contents , true );
114
113
115
- throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
114
+ throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
116
115
}
117
116
118
117
throw $ e ;
0 commit comments