2 * Copyright 2000-2021 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.data.util.sqlcontainer.query.generator;
18 import java.io.Serializable;
19 import java.math.BigDecimal;
21 import java.sql.PreparedStatement;
22 import java.sql.SQLException;
24 import java.sql.Timestamp;
25 import java.sql.Types;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
32 * StatementHelper is a simple helper class that assists TableQuery and the
33 * query generators in filling a PreparedStatement. The actual statement is
34 * generated by the query generator methods, but the resulting statement and all
35 * the parameter values are stored in an instance of StatementHelper.
37 * This class will also fill the values with correct setters into the
38 * PreparedStatement on request.
40 * @deprecated As of 8.0, no replacement available.
43 public class StatementHelper implements Serializable {
45 private String queryString;
47 private List<Object> parameters = new ArrayList<Object>();
48 private Map<Integer, Class<?>> dataTypes = new HashMap<Integer, Class<?>>();
50 public StatementHelper() {
53 public void setQueryString(String queryString) {
54 this.queryString = queryString;
57 public String getQueryString() {
61 public void addParameterValue(Object parameter) {
62 if (parameter != null) {
63 parameters.add(parameter);
64 dataTypes.put(parameters.size() - 1, parameter.getClass());
66 throw new IllegalArgumentException(
67 "You cannot add null parameters using addParameters(Object). "
68 + "Use addParameters(Object,Class) instead");
72 public void addParameterValue(Object parameter, Class<?> type) {
73 parameters.add(parameter);
74 dataTypes.put(parameters.size() - 1, type);
77 public void setParameterValuesToStatement(PreparedStatement pstmt)
79 for (int i = 0; i < parameters.size(); i++) {
80 if (parameters.get(i) == null) {
81 handleNullValue(i, pstmt);
83 pstmt.setObject(i + 1, parameters.get(i));
88 * The following list contains the data types supported by
89 * PreparedStatement but not supported by SQLContainer:
91 * [The list is provided as PreparedStatement method signatures]
93 * setNCharacterStream(int parameterIndex, Reader value)
95 * setNClob(int parameterIndex, NClob value)
97 * setNString(int parameterIndex, String value)
99 * setRef(int parameterIndex, Ref x)
101 * setRowId(int parameterIndex, RowId x)
103 * setSQLXML(int parameterIndex, SQLXML xmlObject)
105 * setBytes(int parameterIndex, byte[] x)
107 * setCharacterStream(int parameterIndex, Reader reader)
109 * setClob(int parameterIndex, Clob x)
111 * setURL(int parameterIndex, URL x)
113 * setArray(int parameterIndex, Array x)
115 * setAsciiStream(int parameterIndex, InputStream x)
117 * setBinaryStream(int parameterIndex, InputStream x)
119 * setBlob(int parameterIndex, Blob x)
123 private void handleNullValue(int i, PreparedStatement pstmt)
124 throws SQLException {
125 Class<?> dataType = dataTypes.get(i);
127 if (BigDecimal.class.equals(dataType)) {
128 pstmt.setBigDecimal(index, null);
129 } else if (Boolean.class.equals(dataType)) {
130 pstmt.setNull(index, Types.BOOLEAN);
131 } else if (Byte.class.equals(dataType)) {
132 pstmt.setNull(index, Types.SMALLINT);
133 } else if (Date.class.equals(dataType)) {
134 pstmt.setDate(index, null);
135 } else if (Double.class.equals(dataType)) {
136 pstmt.setNull(index, Types.DOUBLE);
137 } else if (Float.class.equals(dataType)) {
138 pstmt.setNull(index, Types.FLOAT);
139 } else if (Integer.class.equals(dataType)) {
140 pstmt.setNull(index, Types.INTEGER);
141 } else if (Long.class.equals(dataType)) {
142 pstmt.setNull(index, Types.BIGINT);
143 } else if (Short.class.equals(dataType)) {
144 pstmt.setNull(index, Types.SMALLINT);
145 } else if (String.class.equals(dataType)) {
146 pstmt.setString(index, null);
147 } else if (Time.class.equals(dataType)) {
148 pstmt.setTime(index, null);
149 } else if (Timestamp.class.equals(dataType)) {
150 pstmt.setTimestamp(index, null);
151 } else if (byte[].class.equals(dataType)) {
152 pstmt.setBytes(index, null);
155 if (handleUnrecognizedTypeNullValue(i, pstmt, dataTypes)) {
159 throw new SQLException("Data type for parameter " + i
160 + " not supported by SQLContainer: " + dataType.getName());
165 * Handle unrecognized null values. Override this to handle null values for
166 * platform specific data types that are not handled by the default
167 * implementation of the {@link StatementHelper}.
173 * @return true if handled, false otherwise
175 * @see {@link http://dev.vaadin.com/ticket/9148}
177 protected boolean handleUnrecognizedTypeNullValue(int i,
178 PreparedStatement pstmt, Map<Integer, Class<?>> dataTypes)
179 throws SQLException {