View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.jdbc.core;
18  
19  import java.sql.ResultSet;
20  import java.sql.ResultSetMetaData;
21  import java.sql.SQLException;
22  import java.util.Map;
23  
24  import org.springframework.jdbc.support.JdbcUtils;
25  import org.springframework.util.LinkedCaseInsensitiveMap;
26  
27  /**
28   * {@link RowMapper} implementation that creates a {@code java.util.Map}
29   * for each row, representing all columns as key-value pairs: one
30   * entry for each column, with the column name as key.
31   *
32   * <p>The Map implementation to use and the key to use for each column
33   * in the column Map can be customized through overriding
34   * {@link #createColumnMap} and {@link #getColumnKey}, respectively.
35   *
36   * <p><b>Note:</b> By default, ColumnMapRowMapper will try to build a linked Map
37   * with case-insensitive keys, to preserve column order as well as allow any
38   * casing to be used for column names. This requires Commons Collections on the
39   * classpath (which will be autodetected). Else, the fallback is a standard linked
40   * HashMap, which will still preserve column order but requires the application
41   * to specify the column names in the same casing as exposed by the driver.
42   *
43   * @author Juergen Hoeller
44   * @since 1.2
45   * @see JdbcTemplate#queryForList(String)
46   * @see JdbcTemplate#queryForMap(String)
47   */
48  public class ColumnMapRowMapper implements RowMapper<Map<String, Object>> {
49  
50  	@Override
51  	public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
52  		ResultSetMetaData rsmd = rs.getMetaData();
53  		int columnCount = rsmd.getColumnCount();
54  		Map<String, Object> mapOfColValues = createColumnMap(columnCount);
55  		for (int i = 1; i <= columnCount; i++) {
56  			String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));
57  			Object obj = getColumnValue(rs, i);
58  			mapOfColValues.put(key, obj);
59  		}
60  		return mapOfColValues;
61  	}
62  
63  	/**
64  	 * Create a Map instance to be used as column map.
65  	 * <p>By default, a linked case-insensitive Map will be created.
66  	 * @param columnCount the column count, to be used as initial
67  	 * capacity for the Map
68  	 * @return the new Map instance
69  	 * @see org.springframework.util.LinkedCaseInsensitiveMap
70  	 */
71  	protected Map<String, Object> createColumnMap(int columnCount) {
72  		return new LinkedCaseInsensitiveMap<Object>(columnCount);
73  	}
74  
75  	/**
76  	 * Determine the key to use for the given column in the column Map.
77  	 * @param columnName the column name as returned by the ResultSet
78  	 * @return the column key to use
79  	 * @see java.sql.ResultSetMetaData#getColumnName
80  	 */
81  	protected String getColumnKey(String columnName) {
82  		return columnName;
83  	}
84  
85  	/**
86  	 * Retrieve a JDBC object value for the specified column.
87  	 * <p>The default implementation uses the {@code getObject} method.
88  	 * Additionally, this implementation includes a "hack" to get around Oracle
89  	 * returning a non standard object for their TIMESTAMP datatype.
90  	 * @param rs is the ResultSet holding the data
91  	 * @param index is the column index
92  	 * @return the Object returned
93  	 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue
94  	 */
95  	protected Object getColumnValue(ResultSet rs, int index) throws SQLException {
96  		return JdbcUtils.getResultSetValue(rs, index);
97  	}
98  
99  }