/
StatementHelper.java
179 lines (164 loc) · 6.12 KB
/
StatementHelper.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
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* 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.
*/
package com.vaadin.data.util.sqlcontainer.query.generator;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* StatementHelper is a simple helper class that assists TableQuery and the
* query generators in filling a PreparedStatement. The actual statement is
* generated by the query generator methods, but the resulting statement and all
* the parameter values are stored in an instance of StatementHelper.
*
* This class will also fill the values with correct setters into the
* PreparedStatement on request.
*/
public class StatementHelper implements Serializable {
private String queryString;
private List<Object> parameters = new ArrayList<Object>();
private Map<Integer, Class<?>> dataTypes = new HashMap<Integer, Class<?>>();
public StatementHelper() {
}
public void setQueryString(String queryString) {
this.queryString = queryString;
}
public String getQueryString() {
return queryString;
}
public void addParameterValue(Object parameter) {
if (parameter != null) {
parameters.add(parameter);
dataTypes.put(parameters.size() - 1, parameter.getClass());
} else {
throw new IllegalArgumentException(
"You cannot add null parameters using addParamaters(Object). "
+ "Use addParameters(Object,Class) instead");
}
}
public void addParameterValue(Object parameter, Class<?> type) {
parameters.add(parameter);
dataTypes.put(parameters.size() - 1, type);
}
public void setParameterValuesToStatement(PreparedStatement pstmt)
throws SQLException {
for (int i = 0; i < parameters.size(); i++) {
if (parameters.get(i) == null) {
handleNullValue(i, pstmt);
} else {
pstmt.setObject(i + 1, parameters.get(i));
}
}
/*
* The following list contains the data types supported by
* PreparedStatement but not supported by SQLContainer:
*
* [The list is provided as PreparedStatement method signatures]
*
* setNCharacterStream(int parameterIndex, Reader value)
*
* setNClob(int parameterIndex, NClob value)
*
* setNString(int parameterIndex, String value)
*
* setRef(int parameterIndex, Ref x)
*
* setRowId(int parameterIndex, RowId x)
*
* setSQLXML(int parameterIndex, SQLXML xmlObject)
*
* setBytes(int parameterIndex, byte[] x)
*
* setCharacterStream(int parameterIndex, Reader reader)
*
* setClob(int parameterIndex, Clob x)
*
* setURL(int parameterIndex, URL x)
*
* setArray(int parameterIndex, Array x)
*
* setAsciiStream(int parameterIndex, InputStream x)
*
* setBinaryStream(int parameterIndex, InputStream x)
*
* setBlob(int parameterIndex, Blob x)
*/
}
private void handleNullValue(int i, PreparedStatement pstmt)
throws SQLException {
Class<?> dataType = dataTypes.get(i);
int index = i + 1;
if (BigDecimal.class.equals(dataType)) {
pstmt.setBigDecimal(index, null);
} else if (Boolean.class.equals(dataType)) {
pstmt.setNull(index, Types.BOOLEAN);
} else if (Byte.class.equals(dataType)) {
pstmt.setNull(index, Types.SMALLINT);
} else if (Date.class.equals(dataType)) {
pstmt.setDate(index, null);
} else if (Double.class.equals(dataType)) {
pstmt.setNull(index, Types.DOUBLE);
} else if (Float.class.equals(dataType)) {
pstmt.setNull(index, Types.FLOAT);
} else if (Integer.class.equals(dataType)) {
pstmt.setNull(index, Types.INTEGER);
} else if (Long.class.equals(dataType)) {
pstmt.setNull(index, Types.BIGINT);
} else if (Short.class.equals(dataType)) {
pstmt.setNull(index, Types.SMALLINT);
} else if (String.class.equals(dataType)) {
pstmt.setString(index, null);
} else if (Time.class.equals(dataType)) {
pstmt.setTime(index, null);
} else if (Timestamp.class.equals(dataType)) {
pstmt.setTimestamp(index, null);
} else if (byte[].class.equals(dataType)) {
pstmt.setBytes(index, null);
} else {
if (handleUnrecognizedTypeNullValue(i, pstmt, dataTypes)) {
return;
}
throw new SQLException("Data type for parameter " + i
+ " not supported by SQLContainer: " + dataType.getName());
}
}
/**
* Handle unrecognized null values. Override this to handle null values for
* platform specific data types that are not handled by the default
* implementation of the {@link StatementHelper}.
*
* @param i
* @param pstmt
* @param dataTypes2
*
* @return true if handled, false otherwise
*
* @see {@link http://dev.vaadin.com/ticket/9148}
*/
protected boolean handleUnrecognizedTypeNullValue(int i,
PreparedStatement pstmt, Map<Integer, Class<?>> dataTypes)
throws SQLException {
return false;
}
}