/** * Convenience method for {@code getConfig(ColumnMappers.class).register(type, mapper)} * * @param <T> the type * @param type the generic type to register * @param mapper the mapper to use on that type * @return this */ default <T> This registerColumnMapper(GenericType<T> type, ColumnMapper<T> mapper) { return configure(ColumnMappers.class, c -> c.register(type, mapper)); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(type, mapper)} * * @param type the type to register * @param mapper the mapper to use on that type * @return this */ default This registerColumnMapper(Type type, ColumnMapper<?> mapper) { return configure(ColumnMappers.class, c -> c.register(type, mapper)); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(factory)} * * @param factory column mapper factory * @return this */ default This registerColumnMapper(ColumnMapperFactory factory) { return configure(ColumnMappers.class, c -> c.register(factory)); }
/** * Register a column mapper for a given explicit {@link Type} * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param type the type to match with equals. * @param mapper the column mapper * @return this */ public ColumnMappers register(Type type, ColumnMapper<?> mapper) { return this.register(ColumnMapperFactory.of(type, mapper)); }
/** * Register a column mapper factory. * <p> * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param factory the column mapper factory * @return this */ public ColumnMappers register(ColumnMapperFactory factory) { return register(QualifiedColumnMapperFactory.adapt(factory)); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(mapper)} * * @param mapper column mapper * @return this */ default This registerColumnMapper(ColumnMapper<?> mapper) { return configure(ColumnMappers.class, c -> c.register(mapper)); }
public ColumnMappers() { // TODO move to BuiltInSupportPlugin register(new SqlArrayMapperFactory()); register(new JavaTimeMapperFactory()); register(new SqlTimeMapperFactory()); register(new InternetMapperFactory()); register(new EssentialsMapperFactory()); register(new BoxedMapperFactory()); register(new PrimitiveMapperFactory()); register(new OptionalMapperFactory()); register(new EnumMapperFactory()); register(new NVarcharMapper()); }
/** * Register a column mapper which will have its parameterized type inspected to determine what it maps to. * Column mappers may be reused by {@link RowMapper} to map individual columns. * <p> * The parameter must be concretely parameterized, we use the type argument T to * determine if it applies to a given type. * * @param mapper the column mapper * @return this * @throws UnsupportedOperationException if the ColumnMapper is not a concretely parameterized type */ public ColumnMappers register(ColumnMapper<?> mapper) { return this.register(new InferredColumnMapperFactory(mapper)); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { RegisterColumnMapperFactory registerColumnMapperFactory = (RegisterColumnMapperFactory) annotation; try { ColumnMapperFactory factory = registerColumnMapperFactory.value().newInstance(); registry.get(ColumnMappers.class).register(factory); } catch (Exception e) { throw new IllegalStateException("unable to create a specified column mapper factory", e); } } }
@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().newInstance()); } catch (Exception e) { throw new IllegalStateException("unable to create a specified column mapper", e); } } }
/** * Register a column mapper for a given {@link QualifiedType} * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param type the type to match with equals. * @param mapper the column mapper * @return this */ @Beta public <T> ColumnMappers register(QualifiedType<T> type, ColumnMapper<T> mapper) { return this.register(QualifiedColumnMapperFactory.of(type, mapper)); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(type, mapper)} * * @param type the type to register * @param mapper the mapper to use on that type * @return this */ @Beta default <T> This registerColumnMapper(QualifiedType<T> type, ColumnMapper<T> mapper) { return configure(ColumnMappers.class, c -> c.register(type, mapper)); }
/** * Register a column mapper for a given explicit {@link GenericType} * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param <T> the type * @param type the generic type to match with equals. * @param mapper the column mapper * @return this */ public <T> ColumnMappers register(GenericType<T> type, ColumnMapper<T> mapper) { return this.register(ColumnMapperFactory.of(type.getType(), mapper)); }
@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); } }
@Test public void configColumnMappersRegisterByQualifiedType() { dbRule.getJdbi() .configure(ColumnMappers.class, config -> config.register( QualifiedType.of(String.class).with(Reversed.class), (r, c, ctx) -> reverse(r.getString(c)))) .useHandle(handle -> { handle.execute("insert into something (id, name) values (1, 'abcdef')"); assertThat( handle.select("SELECT name FROM something") .mapTo(QualifiedType.of(String.class).with(Reversed.class)) .findOnly()) .isEqualTo("fedcba"); }); }
@Test public void configColumnMappersRegister() { dbRule.getJdbi() .configure(ColumnMappers.class, config -> config.register(new ReversedStringMapper())) .useHandle(handle -> { handle.execute("insert into something (id, name) values (1, 'abc')"); assertThat( handle.select("SELECT name FROM something") .mapTo(QualifiedType.of(String.class).with(Reversed.class)) .findOnly()) .isEqualTo("cba"); }); }
@Test public void configColumnMappersRegisterFactory() { dbRule.getJdbi() .configure(ColumnMappers.class, config -> config.register(new ReversedStringMapperFactory())) .useHandle(handle -> { handle.execute("insert into something (id, name) values (1, 'xyz')"); assertThat( handle.select("SELECT name FROM something") .mapTo(QualifiedType.of(String.class).with(Reversed.class)) .findOnly()) .isEqualTo("zyx"); }); }
/** * Register a column mapper for a given explicit {@link Type} * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param type the type to match with equals. * @param mapper the column mapper * @return this */ public ColumnMappers register(Type type, ColumnMapper<?> mapper) { return this.register(ColumnMapperFactory.of(type, mapper)); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(factory)} * * @param factory column mapper factory * @return this */ default This registerColumnMapper(ColumnMapperFactory factory) { return configure(ColumnMappers.class, c -> c.register(factory)); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(type, mapper)} * * @param type the type to register * @param mapper the mapper to use on that type * @return this */ default This registerColumnMapper(Type type, ColumnMapper<?> mapper) { return configure(ColumnMappers.class, c -> c.register(type, mapper)); }