Search in sources :

Example 1 with Mappers

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;
    };
}
Also used : ReflectionMapperUtil.getColumnNames(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames) RowMapperFactory(org.jdbi.v3.core.mapper.RowMapperFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Nested(org.jdbi.v3.core.mapper.Nested) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) StatementContext(org.jdbi.v3.core.statement.StatementContext) Objects(java.util.Objects) Introspector(java.beans.Introspector) SQLException(java.sql.SQLException) List(java.util.List) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) PropertyDescriptor(java.beans.PropertyDescriptor) ResultSet(java.sql.ResultSet) BeanInfo(java.beans.BeanInfo) Map(java.util.Map) ReflectionMapperUtil.findColumnIndex(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) RowMapper(org.jdbi.v3.core.mapper.RowMapper) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) Nested(org.jdbi.v3.core.mapper.Nested) Type(java.lang.reflect.Type) Objects(java.util.Objects) RowMapper(org.jdbi.v3.core.mapper.RowMapper)

Example 2 with Mappers

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());
}
Also used : Module(com.google.inject.Module) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Key(com.google.inject.Key) PARAMETER(java.lang.annotation.ElementType.PARAMETER) Singleton(javax.inject.Singleton) Retention(java.lang.annotation.Retention) Inject(javax.inject.Inject) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Map(java.util.Map) Qualifier(javax.inject.Qualifier) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JdbiGlobal(org.jdbi.v3.guice.internal.JdbiGlobal) GuiceTestSupport(org.jdbi.v3.guice.util.GuiceTestSupport) Jdbi(org.jdbi.v3.core.Jdbi) FIELD(java.lang.annotation.ElementType.FIELD) Set(java.util.Set) Target(java.lang.annotation.Target) Names(com.google.inject.name.Names) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) StatementContext(org.jdbi.v3.core.statement.StatementContext) Injector(com.google.inject.Injector) Test(org.junit.jupiter.api.Test) METHOD(java.lang.annotation.ElementType.METHOD) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) TypeLiteral(com.google.inject.TypeLiteral) Jdbi(org.jdbi.v3.core.Jdbi) Injector(com.google.inject.Injector) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) Test(org.junit.jupiter.api.Test)

Example 3 with Mappers

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());
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Injector(com.google.inject.Injector) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) Module(com.google.inject.Module) Test(org.junit.jupiter.api.Test)

Example 4 with Mappers

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);
    }
}
Also used : RowMappers(org.jdbi.v3.core.mapper.RowMappers) RegisterRowMapperFactory(org.jdbi.v3.sqlobject.config.RegisterRowMapperFactory)

Example 5 with Mappers

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);
    }
}
Also used : ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) RegisterColumnMapper(org.jdbi.v3.sqlobject.config.RegisterColumnMapper)

Aggregations

ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 Map (java.util.Map)5 StatementContext (org.jdbi.v3.core.statement.StatementContext)5 Type (java.lang.reflect.Type)4 RowMappers (org.jdbi.v3.core.mapper.RowMappers)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)3 ColumnMappers (org.jdbi.v3.core.mapper.ColumnMappers)3 Nested (org.jdbi.v3.core.mapper.Nested)3 RowMapper (org.jdbi.v3.core.mapper.RowMapper)3 RowMapperFactory (org.jdbi.v3.core.mapper.RowMapperFactory)3 Injector (com.google.inject.Injector)2 Module (com.google.inject.Module)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)2 Jdbi (org.jdbi.v3.core.Jdbi)2 SingleColumnMapper (org.jdbi.v3.core.mapper.SingleColumnMapper)2