Search in sources :

Example 6 with CypherQuery

use of org.neo4j.ogm.cypher.query.CypherQuery in project neo4j-ogm by neo4j.

the class DeleteDelegate method delete.

public <T> Object delete(Class<T> clazz, Iterable<Filter> filters, boolean listResults) {
    ClassInfo classInfo = session.metaData().classInfo(clazz.getSimpleName());
    if (classInfo != null) {
        resolvePropertyAnnotations(clazz, filters);
        CypherQuery query;
        if (classInfo.isRelationshipEntity()) {
            query = new RelationshipDeleteStatements().deleteAndList(classInfo.neo4jName(), filters);
        } else {
            query = new NodeDeleteStatements().deleteAndList(classInfo.neo4jName(), filters);
        }
        if (listResults) {
            return list(query, classInfo.isRelationshipEntity());
        }
        return count(query, classInfo.isRelationshipEntity());
    }
    throw new RuntimeException(clazz.getName() + " is not a persistable class");
}
Also used : CypherQuery(org.neo4j.ogm.cypher.query.CypherQuery) RelationshipDeleteStatements(org.neo4j.ogm.session.request.strategy.impl.RelationshipDeleteStatements) NodeDeleteStatements(org.neo4j.ogm.session.request.strategy.impl.NodeDeleteStatements) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 7 with CypherQuery

use of org.neo4j.ogm.cypher.query.CypherQuery in project neo4j-ogm by neo4j.

the class ExecuteQueriesDelegate method countEntitiesOfType.

public long countEntitiesOfType(Class<?> entity) {
    ClassInfo classInfo = session.metaData().classInfo(entity.getName());
    if (classInfo == null) {
        return 0;
    }
    CypherQuery countStatement;
    if (classInfo.isRelationshipEntity()) {
        ClassInfo startNodeInfo = null;
        ClassInfo endNodeInfo = null;
        for (FieldInfo fieldInfo : classInfo.fieldsInfo().fields()) {
            if (fieldInfo.hasAnnotation(StartNode.class)) {
                startNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
            } else if (fieldInfo.hasAnnotation(EndNode.class)) {
                endNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
            }
            if (endNodeInfo != null && startNodeInfo != null) {
                break;
            }
        }
        String start = startNodeInfo.neo4jName();
        String end = endNodeInfo.neo4jName();
        String type = classInfo.neo4jName();
        countStatement = new CountStatements().countEdges(start, type, end);
    } else {
        Collection<String> labels = classInfo.staticLabels();
        if (labels.isEmpty()) {
            return 0;
        }
        countStatement = new CountStatements().countNodes(labels);
    }
    return session.doInTransaction(() -> {
        try (Response<RowModel> response = session.requestHandler().execute((RowModelRequest) countStatement)) {
            RowModel queryResult = response.next();
            return queryResult == null ? 0 : ((Number) queryResult.getValues()[0]).longValue();
        }
    }, Transaction.Type.READ_ONLY);
}
Also used : EndNode(org.neo4j.ogm.annotation.EndNode) RowModel(org.neo4j.ogm.model.RowModel) CypherQuery(org.neo4j.ogm.cypher.query.CypherQuery) CountStatements(org.neo4j.ogm.session.request.strategy.impl.CountStatements) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 8 with CypherQuery

use of org.neo4j.ogm.cypher.query.CypherQuery in project neo4j-ogm by neo4j.

the class CountStatementsTest method testCountEdgesWithTypeAndFilters.

@Test
public void testCountEdgesWithTypeAndFilters() throws Exception {
    CypherQuery query = statements.countEdges("INFLUENCE", new Filters().add(new Filter("score", ComparisonOperator.EQUALS, -12.2)));
    assertThat(query.getStatement()).isEqualTo("MATCH (n)-[r0:`INFLUENCE`]->(m) WHERE r0.`score` = $`score_0`  RETURN COUNT(r0)");
    assertThat(query.getParameters().toString()).isEqualTo("{score_0=-12.2}");
}
Also used : Filters(org.neo4j.ogm.cypher.Filters) Filter(org.neo4j.ogm.cypher.Filter) CypherQuery(org.neo4j.ogm.cypher.query.CypherQuery) Test(org.junit.Test)

Example 9 with CypherQuery

use of org.neo4j.ogm.cypher.query.CypherQuery in project neo4j-ogm by neo4j.

the class NodeDeleteStatementsTest method testDeleteWithLabelAndFilters.

@Test
public void testDeleteWithLabelAndFilters() {
    CypherQuery query = statements.delete("INFLUENCE", new Filters().add(new Filter("score", ComparisonOperator.EQUALS, -12.2)));
    assertThat(query.getStatement()).isEqualTo("MATCH (n:`INFLUENCE`) " + "WHERE n.`score` = $`score_0` " + "WITH n " + "OPTIONAL MATCH (n)-[r0]-() " + "DELETE r0, n");
}
Also used : Filters(org.neo4j.ogm.cypher.Filters) Filter(org.neo4j.ogm.cypher.Filter) CypherQuery(org.neo4j.ogm.cypher.query.CypherQuery) Test(org.junit.Test)

Aggregations

CypherQuery (org.neo4j.ogm.cypher.query.CypherQuery)9 Test (org.junit.Test)6 Filter (org.neo4j.ogm.cypher.Filter)6 Filters (org.neo4j.ogm.cypher.Filters)6 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)3 CountStatements (org.neo4j.ogm.session.request.strategy.impl.CountStatements)2 EndNode (org.neo4j.ogm.annotation.EndNode)1 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)1 RowModel (org.neo4j.ogm.model.RowModel)1 NodeDeleteStatements (org.neo4j.ogm.session.request.strategy.impl.NodeDeleteStatements)1 RelationshipDeleteStatements (org.neo4j.ogm.session.request.strategy.impl.RelationshipDeleteStatements)1