/
StatementCreatorUtil.java
351 lines (325 loc) · 14.4 KB
/
StatementCreatorUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package org.jumpmind.db.sql.jdbc;
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import org.jumpmind.log.Log;
import org.jumpmind.log.LogFactory;
import org.jumpmind.log.LogLevel;
public abstract class StatementCreatorUtil {
private static final Log log = LogFactory.getLog(StatementCreatorUtil.class);
private static final int UNKNOWN_TYPE = Integer.MIN_VALUE;
private static Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<Class<?>, Integer>(32);
static {
/*
* JDBC 3.0 only - not compatible with e.g. MySQL at present
* javaTypeToSqlTypeMap.put(boolean.class, new Integer(Types.BOOLEAN));
* javaTypeToSqlTypeMap.put(Boolean.class, new Integer(Types.BOOLEAN));
*/
javaTypeToSqlTypeMap.put(byte.class, Types.TINYINT);
javaTypeToSqlTypeMap.put(Byte.class, Types.TINYINT);
javaTypeToSqlTypeMap.put(short.class, Types.SMALLINT);
javaTypeToSqlTypeMap.put(Short.class, Types.SMALLINT);
javaTypeToSqlTypeMap.put(int.class, Types.INTEGER);
javaTypeToSqlTypeMap.put(Integer.class, Types.INTEGER);
javaTypeToSqlTypeMap.put(long.class, Types.BIGINT);
javaTypeToSqlTypeMap.put(Long.class, Types.BIGINT);
javaTypeToSqlTypeMap.put(BigInteger.class, Types.BIGINT);
javaTypeToSqlTypeMap.put(float.class, Types.FLOAT);
javaTypeToSqlTypeMap.put(Float.class, Types.FLOAT);
javaTypeToSqlTypeMap.put(double.class, Types.DOUBLE);
javaTypeToSqlTypeMap.put(Double.class, Types.DOUBLE);
javaTypeToSqlTypeMap.put(BigDecimal.class, Types.DECIMAL);
javaTypeToSqlTypeMap.put(java.sql.Date.class, Types.DATE);
javaTypeToSqlTypeMap.put(java.sql.Time.class, Types.TIME);
javaTypeToSqlTypeMap.put(java.sql.Timestamp.class, Types.TIMESTAMP);
javaTypeToSqlTypeMap.put(Blob.class, Types.BLOB);
javaTypeToSqlTypeMap.put(Clob.class, Types.CLOB);
}
public static void setValues(PreparedStatement ps, Object[] args, int[] argTypes,
ILobHandler lobHandler) throws SQLException {
for (int i = 1; i <= args.length; i++) {
Object arg = args[i - 1];
int argType = argTypes != null && argTypes.length > i ? argTypes[i - 1] : UNKNOWN_TYPE;
if (argType == Types.BLOB && lobHandler != null) {
lobHandler.setBlobAsBytes(ps, i, (byte[]) arg);
} else if (argType == Types.CLOB && lobHandler != null) {
lobHandler.setClobAsString(ps, i, (String) arg);
} else {
setParameterValue(ps, i, argType, arg);
}
}
}
public static void setValues(PreparedStatement ps, Object[] args) throws SQLException {
if (args != null) {
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
doSetValue(ps, i + 1, arg);
}
}
}
/**
* Set the value for prepared statements specified parameter index using the
* passed in value. This method can be overridden by sub-classes if needed.
*
* @param ps
* the PreparedStatement
* @param parameterPosition
* index of the parameter position
* @param argValue
* the value to set
* @throws SQLException
*/
public static void doSetValue(PreparedStatement ps, int parameterPosition, Object argValue)
throws SQLException {
setParameterValue(ps, parameterPosition, UNKNOWN_TYPE, argValue);
}
/**
* Derive a default SQL type from the given Java type.
*
* @param javaType
* the Java type to translate
* @return the corresponding SQL type, or <code>null</code> if none found
*/
public static int javaTypeToSqlParameterType(Class<?> javaType) {
Integer sqlType = javaTypeToSqlTypeMap.get(javaType);
if (sqlType != null) {
return sqlType;
}
if (Number.class.isAssignableFrom(javaType)) {
return Types.NUMERIC;
}
if (isStringValue(javaType)) {
return Types.VARCHAR;
}
if (isDateValue(javaType) || Calendar.class.isAssignableFrom(javaType)) {
return Types.TIMESTAMP;
}
return UNKNOWN_TYPE;
}
/**
* Set the value for a parameter. The method used is based on the SQL type
* of the parameter and we can handle complex types like arrays and LOBs.
*
* @param ps
* the prepared statement or callable statement
* @param paramIndex
* index of the parameter we are setting
* @param sqlType
* the SQL type of the parameter
* @param inValue
* the value to set (plain value or a SqlTypeValue)
* @throws SQLException
* if thrown by PreparedStatement methods
* @see SqlTypeValue
*/
public static void setParameterValue(PreparedStatement ps, int paramIndex, int sqlType,
Object inValue) throws SQLException {
setParameterValueInternal(ps, paramIndex, sqlType, null, null, inValue);
}
/**
* Set the value for a parameter. The method used is based on the SQL type
* of the parameter and we can handle complex types like arrays and LOBs.
*
* @param ps
* the prepared statement or callable statement
* @param paramIndex
* index of the parameter we are setting
* @param sqlType
* the SQL type of the parameter
* @param typeName
* the type name of the parameter (optional, only used for SQL
* NULL and SqlTypeValue)
* @param inValue
* the value to set (plain value or a SqlTypeValue)
* @throws SQLException
* if thrown by PreparedStatement methods
* @see SqlTypeValue
*/
public static void setParameterValue(PreparedStatement ps, int paramIndex, int sqlType,
String typeName, Object inValue) throws SQLException {
setParameterValueInternal(ps, paramIndex, sqlType, typeName, null, inValue);
}
/**
* Set the value for a parameter. The method used is based on the SQL type
* of the parameter and we can handle complex types like arrays and LOBs.
*
* @param ps
* the prepared statement or callable statement
* @param paramIndex
* index of the parameter we are setting
* @param sqlType
* the SQL type of the parameter
* @param typeName
* the type name of the parameter (optional, only used for SQL
* NULL and SqlTypeValue)
* @param scale
* the number of digits after the decimal point (for DECIMAL and
* NUMERIC types)
* @param inValue
* the value to set (plain value or a SqlTypeValue)
* @throws SQLException
* if thrown by PreparedStatement methods
* @see SqlTypeValue
*/
private static void setParameterValueInternal(PreparedStatement ps, int paramIndex,
int sqlType, String typeName, Integer scale, Object inValue) throws SQLException {
String typeNameToUse = typeName;
int sqlTypeToUse = sqlType;
Object inValueToUse = inValue;
if (inValueToUse == null) {
setNull(ps, paramIndex, sqlTypeToUse, typeNameToUse);
} else {
setValue(ps, paramIndex, sqlTypeToUse, typeNameToUse, scale, inValueToUse);
}
}
/**
* Set the specified PreparedStatement parameter to null, respecting
* database-specific peculiarities.
*/
private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, String typeName)
throws SQLException {
if (sqlType == UNKNOWN_TYPE) {
boolean useSetObject = false;
sqlType = Types.NULL;
try {
DatabaseMetaData dbmd = ps.getConnection().getMetaData();
String databaseProductName = dbmd.getDatabaseProductName();
String jdbcDriverName = dbmd.getDriverName();
if (databaseProductName.startsWith("Informix")
|| jdbcDriverName.startsWith("Microsoft SQL Server")) {
useSetObject = true;
} else if (databaseProductName.startsWith("DB2")
|| jdbcDriverName.startsWith("jConnect")
|| jdbcDriverName.startsWith("SQLServer")
|| jdbcDriverName.startsWith("Apache Derby")) {
sqlType = Types.VARCHAR;
}
} catch (Throwable ex) {
log.log(LogLevel.DEBUG, "Could not check database or driver name", ex);
}
if (useSetObject) {
ps.setObject(paramIndex, null);
} else {
ps.setNull(paramIndex, sqlType);
}
} else if (typeName != null) {
ps.setNull(paramIndex, sqlType, typeName);
} else {
ps.setNull(paramIndex, sqlType);
}
}
private static void setValue(PreparedStatement ps, int paramIndex, int sqlType,
String typeName, Integer scale, Object inValue) throws SQLException {
if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR
|| (sqlType == Types.CLOB && isStringValue(inValue.getClass()))) {
ps.setString(paramIndex, inValue.toString());
} else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
if (inValue instanceof BigDecimal) {
ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
} else if (scale != null) {
ps.setObject(paramIndex, inValue, sqlType, scale);
} else {
ps.setObject(paramIndex, inValue, sqlType);
}
} else if (sqlType == Types.DATE) {
if (inValue instanceof java.util.Date) {
if (inValue instanceof java.sql.Date) {
ps.setDate(paramIndex, (java.sql.Date) inValue);
} else {
ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
}
} else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
} else {
ps.setObject(paramIndex, inValue, Types.DATE);
}
} else if (sqlType == Types.TIME) {
if (inValue instanceof java.util.Date) {
if (inValue instanceof java.sql.Time) {
ps.setTime(paramIndex, (java.sql.Time) inValue);
} else {
ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
}
} else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
} else {
ps.setObject(paramIndex, inValue, Types.TIME);
}
} else if (sqlType == Types.TIMESTAMP) {
if (inValue instanceof java.util.Date) {
if (inValue instanceof java.sql.Timestamp) {
ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue, Calendar.getInstance());
} else {
ps.setTimestamp(paramIndex,
new java.sql.Timestamp(((java.util.Date) inValue).getTime()), Calendar.getInstance());
}
} else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
} else {
ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
}
} else if (sqlType == UNKNOWN_TYPE) {
if (isStringValue(inValue.getClass())) {
ps.setString(paramIndex, inValue.toString());
} else if (isDateValue(inValue.getClass())) {
ps.setTimestamp(paramIndex,
new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
} else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
} else {
// Fall back to generic setObject call without SQL type
// specified.
ps.setObject(paramIndex, inValue);
}
} else {
// Fall back to generic setObject call with SQL type specified.
ps.setObject(paramIndex, inValue, sqlType);
}
}
/**
* Check whether the given value can be treated as a String value.
*/
private static boolean isStringValue(Class<?> inValueType) {
// Consider any CharSequence (including StringBuffer and StringBuilder)
// as a String.
return (CharSequence.class.isAssignableFrom(inValueType) || StringWriter.class
.isAssignableFrom(inValueType));
}
/**
* Check whether the given value is a <code>java.util.Date</code> (but not
* one of the JDBC-specific subclasses).
*/
private static boolean isDateValue(Class<?> inValueType) {
return (java.util.Date.class.isAssignableFrom(inValueType) && !(java.sql.Date.class
.isAssignableFrom(inValueType) || java.sql.Time.class.isAssignableFrom(inValueType) || java.sql.Timestamp.class
.isAssignableFrom(inValueType)));
}
}