Search in sources :

Example 1 with TypeMapper

use of io.micronaut.data.runtime.mapper.TypeMapper in project micronaut-data by micronaut-projects.

the class DefaultJdbcRepositoryOperations method findOne.

@Nullable
@Override
public <T, R> R findOne(@NonNull PreparedQuery<T, R> preparedQuery) {
    return executeRead(connection -> {
        RuntimePersistentEntity<T> persistentEntity = getEntity(preparedQuery.getRootEntity());
        try (PreparedStatement ps = prepareStatement(connection, connection::prepareStatement, preparedQuery, false, true)) {
            try (ResultSet rs = ps.executeQuery()) {
                Class<R> resultType = preparedQuery.getResultType();
                if (preparedQuery.getResultDataType() == DataType.ENTITY) {
                    RuntimePersistentEntity<R> resultPersistentEntity = getEntity(resultType);
                    final Set<JoinPath> joinFetchPaths = preparedQuery.getJoinFetchPaths();
                    SqlResultEntityTypeMapper<ResultSet, R> mapper = new SqlResultEntityTypeMapper<>(resultPersistentEntity, columnNameResultSetReader, joinFetchPaths, jsonCodec, (loadedEntity, o) -> {
                        if (loadedEntity.hasPostLoadEventListeners()) {
                            return triggerPostLoad(o, loadedEntity, preparedQuery.getAnnotationMetadata());
                        } else {
                            return o;
                        }
                    }, conversionService);
                    SqlResultEntityTypeMapper.PushingMapper<ResultSet, R> oneMapper = mapper.readOneWithJoins();
                    if (rs.next()) {
                        oneMapper.processRow(rs);
                    }
                    while (!joinFetchPaths.isEmpty() && rs.next()) {
                        oneMapper.processRow(rs);
                    }
                    R result = oneMapper.getResult();
                    if (preparedQuery.hasResultConsumer()) {
                        preparedQuery.getParameterInRole(SqlResultConsumer.ROLE, SqlResultConsumer.class).ifPresent(consumer -> consumer.accept(result, newMappingContext(rs)));
                    }
                    return result;
                } else if (rs.next()) {
                    if (preparedQuery.isDtoProjection()) {
                        TypeMapper<ResultSet, R> introspectedDataMapper = new DTOMapper<>(persistentEntity, columnNameResultSetReader, jsonCodec, conversionService);
                        return introspectedDataMapper.map(rs, resultType);
                    } else {
                        Object v = columnIndexResultSetReader.readDynamic(rs, 1, preparedQuery.getResultDataType());
                        if (v == null) {
                            return null;
                        } else if (resultType.isInstance(v)) {
                            return (R) v;
                        } else {
                            return columnIndexResultSetReader.convertRequired(v, resultType);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            throw new DataAccessException("Error executing SQL Query: " + e.getMessage(), e);
        }
        return null;
    });
}
Also used : TypeMapper(io.micronaut.data.runtime.mapper.TypeMapper) SqlTypeMapper(io.micronaut.data.runtime.mapper.sql.SqlTypeMapper) SqlResultEntityTypeMapper(io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper) JoinPath(io.micronaut.data.model.query.JoinPath) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SqlResultConsumer(io.micronaut.data.jdbc.mapper.SqlResultConsumer) ResultSet(java.sql.ResultSet) SqlResultEntityTypeMapper(io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper) DataAccessException(io.micronaut.data.exceptions.DataAccessException) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

Nullable (io.micronaut.core.annotation.Nullable)1 DataAccessException (io.micronaut.data.exceptions.DataAccessException)1 SqlResultConsumer (io.micronaut.data.jdbc.mapper.SqlResultConsumer)1 JoinPath (io.micronaut.data.model.query.JoinPath)1 TypeMapper (io.micronaut.data.runtime.mapper.TypeMapper)1 SqlResultEntityTypeMapper (io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper)1 SqlTypeMapper (io.micronaut.data.runtime.mapper.sql.SqlTypeMapper)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1