Search in sources :

Example 26 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class ExistingNodeStatementBuilder method build.

@Override
public Statement build() {
    final Map<String, Object> parameters = new HashMap<>();
    final StringBuilder queryBuilder = new StringBuilder();
    if (existingNodes != null && existingNodes.size() > 0) {
        Node firstNode = existingNodes.iterator().next();
        queryBuilder.append("UNWIND $rows as row MATCH (n) WHERE ID(n)=row.nodeId ");
        if (firstNode.hasVersionProperty()) {
            queryBuilder.append(OptimisticLockingUtils.getFragmentForExistingNodesAndRelationships(firstNode, "n"));
        }
        Set<String> previousDynamicLabels = firstNode.getPreviousDynamicLabels();
        for (String label : previousDynamicLabels) {
            queryBuilder.append(String.format(" REMOVE n:`%s` ", label));
        }
        queryBuilder.append(firstNode.createPropertyRemovalFragment("n"));
        queryBuilder.append("SET n");
        for (String label : firstNode.getLabels()) {
            queryBuilder.append(":`").append(label).append("`");
        }
        queryBuilder.append(" SET n += row.props RETURN row.nodeId as ref, ID(n) as id, $type as type");
        List<Map> rows = existingNodes.stream().map(node -> node.toRow("nodeId")).collect(toList());
        parameters.put("type", "node");
        parameters.put("rows", rows);
        if (firstNode.hasVersionProperty()) {
            OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), firstNode.getLabels(), firstNode.getVersion().getKey());
            return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
        }
    }
    return statementFactory.statement(queryBuilder.toString(), parameters);
}
Also used : List(java.util.List) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) CypherStatementBuilder(org.neo4j.ogm.cypher.compiler.CypherStatementBuilder) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Node(org.neo4j.ogm.model.Node) Statement(org.neo4j.ogm.request.Statement) StatementFactory(org.neo4j.ogm.request.StatementFactory) HashMap(java.util.HashMap) Node(org.neo4j.ogm.model.Node) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) Map(java.util.Map) HashMap(java.util.HashMap)

Example 27 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class NewNodeStatementBuilder method build.

@Override
public Statement build() {
    final Map<String, Object> parameters = new HashMap<>();
    final StringBuilder queryBuilder = new StringBuilder();
    if (newNodes != null && newNodes.size() > 0) {
        Node firstNode = newNodes.iterator().next();
        queryBuilder.append("UNWIND $rows as row ");
        boolean hasPrimaryIndex = firstNode.getPrimaryIndex() != null;
        if (hasPrimaryIndex) {
            queryBuilder.append("MERGE (n");
        } else {
            queryBuilder.append("CREATE (n");
        }
        for (String label : firstNode.getLabels()) {
            queryBuilder.append(":`").append(label).append("`");
        }
        if (hasPrimaryIndex) {
            String propertiesToMergeOn = Arrays.stream(firstNode.getPrimaryIndex().split(PROPERTY_SEPARATOR)).map(p -> p + ": row.props." + p).collect(joining(",", "{", "}"));
            queryBuilder.append(propertiesToMergeOn);
        }
        // Closing MERGE or CREATE
        queryBuilder.append(") ");
        if (firstNode.hasVersionProperty() && hasPrimaryIndex) {
            queryBuilder.append(getFragmentForNewOrExistingNodes(firstNode, "n"));
        }
        queryBuilder.append("SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
        List<Map> rows = newNodes.stream().map(node -> node.toRow("nodeRef")).collect(toList());
        parameters.put("type", "node");
        parameters.put("rows", rows);
        if (firstNode.hasVersionProperty()) {
            OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), firstNode.getLabels(), firstNode.getVersion().getKey());
            return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
        }
    }
    return statementFactory.statement(queryBuilder.toString(), parameters);
}
Also used : Arrays(java.util.Arrays) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) OptimisticLockingUtils(org.neo4j.ogm.cypher.compiler.builders.statement.OptimisticLockingUtils) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) NodeQueryStatements(org.neo4j.ogm.session.request.strategy.impl.NodeQueryStatements) Statement(org.neo4j.ogm.request.Statement) StatementFactory(org.neo4j.ogm.request.StatementFactory) List(java.util.List) CypherStatementBuilder(org.neo4j.ogm.cypher.compiler.CypherStatementBuilder) Map(java.util.Map) Node(org.neo4j.ogm.model.Node) HashMap(java.util.HashMap) Node(org.neo4j.ogm.model.Node) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToCreateDifferentRelationshipsToTheSameDocument.

@Test
public void shouldBeAbleToCreateDifferentRelationshipsToTheSameDocument() {
    Folder folder = new Folder();
    Document document = new Document();
    document.setFolder(folder);
    folder.getDocuments().add(document);
    folder.getArchived().add(document);
    // save folder
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.createNodesStatements();
    List<String> createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    List<String> createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(2);
    assertThat(createRelStatements.contains("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type")).isTrue();
    assertThat(createRelStatements.contains("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`ARCHIVED`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type")).isTrue();
    boolean archivedType = false;
    boolean containsType = false;
    for (Statement statement : statements) {
        if (statement.getStatement().contains("ARCHIVED")) {
            archivedType = true;
        }
        if (statement.getStatement().contains("CONTAINS")) {
            containsType = true;
        }
    }
    assertThat(archivedType).isTrue();
    assertThat(containsType).isTrue();
    // save document
    compiler = mapper.map(document).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.createNodesStatements();
    createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    statements = compiler.createRelationshipsStatements();
    createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(2);
    assertThat(createRelStatements.contains("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type")).isTrue();
    assertThat(createRelStatements.contains("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`ARCHIVED`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type")).isTrue();
    archivedType = false;
    containsType = false;
    for (Statement statement : statements) {
        if (statement.getStatement().contains("ARCHIVED")) {
            archivedType = true;
        }
        if (statement.getStatement().contains("CONTAINS")) {
            containsType = true;
        }
    }
    assertThat(archivedType).isTrue();
    assertThat(containsType).isTrue();
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) ArrayList(java.util.ArrayList) List(java.util.List) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 29 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToRemoveTheOnlyRegisteredRelationship.

@Test
public void shouldBeAbleToRemoveTheOnlyRegisteredRelationship() {
    Folder folder = new Folder();
    Document document = new Document();
    folder.getDocuments().add(document);
    document.setFolder(folder);
    folder.setId(0L);
    document.setId(1L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(document);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", document.getId(), null, Folder.class, Document.class));
    document.setFolder(null);
    folder.getDocuments().clear();
    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", document.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(document).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 30 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldSaveNewFolderWithTwoDocuments.

@Test
public void shouldSaveNewFolderWithTwoDocuments() {
    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);
    // save folder
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.createNodesStatements();
    List<String> createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("Folder")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Document")) {
            assertThat(rows).hasSize(2);
        }
    }
    statements = compiler.createRelationshipsStatements();
    List<String> createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(1);
    assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    List rows = (List) statements.get(0).getParameters().get("rows");
    assertThat(rows).hasSize(2);
    // Save doc1
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.createNodesStatements();
    createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("Folder")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Document")) {
            assertThat(rows).hasSize(2);
        }
    }
    // Save doc2
    statements = compiler.createRelationshipsStatements();
    createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(1);
    assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    rows = (List) statements.get(0).getParameters().get("rows");
    assertThat(rows).hasSize(2);
    compiler = mapper.map(doc2).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.createNodesStatements();
    createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("Folder")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Document")) {
            assertThat(rows).hasSize(2);
        }
    }
    statements = compiler.createRelationshipsStatements();
    createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(1);
    assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    rows = (List) statements.get(0).getParameters().get("rows");
    assertThat(rows).hasSize(2);
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) ArrayList(java.util.ArrayList) List(java.util.List) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Aggregations

Statement (org.neo4j.ogm.request.Statement)38 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)19 List (java.util.List)15 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)10 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)7 Map (java.util.Map)6 Document (org.neo4j.ogm.domain.filesystem.Document)6 Folder (org.neo4j.ogm.domain.filesystem.Folder)6 RowModel (org.neo4j.ogm.model.RowModel)6 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)5 Teacher (org.neo4j.ogm.domain.education.Teacher)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 DefaultRowModelRequest (org.neo4j.ogm.cypher.query.DefaultRowModelRequest)3 Forum (org.neo4j.ogm.domain.forum.Forum)3 ForumTopicLink (org.neo4j.ogm.domain.forum.ForumTopicLink)3 Topic (org.neo4j.ogm.domain.forum.Topic)3 Node (org.neo4j.ogm.model.Node)3 Arrays (java.util.Arrays)2