Search in sources :

Example 16 with Statement

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

the class CompilerTest method createSimpleRelationshipWithIllegalCharactersBetweenObjects.

// DATAGRAPH-589
@Test
public void createSimpleRelationshipWithIllegalCharactersBetweenObjects() {
    Artist theBeatles = new Artist("The Beatles");
    Album please = new Album("Please Please Me");
    theBeatles.getAlbums().add(please);
    please.setArtist(theBeatles);
    Compiler compiler = mapAndCompile(theBeatles, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`l'artiste`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`l'album`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`HAS-ALBUM`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) Statement(org.neo4j.ogm.request.Statement) Album(org.neo4j.ogm.domain.music.Album) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 17 with Statement

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

the class CompilerTest method createIncomingRelationWhenSpecified.

// DATAGRAPH-594
@Test
public void createIncomingRelationWhenSpecified() {
    Mortal adam = new Mortal("Adam");
    Mortal vince = new Mortal("Vince");
    adam.getKnownBy().add(vince);
    Compiler compiler = mapAndCompile(adam, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Mortal`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(2);
    }
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`KNOWN_BY`]->(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(1);
    Map row = (Map) rows.get(0);
    assertThat(row.get("startNodeId")).isEqualTo(mappingContext.nativeId(vince));
    assertThat(row.get("endNodeId")).isEqualTo(mappingContext.nativeId(adam));
}
Also used : Statement(org.neo4j.ogm.request.Statement) Mortal(org.neo4j.ogm.domain.social.Mortal) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 18 with Statement

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

the class CompilerTest method shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep.

@Test
public void shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep() {
    Student sheila = new Student();
    sheila.setName("Sheila Smythe");
    Student gary = new Student();
    gary.setName("Gary Jones");
    Student winston = new Student();
    winston.setName("Winston Charles");
    Course physics = new Course();
    physics.setName("GCSE Physics");
    physics.setStudents(Arrays.asList(gary, sheila));
    Course maths = new Course();
    maths.setName("A-Level Mathematics");
    maths.setStudents(Arrays.asList(sheila, winston));
    Teacher teacher = new Teacher();
    teacher.setName("Mrs Kapoor");
    teacher.setCourses(Arrays.asList(physics, maths));
    // Save teacher
    Compiler compiler = mapAndCompile(teacher, -1);
    List<Statement> createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`Course`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`DomainObject`:`Student`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("Teacher")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Student")) {
            assertThat(rows).hasSize(3);
        }
        if (statement.getStatement().contains("Course")) {
            assertThat(rows).hasSize(2);
        }
    }
    List<Statement> createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`COURSES`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`STUDENTS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : createRelsStatements) {
        List rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("STUDENTS")) {
            assertThat(rows).hasSize(4);
        }
        if (statement.getStatement().contains("COURSES")) {
            assertThat(rows).hasSize(2);
        }
    }
}
Also used : Statement(org.neo4j.ogm.request.Statement) Teacher(org.neo4j.ogm.domain.education.Teacher) ArrayList(java.util.ArrayList) List(java.util.List) Student(org.neo4j.ogm.domain.education.Student) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 19 with Statement

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

the class RequestExecutor method executeStatements.

private void executeStatements(CompileContext context, List<ReferenceMapping> entityReferenceMappings, List<ReferenceMapping> relReferenceMappings, List<Statement> statements) {
    if (statements.size() > 0) {
        List<Statement> noCheckStatements = new ArrayList<>();
        for (Statement statement : statements) {
            if (statement.optimisticLockingConfig().isPresent()) {
                DefaultRequest request = new DefaultRequest(statement);
                try (Response<RowModel> response = session.requestHandler().execute(request)) {
                    List<RowModel> rowModels = response.toList();
                    session.optimisticLockingChecker().checkResultsCount(rowModels, statement);
                    registerEntityIds(context, rowModels, entityReferenceMappings, relReferenceMappings);
                }
            } else {
                noCheckStatements.add(statement);
            }
        }
        DefaultRequest defaultRequest = new DefaultRequest();
        defaultRequest.setStatements(noCheckStatements);
        try (Response<RowModel> response = session.requestHandler().execute(defaultRequest)) {
            registerEntityIds(context, response.toList(), entityReferenceMappings, relReferenceMappings);
        }
    }
}
Also used : Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) RowModel(org.neo4j.ogm.model.RowModel)

Example 20 with Statement

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

the class EmbeddedRequest method execute.

@Override
public Response<RowModel> execute(DefaultRequest query) {
    // TODO this is a hack to get the embedded driver to work with executing multiple statements
    final List<RowModel> rowmodels = new ArrayList<>();
    String[] columns = null;
    for (Statement statement : query.getStatements()) {
        Result result = executeRequest(statement);
        if (columns == null) {
            columns = result.columns().toArray(new String[result.columns().size()]);
        }
        RowModelResponse rowModelResponse = new RowModelResponse(result, entityAdapter);
        RowModel model;
        while ((model = rowModelResponse.next()) != null) {
            rowmodels.add(model);
        }
        result.close();
    }
    final String[] finalColumns = columns;
    return new Response<>() {

        int currentRow = 0;

        @Override
        public RowModel next() {
            if (currentRow < rowmodels.size()) {
                return rowmodels.get(currentRow++);
            }
            return null;
        }

        @Override
        public void close() {
        }

        @Override
        public String[] columns() {
            return finalColumns;
        }
    };
}
Also used : Response(org.neo4j.ogm.response.Response) EmptyResponse(org.neo4j.ogm.response.EmptyResponse) GraphRowModelResponse(org.neo4j.ogm.drivers.embedded.response.GraphRowModelResponse) GraphModelResponse(org.neo4j.ogm.drivers.embedded.response.GraphModelResponse) RowModelResponse(org.neo4j.ogm.drivers.embedded.response.RowModelResponse) RestModelResponse(org.neo4j.ogm.drivers.embedded.response.RestModelResponse) GraphRowModelResponse(org.neo4j.ogm.drivers.embedded.response.GraphRowModelResponse) RowModelResponse(org.neo4j.ogm.drivers.embedded.response.RowModelResponse) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) RowModel(org.neo4j.ogm.model.RowModel) Result(org.neo4j.graphdb.Result)

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