Search in sources :

Example 1 with RowModel

use of org.neo4j.ogm.model.RowModel in project neo4j-ogm by neo4j.

the class BoltRequest method execute.

@Override
public Response<RowModel> execute(DefaultRequest query) {
    final List<RowModel> rowModels = new ArrayList<>();
    String[] columns = null;
    for (Statement statement : query.getStatements()) {
        Result result = executeRequest(statement);
        if (columns == null) {
            try {
                List<String> columnSet = result.keys();
                columns = columnSet.toArray(new String[columnSet.size()]);
            } catch (ClientException e) {
                throw new CypherException(e.code(), e.getMessage(), e);
            }
        }
        try (RowModelResponse rowModelResponse = new RowModelResponse(result, entityAdapter)) {
            RowModel model;
            while ((model = rowModelResponse.next()) != null) {
                rowModels.add(model);
            }
            result.consume();
        } catch (ClientException e) {
            throw new CypherException(e.code(), e.getMessage(), e);
        }
    }
    return new MultiStatementBasedResponse(columns, rowModels);
}
Also used : RowModelResponse(org.neo4j.ogm.drivers.bolt.response.RowModelResponse) GraphRowModelResponse(org.neo4j.ogm.drivers.bolt.response.GraphRowModelResponse) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) RowModel(org.neo4j.ogm.model.RowModel) ClientException(org.neo4j.driver.exceptions.ClientException) CypherException(org.neo4j.ogm.exception.CypherException) Result(org.neo4j.driver.Result)

Example 2 with RowModel

use of org.neo4j.ogm.model.RowModel in project neo4j-ogm by neo4j.

the class ExecuteQueriesDelegate method mapScalarResponse.

private static <T> Iterable<T> mapScalarResponse(Class<T> type, Response<RowModel> response) {
    // We need to execute the request in any case, but can skip processing the result when
    // it's not assignable to the requested type.
    Collection<T> result;
    if (VOID_TYPES.contains(type)) {
        result = Collections.emptyList();
    } else {
        result = new ArrayList<>();
        RowModel model;
        while ((model = response.next()) != null) {
            result.add(extractColumnValue(type, model));
        }
    }
    return result;
}
Also used : RowModel(org.neo4j.ogm.model.RowModel)

Example 3 with RowModel

use of org.neo4j.ogm.model.RowModel in project neo4j-ogm by neo4j.

the class JsonRowResponseTest method shouldParseDataInCreateRowResponseCorrectly.

@Test
public void shouldParseDataInCreateRowResponseCorrectly() throws IOException {
    when(entity.getContent()).thenReturn(createRowResults());
    try (Response<RowModel> rsp = new RowModelResponse(response)) {
        RowModel rowModel = rsp.next();
        assertThat(rowModel).isNotNull();
        Object[] rows = rowModel.getValues();
        assertThat(rows.length).isEqualTo(4);
        assertThat(rows[0]).isEqualTo(388L);
        assertThat(rows[1]).isEqualTo(527L);
        assertThat(rows[2]).isEqualTo(389L);
        assertThat(rows[3]).isEqualTo(528L);
    }
}
Also used : RowModel(org.neo4j.ogm.model.RowModel) Test(org.junit.Test)

Example 4 with RowModel

use of org.neo4j.ogm.model.RowModel in project neo4j-ogm by neo4j.

the class JsonRowResponseTest method shouldParseDataInRowResponseCorrectly.

@Test
public void shouldParseDataInRowResponseCorrectly() throws IOException {
    when(entity.getContent()).thenReturn(rowResultsAndNoErrors());
    try (Response<RowModel> rsp = new RowModelResponse(response)) {
        RowModel rowModel = rsp.next();
        assertThat(rowModel).isNotNull();
        Object[] rows = rowModel.getValues();
        assertThat(rows.length).isEqualTo(1);
        List<List<Map>> data = (List<List<Map>>) rows[0];
        assertThat(data.get(0).get(0).get("name")).isEqualTo("My Test");
    }
}
Also used : RowModel(org.neo4j.ogm.model.RowModel) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 5 with RowModel

use of org.neo4j.ogm.model.RowModel in project neo4j-ogm by neo4j.

the class RequestExecutor method registerEntityIds.

/**
 * Register ids of nodes created or updated back into the compile context. New identities are required for use
 * in other parts of the query that depend upon these new entities. New relationships, for example, may require
 * the IDs of nodes created in the same request. Existing entity ids are registered because they
 * will need to be updated in the mapping context
 * Note that the mapping context is not updated at this point.
 *
 * @param context           the compile context
 * @param response          query response
 * @param entityRefMappings mapping of entity reference used in the compile context and the entity id from the database
 */
private void registerEntityIds(CompileContext context, List<RowModel> response, List<ReferenceMapping> entityRefMappings, List<ReferenceMapping> relEntityRefMappings) {
    for (RowModel rowModel : response) {
        Object[] results = rowModel.getValues();
        String[] variables = rowModel.variables();
        Long entityRef = null;
        Long entityId = null;
        String type = null;
        for (int i = 0; i < variables.length; i++) {
            if (variables[i].equals("id")) {
                entityId = ((Number) results[i]).longValue();
            }
            if (variables[i].equals("ref")) {
                entityRef = ((Number) results[i]).longValue();
            }
            if (variables[i].equals("type")) {
                type = (String) results[i];
            }
        }
        if (type != null && type.equals("node")) {
            entityRefMappings.add(new ReferenceMapping(entityRef, entityId));
            if (entityRef != null && entityRef.equals(entityId)) {
                LOGGER.debug("to update: nodeEntity {}:{}", entityRef, entityId);
            } else {
                LOGGER.debug("to create: nodeEntity {}:{}", entityRef, entityId);
                context.registerNewId(entityRef, entityId);
            }
        } else if (type != null && type.equals("rel")) {
            relEntityRefMappings.add(new ReferenceMapping(entityRef, entityId));
            if (entityRef != null && entityRef.equals(entityId)) {
                LOGGER.debug("to (maybe) update: relEntity {}:{}", entityRef, entityId);
            } else {
                LOGGER.debug("to (maybe) create: relEntity {}:{}", entityRef, entityId);
                context.registerNewId(entityRef, entityId);
            }
        }
    }
}
Also used : RowModel(org.neo4j.ogm.model.RowModel)

Aggregations

RowModel (org.neo4j.ogm.model.RowModel)13 Statement (org.neo4j.ogm.request.Statement)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 List (java.util.List)3 Test (org.junit.Test)3 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)3 CypherQuery (org.neo4j.ogm.cypher.query.CypherQuery)2 DefaultRowModelRequest (org.neo4j.ogm.cypher.query.DefaultRowModelRequest)2 RowModelRequest (org.neo4j.ogm.request.RowModelRequest)2 Response (org.neo4j.ogm.response.Response)2 PersistenceEvent (org.neo4j.ogm.session.event.PersistenceEvent)2 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Result (org.neo4j.driver.Result)1 ClientException (org.neo4j.driver.exceptions.ClientException)1