/** * Constructs a row mapper which maps the given column number. * @param mapper the column mapper to delegate to for mapping * @param columnNumber the column number (1-based) to map */ public SingleColumnMapper(ColumnMapper<T> mapper, int columnNumber) { this.delegate = (r, ctx) -> mapper.map(r, columnNumber, ctx); }
/** * Constructs a row mapper which maps the column with the given label. * @param mapper the column mapper to delegate to for mapping * @param columnLabel the label of the column to map */ public SingleColumnMapper(ColumnMapper<T> mapper, String columnLabel) { this.delegate = (r, ctx) -> mapper.map(r, columnLabel, ctx); }
/** * Map the given column of the current row of the result set to an Object. This method should not cause the result * set to advance; allow Jdbi to do that, please. * * @param r the result set being iterated * @param columnLabel the column label to map * @param ctx the statement context * @return the value to return for this column * @throws SQLException if anything goes wrong go ahead and let this percolate; Jdbi will handle it */ default T map(ResultSet r, String columnLabel, StatementContext ctx) throws SQLException { return map(r, r.findColumn(columnLabel), ctx); } }
private static <Opt, Box> ColumnMapper<?> optionalMapper(ColumnGetter<Box> columnGetter, Supplier<Opt> empty, Function<Box, Opt> present) { return (ColumnMapper<Opt>) (r, columnNumber, ctx) -> { final Box boxed = ((ColumnMapper<Box>) new GetterMapper<>(columnGetter)).map(r, columnNumber, ctx); return boxed == null ? empty.get() : present.apply(boxed); }; } }
private Object buildFromResultSet(java.sql.Array array, StatementContext ctx) throws SQLException { List<Object> list = new ArrayList<>(); try (ResultSet rs = array.getResultSet()) { while (rs.next()) { list.add(elementMapper.map(rs, 2, ctx)); } } Object ary = Array.newInstance(componentType, list.size()); if (componentType.isPrimitive()) { for (int i = 0; i < list.size(); i++) { Array.set(ary, i, list.get(i)); } return ary; } return list.toArray((Object[]) ary); } }
@Override public RowMapper<Map<String, T>> specialize(ResultSet rs, StatementContext ctx) throws SQLException { List<String> keyNames = getMapKeys(rs.getMetaData(), ctx.getConfig(MapMappers.class).getCaseChange()); return (r, c) -> { Map<String, T> row = new HashMap<>(); for (int i = 0; i < keyNames.size(); i++) { T value = mapper.map(r, i + 1, ctx); row.put(keyNames.get(i), value); } return row; }; }
private R buildFromResultSet(Array array, StatementContext ctx) throws SQLException { A result = collector.supplier().get(); BiConsumer<A, T> accumulator = collector.accumulator(); try (ResultSet rs = array.getResultSet()) { while (rs.next()) { accumulator.accept(result, elementMapper.map(rs, 2, ctx)); } } return collector.finisher().apply(result); } }
/** * Use a column mapper to extract a type from the current ResultSet row. * @param column the column name * @param type the Type of the type * @return the materialized object */ public Object getColumn(String column, Type type) { try { return columnMapperFor(type).map(rs, column, ctx); } catch (SQLException e) { throw new MappingException(e); } }
/** * Use a column mapper to extract a type from the current ResultSet row. * @param column the column name * @param type the Class of the type * @return the materialized object */ public Object getColumn(int column, Type type) { try { return columnMapperFor(type).map(rs, column, ctx); } catch (SQLException e) { throw new MappingException(e); } }
@SuppressWarnings("unchecked") @Override public Optional<T> map(ResultSet r, int columnNumber, StatementContext ctx) throws SQLException { final ColumnMapper<?> mapper = ctx.findColumnMapperFor( GenericTypes.findGenericParameter(type, Optional.class) .orElseThrow(() -> new NoSuchMapperException("No mapper for raw Optional type"))) .orElseThrow(() -> new NoSuchMapperException("No mapper for type " + type + " nested in Optional")); return (Optional<T>) Optional.ofNullable(mapper.map(r, columnNumber, ctx)); } }
@SuppressWarnings("unchecked") @Override public Option<T> map(ResultSet r, int columnNumber, StatementContext ctx) throws SQLException { final ColumnMapper<?> mapper = ctx.findColumnMapperFor(nestedType) .orElseThrow(() -> new NoSuchMapperException("No mapper for type " + nestedType + " nested in Option")); return (Option<T>) Option.of(mapper.map(r, columnNumber, ctx)); }
setters.add(obj -> member.write(obj, columnMapper.map(rs, columnIndex, ctx)));
@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); }); } }
@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"); }); } }
/** * Constructs a row mapper which maps the column with the given label. * @param mapper the column mapper to delegate to for mapping * @param columnLabel the label of the column to map */ public SingleColumnMapper(ColumnMapper<T> mapper, String columnLabel) { this.delegate = (r, ctx) -> mapper.map(r, columnLabel, ctx); }
/** * Constructs a row mapper which maps the given column number. * @param mapper the column mapper to delegate to for mapping * @param columnNumber the column number (1-based) to map */ public SingleColumnMapper(ColumnMapper<T> mapper, int columnNumber) { this.delegate = (r, ctx) -> mapper.map(r, columnNumber, ctx); }
/** * Map the given column of the current row of the result set to an Object. This method should not cause the result * set to advance; allow Jdbi to do that, please. * * @param r the result set being iterated * @param columnLabel the column label to map * @param ctx the statement context * @return the value to return for this column * @throws SQLException if anything goes wrong go ahead and let this percolate; Jdbi will handle it */ default T map(ResultSet r, String columnLabel, StatementContext ctx) throws SQLException { return map(r, r.findColumn(columnLabel), ctx); } }
private C buildFromResultSet(java.sql.Array array, StatementContext ctx) throws SQLException { C result = collectionSupplier.get(); try (ResultSet rs = array.getResultSet()) { while (rs.next()) { result.add(elementMapper.map(rs, 2, ctx)); } } return result; } }
/** * Use a column mapper to extract a type from the current ResultSet row. * @param column the column name * @param type the Class of the type * @return the materialized object */ public Object getColumn(int column, Type type) { try { return columnMapperFor(type).map(rs, column, ctx); } catch (SQLException e) { throw new MappingException(e); } }
/** * Use a column mapper to extract a type from the current ResultSet row. * @param column the column name * @param type the Type of the type * @return the materialized object */ public Object getColumn(String column, Type type) { try { return columnMapperFor(type).map(rs, column, ctx); } catch (SQLException e) { throw new MappingException(e); } }