Search in sources :

Example 6 with DefaultRowModelRequest

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

the class NodeDeleteStatements method delete.

@Override
public CypherQuery delete(Long id, Object object, ClassInfo classInfo) {
    FieldInfo versionField = classInfo.getVersionField();
    Long version = (Long) versionField.read(object);
    OptimisticLockingConfig optimisticLockingConfig = new OptimisticLockingConfig(1, classInfo.staticLabels().toArray(new String[] {}), versionField.property());
    Map<String, Object> params = new HashMap<>();
    params.put("id", id);
    params.put("version", version);
    params.put("type", "node");
    return new DefaultRowModelRequest("MATCH (n) " + "  WHERE id(n) = $id AND n.`" + versionField.property() + "` = $version " + "SET " + " n.`" + versionField.property() + "` = n.`" + versionField.property() + "` + 1 " + "WITH n " + " WHERE n.`" + versionField.property() + "` = $version + 1 " + "OPTIONAL MATCH (n)-[r0]-() " + "DELETE r0, n " + // Use DISTINCT because node may have multiple relationships
    "RETURN DISTINCT id(n) AS id", params, optimisticLockingConfig);
}
Also used : HashMap(java.util.HashMap) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 7 with DefaultRowModelRequest

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

the class NodeDeleteStatements method delete.

@Override
public CypherQuery delete(String label, Iterable<Filter> filters) {
    FilteredQuery query = FilteredQueryBuilder.buildNodeQuery(label, filters);
    query.setReturnClause(" OPTIONAL MATCH (n)-[r0]-() DELETE r0, n");
    return new DefaultRowModelRequest(query.statement(), query.parameters());
}
Also used : FilteredQuery(org.neo4j.ogm.session.request.FilteredQuery) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 8 with DefaultRowModelRequest

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

the class NodeDeleteStatements method deleteAndList.

@Override
public CypherQuery deleteAndList(String label, Iterable<Filter> filters) {
    FilteredQuery query = FilteredQueryBuilder.buildNodeQuery(label, filters);
    query.setReturnClause(" OPTIONAL MATCH (n)-[r0]-() DELETE r0, n RETURN ID(n)");
    return new DefaultRowModelRequest(query.statement(), query.parameters());
}
Also used : FilteredQuery(org.neo4j.ogm.session.request.FilteredQuery) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 9 with DefaultRowModelRequest

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

the class RelationshipDeleteStatements method deleteAndList.

@Override
public CypherQuery deleteAndList(String type, Iterable<Filter> filters) {
    FilteredQuery query = FilteredQueryBuilder.buildRelationshipQuery(type, filters);
    query.setReturnClause(" DELETE r0 RETURN ID(r0)");
    return new DefaultRowModelRequest(query.statement(), query.parameters());
}
Also used : FilteredQuery(org.neo4j.ogm.session.request.FilteredQuery) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 10 with DefaultRowModelRequest

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

the class DeleteDelegate method deleteAll.

/**
 * Deletes all nodes of a given type. They will get discovered by using the matching label for that type.
 * To avoid a delete of every node in the database the method will abort the delete operation if no label
 * can be determined.
 *
 * @param type  The type of the nodes/objects to be deleted.
 * @param <T>   The type to work with
 */
public <T> void deleteAll(Class<T> type) {
    ClassInfo classInfo = session.metaData().classInfo(type.getName());
    if (classInfo != null) {
        String entityLabel = classInfo.neo4jName();
        if (entityLabel == null) {
            session.warn("Unable to find database label for entity " + type.getName() + " : no results will be returned. Make sure the class is registered, " + "and not abstract without @NodeEntity annotation");
            return;
        }
        Statement request = getDeleteStatementsBasedOnType(type).delete(entityLabel);
        RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
        session.notifyListeners(new PersistenceEvent(type, Event.TYPE.PRE_DELETE));
        session.doInTransaction(() -> {
            try (Response<RowModel> response = session.requestHandler().execute(query)) {
                session.context().removeType(type);
                if (session.eventsEnabled()) {
                    session.notifyListeners(new PersistenceEvent(type, Event.TYPE.POST_DELETE));
                }
            }
        }, Transaction.Type.READ_WRITE);
    } else {
        session.warn(type.getName() + " is not a persistable class");
    }
}
Also used : Statement(org.neo4j.ogm.request.Statement) PersistenceEvent(org.neo4j.ogm.session.event.PersistenceEvent) RowModelRequest(org.neo4j.ogm.request.RowModelRequest) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest) RowModel(org.neo4j.ogm.model.RowModel) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Aggregations

DefaultRowModelRequest (org.neo4j.ogm.cypher.query.DefaultRowModelRequest)11 FilteredQuery (org.neo4j.ogm.session.request.FilteredQuery)6 RowModelRequest (org.neo4j.ogm.request.RowModelRequest)3 Statement (org.neo4j.ogm.request.Statement)3 HashMap (java.util.HashMap)2 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)2 RowModel (org.neo4j.ogm.model.RowModel)2 OptimisticLockingConfig (org.neo4j.ogm.request.OptimisticLockingConfig)2 PersistenceEvent (org.neo4j.ogm.session.event.PersistenceEvent)2 NodeDeleteStatements (org.neo4j.ogm.session.request.strategy.impl.NodeDeleteStatements)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 MappingContext (org.neo4j.ogm.context.MappingContext)1