Skip to content

Commit e05524e

Browse files
committed
Transaction test case code
1 parent b471ee2 commit e05524e

File tree

8 files changed

+222
-159
lines changed

8 files changed

+222
-159
lines changed

run.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI;
4+
require './vendor/autoload.php'; // Make sure Guzzle is installed (composer require guzzlehttp/guzzle)
5+
6+
use GuzzleHttp\Client;
7+
8+
9+
$neo4j_url = 'https://6f72daa1.databases.neo4j.io/';
10+
$username = 'neo4j';
11+
//$password = 'your_password';
12+
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
13+
14+
$query = 'CREATE (n:Person {name: "Bobby"}) RETURN n';
15+
16+
$auth = base64_encode("$username:$password");
17+
18+
$payload = json_encode([
19+
'statement' => $query,
20+
]);
21+
22+
$headers = [
23+
'Authorization' => 'Basic ' . $auth,
24+
'Content-Type' => 'application/json',
25+
'Accept' => 'application/json',
26+
];
27+
28+
29+
$client = new Client();
30+
31+
$response = $client->post('https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2/tx', [
32+
'headers' => $headers,
33+
'body' => $payload,
34+
]);
35+
$responseData = json_decode($response->getBody(), true);
36+
37+
print_r($responseData);

src/Neo4jQueryAPI.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,16 @@ public function run(string $cypher, array $parameters, string $database = 'neo4j
8585
}
8686
}
8787

88-
public function beginTransaction(string $cypher = null, array $parameters = [], string $database = 'neo4j'): Transaction
88+
public function beginTransaction(string $database = 'neo4j'): Transaction
8989
{
90+
$response = $this->client->post("/db/neo4j/query/v2/tx");
91+
92+
$clusterAffinity = $response->getHeaderLine('neo4j-cluster-affinity');
93+
$responseData = json_decode($response->getBody(), true);
94+
$transactionId = $responseData['transaction']['id'];
9095

91-
}
9296

97+
98+
return new Transaction($this->client, $clusterAffinity, $transactionId);
99+
}
93100
}

src/Transaction.php

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,83 @@
33
namespace Neo4j\QueryAPI;
44

55
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;
711

812
class Transaction
913
{
10-
private Client $client;
11-
private string $baseUrl;
12-
private array $headers;
13-
private ?string $transactionId = null;
1414

15-
public function __construct(string $baseUrl, string $username, string $password)
15+
public function __construct(private ClientInterface $client, private string $clusterAffinity, private string $transactionId)
1616
{
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-
];
2517
}
2618

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
2827
{
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' => [
3334

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
3737

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,
4938
],
50-
],
51-
]);
39+
]);
5240

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);
5743

58-
$responseData = json_decode($response->getBody(), true);
44+
// Initialize the OGM (Object Graph Mapping) class
45+
$ogm = new OGM();
5946

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'];
6250

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);
6860

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
7362

74-
$responseData = json_decode($response->getBody(), true);
75-
$this->transactionId = null;
7663

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+
]);
7875
}
7976

8077
public function rollback(): void
8178
{
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+
]);
8884
}
8985
}

src/run_transaction.php

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

src/runtransaction.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,22 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
use Neo4j\QueryAPI\Transaction;
5+
use Neo4j\QueryAPI\Neo4jQueryAPI;
66

7-
$baseUrl = 'https://6f72daa1.databases.neo4j.io';
7+
8+
$neo4jUrl = 'https://6f72daa1.databases.neo4j.io/';
89
$username = 'neo4j';
910
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
1011

11-
try {
12-
$transaction = new Transaction($baseUrl, $username, $password);
12+
$api = Neo4jQueryAPI::login($neo4jUrl, $username, $password);
13+
14+
$transaction = $api->beginTransaction();
1315

14-
$query = 'CREATE (n:Person {name: $name, age: $age}) RETURN n';
15-
$parameters = [
16-
'name' => 'Alice',
17-
'age' => 30,
18-
];
16+
$query = 'CREATE (n:Person {name: "Bobby"}) RETURN n';
1917

20-
$resultSet = $transaction->run($query, $parameters);
18+
$response = $transaction->run($query);
2119

22-
echo "Query executed successfully. Results:\n";
23-
foreach ($resultSet as $row) {
24-
print_r($row);
25-
}
20+
print_r($response);
2621

27-
$transaction->commit();
28-
echo "Transaction committed successfully.\n";
22+
$transaction->commit();
2923

30-
} catch (Exception $e) {
31-
echo "An error occurred: " . $e->getMessage() . "\n";
32-
}

test.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use GuzzleHttp\Client;
66

77
//$neo4j_url = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2/tx';
8-
//$neo4j_url = 'http://localhost:7474/db/neo4j/query/v2/tx';
9-
$username = 'neo4j';
10-
$password = 'your_password';
11-
//$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
8+
$neo4j_url = 'https://6f72daa1.databases.neo4j.io/';
9+
$username = 'neo4j';
10+
//$password = 'your_password';
11+
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
1212

13-
$query = 'CREATE (n:Person) RETURN n';
13+
$query = 'CREATE (n:Person {name: "Bobby"}) RETURN n';
1414

1515
$auth = base64_encode("$username:$password");
1616

@@ -20,27 +20,27 @@
2020

2121
$headers = [
2222
'Authorization' => 'Basic ' . $auth,
23-
'Content-Type' => 'application/json',
24-
'Accept' => 'application/json',
23+
'Content-Type' => 'application/json',
24+
'Accept' => 'application/json',
2525
];
2626

2727

2828
$client = new Client();
2929

3030
$response = $client->post('https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2/tx', [
3131
'headers' => $headers,
32-
'body' => $payload,
32+
'body' => $payload,
3333
]);
3434
$clusterAffinity = $response->getHeaderLine('neo4j-cluster-affinity');
3535
$responseData = json_decode($response->getBody(), true);
3636
$headers['neo4j-cluster-affinity'] = $clusterAffinity;
3737

3838
$transactionId = $responseData['transaction']['id'];
3939

40-
$response = $client->delete('http://localhost:7474/db/neo4j/query/v2/tx/' . $transactionId, [
40+
/*$response = $client->delete('http://localhost:7474/db/neo4j/query/v2/tx/' . $transactionId, [
4141
'headers' => $headers,
4242
'body' => $payload,
43-
]);
43+
]);*/
4444
$responseData = json_decode($response->getBody(), true);
4545

4646

0 commit comments

Comments
 (0)