@@ -55,59 +55,56 @@ public static function login(string $address, AuthenticateInterface $auth = null
55
55
*/
56
56
public function run (string $ cypher , array $ parameters = [], string $ database = 'neo4j ' , Bookmarks $ bookmark = null ): ResultSet
57
57
{
58
- $ payload = [
59
- 'statement ' => $ cypher ,
60
- 'parameters ' => empty ($ parameters ) ? new stdClass () : $ parameters ,
61
- 'includeCounters ' => true ,
62
- ];
63
-
64
- if ($ bookmark !== null ) {
65
- $ payload ['bookmarks ' ] = $ bookmark ->getBookmarks ();
66
- }
67
-
68
- $ request = new Request ('POST ' , '/db/ ' . $ database . '/query/v2 ' );
69
- $ request = $ this ->auth ->authenticate ($ request );
70
- $ request = $ request ->withHeader ('Content-Type ' , 'application/json ' );
71
- $ request = $ request ->withBody (Utils::streamFor (json_encode ($ payload )));
72
-
73
- // Send the request to Neo4j
74
- $ response = $ this ->client ->sendRequest ($ request );
75
-
76
- // Get the response content
77
- $ contents = $ response ->getBody ()->getContents ();
78
- $ data = json_decode ($ contents , true , flags: JSON_THROW_ON_ERROR );
79
-
80
- // Check for Neo4j errors in the response
81
- if (isset ($ data ['errors ' ]) && count ($ data ['errors ' ]) > 0 ) {
82
- $ error = $ data ['errors ' ][0 ];
83
- $ errorCode = $ error ['code ' ] ?? '' ;
84
- $ errorMessage = $ error ['message ' ] ?? '' ;
85
-
86
- // Handle specific error types
87
- if ($ errorCode === 'Neo.ClientError.Schema.EquivalentSchemaRuleAlreadyExists ' ) {
88
- // Provide error details as an array, not a string
89
- $ errorDetails = [
90
- 'code ' => $ errorCode ,
91
- 'message ' => $ errorMessage ,
92
- ];
93
- throw new Neo4jException ($ errorDetails ); // Pass error details as an array
58
+ try {
59
+ // Prepare the payload
60
+ $ payload = [
61
+ 'statement ' => $ cypher ,
62
+ 'parameters ' => empty ($ parameters ) ? new stdClass () : $ parameters ,
63
+ 'includeCounters ' => true ,
64
+ ];
65
+
66
+ // Include bookmarks if provided
67
+ if ($ bookmark !== null ) {
68
+ $ payload ['bookmarks ' ] = $ bookmark ->getBookmarks ();
94
69
}
95
70
96
- // You can handle other Neo4j-specific errors similarly
97
- if ($ errorCode ) {
98
- $ errorDetails = [
99
- 'code ' => $ errorCode ,
100
- 'message ' => $ errorMessage ,
101
- ];
102
- throw new Neo4jException ($ errorDetails ); // Pass error details as an array
103
- }
104
- }
71
+ // Create the HTTP request
72
+ $ request = new Request ('POST ' , '/db/ ' . $ database . '/query/v2 ' );
73
+ $ request = $ this ->auth ->authenticate ($ request );
74
+ $ request = $ request ->withHeader ('Content-Type ' , 'application/json ' );
75
+ $ request = $ request ->withBody (Utils::streamFor (json_encode ($ payload )));
105
76
106
- // If no error, return the result set
107
- return $ this ->parseResultSet ($ data );
108
- }
77
+ // Send the request and get the response
78
+ $ response = $ this ->client ->sendRequest ($ request );
79
+ $ contents = $ response ->getBody ()->getContents ();
80
+
81
+ // Parse the response data
82
+ $ data = json_decode ($ contents , true , flags: JSON_THROW_ON_ERROR );
109
83
84
+ // Check for errors in the response from Neo4j
85
+ if (isset ($ data ['errors ' ]) && count ($ data ['errors ' ]) > 0 ) {
86
+ // If errors exist in the response, throw a Neo4jException
87
+ $ error = $ data ['errors ' ][0 ];
88
+ throw new Neo4jException (
89
+ $ error , // Pass the entire error array instead of just the message
90
+ 0 ,
91
+ null ,
92
+ $ error
93
+
94
+ );
95
+ }
110
96
97
+ // Parse the result set and return it
98
+ return $ this ->parseResultSet ($ data );
99
+
100
+ } catch (RequestExceptionInterface $ e ) {
101
+ // Handle exceptions from the HTTP request
102
+ $ this ->handleException ($ e );
103
+ } catch (Neo4jException $ e ) {
104
+ // Catch Neo4j specific exceptions (if thrown)
105
+ throw $ e ; // Re-throw the exception
106
+ }
107
+ }
111
108
112
109
private function parseResultSet (array $ data ): ResultSet
113
110
{
@@ -169,46 +166,15 @@ private function handleException(RequestExceptionInterface $e): void
169
166
170
167
public function beginTransaction (string $ database = 'neo4j ' ): Transaction
171
168
{
172
- // Create the request to begin a transaction
173
- $ request = new Request ('POST ' , '/db/ ' . $ database . '/query/v2/tx ' );
169
+ $ response = $ this ->client ->sendRequest (new Request ('POST ' , '/db/neo4j/query/v2/tx ' ));
174
170
175
- // Authenticate the request by adding necessary headers (e.g., Authorization)
176
- $ request = $ this ->auth ->authenticate ($ request );
171
+ $ clusterAffinity = $ response ->getHeaderLine ('neo4j-cluster-affinity ' );
172
+ $ responseData = json_decode ($ response ->getBody (), true );
173
+ $ transactionId = $ responseData ['transaction ' ]['id ' ];
177
174
178
- try {
179
- // Send the request
180
- $ response = $ this ->client ->sendRequest ($ request );
181
-
182
- // Check for a successful response (status code 200 or 202)
183
- if ($ response ->getStatusCode () !== 200 && $ response ->getStatusCode () !== 202 ) {
184
- throw new \RuntimeException ('Failed to begin transaction: ' . $ response ->getReasonPhrase ());
185
- }
186
-
187
- // Extract the necessary information from the response
188
- $ clusterAffinity = $ response ->getHeaderLine ('neo4j-cluster-affinity ' );
189
- $ responseData = json_decode ($ response ->getBody (), true );
190
-
191
- // Ensure that the transaction ID exists and is not empty
192
- if (!isset ($ responseData ['transaction ' ]['id ' ]) || empty ($ responseData ['transaction ' ]['id ' ])) {
193
- throw new \Exception ('Transaction ID is missing or empty in the response. ' );
194
- }
195
-
196
- // Get the transaction ID
197
- $ transactionId = $ responseData ['transaction ' ]['id ' ];
198
-
199
- // Return the Transaction object with the necessary details
200
- return new Transaction ($ this ->client , $ clusterAffinity , $ transactionId );
201
-
202
- } catch (\Exception $ e ) {
203
- // Handle any exceptions (e.g., network, authentication issues, etc.)
204
- // Optionally log the error or rethrow a more specific exception
205
- throw new \RuntimeException ('Error initiating transaction: ' . $ e ->getMessage (), 0 , $ e );
206
- }
175
+ return new Transaction ($ this ->client , $ clusterAffinity , $ transactionId );
207
176
}
208
177
209
-
210
-
211
-
212
178
private function createProfileData (array $ data ): ProfiledQueryPlan
213
179
{
214
180
$ ogm = new OGM ();
@@ -263,4 +229,4 @@ private function createProfileData(array $data): ProfiledQueryPlan
263
229
264
230
return $ profiledQueryPlan ;
265
231
}
266
- }
232
+ }
0 commit comments