1313use Psr \Http \Client \RequestExceptionInterface ;
1414use RuntimeException ;
1515use stdClass ;
16+ use Neo4j \QueryAPI \Objects \Bookmarks ;
1617
1718class Neo4jQueryAPI
1819{
20+
1921 private Client $ client ;
2022
2123 public function __construct (Client $ client )
@@ -25,6 +27,10 @@ public function __construct(Client $client)
2527
2628 public static function login (string $ address , string $ username , string $ password ): self
2729 {
30+ $ username = 'neo4j ' ;
31+ $ password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 ' ;
32+ $ connectionUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2 ' ;
33+
2834
2935 $ client = new Client ([
3036 'base_uri ' => rtrim ($ address , '/ ' ),
@@ -43,22 +49,23 @@ public static function login(string $address, string $username, string $password
4349 * @throws Neo4jException
4450 * @throws RequestExceptionInterface
4551 */
46- public function run (string $ cypher , array $ parameters = [], string $ database = 'neo4j ' ): ResultSet
52+ public function run (string $ cypher , array $ parameters = [], string $ database = 'neo4j ' , Bookmarks $ bookmark = null ): ResultSet
4753 {
4854 try {
49- // Prepare the payload for the request
5055 $ payload = [
5156 'statement ' => $ cypher ,
5257 'parameters ' => empty ($ parameters ) ? new stdClass () : $ parameters ,
53- 'includeCounters ' => true
58+ 'includeCounters ' => true ,
5459 ];
5560
56- // Execute the request to the Neo4j server
61+ if ($ bookmark !== null ) {
62+ $ payload ['bookmarks ' ] = $ bookmark ->getBookmarks ();
63+ }
64+
5765 $ response = $ this ->client ->post ('/db/ ' . $ database . '/query/v2 ' , [
5866 'json ' => $ payload ,
5967 ]);
6068
61- // Decode the response body
6269 $ data = json_decode ($ response ->getBody ()->getContents (), true );
6370 $ ogm = new OGM ();
6471
@@ -73,35 +80,47 @@ public function run(string $cypher, array $parameters = [], string $database = '
7380 return new ResultRow ($ data );
7481 }, $ values );
7582
76- return new ResultSet ($ rows , new ResultCounters (
77- containsUpdates: $ data ['counters ' ]['containsUpdates ' ],
78- nodesCreated: $ data ['counters ' ]['nodesCreated ' ],
79- nodesDeleted: $ data ['counters ' ]['nodesDeleted ' ],
80- propertiesSet: $ data ['counters ' ]['propertiesSet ' ],
81- relationshipsCreated: $ data ['counters ' ]['relationshipsCreated ' ],
82- relationshipsDeleted: $ data ['counters ' ]['relationshipsDeleted ' ],
83- labelsAdded: $ data ['counters ' ]['labelsAdded ' ],
84- labelsRemoved: $ data ['counters ' ]['labelsRemoved ' ],
85- indexesAdded: $ data ['counters ' ]['indexesAdded ' ],
86- indexesRemoved: $ data ['counters ' ]['indexesRemoved ' ],
87- constraintsAdded: $ data ['counters ' ]['constraintsAdded ' ],
88- constraintsRemoved: $ data ['counters ' ]['constraintsRemoved ' ],
89- containsSystemUpdates: $ data ['counters ' ]['containsSystemUpdates ' ],
90- systemUpdates: $ data ['counters ' ]['systemUpdates ' ]
91- ));
92- } catch (RequestExceptionInterface $ e ) {
93- $ response = $ e ->getResponse ();
94- if ($ response !== null ) {
95- $ contents = $ response ->getBody ()->getContents ();
96- $ errorResponse = json_decode ($ contents , true );
97-
98- throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
99- }
83+ $ resultCounters = new ResultCounters (
84+ containsUpdates: $ data ['counters ' ]['containsUpdates ' ] ?? false ,
85+ nodesCreated: $ data ['counters ' ]['nodesCreated ' ] ?? 0 ,
86+ nodesDeleted: $ data ['counters ' ]['nodesDeleted ' ] ?? 0 ,
87+ propertiesSet: $ data ['counters ' ]['propertiesSet ' ] ?? 0 ,
88+ relationshipsCreated: $ data ['counters ' ]['relationshipsCreated ' ] ?? 0 ,
89+ relationshipsDeleted: $ data ['counters ' ]['relationshipsDeleted ' ] ?? 0 ,
90+ labelsAdded: $ data ['counters ' ]['labelsAdded ' ] ?? 0 ,
91+ labelsRemoved: $ data ['counters ' ]['labelsRemoved ' ] ?? 0 ,
92+ indexesAdded: $ data ['counters ' ]['indexesAdded ' ] ?? 0 ,
93+ indexesRemoved: $ data ['counters ' ]['indexesRemoved ' ] ?? 0 ,
94+ constraintsAdded: $ data ['counters ' ]['constraintsAdded ' ] ?? 0 ,
95+ constraintsRemoved: $ data ['counters ' ]['constraintsRemoved ' ] ?? 0 ,
96+ containsSystemUpdates: $ data ['counters ' ]['containsSystemUpdates ' ] ?? false ,
97+ systemUpdates: $ data ['counters ' ]['systemUpdates ' ] ?? 0
98+ );
99+
100+ $ resultSet = new ResultSet ($ rows , $ resultCounters , new Bookmarks ($ data ['bookmarks ' ] ?? []));
101+
102+
103+ return $ resultSet ;
104+
105+ } catch (RequestException $ e ) {
106+ {
107+ $ response = $ e ->getResponse ();
108+ if ($ response !== null ) {
109+ $ contents = $ response ->getBody ()->getContents ();
110+ $ errorResponse = json_decode ($ contents , true );
111+
112+ throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
113+ }
100114
101- throw $ e ;
115+ throw $ e ;
116+ }
117+ throw new RuntimeException ('Error executing query: ' . $ e ->getMessage (), 0 , $ e );
102118 }
103119 }
104120
121+
122+
123+
105124 public function beginTransaction (string $ database = 'neo4j ' ): Transaction
106125 {
107126 $ response = $ this ->client ->post ("/db/neo4j/query/v2/tx " );
0 commit comments