Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 49 additions & 30 deletions src/Neo4jQueryAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use Psr\Http\Client\RequestExceptionInterface;
use RuntimeException;
use stdClass;
use Neo4j\QueryAPI\Objects\Bookmarks;

class Neo4jQueryAPI
{

private Client $client;

public function __construct(Client $client)
Expand All @@ -25,6 +27,10 @@ public function __construct(Client $client)

public static function login(string $address, string $username, string $password): self
{
$username = 'neo4j';
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
$connectionUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2';


$client = new Client([
'base_uri' => rtrim($address, '/'),
Expand All @@ -43,22 +49,23 @@ public static function login(string $address, string $username, string $password
* @throws Neo4jException
* @throws RequestExceptionInterface
*/
public function run(string $cypher, array $parameters = [], string $database = 'neo4j'): ResultSet
public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null): ResultSet
{
try {
// Prepare the payload for the request
$payload = [
'statement' => $cypher,
'parameters' => empty($parameters) ? new stdClass() : $parameters,
'includeCounters' => true
'includeCounters' => true,
];

// Execute the request to the Neo4j server
if ($bookmark !== null) {
$payload['bookmarks'] = $bookmark->getBookmarks();
}

$response = $this->client->post('/db/' . $database . '/query/v2', [
'json' => $payload,
]);

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

Expand All @@ -73,35 +80,47 @@ public function run(string $cypher, array $parameters = [], string $database = '
return new ResultRow($data);
}, $values);

return new ResultSet($rows, new ResultCounters(
containsUpdates: $data['counters']['containsUpdates'],
nodesCreated: $data['counters']['nodesCreated'],
nodesDeleted: $data['counters']['nodesDeleted'],
propertiesSet: $data['counters']['propertiesSet'],
relationshipsCreated: $data['counters']['relationshipsCreated'],
relationshipsDeleted: $data['counters']['relationshipsDeleted'],
labelsAdded: $data['counters']['labelsAdded'],
labelsRemoved: $data['counters']['labelsRemoved'],
indexesAdded: $data['counters']['indexesAdded'],
indexesRemoved: $data['counters']['indexesRemoved'],
constraintsAdded: $data['counters']['constraintsAdded'],
constraintsRemoved: $data['counters']['constraintsRemoved'],
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
systemUpdates: $data['counters']['systemUpdates']
));
} catch (RequestExceptionInterface $e) {
$response = $e->getResponse();
if ($response !== null) {
$contents = $response->getBody()->getContents();
$errorResponse = json_decode($contents, true);

throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
}
$resultCounters = new ResultCounters(
containsUpdates: $data['counters']['containsUpdates'] ?? false,
nodesCreated: $data['counters']['nodesCreated'] ?? 0,
nodesDeleted: $data['counters']['nodesDeleted'] ?? 0,
propertiesSet: $data['counters']['propertiesSet'] ?? 0,
relationshipsCreated: $data['counters']['relationshipsCreated'] ?? 0,
relationshipsDeleted: $data['counters']['relationshipsDeleted'] ?? 0,
labelsAdded: $data['counters']['labelsAdded'] ?? 0,
labelsRemoved: $data['counters']['labelsRemoved'] ?? 0,
indexesAdded: $data['counters']['indexesAdded'] ?? 0,
indexesRemoved: $data['counters']['indexesRemoved'] ?? 0,
constraintsAdded: $data['counters']['constraintsAdded'] ?? 0,
constraintsRemoved: $data['counters']['constraintsRemoved'] ?? 0,
containsSystemUpdates: $data['counters']['containsSystemUpdates'] ?? false,
systemUpdates: $data['counters']['systemUpdates'] ?? 0
);

$resultSet = new ResultSet($rows, $resultCounters, new Bookmarks($data['bookmarks'] ?? []));


return $resultSet;

} catch (RequestException $e) {
{
$response = $e->getResponse();
if ($response !== null) {
$contents = $response->getBody()->getContents();
$errorResponse = json_decode($contents, true);

throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
}

throw $e;
throw $e;
}
throw new RuntimeException('Error executing query: ' . $e->getMessage(), 0, $e);
}
}




public function beginTransaction(string $database = 'neo4j'): Transaction
{
$response = $this->client->post("/db/neo4j/query/v2/tx");
Expand Down
28 changes: 28 additions & 0 deletions src/Objects/Bookmarks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Neo4j\QueryAPI\Objects;

class Bookmarks implements \Countable
{
public function __construct(private array $bookmarks)
{
}

public function addBookmarks(?Bookmarks $newBookmarks): void
{
if ($newBookmarks !== null) {
$this->bookmarks = array_unique(array_merge($this->bookmarks, $newBookmarks->bookmarks));
}
}


public function getBookmarks(): array
{
return $this->bookmarks;
}

public function count(): int
{
return count($this->bookmarks);
}
}
45 changes: 14 additions & 31 deletions src/Objects/ResultCounters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
class ResultCounters
{
public function __construct(
private readonly bool $containsUpdates,
private readonly int $nodesCreated,
private readonly int $nodesDeleted,
private readonly int $propertiesSet,
private readonly int $relationshipsCreated,
private readonly int $relationshipsDeleted,
private readonly int $labelsAdded,
private readonly int $labelsRemoved,
private readonly int $indexesAdded,
private readonly int $indexesRemoved,
private readonly int $constraintsAdded,
private readonly int $constraintsRemoved,
private readonly bool $containsSystemUpdates,
private readonly int $systemUpdates
private readonly bool $containsUpdates = false,
private readonly int $nodesCreated = 0,
private readonly int $nodesDeleted = 0,
private readonly int $propertiesSet = 0,
private readonly int $relationshipsCreated = 0,
private readonly int $relationshipsDeleted = 0,
private readonly int $labelsAdded = 0,
private readonly int $labelsRemoved = 0,
private readonly int $indexesAdded = 0,
private readonly int $indexesRemoved = 0,
private readonly int $constraintsAdded = 0,
private readonly int $constraintsRemoved = 0,
private readonly bool $containsSystemUpdates = false,
private readonly int $systemUpdates = 0
) {
}

Expand Down Expand Up @@ -92,23 +92,6 @@ public function getLabelsRemoved(): int
{
return $this->labelsRemoved;
}
public function getBookmarks(): array
{
return $this->bookmarks;
}

public function addBookmark(string $bookmark): void
{
if (!in_array($bookmark, $this->bookmarks)) {
$this->bookmarks[] = $bookmark;
}
}

// public function clearBookmarks(): void
// {
// $this->bookmarks = [];
// }

}


14 changes: 12 additions & 2 deletions src/Results/ResultSet.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
<?php

namespace Neo4j\QueryAPI\Results;

use ArrayIterator;
use Countable;
use IteratorAggregate;
use Neo4j\QueryAPI\Objects\ResultCounters;
use Neo4j\QueryAPI\Objects\Bookmarks; // Make sure to include the Bookmarks class
use Traversable;

class ResultSet implements IteratorAggregate, Countable
{
/**
* @param list<ResultRow> $rows
*/
public function __construct(private readonly array $rows,private ResultCounters $counters)
public function __construct(
private readonly array $rows,
private ResultCounters $counters,
private Bookmarks $bookmarks
)
{
}

public function getIterator(): Traversable
{
return new ArrayIterator($this->rows);
}

public function getQueryCounters(): ?ResultCounters
{
return $this->counters;
Expand All @@ -30,5 +37,8 @@ public function count(): int
return count($this->rows);
}


public function getBookmarks(): ?Bookmarks
{
return $this->bookmarks;
}
}
56 changes: 56 additions & 0 deletions src/neo4jQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

require '../vendor/autoload.php';
use GuzzleHttp\Client;
use Neo4j\QueryAPI\Objects\ResultCounters;

$username = 'neo4j';
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
$connectionUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2';

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

$client = new Client();

$resultCounters = new ResultCounters(
containsUpdates: false,
nodesCreated: 0,
nodesDeleted: 0,
propertiesSet: 0,
relationshipsCreated: 0,
relationshipsDeleted: 0,
labelsAdded: 0,
labelsRemoved: 0,
indexesAdded: 0,
indexesRemoved: 0,
constraintsAdded: 0,
constraintsRemoved: 0,
containsSystemUpdates: false,
systemUpdates: 0
);

$response = $client->post($connectionUrl, [
'json' => [
'statement' => 'CREATE (n:Person {name: $name}) RETURN n.name AS name',
'parameters' => [
'name' => 'Alice'
],
'includeCounters' => true,
'bookmarks' => $resultCounters->getBookmarks(),
],
'headers' => [
'Authorization' => 'Basic ' . $auth,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
]);

$data = json_decode($response->getBody()->getContents(), true);

foreach ($data['counters'] as $key => $value) {
$resultCounters->setCounter($key, $value);
}

if (isset($data['bookmark'])) {
$resultCounters->addBookmark($data['bookmark']);
}
Loading
Loading