1 | |
package org.apache.ojb.broker.metadata; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.sql.Date; |
20 | |
import java.sql.Time; |
21 | |
import java.sql.Timestamp; |
22 | |
import java.sql.Types; |
23 | |
import java.util.Arrays; |
24 | |
import java.io.Serializable; |
25 | |
|
26 | |
import org.apache.commons.lang.ArrayUtils; |
27 | |
import org.apache.commons.lang.ObjectUtils; |
28 | |
import org.apache.commons.lang.SerializationUtils; |
29 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
30 | |
import org.apache.ojb.broker.OJBRuntimeException; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
class FieldTypeClasses |
38 | |
{ |
39 | |
private FieldTypeClasses() |
40 | |
{ |
41 | |
} |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
static FieldType newFieldType(JdbcType jdbcType) |
52 | |
{ |
53 | |
FieldType result = null; |
54 | |
switch (jdbcType.getType()) |
55 | |
{ |
56 | |
case Types.ARRAY: |
57 | |
result = new ArrayFieldType(); |
58 | |
break; |
59 | |
case Types.BIGINT: |
60 | |
result = new LongFieldType(); |
61 | |
break; |
62 | |
case Types.BINARY: |
63 | |
result = new ByteArrayFieldType(); |
64 | |
break; |
65 | |
case Types.BIT: |
66 | |
result = new BooleanFieldType(); |
67 | |
break; |
68 | |
case Types.BLOB: |
69 | |
result = new BlobFieldType(); |
70 | |
break; |
71 | |
case Types.CHAR: |
72 | |
result = new StringFieldType(); |
73 | |
break; |
74 | |
case Types.CLOB: |
75 | |
result = new ClobFieldType(); |
76 | |
break; |
77 | |
case Types.DATE: |
78 | |
result = new DateFieldType(); |
79 | |
break; |
80 | |
case Types.DECIMAL: |
81 | |
result = new BigDecimalFieldType(); |
82 | |
break; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
case Types.DOUBLE: |
88 | |
result = new DoubleFieldType(); |
89 | |
break; |
90 | |
case Types.FLOAT: |
91 | |
result = new FloatFieldType(); |
92 | |
break; |
93 | |
case Types.INTEGER: |
94 | |
result = new IntegerFieldType(); |
95 | |
break; |
96 | |
case Types.JAVA_OBJECT: |
97 | |
result = new JavaObjectFieldType(); |
98 | |
break; |
99 | |
case Types.LONGVARBINARY: |
100 | |
result = new ByteArrayFieldType(); |
101 | |
break; |
102 | |
case Types.LONGVARCHAR: |
103 | |
result = new StringFieldType(); |
104 | |
break; |
105 | |
case Types.NUMERIC: |
106 | |
result = new BigDecimalFieldType(); |
107 | |
break; |
108 | |
case Types.REAL: |
109 | |
result = new FloatFieldType(); |
110 | |
break; |
111 | |
case Types.REF: |
112 | |
result = new RefFieldType(); |
113 | |
break; |
114 | |
case Types.SMALLINT: |
115 | |
result = new ShortFieldType(); |
116 | |
break; |
117 | |
case Types.STRUCT: |
118 | |
result = new StructFieldType(); |
119 | |
break; |
120 | |
case Types.TIME: |
121 | |
result = new TimeFieldType(); |
122 | |
break; |
123 | |
case Types.TIMESTAMP: |
124 | |
result = new TimestampFieldType(); |
125 | |
break; |
126 | |
case Types.TINYINT: |
127 | |
result = new ByteFieldType(); |
128 | |
break; |
129 | |
case Types.VARBINARY: |
130 | |
result = new ByteArrayFieldType(); |
131 | |
break; |
132 | |
case Types.VARCHAR: |
133 | |
result = new StringFieldType(); |
134 | |
break; |
135 | |
case Types.OTHER: |
136 | |
result = new JavaObjectFieldType(); |
137 | |
break; |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
case Types.BOOLEAN: |
145 | |
result = new BooleanFieldType(); |
146 | |
break; |
147 | |
case Types.DATALINK: |
148 | |
result = new URLFieldType(); |
149 | |
break; |
150 | |
|
151 | |
default: |
152 | |
throw new OJBRuntimeException("Unkown or not supported field type specified, specified jdbc type was '" |
153 | |
+ jdbcType + "', as string: " + JdbcTypesHelper.getSqlTypeAsString(jdbcType.getType())); |
154 | |
} |
155 | |
|
156 | |
result.setSqlType(jdbcType); |
157 | |
return result; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
abstract static class BaseFieldType implements FieldType |
164 | |
{ |
165 | |
int sqlType; |
166 | |
|
167 | |
public void setSqlType(JdbcType jdbcType) |
168 | |
{ |
169 | |
sqlType = jdbcType.getType(); |
170 | |
} |
171 | |
|
172 | |
public int getSqlType() |
173 | |
{ |
174 | |
return sqlType; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
Object copyIfCloneable(Object toCopy) |
184 | |
{ |
185 | |
Object result = null; |
186 | |
if(toCopy instanceof Cloneable) |
187 | |
{ |
188 | |
try |
189 | |
{ |
190 | |
Method m = toCopy.getClass().getMethod("clone", ArrayUtils.EMPTY_CLASS_ARRAY); |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
result = m.invoke(toCopy, null); |
198 | |
} |
199 | |
catch(Exception e) |
200 | |
{ |
201 | |
throw new OJBRuntimeException("Can't invoke method 'clone' on object: " + toCopy, e); |
202 | |
} |
203 | |
} |
204 | |
return result; |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
Object copyIfSerializeable(Object toCopy) |
214 | |
{ |
215 | |
Object result = null; |
216 | |
if(toCopy instanceof Serializable) |
217 | |
{ |
218 | |
result = SerializationUtils.clone((Serializable) toCopy); |
219 | |
} |
220 | |
return result; |
221 | |
} |
222 | |
|
223 | |
public String toString() |
224 | |
{ |
225 | |
return new ToStringBuilder(this) |
226 | |
.append("sqlType", sqlType) |
227 | |
.append("sqlTypeAsString", JdbcTypesHelper.getSqlTypeAsString(sqlType)) |
228 | |
.append("isMutable", isMutable()).toString(); |
229 | |
} |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
abstract static class ImmutableFieldType extends BaseFieldType |
236 | |
{ |
237 | |
public boolean isMutable() |
238 | |
{ |
239 | |
return false; |
240 | |
} |
241 | |
|
242 | |
public Object copy(Object source) |
243 | |
{ |
244 | |
return source; |
245 | |
} |
246 | |
|
247 | |
public boolean equals(Object firstValue, Object secondValue) |
248 | |
{ |
249 | |
return ObjectUtils.equals(firstValue, secondValue); |
250 | |
} |
251 | |
} |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
abstract static class MutableFieldType extends BaseFieldType |
257 | |
{ |
258 | |
public boolean isMutable() |
259 | |
{ |
260 | |
return true; |
261 | |
} |
262 | |
|
263 | |
public boolean equals(Object firstValue, Object secondValue) |
264 | |
{ |
265 | |
return ObjectUtils.equals(firstValue, secondValue); |
266 | |
} |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public static class ClobFieldType extends ImmutableFieldType |
274 | |
{ |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public static class BlobFieldType extends ImmutableFieldType |
287 | |
{ |
288 | |
} |
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public static class ArrayFieldType extends ImmutableFieldType |
298 | |
{ |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
public static class RefFieldType extends ImmutableFieldType |
309 | |
{ |
310 | |
} |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public static class StructFieldType extends MutableFieldType |
317 | |
{ |
318 | |
|
319 | |
public Object copy(Object fieldValue) |
320 | |
{ |
321 | |
if(fieldValue == null) return null; |
322 | |
|
323 | |
Object copy = copyIfCloneable(fieldValue); |
324 | |
if(copy == null) |
325 | |
{ |
326 | |
copy = copyIfSerializeable(fieldValue); |
327 | |
} |
328 | |
return copy == null ? fieldValue : copy; |
329 | |
} |
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public static class JavaObjectFieldType extends MutableFieldType |
338 | |
{ |
339 | |
public Object copy(Object fieldValue) |
340 | |
{ |
341 | |
if(fieldValue == null) return null; |
342 | |
|
343 | |
Object copy = copyIfCloneable(fieldValue); |
344 | |
if(copy == null) |
345 | |
{ |
346 | |
copy = copyIfSerializeable(fieldValue); |
347 | |
} |
348 | |
return copy == null ? fieldValue : copy; |
349 | |
} |
350 | |
} |
351 | |
|
352 | |
public static class ByteArrayFieldType extends MutableFieldType |
353 | |
{ |
354 | |
public Object copy(Object fieldValue) |
355 | |
{ |
356 | |
byte[] result = null; |
357 | |
if(fieldValue != null) |
358 | |
{ |
359 | |
byte[] source = (byte[]) fieldValue; |
360 | |
int length = source.length; |
361 | |
result = new byte[length]; |
362 | |
System.arraycopy(fieldValue, 0, result, 0, length); |
363 | |
} |
364 | |
return result; |
365 | |
} |
366 | |
|
367 | |
public boolean equals(Object firstValue, Object secondValue) |
368 | |
{ |
369 | |
return Arrays.equals((byte[]) firstValue, (byte[]) secondValue); |
370 | |
} |
371 | |
} |
372 | |
|
373 | |
public static class DateFieldType extends MutableFieldType |
374 | |
{ |
375 | |
public Object copy(Object fieldValue) |
376 | |
{ |
377 | |
Date source = (Date) fieldValue; |
378 | |
return source != null ? new Date(source.getTime()) : null; |
379 | |
} |
380 | |
} |
381 | |
|
382 | |
public static class TimeFieldType extends MutableFieldType |
383 | |
{ |
384 | |
public Object copy(Object fieldValue) |
385 | |
{ |
386 | |
Time source = (Time) fieldValue; |
387 | |
return source != null ? new Time(source.getTime()) : null; |
388 | |
} |
389 | |
} |
390 | |
|
391 | |
public static class TimestampFieldType extends MutableFieldType |
392 | |
{ |
393 | |
public Object copy(Object fieldValue) |
394 | |
{ |
395 | |
Timestamp result = null; |
396 | |
if(fieldValue != null) |
397 | |
{ |
398 | |
Timestamp source = (Timestamp) fieldValue; |
399 | |
result = (Timestamp) source.clone(); |
400 | |
} |
401 | |
return result; |
402 | |
} |
403 | |
} |
404 | |
|
405 | |
public static class StringFieldType extends ImmutableFieldType |
406 | |
{ |
407 | |
} |
408 | |
|
409 | |
public static class BigDecimalFieldType extends ImmutableFieldType |
410 | |
{ |
411 | |
} |
412 | |
|
413 | |
public static class BooleanFieldType extends ImmutableFieldType |
414 | |
{ |
415 | |
} |
416 | |
|
417 | |
public static class ByteFieldType extends ImmutableFieldType |
418 | |
{ |
419 | |
} |
420 | |
|
421 | |
public static class ShortFieldType extends ImmutableFieldType |
422 | |
{ |
423 | |
|
424 | |
} |
425 | |
|
426 | |
public static class IntegerFieldType extends ImmutableFieldType |
427 | |
{ |
428 | |
|
429 | |
} |
430 | |
|
431 | |
public static class LongFieldType extends ImmutableFieldType |
432 | |
{ |
433 | |
|
434 | |
} |
435 | |
|
436 | |
public static class FloatFieldType extends ImmutableFieldType |
437 | |
{ |
438 | |
|
439 | |
} |
440 | |
|
441 | |
public static class DoubleFieldType extends ImmutableFieldType |
442 | |
{ |
443 | |
|
444 | |
} |
445 | |
|
446 | |
public static class URLFieldType extends ImmutableFieldType |
447 | |
{ |
448 | |
|
449 | |
} |
450 | |
} |