13
13
use Psr \Http \Client \RequestExceptionInterface ;
14
14
use RuntimeException ;
15
15
use stdClass ;
16
+ use Neo4j \QueryAPI \Objects \Bookmarks ;
16
17
17
18
class Neo4jQueryAPI
18
19
{
20
+
19
21
private Client $ client ;
20
22
21
23
public function __construct (Client $ client )
@@ -25,6 +27,10 @@ public function __construct(Client $client)
25
27
26
28
public static function login (string $ address , string $ username , string $ password ): self
27
29
{
30
+ $ username = 'neo4j ' ;
31
+ $ password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 ' ;
32
+ $ connectionUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2 ' ;
33
+
28
34
29
35
$ client = new Client ([
30
36
'base_uri ' => rtrim ($ address , '/ ' ),
@@ -43,22 +49,23 @@ public static function login(string $address, string $username, string $password
43
49
* @throws Neo4jException
44
50
* @throws RequestExceptionInterface
45
51
*/
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
47
53
{
48
54
try {
49
- // Prepare the payload for the request
50
55
$ payload = [
51
56
'statement ' => $ cypher ,
52
57
'parameters ' => empty ($ parameters ) ? new stdClass () : $ parameters ,
53
- 'includeCounters ' => true
58
+ 'includeCounters ' => true ,
54
59
];
55
60
56
- // Execute the request to the Neo4j server
61
+ if ($ bookmark !== null ) {
62
+ $ payload ['bookmarks ' ] = $ bookmark ->getBookmarks ();
63
+ }
64
+
57
65
$ response = $ this ->client ->post ('/db/ ' . $ database . '/query/v2 ' , [
58
66
'json ' => $ payload ,
59
67
]);
60
68
61
- // Decode the response body
62
69
$ data = json_decode ($ response ->getBody ()->getContents (), true );
63
70
$ ogm = new OGM ();
64
71
@@ -73,35 +80,47 @@ public function run(string $cypher, array $parameters = [], string $database = '
73
80
return new ResultRow ($ data );
74
81
}, $ values );
75
82
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
+ }
100
114
101
- throw $ e ;
115
+ throw $ e ;
116
+ }
117
+ throw new RuntimeException ('Error executing query: ' . $ e ->getMessage (), 0 , $ e );
102
118
}
103
119
}
104
120
121
+
122
+
123
+
105
124
public function beginTransaction (string $ database = 'neo4j ' ): Transaction
106
125
{
107
126
$ response = $ this ->client ->post ("/db/neo4j/query/v2/tx " );
0 commit comments