|
3 | 3 | namespace Neo4j\QueryAPI; |
4 | 4 |
|
5 | 5 | use GuzzleHttp\Client; |
6 | | -use Neo4j\QueryAPI\Results\ResultSet; |
| 6 | +use Neo4j\QueryAPI\Exception\Neo4jException; |
| 7 | +use Neo4j\QueryAPI\Results\ResultRow; |
| 8 | +use Psr\Http\Client\ClientInterface; |
| 9 | +use Psr\Http\Client\RequestExceptionInterface; |
| 10 | +use stdClass; |
7 | 11 |
|
8 | 12 | class Transaction |
9 | 13 | { |
10 | | - private Client $client; |
11 | | - private string $baseUrl; |
12 | | - private array $headers; |
13 | | - private ?string $transactionId = null; |
14 | 14 |
|
15 | | - public function __construct(string $baseUrl, string $username, string $password) |
| 15 | + public function __construct(private ClientInterface $client, private string $clusterAffinity, private string $transactionId) |
16 | 16 | { |
17 | | - $this->client = new Client(); |
18 | | - $this->baseUrl = $baseUrl; |
19 | | - $auth = base64_encode("$username:$password"); |
20 | | - $this->headers = [ |
21 | | - 'Authorization' => 'Basic ' . $auth, |
22 | | - 'Content-Type' => 'application/json', |
23 | | - 'Accept' => 'application/json', |
24 | | - ]; |
25 | 17 | } |
26 | 18 |
|
27 | | - public function begin(): void |
| 19 | + /** |
| 20 | + * Create a node in Neo4j with a specified label and properties. |
| 21 | + * |
| 22 | + * @param string $query The Cypher query to be executed. |
| 23 | + * @param $parameters |
| 24 | + * @return array The response data from Neo4j. |
| 25 | + */ |
| 26 | + public function run(string $query, array $parameters): array |
28 | 27 | { |
29 | | - $response = $this->client->post($this->baseUrl .'/db/neo4j'. '/tx', [ |
30 | | - 'headers' => $this->headers, |
31 | | - 'body' => json_encode([]), |
32 | | - ]); |
| 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' => [ |
33 | 34 |
|
34 | | - $responseData = json_decode($response->getBody(), true); |
35 | | - $this->transactionId = $responseData['commit'] ?? null; |
36 | | - } |
| 35 | + 'statement' => $query, |
| 36 | + 'parameters' => empty($parameters) ? new stdClass() : $parameters, // Pass the parameters array here |
37 | 37 |
|
38 | | - public function run(string $statement, array $params = []): ResultSet |
39 | | - { |
40 | | - if (!$this->transactionId) { |
41 | | - $this->begin(); |
42 | | - } |
43 | | - |
44 | | - $payload = json_encode([ |
45 | | - 'statements' => [ |
46 | | - [ |
47 | | - 'statement' => $statement, |
48 | | - 'parameters' => $params, |
49 | 38 | ], |
50 | | - ], |
51 | | - ]); |
| 39 | + ]); |
52 | 40 |
|
53 | | - $response = $this->client->post($this->baseUrl .'/db/neo4j'.'/tx/' . $this->transactionId, [ |
54 | | - 'headers' => $this->headers, |
55 | | - 'body' => $payload, |
56 | | - ]); |
| 41 | + // Decode the response body |
| 42 | + $data = json_decode($response->getBody()->getContents(), true); |
57 | 43 |
|
58 | | - $responseData = json_decode($response->getBody(), true); |
| 44 | + // Initialize the OGM (Object Graph Mapping) class |
| 45 | + $ogm = new OGM(); |
59 | 46 |
|
60 | | - return new ResultSet($responseData['results'] ?? []); |
61 | | - } |
| 47 | + // Extract keys (field names) and values (actual data) |
| 48 | + $keys = $data['results'][0]['columns']; |
| 49 | + $values = $data['results'][0]['data']; |
62 | 50 |
|
63 | | - public function commit(): ?ResultSet |
64 | | - { |
65 | | - if (!$this->transactionId) { |
66 | | - throw new \Exception("No active transaction to commit."); |
67 | | - } |
| 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); |
68 | 60 |
|
69 | | - $response = $this->client->post($this->transactionId, [ |
70 | | - 'headers' => $this->headers, |
71 | | - 'body' => json_encode([]), |
72 | | - ]); |
| 61 | + return $rows; // Return the processed rows as an array of ResultRow objects |
73 | 62 |
|
74 | | - $responseData = json_decode($response->getBody(), true); |
75 | | - $this->transactionId = null; |
76 | 63 |
|
77 | | - return new ResultSet($responseData['results'] ?? []); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + public function commit(): void |
| 69 | + { |
| 70 | + $this->client->post("/db/neo4j/query/v2/tx/{$this->transactionId}/commit", [ |
| 71 | + 'headers' => [ |
| 72 | + 'neo4j-cluster-affinity' => $this->clusterAffinity, |
| 73 | + ] |
| 74 | + ]); |
78 | 75 | } |
79 | 76 |
|
80 | 77 | public function rollback(): void |
81 | 78 | { |
82 | | - if ($this->transactionId) { |
83 | | - $this->client->delete($this->baseUrl . '/tx/' . $this->transactionId, [ |
84 | | - 'headers' => $this->headers, |
85 | | - ]); |
86 | | - $this->transactionId = null; |
87 | | - } |
| 79 | + $this->client->delete("/db/neo4j/query/v2/tx/{$this->transactionId}", [ |
| 80 | + 'headers' => [ |
| 81 | + 'neo4j-cluster-affinity' => $this->clusterAffinity, |
| 82 | + ] |
| 83 | + ]); |
88 | 84 | } |
89 | 85 | } |
0 commit comments