15
15
*/
16
16
package org .apache .ibatis .type ;
17
17
18
+ import java .math .BigDecimal ;
19
+ import java .math .BigInteger ;
20
+ import java .net .URL ;
18
21
import java .sql .Array ;
19
22
import java .sql .CallableStatement ;
20
23
import java .sql .PreparedStatement ;
21
24
import java .sql .ResultSet ;
22
25
import java .sql .SQLException ;
26
+ import java .sql .Time ;
27
+ import java .sql .Timestamp ;
23
28
import java .sql .Types ;
29
+ import java .time .LocalDate ;
30
+ import java .time .LocalDateTime ;
31
+ import java .time .LocalTime ;
32
+ import java .time .OffsetDateTime ;
33
+ import java .time .OffsetTime ;
34
+ import java .util .Calendar ;
35
+ import java .util .concurrent .ConcurrentHashMap ;
24
36
25
37
/**
26
38
* @author Clinton Begin
27
39
*/
28
40
public class ArrayTypeHandler extends BaseTypeHandler <Object > {
29
41
42
+ private static final ConcurrentHashMap <Class <?>, String > STANDARD_MAPPING ;
43
+ static {
44
+ STANDARD_MAPPING = new ConcurrentHashMap <>();
45
+ STANDARD_MAPPING .put (BigDecimal .class , "NUMERIC" );
46
+ STANDARD_MAPPING .put (BigInteger .class , "BIGINT" );
47
+ STANDARD_MAPPING .put (boolean .class , "BOOLEAN" );
48
+ STANDARD_MAPPING .put (Boolean .class , "BOOLEAN" );
49
+ STANDARD_MAPPING .put (byte [].class , "VARBINARY" );
50
+ STANDARD_MAPPING .put (byte .class , "TINYINT" );
51
+ STANDARD_MAPPING .put (Byte .class , "TINYINT" );
52
+ STANDARD_MAPPING .put (Calendar .class , "TIMESTAMP" );
53
+ STANDARD_MAPPING .put (java .sql .Date .class , "DATE" );
54
+ STANDARD_MAPPING .put (java .util .Date .class , "TIMESTAMP" );
55
+ STANDARD_MAPPING .put (double .class , "DOUBLE" );
56
+ STANDARD_MAPPING .put (Double .class , "DOUBLE" );
57
+ STANDARD_MAPPING .put (float .class , "REAL" );
58
+ STANDARD_MAPPING .put (Float .class , "REAL" );
59
+ STANDARD_MAPPING .put (int .class , "INTEGER" );
60
+ STANDARD_MAPPING .put (Integer .class , "INTEGER" );
61
+ STANDARD_MAPPING .put (LocalDate .class , "DATE" );
62
+ STANDARD_MAPPING .put (LocalDateTime .class , "TIMESTAMP" );
63
+ STANDARD_MAPPING .put (LocalTime .class , "TIME" );
64
+ STANDARD_MAPPING .put (long .class , "BIGINT" );
65
+ STANDARD_MAPPING .put (Long .class , "BIGINT" );
66
+ STANDARD_MAPPING .put (OffsetDateTime .class , "TIMESTAMP_WITH_TIMEZONE" );
67
+ STANDARD_MAPPING .put (OffsetTime .class , "TIME_WITH_TIMEZONE" );
68
+ STANDARD_MAPPING .put (Short .class , "SMALLINT" );
69
+ STANDARD_MAPPING .put (String .class , "VARCHAR" );
70
+ STANDARD_MAPPING .put (Time .class , "TIME" );
71
+ STANDARD_MAPPING .put (Timestamp .class , "TIMESTAMP" );
72
+ STANDARD_MAPPING .put (URL .class , "DATALINK" );
73
+ };
74
+
30
75
public ArrayTypeHandler () {
31
76
super ();
32
77
}
@@ -38,23 +83,26 @@ protected void setNullParameter(PreparedStatement ps, int i, JdbcType jdbcType)
38
83
39
84
@ Override
40
85
public void setNonNullParameter (PreparedStatement ps , int i , Object parameter , JdbcType jdbcType ) throws SQLException {
41
- Array array = null ;
42
86
if (parameter instanceof Array ) {
43
87
// it's the user's responsibility to properly free() the Array instance
44
88
ps .setArray (i , (Array )parameter );
45
-
46
89
}
47
90
else {
48
91
if (!parameter .getClass ().isArray ()) {
49
92
throw new TypeException ("ArrayType Handler requires SQL array or java array parameter and does not support type " + parameter .getClass ());
50
93
}
51
- Object [] values = (Object [])parameter ;
52
- array = ps .getConnection ().createArrayOf (jdbcType .name (), values );
94
+ Class <?> componentType = parameter .getClass ().getComponentType ();
95
+ String arrayTypeName = resolveTypeName (componentType );
96
+ Array array = ps .getConnection ().createArrayOf (arrayTypeName , (Object [])parameter );
53
97
ps .setArray (i , array );
54
98
array .free ();
55
99
}
56
100
}
57
101
102
+ protected String resolveTypeName (Class <?> type ) {
103
+ return STANDARD_MAPPING .getOrDefault (type , "JAVA_OBJECT" );
104
+ }
105
+
58
106
@ Override
59
107
public Object getNullableResult (ResultSet rs , String columnName ) throws SQLException {
60
108
return extractArray (rs .getArray (columnName ));
0 commit comments