Skip to content

Commit 6736b1f

Browse files
committed
Transaction query run returned in Resultset format
1 parent e05524e commit 6736b1f

File tree

6 files changed

+55
-217
lines changed

6 files changed

+55
-217
lines changed

src/Transaction.php

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,75 @@
55
use GuzzleHttp\Client;
66
use Neo4j\QueryAPI\Exception\Neo4jException;
77
use Neo4j\QueryAPI\Results\ResultRow;
8+
use Neo4j\QueryAPI\Results\ResultSet;
89
use Psr\Http\Client\ClientInterface;
9-
use Psr\Http\Client\RequestExceptionInterface;
1010
use stdClass;
1111

1212
class Transaction
1313
{
14-
15-
public function __construct(private ClientInterface $client, private string $clusterAffinity, private string $transactionId)
16-
{
14+
public function __construct(
15+
private ClientInterface $client,
16+
private string $clusterAffinity,
17+
private string $transactionId
18+
) {
1719
}
1820

1921
/**
20-
* Create a node in Neo4j with a specified label and properties.
22+
* Execute a Cypher query within the transaction.
2123
*
2224
* @param string $query The Cypher query to be executed.
23-
* @param $parameters
24-
* @return array The response data from Neo4j.
25+
* @param array $parameters Parameters for the query.
26+
* @return ResultSet The result rows in ResultSet format.
27+
* @throws Neo4jException If the response structure is invalid.
2528
*/
26-
public function run(string $query, array $parameters): array
29+
public function run(string $query, array $parameters): ResultSet
2730
{
28-
// Execute the request to the Neo4j server
29-
$response = $this->client->post("/db/neo4j/query/v2/tx", [
30-
'headers' => [
31-
'neo4j-cluster-affinity' => $this->clusterAffinity,
32-
],
33-
'json' => [
31+
$response = $this->client->post("/db/neo4j/query/v2/tx/{$this->transactionId}", [
32+
'headers' => [
33+
'neo4j-cluster-affinity' => $this->clusterAffinity,
34+
],
35+
'json' => [
36+
'statement' => $query,
37+
'parameters' => empty($parameters) ? new stdClass() : $parameters,
38+
],
39+
]);
3440

35-
'statement' => $query,
36-
'parameters' => empty($parameters) ? new stdClass() : $parameters, // Pass the parameters array here
41+
$responseBody = $response->getBody()->getContents();
42+
$data = json_decode($responseBody, true);
3743

38-
],
44+
if (!isset($data['data']['fields'], $data['data']['values'])) {
45+
throw new Neo4jException([
46+
'message' => 'Unexpected response structure from Neo4j',
47+
'response' => $data,
3948
]);
40-
41-
// Decode the response body
42-
$data = json_decode($response->getBody()->getContents(), true);
43-
44-
// Initialize the OGM (Object Graph Mapping) class
45-
$ogm = new OGM();
46-
47-
// Extract keys (field names) and values (actual data)
48-
$keys = $data['results'][0]['columns'];
49-
$values = $data['results'][0]['data'];
50-
51-
// Process each row of the result and map them using OGM
52-
$rows = array_map(function ($resultRow) use ($ogm, $keys) {
53-
$data = [];
54-
foreach ($keys as $index => $key) {
55-
$fieldData = $resultRow['row'][$index] ?? null;
56-
$data[$key] = $ogm->map($fieldData); // Map the field data to the appropriate object format
57-
}
58-
return new ResultRow($data); // Wrap the mapped data in a ResultRow object
59-
}, $values);
60-
61-
return $rows; // Return the processed rows as an array of ResultRow objects
62-
63-
49+
}
50+
51+
$keys = $data['data']['fields'];
52+
$values = $data['data']['values'];
53+
54+
if (empty($values)) {
55+
return new ResultSet([]);
56+
}
57+
58+
$ogm = new OGM();
59+
$rows = array_map(function ($resultRow) use ($ogm, $keys) {
60+
$data = [];
61+
foreach ($keys as $index => $key) {
62+
$fieldData = $resultRow[$index] ?? null;
63+
$data[$key] = $ogm->map($fieldData);
64+
}
65+
return new ResultRow($data);
66+
}, $values);
67+
68+
return new ResultSet($rows);
6469
}
6570

66-
67-
6871
public function commit(): void
6972
{
7073
$this->client->post("/db/neo4j/query/v2/tx/{$this->transactionId}/commit", [
7174
'headers' => [
7275
'neo4j-cluster-affinity' => $this->clusterAffinity,
73-
]
76+
],
7477
]);
7578
}
7679

@@ -79,7 +82,7 @@ public function rollback(): void
7982
$this->client->delete("/db/neo4j/query/v2/tx/{$this->transactionId}", [
8083
'headers' => [
8184
'neo4j-cluster-affinity' => $this->clusterAffinity,
82-
]
85+
],
8386
]);
8487
}
8588
}

src/query-api-test.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55

66
require __DIR__ . '/../vendor/autoload.php';
77

8-
9-
// Login to the Neo4j instance
108
$api = Transaction::login(
119
'https://bb79fe35.databases.neo4j.io',
1210
'neo4j',
1311
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0'
1412
);
1513

16-
// Run a query to fetch results
1714
$query = 'MATCH (n:Person) RETURN n.name LIMIT 10';
1815
$results = $api->run($query, []);
1916

src/run_neo4j_query.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/runtransaction.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Neo4j\QueryAPI\Neo4jQueryAPI;
66

7-
87
$neo4jUrl = 'https://6f72daa1.databases.neo4j.io/';
98
$username = 'neo4j';
109
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
@@ -15,9 +14,8 @@
1514

1615
$query = 'CREATE (n:Person {name: "Bobby"}) RETURN n';
1716

18-
$response = $transaction->run($query);
17+
$response = $transaction->run($query, []);
1918

2019
print_r($response);
2120

2221
$transaction->commit();
23-

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,27 @@ public function testTransactionCommit(): void
4747
$name = (string)mt_rand(1, 100000);
4848

4949
// Create a node within the transaction
50-
$tsx->run('CREATE (x:Human {name: $name})', ['name' => $name]); // Pass the array here
50+
$tsx->run("CREATE (x:Human {name: \$name})", ['name' => $name]);
5151

5252
// Validate that the node does not exist in the database before the transaction is committed
53-
$results = $this->api->run('MATCH (x:Human {name: $name}) RETURN x', ['name' => $name]);
53+
$results = $this->api->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
5454
$this->assertCount(0, $results);
5555

5656
// Validate that the node exists within the transaction
57-
$results = $tsx->run('MATCH (x:Human {name: $name}) RETURN x', ['name' => $name]);
57+
$results = $tsx->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
5858
$this->assertCount(1, $results);
5959

6060
// Commit the transaction
6161
$tsx->commit();
6262

6363
// Validate that the node now exists in the database
64-
$results = $this->api->run('MATCH (x:Human {name: $name}) RETURN x', ['name' => $name]);
65-
$this->assertCount(0, $results);
64+
$results = $this->api->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
65+
$this->assertCount(1, $results); // Updated to expect 1 result
6666
}
6767

6868

6969

70+
7071
/**
7172
* @throws GuzzleException
7273
*/

tests/Integration/Neo4jTransactionTest.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)