use of org.jdbi.v3.core.mapper.Mappers 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.Mappers in project jdbi by jdbi.
the class TestMultipleConfigurationModules method testDefinitionModule.
@Test
public void testDefinitionModule() {
Injector inj = GuiceTestSupport.createTestInjector(binder -> {
binder.bind(DataSource.class).annotatedWith(Foo.class).toInstance(new JdbcDataSource());
}, new AbstractJdbiConfigurationModule() {
@Override
public void configureJdbi() {
bindColumnMapper().to(DummyAMapper.class);
bindColumnMapper(A_TYPE).to(DummyAMapper.class);
bindCustomizer().toInstance(jdbi -> {
});
}
}, new AbstractJdbiDefinitionModule(Foo.class) {
@Override
public void configureJdbi() {
bindColumnMapper().to(DummyBMapper.class);
bindColumnMapper(B_TYPE).to(DummyBMapper.class);
bindCustomizer().toInstance(jdbi -> {
});
}
});
assertNotNull(inj);
Jdbi jdbi = inj.getInstance(Key.get(Jdbi.class, Foo.class));
ColumnMappers mappers = jdbi.getConfig(ColumnMappers.class);
assertTrue(mappers.findFor(A_TYPE).isPresent());
assertTrue(mappers.findFor(B_TYPE).isPresent());
}
use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class TestMultipleConfigurationModules method testNestedModule.
@Test
public void testNestedModule() {
Module definitionModule = new AbstractJdbiDefinitionModule(Foo.class) {
@Override
public void configureJdbi() {
bindColumnMapper().to(DummyBMapper.class);
bindColumnMapper(B_TYPE).to(DummyBMapper.class);
bindCustomizer().toInstance(jdbi -> {
});
}
};
Module configModule = new AbstractJdbiConfigurationModule() {
@Override
public void configureJdbi() {
bindColumnMapper().to(DummyAMapper.class);
bindColumnMapper(A_TYPE).to(DummyAMapper.class);
bindCustomizer().toInstance(jdbi -> {
});
// it is possible to nest a definition module in a configuration module,
// as configuration modules are just regular modules while definition modules
// are private modules. The opposite is *NOT* possible (as definition modules are private modules).
install(definitionModule);
}
};
Injector inj = GuiceTestSupport.createTestInjector(binder -> {
binder.bind(DataSource.class).annotatedWith(Foo.class).toInstance(new JdbcDataSource());
}, configModule);
assertNotNull(inj);
Jdbi jdbi = inj.getInstance(Key.get(Jdbi.class, Foo.class));
ColumnMappers mappers = jdbi.getConfig(ColumnMappers.class);
assertTrue(mappers.findFor(A_TYPE).isPresent());
assertTrue(mappers.findFor(B_TYPE).isPresent());
}
use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class RegisterRowMapperFactoryImpl method configureForType.
@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
RegisterRowMapperFactory registerRowMapperFactory = (RegisterRowMapperFactory) annotation;
RowMappers mappers = registry.get(RowMappers.class);
try {
mappers.register(registerRowMapperFactory.value().getDeclaredConstructor().newInstance());
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Unable to instantiate row mapper factory class " + registerRowMapperFactory.value(), e);
}
}
use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class RegisterColumnMapperImpl method configureForType.
@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
RegisterColumnMapper registerColumnMapper = (RegisterColumnMapper) annotation;
ColumnMappers mappers = registry.get(ColumnMappers.class);
try {
mappers.register(registerColumnMapper.value().getDeclaredConstructor().newInstance());
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Unable to instantiate column mapper class " + registerColumnMapper.value(), e);
}
}
Aggregations