Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ColumnMapRowMapper |
|
| 1.25;1.25 |
1 | package liquibase.executor.jvm; | |
2 | ||
3 | import liquibase.util.JdbcUtils; | |
4 | ||
5 | import java.sql.ResultSet; | |
6 | import java.sql.ResultSetMetaData; | |
7 | import java.sql.SQLException; | |
8 | import java.util.LinkedHashMap; | |
9 | import java.util.Map; | |
10 | ||
11 | /** | |
12 | * {@link RowMapper} implementation that creates a <code>java.util.Map</code> for each row, representing all columns as | |
13 | * key-value pairs: one entry for each column, with the column name as key. | |
14 | * <p/> | |
15 | * <p> | |
16 | * The Map implementation to use and the key to use for each column in the column Map can be customized through | |
17 | * overriding {@link #createColumnMap} and {@link #getColumnKey}, respectively. | |
18 | * <p/> | |
19 | * | |
20 | * @author Spring Framework | |
21 | */ | |
22 | @SuppressWarnings({ "unchecked" }) | |
23 | 0 | class ColumnMapRowMapper implements RowMapper { |
24 | ||
25 | public Object mapRow(ResultSet rs, int rowNum) throws SQLException { | |
26 | 0 | ResultSetMetaData rsmd = rs.getMetaData(); |
27 | 0 | int columnCount = rsmd.getColumnCount(); |
28 | 0 | Map mapOfColValues = createColumnMap(columnCount); |
29 | 0 | for (int i = 1; i <= columnCount; i++) { |
30 | 0 | String key = getColumnKey(rsmd.getColumnName(i)); |
31 | 0 | Object obj = getColumnValue(rs, i); |
32 | 0 | mapOfColValues.put(key, obj); |
33 | } | |
34 | 0 | return mapOfColValues; |
35 | } | |
36 | ||
37 | /** | |
38 | * Create a Map instance to be used as column map. | |
39 | * | |
40 | * @param columnCount | |
41 | * the column count, to be used as initial capacity for the Map | |
42 | * @return the new Map instance | |
43 | */ | |
44 | protected Map createColumnMap(int columnCount) { | |
45 | 0 | return new LinkedHashMap(columnCount); |
46 | } | |
47 | ||
48 | /** | |
49 | * Determine the key to use for the given column in the column Map. | |
50 | * | |
51 | * @param columnName | |
52 | * the column name as returned by the ResultSet | |
53 | * @return the column key to use | |
54 | * @see java.sql.ResultSetMetaData#getColumnName | |
55 | */ | |
56 | protected String getColumnKey(String columnName) { | |
57 | 0 | return columnName.toUpperCase(); |
58 | } | |
59 | ||
60 | /** | |
61 | * Retrieve a JDBC object value for the specified column. | |
62 | * <p> | |
63 | * The default implementation uses the <code>getObject</code> method. Additionally, this implementation includes a | |
64 | * "hack" to get around Oracle returning a non standard object for their TIMESTAMP datatype. | |
65 | * | |
66 | * @param rs | |
67 | * is the ResultSet holding the data | |
68 | * @param index | |
69 | * is the column index | |
70 | * @return the Object returned | |
71 | */ | |
72 | protected Object getColumnValue(ResultSet rs, int index) throws SQLException { | |
73 | 0 | return JdbcUtils.getResultSetValue(rs, index); |
74 | } | |
75 | ||
76 | } |