/** * Obtain a column mapper for the given type. * * @param <T> the type to map * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ @SuppressWarnings("unchecked") public <T> Optional<ColumnMapper<T>> findFor(Class<T> type) { ColumnMapper<T> mapper = (ColumnMapper<T>) findFor((Type) type).orElse(null); return Optional.ofNullable(mapper); }
/** * Obtain a column mapper for the given type. * * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ public Optional<ColumnMapper<?>> findFor(Type type) { return findFor(QualifiedType.of(type)).map(m -> (ColumnMapper<?>) m); }
private Optional<ColumnMapper<?>> elementTypeMapper(Type elementType, ConfigRegistry config) { Optional<ColumnMapper<?>> mapper = config.get(ColumnMappers.class).findFor(elementType); if (!mapper.isPresent() && elementType == Object.class) { return Optional.of((rs, num, context) -> rs.getObject(num)); } return mapper; } }
/** * Obtain a column mapper for the given type in this context. * * @param <T> the type to map * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ public <T> Optional<ColumnMapper<T>> findColumnMapperFor(Class<T> type) { return getConfig(ColumnMappers.class).findFor(type); }
/** * Obtain a column mapper for the given type in this context. * * @param <T> the type to map * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ public <T> Optional<ColumnMapper<T>> findColumnMapperFor(GenericType<T> type) { return getConfig(ColumnMappers.class).findFor(type); }
/** * Obtain a column mapper for the given type. * * @param <T> the type to map * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ @SuppressWarnings("unchecked") public <T> Optional<ColumnMapper<T>> findFor(GenericType<T> type) { ColumnMapper<T> mapper = (ColumnMapper<T>) findFor(type.getType()).orElse(null); return Optional.ofNullable(mapper); }
/** * Obtain a column mapper for the given type in this context. * * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ public Optional<ColumnMapper<?>> findColumnMapperFor(Type type) { return getConfig(ColumnMappers.class).findFor(type); }
@Beta public static <T> RowMapper<Map<String, T>> getMapperForValueType(Class<T> valueType, ConfigRegistry config) { return config.get(ColumnMappers.class) .findFor(valueType) .map(GenericMapMapper::new) .orElseThrow(() -> new RuntimeException("no column mapper found for type " + valueType)); }
@Beta public static <T> RowMapper<Map<String, T>> getMapperForValueType(GenericType<T> valueType, ConfigRegistry config) { return config.get(ColumnMappers.class) .findFor(valueType) .map(GenericMapMapper::new) .orElseThrow(() -> new RuntimeException("no column mapper found for type " + valueType)); }
Optional<RowMapper<?>> getColumnMapper(Type type, int tupleIndex, ConfigRegistry config) { int colIndex = tupleIndex; return config.get(ColumnMappers.class) .findFor(type) .map(cm -> new SingleColumnMapper<>(cm, colIndex)); }
private Optional<RowMapper<?>> getColumnMapperForDefinedColumn(Type type, String col, ConfigRegistry config) { return config .get(ColumnMappers.class) .findFor(type) .map(cm -> new SingleColumnMapper<>(cm, col)); }
/** * Obtain a column mapper for the given qualified type in this context. * * @param type the qualified target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ @Beta public <T> Optional<ColumnMapper<T>> findColumnMapperFor(QualifiedType<T> type) { return getConfig(ColumnMappers.class).findFor(type); }
/** * Obtain a mapper for the given qualified type. If the type is unqualified, * and a row mapper is registered for the given type, it is returned. If a * column mapper is registered for the given qualified type, it is adapted * into a row mapper, mapping the first column of the result set. If neither * a row or column mapper is registered, empty is returned. * * @param type the target qualified type to map to * @return a mapper for the given type, or empty if no row or column mapper * is registered for the given type. */ @Beta public <T> Optional<RowMapper<T>> findFor(QualifiedType<T> type) { if (type.getQualifiers().isEmpty()) { Optional<RowMapper<T>> result = rowMappers.findFor(type.getType()).map(m -> (RowMapper<T>) m); if (result.isPresent()) { return result; } } return columnMappers.findFor(type).map(SingleColumnMapper::new); }
@Override public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) { if (String.class.equals(type)) { return Optional.empty(); } ColumnMappers cm = config.get(ColumnMappers.class); // look for specialized json support first, revert to simple String mapping if absent ColumnMapper<String> jsonStringMapper = JdbiOptionals.findFirstPresent( () -> cm.findFor(QualifiedType.of(String.class).with(Json.class)), () -> cm.findFor(String.class)) .orElseThrow(() -> new UnableToProduceResultException(JSON_NOT_RETRIEVABLE)); final JsonMapper mapper = config.get(JsonConfig.class).getJsonMapper(); return Optional.of((rs, i, ctx) -> { String json = jsonStringMapper.map(rs, i, ctx); return json == null ? null : mapper.fromJson(type, json, config); }); } }
private static RowMapper<?> getKeyMapper(Type keyType, ConfigRegistry config) { String column = config.get(MapEntryMappers.class).getKeyColumn(); if (column == null) { return config.get(RowMappers.class) .findFor(keyType) .orElseThrow(() -> new NoSuchMapperException("No row mapper registered for map key " + keyType)); } else { return config.get(ColumnMappers.class) .findFor(keyType) .map(mapper -> new SingleColumnMapper<>(mapper, column)) .orElseThrow(() -> new NoSuchMapperException("No column mapper registered for map key " + keyType + " in column " + column)); } }
private static RowMapper<?> getValueMapper(Type valueType, ConfigRegistry config) { String column = config.get(MapEntryMappers.class).getValueColumn(); if (column == null) { return config.get(RowMappers.class) .findFor(valueType) .orElseThrow(() -> new NoSuchMapperException("No row mapper registered for map value " + valueType)); } else { return config.get(ColumnMappers.class) .findFor(valueType) .map(mapper -> new SingleColumnMapper<>(mapper, column)) .orElseThrow(() -> new NoSuchMapperException("No column mapper registered for map value " + valueType + " in column " + column)); } }
@Test @SuppressWarnings("unchecked") public void testRegisterByGenericType() throws Exception { ColumnMapper<Iterable<Calendar>> mapper = mock(ColumnMapper.class); GenericType<Iterable<Calendar>> iterableOfCalendarType = new GenericType<Iterable<Calendar>>() {}; h.registerColumnMapper(iterableOfCalendarType, mapper); assertThat(h.getConfig(ColumnMappers.class).findFor(iterableOfCalendarType)) .contains(mapper); } }
@Test public void findNVarcharMapper() throws Exception { dbRule.getJdbi().useHandle(handle -> { ResultSet rs = mock(ResultSet.class); when(rs.getNString(anyInt())).thenReturn("value"); assertThat( handle.getConfig(Mappers.class) .findFor(NVARCHAR_STRING) .orElseThrow(IllegalStateException::new) .map(rs, null)) .isEqualTo("value"); assertThat( handle.getConfig(ColumnMappers.class) .findFor(NVARCHAR_STRING) .orElseThrow(IllegalStateException::new) .map(rs, 1, null)) .isEqualTo("value"); }); } }
private Optional<ColumnMapper<?>> elementTypeMapper(Type elementType, ConfigRegistry config) { Optional<ColumnMapper<?>> mapper = config.get(ColumnMappers.class).findFor(elementType); if (!mapper.isPresent() && elementType == Object.class) { return Optional.of((rs, num, context) -> rs.getObject(num)); } return mapper; } }
@Test @SuppressWarnings("unchecked") public void testRegisterByGenericType() throws Exception { ColumnMapper<Iterable<Calendar>> mapper = mock(ColumnMapper.class); GenericType<Iterable<Calendar>> iterableOfCalendarType = new GenericType<Iterable<Calendar>>() {}; h.registerColumnMapper(iterableOfCalendarType, mapper); assertThat(h.getConfig(ColumnMappers.class).findFor(iterableOfCalendarType)) .contains(mapper); } }