Skip to content

Commit 133adcd

Browse files
committed
Merge remote-tracking branch 'origin/main' into create-query-profile-1951106664
2 parents 36989cc + 78b7002 commit 133adcd

11 files changed

+758
-524
lines changed

src/Neo4jQueryAPI.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
use Psr\Http\Client\RequestExceptionInterface;
1717
use RuntimeException;
1818
use stdClass;
19+
use Neo4j\QueryAPI\Objects\Bookmarks;
1920

20-
/**
21-
* @method parseChildren(mixed $children)
22-
*/
2321
class Neo4jQueryAPI
2422
{
23+
2524
private Client $client;
2625

2726
public function __construct(Client $client)
@@ -48,26 +47,26 @@ public static function login(string $address, string $username, string $password
4847
* @throws Neo4jException
4948
* @throws RequestExceptionInterface
5049
*/
51-
public function run(string $cypher, array $parameters = [], string $database = 'neo4j'): ResultSet
50+
public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null): ResultSet
5251
{
5352
try {
54-
// Prepare the payload for the request
5553
$payload = [
5654
'statement' => $cypher,
5755
'parameters' => empty($parameters) ? new stdClass() : $parameters,
58-
'includeCounters' => true
56+
'includeCounters' => true,
5957
];
6058

61-
// Execute the request to the Neo4j server
59+
if ($bookmark !== null) {
60+
$payload['bookmarks'] = $bookmark->getBookmarks();
61+
}
62+
6263
$response = $this->client->post('/db/' . $database . '/query/v2', [
6364
'json' => $payload,
6465
]);
6566

66-
// Decode the response body
6767
$data = json_decode($response->getBody()->getContents(), true);
6868
$ogm = new OGM();
6969

70-
// Extract result rows
7170
$keys = $data['data']['fields'];
7271
$values = $data['data']['values'];
7372
$rows = array_map(function ($resultRow) use ($ogm, $keys) {
@@ -79,31 +78,31 @@ public function run(string $cypher, array $parameters = [], string $database = '
7978
return new ResultRow($data);
8079
}, $values);
8180

82-
8381
if (isset($data['profiledQueryPlan'])) {
8482
$profile = $this->createProfileData($data['profiledQueryPlan']);
8583
}
8684

85+
$resultCounters = new ResultCounters(
86+
containsUpdates: $data['counters']['containsUpdates'] ?? false,
87+
nodesCreated: $data['counters']['nodesCreated'] ?? 0,
88+
nodesDeleted: $data['counters']['nodesDeleted'] ?? 0,
89+
propertiesSet: $data['counters']['propertiesSet'] ?? 0,
90+
relationshipsCreated: $data['counters']['relationshipsCreated'] ?? 0,
91+
relationshipsDeleted: $data['counters']['relationshipsDeleted'] ?? 0,
92+
labelsAdded: $data['counters']['labelsAdded'] ?? 0,
93+
labelsRemoved: $data['counters']['labelsRemoved'] ?? 0,
94+
indexesAdded: $data['counters']['indexesAdded'] ?? 0,
95+
indexesRemoved: $data['counters']['indexesRemoved'] ?? 0,
96+
constraintsAdded: $data['counters']['constraintsAdded'] ?? 0,
97+
constraintsRemoved: $data['counters']['constraintsRemoved'] ?? 0,
98+
containsSystemUpdates: $data['counters']['containsSystemUpdates'] ?? false,
99+
systemUpdates: $data['counters']['systemUpdates'] ?? 0
100+
);
87101

88-
// Return a ResultSet containing rows, counters, and the profiled query plan
89102
return new ResultSet(
90103
$rows,
91-
new ResultCounters(
92-
containsUpdates: $data['counters']['containsUpdates'],
93-
nodesCreated: $data['counters']['nodesCreated'],
94-
nodesDeleted: $data['counters']['nodesDeleted'],
95-
propertiesSet: $data['counters']['propertiesSet'],
96-
relationshipsCreated: $data['counters']['relationshipsCreated'],
97-
relationshipsDeleted: $data['counters']['relationshipsDeleted'],
98-
labelsAdded: $data['counters']['labelsAdded'],
99-
labelsRemoved: $data['counters']['labelsRemoved'],
100-
indexesAdded: $data['counters']['indexesAdded'],
101-
indexesRemoved: $data['counters']['indexesRemoved'],
102-
constraintsAdded: $data['counters']['constraintsAdded'],
103-
constraintsRemoved: $data['counters']['constraintsRemoved'],
104-
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
105-
systemUpdates: $data['counters']['systemUpdates']
106-
),
104+
$resultCounters,
105+
new Bookmarks($data['bookmarks'] ?? []),
107106
$profile
108107
);
109108
} catch (RequestExceptionInterface $e) {
@@ -112,7 +111,7 @@ public function run(string $cypher, array $parameters = [], string $database = '
112111
$contents = $response->getBody()->getContents();
113112
$errorResponse = json_decode($contents, true);
114113

115-
throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
114+
throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
116115
}
117116

118117
throw $e;

src/Objects/Bookmarks.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects;
4+
5+
class Bookmarks implements \Countable
6+
{
7+
public function __construct(private array $bookmarks)
8+
{
9+
}
10+
11+
public function addBookmarks(?Bookmarks $newBookmarks): void
12+
{
13+
if ($newBookmarks !== null) {
14+
$this->bookmarks = array_unique(array_merge($this->bookmarks, $newBookmarks->bookmarks));
15+
}
16+
}
17+
18+
19+
public function getBookmarks(): array
20+
{
21+
return $this->bookmarks;
22+
}
23+
24+
public function count(): int
25+
{
26+
return count($this->bookmarks);
27+
}
28+
}

src/Objects/ResultCounters.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
class ResultCounters
66
{
77
public function __construct(
8-
private readonly bool $containsUpdates,
9-
private readonly int $nodesCreated,
10-
private readonly int $nodesDeleted,
11-
private readonly int $propertiesSet,
12-
private readonly int $relationshipsCreated,
13-
private readonly int $relationshipsDeleted,
14-
private readonly int $labelsAdded,
15-
private readonly int $labelsRemoved,
16-
private readonly int $indexesAdded,
17-
private readonly int $indexesRemoved,
18-
private readonly int $constraintsAdded,
19-
private readonly int $constraintsRemoved,
20-
private readonly bool $containsSystemUpdates,
21-
private readonly int $systemUpdates
8+
private readonly bool $containsUpdates = false,
9+
private readonly int $nodesCreated = 0,
10+
private readonly int $nodesDeleted = 0,
11+
private readonly int $propertiesSet = 0,
12+
private readonly int $relationshipsCreated = 0,
13+
private readonly int $relationshipsDeleted = 0,
14+
private readonly int $labelsAdded = 0,
15+
private readonly int $labelsRemoved = 0,
16+
private readonly int $indexesAdded = 0,
17+
private readonly int $indexesRemoved = 0,
18+
private readonly int $constraintsAdded = 0,
19+
private readonly int $constraintsRemoved = 0,
20+
private readonly bool $containsSystemUpdates = false,
21+
private readonly int $systemUpdates = 0
2222
) {
2323
}
2424

@@ -92,23 +92,6 @@ public function getLabelsRemoved(): int
9292
{
9393
return $this->labelsRemoved;
9494
}
95-
public function getBookmarks(): array
96-
{
97-
return $this->bookmarks;
98-
}
99-
100-
public function addBookmark(string $bookmark): void
101-
{
102-
if (!in_array($bookmark, $this->bookmarks)) {
103-
$this->bookmarks[] = $bookmark;
104-
}
105-
}
106-
107-
// public function clearBookmarks(): void
108-
// {
109-
// $this->bookmarks = [];
110-
// }
111-
11295
}
11396

11497

src/Query_Profile.php

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

src/Query_Profile_run.php

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

src/Results/ResultSet.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Neo4j\QueryAPI\Results;
34

45
use ArrayIterator;
@@ -8,22 +9,27 @@
89
use Neo4j\QueryAPI\Objects\ProfiledQueryPlan;
910
use Neo4j\QueryAPI\Objects\QueryArguments;
1011
use Neo4j\QueryAPI\Objects\ResultCounters;
12+
use Neo4j\QueryAPI\Objects\Bookmarks; // Make sure to include the Bookmarks class
1113
use Traversable;
1214

1315
class ResultSet implements IteratorAggregate, Countable
1416
{
1517
/**
1618
* @param list<ResultRow> $rows
1719
*/
18-
public function __construct(private readonly array $rows, private ResultCounters $counters, private ?ProfiledQueryPlan $profiledQueryPlan = null)
20+
public function __construct(
21+
private readonly array $rows,
22+
private ResultCounters $counters,
23+
private Bookmarks $bookmarks,
24+
private ?ProfiledQueryPlan $profiledQueryPlan = null
25+
)
1926
{
2027
}
2128

2229
public function getIterator(): Traversable
2330
{
2431
return new ArrayIterator($this->rows);
2532
}
26-
2733
public function getQueryCounters(): ?ResultCounters
2834
{
2935
return $this->counters;
@@ -34,11 +40,6 @@ public function getProfiledQueryPlan(): ?ProfiledQueryPlan
3440
return $this->profiledQueryPlan;
3541
}
3642

37-
public function getQueryArguments(): ?QueryArguments
38-
{
39-
return $this->queryArguments;
40-
}
41-
4243
public function getChildQueryPlan(): ?ChildQueryPlan
4344
{
4445
return $this->childQueryPlan;
@@ -48,5 +49,9 @@ public function count(): int
4849
{
4950
return count($this->rows);
5051
}
51-
}
5252

53+
public function getBookmarks(): ?Bookmarks
54+
{
55+
return $this->bookmarks;
56+
}
57+
}

src/Transaction.php

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Neo4j\QueryAPI;
44

55
use Neo4j\QueryAPI\Exception\Neo4jException;
6+
use Neo4j\QueryAPI\Objects\Bookmarks;
67
use Neo4j\QueryAPI\Objects\ResultCounters;
78
use Neo4j\QueryAPI\Results\ResultRow;
89
use Neo4j\QueryAPI\Results\ResultSet;
@@ -53,7 +54,41 @@ public function run(string $query, array $parameters): ResultSet
5354
$values = $data['data']['values'];
5455

5556
if (empty($values)) {
56-
return new ResultSet([], new ResultCounters(
57+
return new ResultSet(
58+
rows: [],
59+
counters: new ResultCounters(
60+
containsUpdates: $data['counters']['containsUpdates'],
61+
nodesCreated: $data['counters']['nodesCreated'],
62+
nodesDeleted: $data['counters']['nodesDeleted'],
63+
propertiesSet: $data['counters']['propertiesSet'],
64+
relationshipsCreated: $data['counters']['relationshipsCreated'],
65+
relationshipsDeleted: $data['counters']['relationshipsDeleted'],
66+
labelsAdded: $data['counters']['labelsAdded'],
67+
labelsRemoved: $data['counters']['labelsRemoved'],
68+
indexesAdded: $data['counters']['indexesAdded'],
69+
indexesRemoved: $data['counters']['indexesRemoved'],
70+
constraintsAdded: $data['counters']['constraintsAdded'],
71+
constraintsRemoved: $data['counters']['constraintsRemoved'],
72+
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
73+
systemUpdates: $data['counters']['systemUpdates']
74+
),
75+
bookmarks: new Bookmarks($data['bookmarks'] ?? [])
76+
);
77+
}
78+
79+
$ogm = new OGM();
80+
$rows = array_map(function ($resultRow) use ($ogm, $keys) {
81+
$data = [];
82+
foreach ($keys as $index => $key) {
83+
$fieldData = $resultRow[$index] ?? null;
84+
$data[$key] = $ogm->map($fieldData);
85+
}
86+
return new ResultRow($data);
87+
}, $values);
88+
89+
return new ResultSet(
90+
rows: $rows,
91+
counters: new ResultCounters(
5792
containsUpdates: $data['counters']['containsUpdates'],
5893
nodesCreated: $data['counters']['nodesCreated'],
5994
nodesDeleted: $data['counters']['nodesDeleted'],
@@ -68,35 +103,9 @@ public function run(string $query, array $parameters): ResultSet
68103
constraintsRemoved: $data['counters']['constraintsRemoved'],
69104
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
70105
systemUpdates: $data['counters']['systemUpdates']
71-
));
72-
}
73-
74-
$ogm = new OGM();
75-
$rows = array_map(function ($resultRow) use ($ogm, $keys) {
76-
$data = [];
77-
foreach ($keys as $index => $key) {
78-
$fieldData = $resultRow[$index] ?? null;
79-
$data[$key] = $ogm->map($fieldData);
80-
}
81-
return new ResultRow($data);
82-
}, $values);
83-
84-
return new ResultSet($rows, new ResultCounters(
85-
containsUpdates: $data['counters']['containsUpdates'],
86-
nodesCreated: $data['counters']['nodesCreated'],
87-
nodesDeleted: $data['counters']['nodesDeleted'],
88-
propertiesSet: $data['counters']['propertiesSet'],
89-
relationshipsCreated: $data['counters']['relationshipsCreated'],
90-
relationshipsDeleted: $data['counters']['relationshipsDeleted'],
91-
labelsAdded: $data['counters']['labelsAdded'],
92-
labelsRemoved: $data['counters']['labelsRemoved'],
93-
indexesAdded: $data['counters']['indexesAdded'],
94-
indexesRemoved: $data['counters']['indexesRemoved'],
95-
constraintsAdded: $data['counters']['constraintsAdded'],
96-
constraintsRemoved: $data['counters']['constraintsRemoved'],
97-
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
98-
systemUpdates: $data['counters']['systemUpdates']
99-
));
106+
),
107+
bookmarks: new Bookmarks($data['bookmarks'] ?? [])
108+
);
100109
}
101110

102111
public function commit(): void

0 commit comments

Comments
 (0)