/** * Create a new RowMapper for reading columns as key-value pairs. * @return the RowMapper to use * @see ColumnMapRowMapper */ protected RowMapper<Map<String, Object>> getColumnMapRowMapper() { return new ColumnMapRowMapper(); }
@Override public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Map<String, Object> mapOfColumnValues = createColumnMap(columnCount); for (int i = 1; i <= columnCount; i++) { String column = JdbcUtils.lookupColumnName(rsmd, i); mapOfColumnValues.putIfAbsent(getColumnKey(column), getColumnValue(rs, i)); } return mapOfColumnValues; }
@Override public void processRow(ResultSet rs) throws SQLException { Map<String, Object> row = new ColumnMapRowMapper().mapRow(rs, 0); myRows.add(row); if (myRows.size() >= myBatchSize) { submitNext(); } }
/** * Retrieve all index names */ public static Set<String> getIndexNames(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName) throws SQLException { DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource()); try (Connection connection = dataSource.getConnection()) { return theConnectionProperties.getTxTemplate().execute(t -> { DatabaseMetaData metadata; try { metadata = connection.getMetaData(); ResultSet indexes = metadata.getIndexInfo(connection.getCatalog(), connection.getSchema(), massageIdentifier(metadata, theTableName), false, true); Set<String> indexNames = new HashSet<>(); while (indexes.next()) { ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0)); String indexName = indexes.getString("INDEX_NAME"); indexName = toUpperCase(indexName, Locale.US); indexNames.add(indexName); } return indexNames; } catch (SQLException e) { throw new InternalErrorException(e); } }); } }
@Override public List<Map<String, Object>> queryForList(String sql, SqlParameterSource paramSource) throws DataAccessException { return query(sql, paramSource, new ColumnMapRowMapper()); }
public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Map<String, Object> mapOfColValues = createColumnMap(columnCount); for (int i = 1; i <= columnCount; i++) { String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i)); Object obj = getColumnValue(rs, i); mapOfColValues.put(key, obj); } return mapOfColValues; }
@Override public Map<String, Object> queryForMap(String sql, Map<String, ?> paramMap) throws DataAccessException { Map<String, Object> result = queryForObject(sql, paramMap, new ColumnMapRowMapper()); Assert.state(result != null, "No result map"); return result; }
@Override public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Map<String, Object> mapOfColumnValues = createColumnMap(columnCount); for (int i = 1; i <= columnCount; i++) { String column = JdbcUtils.lookupColumnName(rsmd, i); mapOfColumnValues.putIfAbsent(getColumnKey(column), getColumnValue(rs, i)); } return mapOfColumnValues; }
@Override public Map<String, Object> queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException { Map<String, Object> result = queryForObject(sql, paramSource, new ColumnMapRowMapper()); Assert.state(result != null, "No result map"); return result; }
@Override public SqlParameter createDefaultOutParameter(String parameterName, CallParameterMetaData meta) { if (meta.getSqlType() == Types.OTHER && "refcursor".equals(meta.getTypeName())) { return new SqlOutParameter(parameterName, getRefCursorSqlType(), new ColumnMapRowMapper()); } else { return super.createDefaultOutParameter(parameterName, meta); } }
@Override public SqlParameter createDefaultOutParameter(String parameterName, CallParameterMetaData meta) { if (meta.getSqlType() == Types.OTHER && REF_CURSOR_NAME.equals(meta.getTypeName())) { return new SqlOutParameter(parameterName, getRefCursorSqlType(), new ColumnMapRowMapper()); } else { return super.createDefaultOutParameter(parameterName, meta); } }
@Test(expected = IndexOutOfBoundsException.class) public void testSubListExtendsBeyondSize() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo", new ColumnMapRowMapper(), 3); list.subList(1, 40); }
@Test public void testJumpOverPages() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo", new ColumnMapRowMapper(), 3); Map<String, Object> map = list.get(3); assertNotNull(map.get("name")); }
@Test public void testOrderBy() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo order by id asc", Collections.<String, Object> singletonMap("id", 0), new ColumnMapRowMapper(), 3); assertEquals(5, list.size()); Set<String> names = new HashSet<String>(); for (Map<String, Object> map : list) { String name = (String) map.get("name"); assertNotNull(name); names.add(name); } assertEquals(5, names.size()); }
@Test public void testIterationWithDeletedElements() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo where id>=:id", Collections.<String, Object> singletonMap("id", 0), new ColumnMapRowMapper(), 3); jdbcTemplate.update("DELETE from foo where id>3"); assertEquals(5, list.size()); Set<String> names = new HashSet<String>(); for (Map<String, Object> map : list) { String name = (String) map.get("name"); assertNotNull(name); names.add(name); } assertEquals(4, names.size()); }
@Test public void testSubListFromDeletedElements() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo", new ColumnMapRowMapper(), 3); jdbcTemplate.update("DELETE from foo where id>3"); list = list.subList(1, list.size()); assertEquals(4, list.size()); int count = 0; for (Map<String, Object> map : list) { count++; assertNotNull(map.get("name")); } assertEquals(3, count); // count is less than original size estimate }
@Test public void testIterationOverSubList() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo", new ColumnMapRowMapper(), 3); list = list.subList(1, 4); assertEquals(3, list.size()); int count = 0; for (Map<String, Object> map : list) { count++; assertNotNull(map.get("name")); } assertEquals(3, count); }
@Test public void testIterationOverSubListWithSameSize() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo", new ColumnMapRowMapper(), 3); list = list.subList(0, 5); assertEquals(5, list.size()); int count = 0; for (Map<String, Object> map : list) { count++; assertNotNull(map.get("name")); } assertEquals(5, count); }
@Test public void testIterationOverPages() throws Exception { list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo where id>=:id", Collections.<String, Object> singletonMap("id", 0), new ColumnMapRowMapper(), 3); assertEquals(5, list.size()); Set<String> names = new HashSet<String>(); for (Map<String, Object> map : list) { String name = (String) map.get("name"); assertNotNull(name); names.add(name); } assertEquals(5, names.size()); names = new HashSet<String>(); for (Map<String, Object> map : list) { String name = (String) map.get("name"); assertNotNull(name); names.add(name); } assertEquals(5, names.size()); }
public void setRowMapper(RowMapper<?> rowMapper) { this.rowMapper = rowMapper; if (rowMapper == null) { this.rowMapper = new ColumnMapRowMapper(); } }