Search in sources :

Example 1 with PojoBuilder

use of org.jdbi.v3.core.mapper.reflect.internal.PojoProperties.PojoBuilder in project jdbi by jdbi.

the class PojoMapper method specialize0.

private Optional<RowMapper<T>> specialize0(StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) {
    final List<PropertyData<T>> propList = new ArrayList<>();
    for (PojoProperty<T> property : getProperties(ctx.getConfig()).getProperties().values()) {
        Nested anno = property.getAnnotation(Nested.class).orElse(null);
        if (property.getAnnotation(Unmappable.class).map(Unmappable::value).orElse(false)) {
            continue;
        }
        if (anno == null) {
            String paramName = prefix + getName(property);
            findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(property)).ifPresent(index -> {
                @SuppressWarnings({ "unchecked", "rawtypes" }) ColumnMapper<?> mapper = ctx.findColumnMapperFor(property.getQualifiedType().mapType(GenericTypes::box)).orElseGet(() -> (ColumnMapper) defaultColumnMapper(property));
                propList.add(new PropertyData<>(property, new SingleColumnMapper<>(mapper, index + 1)));
                unmatchedColumns.remove(columnNames.get(index));
            });
        } else {
            String nestedPrefix = prefix + anno.value();
            if (anyColumnsStartWithPrefix(columnNames, nestedPrefix, columnNameMatchers)) {
                nestedMappers.computeIfAbsent(property, d -> createNestedMapper(ctx, d, nestedPrefix)).specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns).ifPresent(nestedMapper -> propList.add(new PropertyData<>(property, nestedMapper)));
            }
        }
    }
    if (propList.isEmpty() && !columnNames.isEmpty()) {
        return Optional.empty();
    }
    propList.sort(Comparator.comparing(p -> p.propagateNull ? 1 : 0));
    final Optional<String> nullMarkerColumn = Optional.ofNullable(GenericTypes.getErasedType(type).getAnnotation(PropagateNull.class)).map(PropagateNull::value);
    return Optional.of((r, c) -> {
        if (propagateNull(r, nullMarkerColumn)) {
            return null;
        }
        final PojoBuilder<T> pojo = getProperties(c.getConfig()).create();
        for (PropertyData<T> p : propList) {
            Object value = p.mapper.map(r, ctx);
            if (p.propagateNull && (value == null || (p.isPrimitive && r.wasNull()))) {
                return null;
            }
            if (value != null) {
                pojo.set(p.property, value);
            }
        }
        return pojo.build();
    });
}
Also used : PojoProperty(org.jdbi.v3.core.mapper.reflect.internal.PojoProperties.PojoProperty) PropagateNull(org.jdbi.v3.core.mapper.PropagateNull) ReflectionMappers(org.jdbi.v3.core.mapper.reflect.ReflectionMappers) Nested(org.jdbi.v3.core.mapper.Nested) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) ColumnName(org.jdbi.v3.core.mapper.reflect.ColumnName) UnableToProduceResultException(org.jdbi.v3.core.result.UnableToProduceResultException) ResultSet(java.sql.ResultSet) Map(java.util.Map) GenericTypes(org.jdbi.v3.core.generic.GenericTypes) RowMapper(org.jdbi.v3.core.mapper.RowMapper) ColumnNameMatcher(org.jdbi.v3.core.mapper.reflect.ColumnNameMatcher) ReflectionMapperUtil.getColumnNames(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StatementContext(org.jdbi.v3.core.statement.StatementContext) Unchecked(org.jdbi.v3.core.internal.exceptions.Unchecked) PojoBuilder(org.jdbi.v3.core.mapper.reflect.internal.PojoProperties.PojoBuilder) List(java.util.List) Type(java.lang.reflect.Type) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) ReflectionMapperUtil.findColumnIndex(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex) Optional(java.util.Optional) NoSuchMapperException(org.jdbi.v3.core.mapper.NoSuchMapperException) ReflectionMapperUtil.anyColumnsStartWithPrefix(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.anyColumnsStartWithPrefix) Comparator(java.util.Comparator) Unmappable(org.jdbi.v3.core.annotation.Unmappable) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) ArrayList(java.util.ArrayList) Nested(org.jdbi.v3.core.mapper.Nested) GenericTypes(org.jdbi.v3.core.generic.GenericTypes) PropagateNull(org.jdbi.v3.core.mapper.PropagateNull)

Aggregations

Type (java.lang.reflect.Type)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Unmappable (org.jdbi.v3.core.annotation.Unmappable)1 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)1 GenericTypes (org.jdbi.v3.core.generic.GenericTypes)1 Unchecked (org.jdbi.v3.core.internal.exceptions.Unchecked)1 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)1 Nested (org.jdbi.v3.core.mapper.Nested)1 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)1 PropagateNull (org.jdbi.v3.core.mapper.PropagateNull)1 RowMapper (org.jdbi.v3.core.mapper.RowMapper)1 SingleColumnMapper (org.jdbi.v3.core.mapper.SingleColumnMapper)1 ColumnName (org.jdbi.v3.core.mapper.reflect.ColumnName)1