Search in sources :

Example 6 with ColumnNameMatcher

use of org.jdbi.v3.core.mapper.reflect.ColumnNameMatcher in project jdbi by jdbi.

the class FieldMapper method specialize0.

private Optional<RowMapper<T>> specialize0(StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) {
    final List<FieldData> fields = new ArrayList<>();
    for (Class<?> aType = type; aType != null; aType = aType.getSuperclass()) {
        for (Field field : aType.getDeclaredFields()) {
            Nested anno = field.getAnnotation(Nested.class);
            if (anno == null) {
                String paramName = prefix + paramName(field);
                findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(field)).ifPresent(index -> {
                    QualifiedType<?> fieldType = QualifiedType.of(field.getGenericType()).withAnnotations(ctx.getConfig(Qualifiers.class).findFor(field));
                    @SuppressWarnings("unchecked") ColumnMapper<?> mapper = ctx.findColumnMapperFor(fieldType).orElse((ColumnMapper) (r, n, c) -> r.getObject(n));
                    fields.add(new FieldData(field, new SingleColumnMapper<>(mapper, index + 1)));
                    unmatchedColumns.remove(columnNames.get(index));
                });
            } else {
                String nestedPrefix = prefix + anno.value().toLowerCase();
                if (anyColumnsStartWithPrefix(columnNames, nestedPrefix, columnNameMatchers)) {
                    nestedMappers.computeIfAbsent(field, f -> new FieldMapper<>(field.getType(), nestedPrefix)).specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns).ifPresent(mapper -> fields.add(new FieldData(field, mapper)));
                }
            }
        }
    }
    if (fields.isEmpty() && !columnNames.isEmpty()) {
        return Optional.empty();
    }
    Collections.sort(fields, Comparator.comparing(f -> f.propagateNull ? 1 : 0));
    final Optional<String> nullMarkerColumn = Optional.ofNullable(type.getAnnotation(PropagateNull.class)).map(PropagateNull::value);
    return Optional.of((r, c) -> {
        if (PojoMapper.propagateNull(r, nullMarkerColumn)) {
            return null;
        }
        T obj = construct();
        for (FieldData f : fields) {
            Object value = f.mapper.map(r, ctx);
            if (f.propagateNull && (value == null || (f.isPrimitive && r.wasNull()))) {
                return null;
            }
            writeField(obj, f.field, value);
        }
        return obj;
    });
}
Also used : RowMapperFactory(org.jdbi.v3.core.mapper.RowMapperFactory) ReflectionMapperUtil.getColumnNames(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames) PropagateNull(org.jdbi.v3.core.mapper.PropagateNull) PojoMapper(org.jdbi.v3.core.mapper.reflect.internal.PojoMapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Nested(org.jdbi.v3.core.mapper.Nested) Field(java.lang.reflect.Field) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) String.format(java.lang.String.format) ArrayList(java.util.ArrayList) StatementContext(org.jdbi.v3.core.statement.StatementContext) SQLException(java.sql.SQLException) List(java.util.List) ResultSet(java.sql.ResultSet) Map(java.util.Map) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) ReflectionMapperUtil.findColumnIndex(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex) Optional(java.util.Optional) Qualifiers(org.jdbi.v3.core.qualifier.Qualifiers) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) ReflectionMapperUtil.anyColumnsStartWithPrefix(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.anyColumnsStartWithPrefix) Comparator(java.util.Comparator) Collections(java.util.Collections) RowMapper(org.jdbi.v3.core.mapper.RowMapper) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) ArrayList(java.util.ArrayList) Nested(org.jdbi.v3.core.mapper.Nested) Field(java.lang.reflect.Field) PropagateNull(org.jdbi.v3.core.mapper.PropagateNull)

Aggregations

ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Nested (org.jdbi.v3.core.mapper.Nested)6 RowMapper (org.jdbi.v3.core.mapper.RowMapper)6 SingleColumnMapper (org.jdbi.v3.core.mapper.SingleColumnMapper)6 ReflectionMapperUtil.findColumnIndex (org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex)6 ReflectionMapperUtil.getColumnNames (org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames)6 StatementContext (org.jdbi.v3.core.statement.StatementContext)6 RowMapperFactory (org.jdbi.v3.core.mapper.RowMapperFactory)5 Type (java.lang.reflect.Type)4 Optional (java.util.Optional)4 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)4 Comparator (java.util.Comparator)3 PropagateNull (org.jdbi.v3.core.mapper.PropagateNull)3 ReflectionMapperUtil.anyColumnsStartWithPrefix (org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.anyColumnsStartWithPrefix)3 ConstructorProperties (java.beans.ConstructorProperties)2