Search in sources :

Example 1 with Compiler

use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.

the class EntityGraphMapperTest method doNothingIfNothingHasChanged.

@Test
public void doNothingIfNothingHasChanged() {
    Long existingNodeId = (Long) session.query("CREATE (s:Student:DomainObject {name:'Sheila Smythe'}) RETURN id(s) AS id", emptyMap()).queryResults().iterator().next().get("id");
    Student sheila = new Student();
    sheila.setId(existingNodeId);
    sheila.setName("Sheila Smythe");
    mappingContext.addNodeEntity(sheila);
    session.clear();
    Compiler compiler = this.mapper.map(sheila).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    Statements cypher = new Statements(compiler.getAllStatements());
    assertThat(cypher.getStatements()).isEmpty();
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Statements(org.neo4j.ogm.request.Statements) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Example 2 with Compiler

use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.

the class RequestExecutor method executeSave.

/**
 * Execute a save request.
 * Decides how the request is split depending upon characteristics of what is to be saved.
 * Processes the response(s) and updates the mapping context.
 *
 * @param context the CompileContext for this request
 */
public void executeSave(CompileContext context) {
    Compiler compiler = context.getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<ReferenceMapping> entityReferenceMappings = new ArrayList<>();
    List<ReferenceMapping> relReferenceMappings = new ArrayList<>();
    boolean forceTx = compiler.updateNodesStatements().stream().anyMatch(st -> st.optimisticLockingConfig().isPresent()) || compiler.updateRelationshipStatements().stream().anyMatch(st -> st.optimisticLockingConfig().isPresent());
    session.doInTransaction(() -> {
        // we must create the new nodes first, and then use their node IDs when creating relationships between them
        if (compiler.hasStatementsDependentOnNewNodes()) {
            // execute the statements to create new nodes. The ids will be returned
            // and will be used in subsequent statements that refer to these new nodes.
            executeStatements(context, entityReferenceMappings, relReferenceMappings, compiler.createNodesStatements());
            List<Statement> statements = new ArrayList<>();
            statements.addAll(compiler.createRelationshipsStatements());
            statements.addAll(compiler.updateNodesStatements());
            statements.addAll(compiler.updateRelationshipStatements());
            statements.addAll(compiler.deleteRelationshipStatements());
            statements.addAll(compiler.deleteRelationshipEntityStatements());
            executeStatements(context, entityReferenceMappings, relReferenceMappings, statements);
        } else {
            // only update / delete statements
            List<Statement> statements = compiler.getAllStatements();
            executeStatements(context, entityReferenceMappings, relReferenceMappings, statements);
        }
    }, forceTx, Transaction.Type.READ_WRITE);
    // Update the mapping context now that the request is successful
    updateNodeEntities(context, entityReferenceMappings);
    updateRelationshipEntities(context, relReferenceMappings);
    updateRelationships(context, relReferenceMappings);
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RelationshipEntity(org.neo4j.ogm.annotation.RelationshipEntity) AbstractTransaction(org.neo4j.ogm.transaction.AbstractTransaction) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Response(org.neo4j.ogm.response.Response) HashMap(java.util.HashMap) CompileContext(org.neo4j.ogm.cypher.compiler.CompileContext) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappingContext(org.neo4j.ogm.context.MappingContext) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) List(java.util.List) RowModel(org.neo4j.ogm.model.RowModel) Map(java.util.Map) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) Transaction(org.neo4j.ogm.transaction.Transaction) EntityUtils(org.neo4j.ogm.utils.EntityUtils) TransientRelationship(org.neo4j.ogm.context.TransientRelationship) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList)

Example 3 with Compiler

use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToRemoveAnyRegisteredRelationship.

@Test
public void shouldBeAbleToRemoveAnyRegisteredRelationship() {
    // given
    Folder folder = new Folder();
    Document doc1 = new Document();
    Document doc2 = new Document();
    folder.getDocuments().add(doc1);
    folder.getDocuments().add(doc2);
    doc1.setFolder(folder);
    doc2.setFolder(folder);
    folder.setId(0L);
    doc1.setId(1L);
    doc2.setId(2L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(doc1);
    mappingContext.addNodeEntity(doc2);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    // when
    doc2.setFolder(null);
    folder.getDocuments().remove(doc2);
    // then
    assertThat(folder.getDocuments()).hasSize(1);
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc2).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 4 with Compiler

use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.

the class MergeWithPrimaryIndexTests method newNodeUsesGraphIdWhenPrimaryIndexNotPresent.

@Test
public void newNodeUsesGraphIdWhenPrimaryIndexNotPresent() {
    Pizza pizza = new Pizza("Plain");
    assertThat(pizza.getId()).isNull();
    Compiler compiler = mapAndCompile(pizza);
    assertThat(compiler.hasStatementsDependentOnNewNodes()).isFalse();
    assertThat(compiler.createNodesStatements().get(0).getStatement()).isEqualTo("UNWIND $rows as row CREATE (n:`Pizza`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
}
Also used : Pizza(org.neo4j.ogm.domain.pizza.Pizza) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Test(org.junit.Test)

Example 5 with Compiler

use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.

the class MergeWithPrimaryIndexTests method newNodeUsesPrimaryIndexWhenPresent.

@Test
public void newNodeUsesPrimaryIndexWhenPresent() {
    User newUser = new User("bachmania", "Michal Bachman", "password");
    Compiler compiler = mapAndCompile(newUser);
    assertThat(compiler.hasStatementsDependentOnNewNodes()).isFalse();
    assertThat(compiler.createNodesStatements().get(0).getStatement()).isEqualTo("UNWIND $rows as row MERGE (n:`User`{login: row.props.login}) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) User(org.neo4j.ogm.domain.cineasts.annotated.User) Test(org.junit.Test)

Aggregations

Compiler (org.neo4j.ogm.cypher.compiler.Compiler)12 Test (org.junit.Test)10 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)9 Document (org.neo4j.ogm.domain.filesystem.Document)7 Folder (org.neo4j.ogm.domain.filesystem.Folder)7 Statement (org.neo4j.ogm.request.Statement)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)4 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RelationshipEntity (org.neo4j.ogm.annotation.RelationshipEntity)1 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)1 MappingContext (org.neo4j.ogm.context.MappingContext)1 TransientRelationship (org.neo4j.ogm.context.TransientRelationship)1 User (org.neo4j.ogm.domain.cineasts.annotated.User)1 Student (org.neo4j.ogm.domain.education.Student)1 Pizza (org.neo4j.ogm.domain.pizza.Pizza)1 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)1