Skip to content

Commit 8044995

Browse files
committed
feat: add count method to DefaultNeo4JDatabaseManager
Implemented a count method for executing count queries on entities using the Neo4J database, enhancing query capabilities and ensuring proper error handling. Signed-off-by: Maximillian Arruda <[email protected]>
1 parent c28209e commit 8044995

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

jnosql-neo4j/src/main/java/org/eclipse/jnosql/databases/neo4j/communication/DefaultNeo4JDatabaseManager.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ public Stream<CommunicationEntity> select(SelectQuery query) {
149149
}
150150
}
151151

152+
public long count(SelectQuery query) {
153+
Objects.requireNonNull(query, "query is required");
154+
Map<String, Object> parameters = new HashMap<>();
155+
String cypher = Neo4JQueryBuilder.INSTANCE.buildCountQuery(query, parameters);
156+
157+
LOGGER.fine("Executing Cypher Query for counting entities: " + cypher);
158+
try (Transaction tx = session.beginTransaction()) {
159+
long count = tx.run(cypher, Values.parameters(flattenMap(parameters)))
160+
.single()
161+
.get("count")
162+
.asLong();
163+
tx.commit();
164+
return count;
165+
} catch (Exception e) {
166+
LOGGER.severe("Error executing count query: " + e.getMessage());
167+
throw new CommunicationException("Error executing count query", e);
168+
}
169+
}
170+
152171
@Override
153172
public long count(String entity) {
154173
Objects.requireNonNull(entity, "entity is required");

0 commit comments

Comments
 (0)