1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20 import java.io.Serializable;
21 import java.sql.Date;
22 import java.sql.ResultSet;
23 import java.sql.ResultSetMetaData;
24 import java.sql.SQLException;
25 import java.sql.Time;
26 import java.sql.Timestamp;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Map;
30
31
32
33
34
35
36
37 abstract class JDBCDynaClass implements DynaClass, Serializable {
38
39
40
41
42
43
44
45 protected boolean lowerCase = true;
46
47
48
49
50 private boolean useColumnLabel;
51
52
53
54
55
56 protected DynaProperty[] properties = null;
57
58
59
60
61
62
63
64 protected Map<String, DynaProperty> propertiesMap = new HashMap<String, DynaProperty>();
65
66
67
68
69
70 private Map<String, String> columnNameXref;
71
72
73
74
75
76
77
78
79
80 public String getName() {
81
82 return (this.getClass().getName());
83
84 }
85
86
87
88
89
90
91
92
93
94
95 public DynaProperty getDynaProperty(final String name) {
96
97 if (name == null) {
98 throw new IllegalArgumentException("No property name specified");
99 }
100 return (propertiesMap.get(name));
101
102 }
103
104
105
106
107
108
109 public DynaProperty[] getDynaProperties() {
110
111 return (properties);
112
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126 public DynaBean newInstance()
127 throws IllegalAccessException, InstantiationException {
128
129 throw new UnsupportedOperationException("newInstance() not supported");
130
131 }
132
133
134
135
136
137
138 public void setUseColumnLabel(final boolean useColumnLabel) {
139 this.useColumnLabel = useColumnLabel;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153 protected Class<?> loadClass(final String className) throws SQLException {
154
155 try {
156 ClassLoader cl = Thread.currentThread().getContextClassLoader();
157 if (cl == null) {
158 cl = this.getClass().getClassLoader();
159 }
160
161 return Class.forName(className, false, cl);
162 } catch (final Exception e) {
163 throw new SQLException(
164 "Cannot load column class '" + className + "': " + e);
165 }
166
167 }
168
169
170
171
172
173
174
175
176
177
178 protected DynaProperty createDynaProperty(
179 final ResultSetMetaData metadata,
180 final int i)
181 throws SQLException {
182
183 String columnName = null;
184 if (useColumnLabel) {
185 columnName = metadata.getColumnLabel(i);
186 }
187 if (columnName == null || columnName.trim().length() == 0) {
188 columnName = metadata.getColumnName(i);
189 }
190 final String name = lowerCase ? columnName.toLowerCase() : columnName;
191 if (!name.equals(columnName)) {
192 if (columnNameXref == null) {
193 columnNameXref = new HashMap<String, String>();
194 }
195 columnNameXref.put(name, columnName);
196 }
197 String className = null;
198 try {
199 final int sqlType = metadata.getColumnType(i);
200 switch (sqlType) {
201 case java.sql.Types.DATE:
202 return new DynaProperty(name, java.sql.Date.class);
203 case java.sql.Types.TIMESTAMP:
204 return new DynaProperty(name, java.sql.Timestamp.class);
205 case java.sql.Types.TIME:
206 return new DynaProperty(name, java.sql.Time.class);
207 default:
208 className = metadata.getColumnClassName(i);
209 }
210 } catch (final SQLException e) {
211
212
213 }
214
215
216
217 Class<?> clazz = Object.class;
218 if (className != null) {
219 clazz = loadClass(className);
220 }
221 return new DynaProperty(name, clazz);
222
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236 protected void introspect(final ResultSet resultSet) throws SQLException {
237
238
239 final ArrayList<DynaProperty> list = new ArrayList<DynaProperty>();
240 final ResultSetMetaData metadata = resultSet.getMetaData();
241 final int n = metadata.getColumnCount();
242 for (int i = 1; i <= n; i++) {
243 final DynaProperty dynaProperty = createDynaProperty(metadata, i);
244 if (dynaProperty != null) {
245 list.add(dynaProperty);
246 }
247 }
248
249
250 properties =
251 list.toArray(new DynaProperty[list.size()]);
252 for (DynaProperty propertie : properties) {
253 propertiesMap.put(propertie.getName(), propertie);
254 }
255
256 }
257
258
259
260
261
262
263
264
265
266 protected Object getObject(final ResultSet resultSet, final String name) throws SQLException {
267
268 final DynaProperty property = getDynaProperty(name);
269 if (property == null) {
270 throw new IllegalArgumentException("Invalid name '" + name + "'");
271 }
272 final String columnName = getColumnName(name);
273 final Class<?> type = property.getType();
274
275
276 if (type.equals(Date.class)) {
277 return resultSet.getDate(columnName);
278 }
279
280
281 if (type.equals(Timestamp.class)) {
282 return resultSet.getTimestamp(columnName);
283 }
284
285
286 if (type.equals(Time.class)) {
287 return resultSet.getTime(columnName);
288 }
289
290 return resultSet.getObject(columnName);
291 }
292
293
294
295
296
297
298
299
300 protected String getColumnName(final String name) {
301 if (columnNameXref != null && columnNameXref.containsKey(name)) {
302 return columnNameXref.get(name);
303 } else {
304 return name;
305 }
306 }
307
308 }