1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbutils;
18
19 import java.sql.ResultSet;
20 import java.sql.ResultSetMetaData;
21 import java.sql.SQLException;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 /**
28 * Basic implementation of the <code>RowProcessor</code> interface.
29 *
30 * <p>
31 * This class is thread-safe.
32 * </p>
33 *
34 * @see RowProcessor
35 */
36 public class BasicRowProcessor implements RowProcessor {
37
38 /**
39 * The default BeanProcessor instance to use if not supplied in the
40 * constructor.
41 */
42 private static final BeanProcessor defaultConvert = new BeanProcessor();
43
44 /**
45 * The Singleton instance of this class.
46 */
47 private static final BasicRowProcessor instance = new BasicRowProcessor();
48
49 /**
50 * Returns the Singleton instance of this class.
51 *
52 * @return The single instance of this class.
53 * @deprecated Create instances with the constructors instead. This will
54 * be removed after DbUtils 1.1.
55 */
56 public static BasicRowProcessor instance() {
57 return instance;
58 }
59
60 /**
61 * Use this to process beans.
62 */
63 private BeanProcessor convert = null;
64
65 /**
66 * BasicRowProcessor constructor. Bean processing defaults to a
67 * BeanProcessor instance.
68 */
69 public BasicRowProcessor() {
70 this(defaultConvert);
71 }
72
73 /**
74 * BasicRowProcessor constructor.
75 * @param convert The BeanProcessor to use when converting columns to
76 * bean properties.
77 * @since DbUtils 1.1
78 */
79 public BasicRowProcessor(BeanProcessor convert) {
80 super();
81 this.convert = convert;
82 }
83
84 /**
85 * Convert a <code>ResultSet</code> row into an <code>Object[]</code>.
86 * This implementation copies column values into the array in the same
87 * order they're returned from the <code>ResultSet</code>. Array elements
88 * will be set to <code>null</code> if the column was SQL NULL.
89 *
90 * @see org.apache.commons.dbutils.RowProcessor#toArray(java.sql.ResultSet)
91 */
92 public Object[] toArray(ResultSet rs) throws SQLException {
93 ResultSetMetaData meta = rs.getMetaData();
94 int cols = meta.getColumnCount();
95 Object[] result = new Object[cols];
96
97 for (int i = 0; i < cols; i++) {
98 result[i] = rs.getObject(i + 1);
99 }
100
101 return result;
102 }
103
104 /**
105 * Convert a <code>ResultSet</code> row into a JavaBean. This
106 * implementation delegates to a BeanProcessor instance.
107 * @see org.apache.commons.dbutils.RowProcessor#toBean(java.sql.ResultSet, java.lang.Class)
108 * @see org.apache.commons.dbutils.BeanProcessor#toBean(java.sql.ResultSet, java.lang.Class)
109 */
110 public Object toBean(ResultSet rs, Class type) throws SQLException {
111 return this.convert.toBean(rs, type);
112 }
113
114 /**
115 * Convert a <code>ResultSet</code> into a <code>List</code> of JavaBeans.
116 * This implementation delegates to a BeanProcessor instance.
117 * @see org.apache.commons.dbutils.RowProcessor#toBeanList(java.sql.ResultSet, java.lang.Class)
118 * @see org.apache.commons.dbutils.BeanProcessor#toBeanList(java.sql.ResultSet, java.lang.Class)
119 */
120 public List toBeanList(ResultSet rs, Class type) throws SQLException {
121 return this.convert.toBeanList(rs, type);
122 }
123
124 /**
125 * Convert a <code>ResultSet</code> row into a <code>Map</code>. This
126 * implementation returns a <code>Map</code> with case insensitive column
127 * names as keys. Calls to <code>map.get("COL")</code> and
128 * <code>map.get("col")</code> return the same value.
129 * @see org.apache.commons.dbutils.RowProcessor#toMap(java.sql.ResultSet)
130 */
131 public Map toMap(ResultSet rs) throws SQLException {
132 Map result = new CaseInsensitiveHashMap();
133 ResultSetMetaData rsmd = rs.getMetaData();
134 int cols = rsmd.getColumnCount();
135
136 for (int i = 1; i <= cols; i++) {
137 result.put(rsmd.getColumnName(i), rs.getObject(i));
138 }
139
140 return result;
141 }
142
143 /**
144 * A Map that converts all keys to lowercase Strings for case insensitive
145 * lookups. This is needed for the toMap() implementation because
146 * databases don't consistenly handle the casing of column names.
147 */
148 private static class CaseInsensitiveHashMap extends HashMap {
149
150 /**
151 * @see java.util.Map#containsKey(java.lang.Object)
152 */
153 public boolean containsKey(Object key) {
154 return super.containsKey(key.toString().toLowerCase());
155 }
156
157 /**
158 * @see java.util.Map#get(java.lang.Object)
159 */
160 public Object get(Object key) {
161 return super.get(key.toString().toLowerCase());
162 }
163
164 /**
165 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
166 */
167 public Object put(Object key, Object value) {
168 return super.put(key.toString().toLowerCase(), value);
169 }
170
171 /**
172 * @see java.util.Map#putAll(java.util.Map)
173 */
174 public void putAll(Map m) {
175 Iterator iter = m.keySet().iterator();
176 while (iter.hasNext()) {
177 Object key = iter.next();
178 Object value = m.get(key);
179 this.put(key, value);
180 }
181 }
182
183 /**
184 * @see java.util.Map#remove(java.lang.Object)
185 */
186 public Object remove(Object key) {
187 return super.remove(key.toString().toLowerCase());
188 }
189 }
190
191 }