use of org.jdbi.v3.core.mapper.ColumnMapper in project jdbi by jdbi.
the class BeanMapper method specialize0.
private RowMapper<T> specialize0(ResultSet rs, StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) throws SQLException {
final List<RowMapper<?>> mappers = new ArrayList<>();
final List<PropertyDescriptor> properties = new ArrayList<>();
for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
Nested anno = Stream.of(descriptor.getReadMethod(), descriptor.getWriteMethod()).filter(Objects::nonNull).map(m -> m.getAnnotation(Nested.class)).filter(Objects::nonNull).findFirst().orElse(null);
if (anno == null) {
String paramName = prefix + paramName(descriptor);
findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(descriptor)).ifPresent(index -> {
Type type = descriptor.getReadMethod().getGenericReturnType();
ColumnMapper<?> mapper = ctx.findColumnMapperFor(type).orElse((r, n, c) -> r.getObject(n));
mappers.add(new SingleColumnMapper<>(mapper, index + 1));
properties.add(descriptor);
unmatchedColumns.remove(columnNames.get(index));
});
} else {
String nestedPrefix = prefix + anno.value();
RowMapper<?> nestedMapper = nestedMappers.computeIfAbsent(descriptor, d -> new BeanMapper<>(d.getPropertyType(), nestedPrefix)).specialize0(rs, ctx, columnNames, columnNameMatchers, unmatchedColumns);
mappers.add(nestedMapper);
properties.add(descriptor);
}
}
if (mappers.isEmpty() && columnNames.size() > 0) {
throw new IllegalArgumentException(String.format("Mapping bean type %s " + "didn't find any matching columns in result set", type));
}
return (r, c) -> {
T bean = construct();
for (int i = 0; i < mappers.size(); i++) {
RowMapper<?> mapper = mappers.get(i);
PropertyDescriptor property = properties.get(i);
Object value = mapper.map(r, ctx);
writeProperty(bean, property, value);
}
return bean;
};
}
use of org.jdbi.v3.core.mapper.ColumnMapper in project jdbi by jdbi.
the class SqlArrayMapperFactory method build.
@Override
@SuppressWarnings("unchecked")
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
final Class<?> erasedType = GenericTypes.getErasedType(type);
if (erasedType.isArray()) {
Class<?> elementType = erasedType.getComponentType();
return elementTypeMapper(elementType, config).map(elementMapper -> new ArrayColumnMapper(elementMapper, elementType));
}
JdbiCollectors collectorRegistry = config.get(JdbiCollectors.class);
return collectorRegistry.findFor(type).flatMap(collector -> collectorRegistry.findElementTypeFor(type).flatMap(elementType -> elementTypeMapper(elementType, config)).map(elementMapper -> new CollectorColumnMapper(elementMapper, collector)));
}
use of org.jdbi.v3.core.mapper.ColumnMapper in project jdbi by jdbi.
the class JpaMapper method specialize.
@Override
public RowMapper<C> specialize(ResultSet rs, StatementContext ctx) throws SQLException {
Constructor<C> constructor;
try {
constructor = clazz.getDeclaredConstructor();
} catch (ReflectiveOperationException e) {
throw new EntityMemberAccessException("Unable to get constructor for " + clazz, e);
}
constructor.setAccessible(true);
List<MemberSetter<C>> setters = new ArrayList<>();
for (int colIndex = rs.getMetaData().getColumnCount(); colIndex >= 1; colIndex--) {
String columnLabel = rs.getMetaData().getColumnLabel(colIndex);
JpaMember member = jpaClass.lookupMember(columnLabel);
if (member != null) {
Type memberType = member.getType();
ColumnMapper<?> columnMapper = ctx.findColumnMapperFor(memberType).orElseThrow(() -> new NoSuchMapperException("No column mapper for " + memberType));
final int columnIndex = colIndex;
setters.add(obj -> member.write(obj, columnMapper.map(rs, columnIndex, ctx)));
}
}
return (r, c) -> {
C obj;
try {
obj = constructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new EntityMemberAccessException("Unable to invoke " + constructor, e);
}
for (MemberSetter<C> setter : setters) {
setter.mapAndSetMember(obj);
}
return obj;
};
}
use of org.jdbi.v3.core.mapper.ColumnMapper in project jdbi by jdbi.
the class StatementContextTest method testMapperForDelegatesToRegistry.
@Test
public void testMapperForDelegatesToRegistry() {
ColumnMapper<Foo> mapper = new FooMapper();
ConfigRegistry config = new ConfigRegistry();
config.get(ColumnMappers.class).register(mapper);
final StatementContext context = StatementContextAccess.createContext(config);
assertThat(context.findColumnMapperFor(Foo.class)).contains(mapper);
}
use of org.jdbi.v3.core.mapper.ColumnMapper in project jdbi by jdbi.
the class FieldMapper method specialize0.
private RowMapper<T> specialize0(ResultSet rs, StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) throws SQLException {
final List<RowMapper<?>> mappers = new ArrayList<>();
final List<Field> 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 -> {
Type type = field.getGenericType();
ColumnMapper<?> mapper = ctx.findColumnMapperFor(type).orElse((r, n, c) -> rs.getObject(n));
mappers.add(new SingleColumnMapper(mapper, index + 1));
fields.add(field);
unmatchedColumns.remove(columnNames.get(index));
});
} else {
String nestedPrefix = prefix + anno.value().toLowerCase();
RowMapper<?> mapper = nestedMappers.computeIfAbsent(field, f -> new FieldMapper<>(field.getType(), nestedPrefix)).specialize0(rs, ctx, columnNames, columnNameMatchers, unmatchedColumns);
mappers.add(mapper);
fields.add(field);
}
}
}
if (mappers.isEmpty() && columnNames.size() > 0) {
throw new IllegalArgumentException(String.format("Mapping fields for type %s " + "didn't find any matching columns in result set", type));
}
return (r, c) -> {
T obj = construct();
for (int i = 0; i < mappers.size(); i++) {
RowMapper<?> mapper = mappers.get(i);
Field field = fields.get(i);
Object value = mapper.map(rs, ctx);
writeField(obj, field, value);
}
return obj;
};
}
Aggregations