diff --git a/queryCounters/Neo4jBookmarkIntegration.php b/queryCounters/Neo4jBookmarkIntegration.php deleted file mode 100644 index 189fe17e..00000000 --- a/queryCounters/Neo4jBookmarkIntegration.php +++ /dev/null @@ -1,66 +0,0 @@ -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) { - if (property_exists($resultCounters, $key)) { - $resultCounters->$key = $value; - } -} - -if (isset($data['bookmark'])) { - $resultCounters->addBookmark($data['bookmark']); -} - -echo json_encode([ - 'data' => $data['results'][0]['data'] ?? [], - 'counters' => $data['counters'], - 'bookmarks' => $resultCounters->getBookmarks(), -], JSON_PRETTY_PRINT); - -echo ('test'); \ No newline at end of file diff --git a/queryCounters/queryCounters3.php b/queryCounters/queryCounters3.php deleted file mode 100644 index 3991b443..00000000 --- a/queryCounters/queryCounters3.php +++ /dev/null @@ -1,57 +0,0 @@ -post('https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2', [ - 'json' => [ - 'statement' => "CREATE (n:Person {name: 'Peter'}) RETURN n.name AS name", - 'includeCounters' => true, - - ], - 'headers' => [ - 'Authorization' => 'Basic ' . $auth, - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - 'Impersonate-User' => $impersonatedUser - ] -]); - - -$data = json_decode($response->getBody()->getContents(), true); - - - -$counters = new \Neo4j\QueryAPI\Objects\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'] -); - - - diff --git a/queryCounters/queryCountersTest.php b/queryCounters/queryCountersTest.php deleted file mode 100644 index 127c168d..00000000 --- a/queryCounters/queryCountersTest.php +++ /dev/null @@ -1,52 +0,0 @@ - 1, - 'relationships_created' => 2, - 'properties_set' => 3, - ]); - - $this->assertEquals(1, $counters->get('nodes_created')); - $this->assertEquals(2, $counters->get('relationships_created')); - $this->assertEquals(3, $counters->get('properties_set')); - } - - public function testGetNonExistingCounterReturnsZero(): void - { - $counters = new queryCounters([ - 'nodes_created' => 1, - ]); - - $this->assertEquals(0, $counters->get('labels_added')); - $this->assertEquals(0, $counters->get('relationships_deleted')); - } - - public function testToArray(): void - { - $inputCounters = [ - 'nodes_created' => 1, - 'relationships_created' => 2, - 'properties_set' => 3, - ]; - - $counters = new queryCounters($inputCounters); - - $this->assertEquals($inputCounters, $counters->toArray()); - } - - public function testEmptyCountersArray(): void - { - $counters = new queryCounters([]); - - $this->assertEquals(0, $counters->get('nodes_created')); - $this->assertEquals(0, $counters->get('relationships_created')); - $this->assertEmpty($counters->toArray()); - } -} diff --git a/src/Neo4jQueryAPI.php b/src/Neo4jQueryAPI.php index 52e474d6..c47e6ea7 100644 --- a/src/Neo4jQueryAPI.php +++ b/src/Neo4jQueryAPI.php @@ -6,6 +6,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\RequestException; +use Neo4j\QueryAPI\Objects\ResultCounters; use Neo4j\QueryAPI\Results\ResultRow; use Neo4j\QueryAPI\Results\ResultSet; use Neo4j\QueryAPI\Exception\Neo4jException; @@ -42,13 +43,14 @@ 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'): ResultSet { try { // Prepare the payload for the request $payload = [ 'statement' => $cypher, 'parameters' => empty($parameters) ? new stdClass() : $parameters, + 'includeCounters' => true ]; // Execute the request to the Neo4j server @@ -71,7 +73,22 @@ public function run(string $cypher, array $parameters, string $database = 'neo4j return new ResultRow($data); }, $values); - return new ResultSet($rows); + 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) { diff --git a/src/Objects/ResultCounters.php b/src/Objects/ResultCounters.php index b5cad70d..0e6cee74 100644 --- a/src/Objects/ResultCounters.php +++ b/src/Objects/ResultCounters.php @@ -18,10 +18,8 @@ public function __construct( private readonly int $constraintsAdded, private readonly int $constraintsRemoved, private readonly bool $containsSystemUpdates, - private readonly int $systemUpdates, -// array $bookmarks = [] + private readonly int $systemUpdates ) { - $this->bookmarks = $bookmarks; } diff --git a/src/Transaction.php b/src/Transaction.php deleted file mode 100644 index 82a2d12f..00000000 --- a/src/Transaction.php +++ /dev/null @@ -1,85 +0,0 @@ -client->post("/db/neo4j/query/v2/tx", [ - 'headers' => [ - 'neo4j-cluster-affinity' => $this->clusterAffinity, - ], - 'json' => [ - - 'statement' => $query, - 'parameters' => empty($parameters) ? new stdClass() : $parameters, // Pass the parameters array here - - ], - ]); - - // Decode the response body - $data = json_decode($response->getBody()->getContents(), true); - - // Initialize the OGM (Object Graph Mapping) class - $ogm = new OGM(); - - // Extract keys (field names) and values (actual data) - $keys = $data['results'][0]['columns']; - $values = $data['results'][0]['data']; - - // Process each row of the result and map them using OGM - $rows = array_map(function ($resultRow) use ($ogm, $keys) { - $data = []; - foreach ($keys as $index => $key) { - $fieldData = $resultRow['row'][$index] ?? null; - $data[$key] = $ogm->map($fieldData); // Map the field data to the appropriate object format - } - return new ResultRow($data); // Wrap the mapped data in a ResultRow object - }, $values); - - return $rows; // Return the processed rows as an array of ResultRow objects - - - } - - - - public function commit(): void - { - $this->client->post("/db/neo4j/query/v2/tx/{$this->transactionId}/commit", [ - 'headers' => [ - 'neo4j-cluster-affinity' => $this->clusterAffinity, - ] - ]); - } - - public function rollback(): void - { - $this->client->delete("/db/neo4j/query/v2/tx/{$this->transactionId}", [ - 'headers' => [ - 'neo4j-cluster-affinity' => $this->clusterAffinity, - ] - ]); - } -} diff --git a/src/query-api-test.php b/src/query-api-test.php deleted file mode 100644 index 139597f9..00000000 --- a/src/query-api-test.php +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/src/run_neo4j_query.php b/src/run_neo4j_query.php deleted file mode 100644 index 83faf532..00000000 --- a/src/run_neo4j_query.php +++ /dev/null @@ -1,82 +0,0 @@ - [] - ]); - - curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); - - $response = curl_exec($ch); - if(curl_errno($ch)) { - echo 'Error starting transaction: ' . curl_error($ch); - return; - } - - echo "Transaction Start Response: "; - print_r($response); - - $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); - if ($http_status === 403) { - echo "403 Forbidden: Check credentials and permissions.\n"; - } - - $transaction_data = json_decode($response, true); - - if (!isset($transaction_data['results'])) { - echo "Transaction creation failed or missing results. Response: "; - print_r($transaction_data); - return; - } - - $query_data = [ - "statements" => [ - [ - "statement" => $query - ] - ] - ]; - - curl_setopt($ch, CURLOPT_URL, $neo4j_url . '/db/neo4j/query/v2/tx'); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query_data)); - - $response = curl_exec($ch); - if(curl_errno($ch)) { - echo 'Error running query: ' . curl_error($ch); - return; - } - - $commit_data = json_decode($response, true); - if (isset($commit_data['errors']) && count($commit_data['errors']) > 0) { - echo "Query error: " . $commit_data['errors'][0]['message']; - return; - } - - echo "Transaction successful. Data returned: "; - print_r($commit_data); - - curl_close($ch); -} - -$query = 'MATCH (n) RETURN n LIMIT 5'; - -runNeo4jTransaction($query); \ No newline at end of file diff --git a/src/run_query.php b/src/run_query.php deleted file mode 100644 index 95f11776..00000000 --- a/src/run_query.php +++ /dev/null @@ -1,38 +0,0 @@ -run($query, [], 'neo4j', false); - echo "Plain JSON Results:\n"; - echo "
"; - print_r($plainResults); - echo ""; - - // Fetch results in Neo4j-extended JSON format - $extendedResults = $api->run($query, [], 'neo4j', true); - echo "Extended JSON Results:\n"; - echo "
"; - print_r($extendedResults); - echo ""; - -} catch (RequestException $e) { - echo "Request Error: " . $e->getMessage(); -} catch (RuntimeException $e) { - echo "Runtime Error: " . $e->getMessage(); -} catch (Exception $e) { - echo "General Error: " . $e->getMessage(); -} \ No newline at end of file diff --git a/src/runtransaction.php b/src/runtransaction.php deleted file mode 100644 index 7ff5a9f9..00000000 --- a/src/runtransaction.php +++ /dev/null @@ -1,23 +0,0 @@ -beginTransaction(); - -$query = 'CREATE (n:Person {name: "Bobby"}) RETURN n'; - -$response = $transaction->run($query); - -print_r($response); - -$transaction->commit(); - diff --git a/tests/Integration/Neo4jOGMTest.php b/tests/Integration/Neo4jOGMTest.php index ac622489..4c80889f 100644 --- a/tests/Integration/Neo4jOGMTest.php +++ b/tests/Integration/Neo4jOGMTest.php @@ -421,6 +421,4 @@ public function testWithString(string $query, array $parameters, ?string $expect $this->assertEquals($expectedResult, $actual); } - - } diff --git a/tests/Integration/Neo4jQueryAPIIntegrationTest.php b/tests/Integration/Neo4jQueryAPIIntegrationTest.php index 7557778a..8c980fd5 100644 --- a/tests/Integration/Neo4jQueryAPIIntegrationTest.php +++ b/tests/Integration/Neo4jQueryAPIIntegrationTest.php @@ -7,7 +7,6 @@ use Neo4j\QueryAPI\Neo4jQueryAPI; use Neo4j\QueryAPI\Results\ResultRow; use Neo4j\QueryAPI\Results\ResultSet; -use Neo4j\QueryAPI\Transaction; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -38,6 +37,13 @@ private function initializeApi(): Neo4jQueryAPI ); } + public function testCounters(): void + { + $result = $this->api->run('CREATE (x:Node {hello: "world"})'); + + $this->assertEquals(1, $result->getQueryCounters()->getNodesCreated()); + } + public function testTransactionCommit(): void { // Begin a new transaction diff --git a/tests/Integration/Neo4jQueryApiIntegrationTempTest.php b/tests/Integration/Neo4jQueryApiIntegrationTempTest.php deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/Integration/Neo4jTransactionTest.php b/tests/Integration/Neo4jTransactionTest.php deleted file mode 100644 index 4bc5ecdc..00000000 --- a/tests/Integration/Neo4jTransactionTest.php +++ /dev/null @@ -1,79 +0,0 @@ -transaction = new Transaction($this->neo4jUrl, $this->username, $this->password); - } - - /** - * Test case for committing a transaction. - */ - public function testTransactionCommit(): void - { - $transactionData = $this->transaction->startTransaction(); - $transactionId = $transactionData['transactionId']; - $clusterAffinity = $transactionData['clusterAffinity']; - - $name = 'TestHuman_' . mt_rand(1, 100000); - $query = "CREATE (x:Human {name: '$name'})"; - $this->transaction->run($query); - - $query = "MATCH (x:Human {name: '$name'}) RETURN x"; - $results = $this->transaction->run($query); - $this->assertCount(2, $results); - - $query = "MATCH (x:Human {name: '$name'}) RETURN x"; - $results = $this->transaction->run($query); - $this->assertCount(2, $results); - - $this->transaction->commit($transactionId, $clusterAffinity); - - $results = $this->transaction->run("MATCH (x:Human {name: '$name'}) RETURN x"); - $this->assertCount(2, $results); - } - - /** - * Test case for rolling back a transaction. - */ - public function testTransactionRollback(): void - { - - $transactionData = $this->transaction->startTransaction(); - $transactionId = $transactionData['transactionId']; - $clusterAffinity = $transactionData['clusterAffinity']; - - $name = 'TestHuman_' . mt_rand(1, 100000); - $query = "CREATE (x:Human {name: '$name'})"; - $this->transaction->run($query); - - - $query = "MATCH (x:Human {name: '$name'}) RETURN x"; - $results = $this->transaction->run($query); - $this->assertCount(2, $results); - - $query = "MATCH (x:Human {name: '$name'}) RETURN x"; - $results = $this->transaction->run($query); - $this->assertCount(2, $results); - - $rollbackResponse = $this->transaction->rollback($transactionId, $clusterAffinity); - $this->assertArrayHasKey('status', $rollbackResponse); - - $results = $this->transaction->run("MATCH (x:Human {name: '$name'}) RETURN x"); - $this->assertCount(2, $results); - } -}