/** * Static factory method to create a new {@code SingleColumnRowMapper} * (with the required type specified only once). * @param requiredType the type that each result object is expected to match * @since 4.1 */ public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType) { return new SingleColumnRowMapper<>(requiredType); }
@Test(expected = TypeMismatchDataAccessException.class) // SPR-16483 public void doesNotUseConversionService() throws SQLException { SingleColumnRowMapper<LocalDateTime> rowMapper = SingleColumnRowMapper.newInstance(LocalDateTime.class, null); ResultSet resultSet = mock(ResultSet.class); ResultSetMetaData metaData = mock(ResultSetMetaData.class); given(metaData.getColumnCount()).willReturn(1); given(resultSet.getMetaData()).willReturn(metaData); given(resultSet.getObject(1, LocalDateTime.class)) .willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getTimestamp(1)).willReturn(new Timestamp(0)); rowMapper.mapRow(resultSet, 1); }
Object result = getColumnValue(rs, 1, this.requiredType); if (result != null && this.requiredType != null && !this.requiredType.isInstance(result)) { return (T) convertValueToRequiredType(result, this.requiredType);
/** * Static factory method to create a new {@code SingleColumnRowMapper} * (with the required type specified only once). * @param requiredType the type that each result object is expected to match * @param conversionService the {@link ConversionService} for converting a fetched value * @since 5.0.4 */ public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType, @Nullable ConversionService conversionService) { SingleColumnRowMapper<T> rowMapper = newInstance(requiredType); rowMapper.setConversionService(conversionService); return rowMapper; }
private SingleColumnRowMapper<String> stringRowMapper() { return SingleColumnRowMapper.newInstance(String.class); }
/** * This implementation of this method extracts a single value from the * single row returned by the function. If there are a different number * of rows returned, this is treated as an error. */ @Override @Nullable protected T mapRow(ResultSet rs, int rowNum) throws SQLException { return this.rowMapper.mapRow(rs, rowNum); }
/** * Create a new {@code SingleColumnRowMapper}. * <p>Consider using the {@link #newInstance} factory method instead, * which allows for specifying the required type once only. * @param requiredType the type that each result object is expected to match */ public SingleColumnRowMapper(Class<T> requiredType) { setRequiredType(requiredType); }
return (T) convertValueToRequiredType(result, this.requiredType);
public List<Long> listToplevelCorpora() { String sql = "SELECT id FROM corpus WHERE top_level = 'y'"; return getJdbcTemplate().query(sql, SingleColumnRowMapper. newInstance(Long.class)); }
/** * This implementation of this method extracts a single value from the * single row returned by the function. If there are a different number * of rows returned, this is treated as an error. */ @Override protected T mapRow(ResultSet rs, int rowNum) throws SQLException { return this.rowMapper.mapRow(rs, rowNum); }
/** * Specify the type that the result object is required to match. * <p>If not specified, the result value will be exposed as * returned by the JDBC driver. */ public void setResultType(Class<T> resultType) { this.rowMapper.setRequiredType(resultType); }
/** * Create a new RowMapper for reading result objects from a single column. * @param requiredType the type that each result object is expected to match * @return the RowMapper to use * @see SingleColumnRowMapper */ protected <T> RowMapper<T> getSingleColumnRowMapper(Class<T> requiredType) { return new SingleColumnRowMapper<>(requiredType); }
@Override public int getQuotaCount() { MapSqlParameterSource parameterSource = new MapSqlParameterSource(); return getCallsHandler().executeRead ("getQuotaCount", SingleColumnRowMapper.newInstance(Long.class), parameterSource).intValue(); }
@Test // SPR-16483 public void useDefaultConversionService() throws SQLException { Timestamp timestamp = new Timestamp(0); SingleColumnRowMapper<LocalDateTime> rowMapper = SingleColumnRowMapper.newInstance(LocalDateTime.class); ResultSet resultSet = mock(ResultSet.class); ResultSetMetaData metaData = mock(ResultSetMetaData.class); given(metaData.getColumnCount()).willReturn(1); given(resultSet.getMetaData()).willReturn(metaData); given(resultSet.getObject(1, LocalDateTime.class)) .willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getTimestamp(1)).willReturn(timestamp); LocalDateTime actualLocalDateTime = rowMapper.mapRow(resultSet, 1); assertEquals(timestamp.toLocalDateTime(), actualLocalDateTime); }
Object result = getColumnValue(rs, 1, this.requiredType); if (result != null && this.requiredType != null && !this.requiredType.isInstance(result)) { return (T) convertValueToRequiredType(result, this.requiredType);
/** * This implementation of this method extracts a single value from the * single row returned by the function. If there are a different number * of rows returned, this is treated as an error. */ @Override @Nullable protected T mapRow(ResultSet rs, int rowNum) throws SQLException { return this.rowMapper.mapRow(rs, rowNum); }
/** * Specify the type that the result object is required to match. * <p>If not specified, the result value will be exposed as * returned by the JDBC driver. */ public void setResultType(Class<T> resultType) { this.rowMapper.setRequiredType(resultType); }
@Override public <T> List<T> queryForList(String sql, SqlParameterSource paramSource, Class<T> elementType) throws DataAccessException { return query(sql, paramSource, new SingleColumnRowMapper<>(elementType)); }
@Override public String getGlusterHookContent(Guid hookId) { return getCallsHandler().executeRead("GetGlusterHookContentById", SingleColumnRowMapper.newInstance(String.class), createIdParameterMapper(hookId)); }
@Test // SPR-16483 public void useCustomConversionService() throws SQLException { Timestamp timestamp = new Timestamp(0); DefaultConversionService myConversionService = new DefaultConversionService(); myConversionService.addConverter(Timestamp.class, MyLocalDateTime.class, source -> new MyLocalDateTime(source.toLocalDateTime())); SingleColumnRowMapper<MyLocalDateTime> rowMapper = SingleColumnRowMapper.newInstance(MyLocalDateTime.class, myConversionService); ResultSet resultSet = mock(ResultSet.class); ResultSetMetaData metaData = mock(ResultSetMetaData.class); given(metaData.getColumnCount()).willReturn(1); given(resultSet.getMetaData()).willReturn(metaData); given(resultSet.getObject(1, MyLocalDateTime.class)) .willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getObject(1)).willReturn(timestamp); MyLocalDateTime actualMyLocalDateTime = rowMapper.mapRow(resultSet, 1); assertNotNull(actualMyLocalDateTime); assertEquals(timestamp.toLocalDateTime(), actualMyLocalDateTime.value); }