private void handleNullValue(int i, PreparedStatement pstmt)
throws SQLException {
- if (BigDecimal.class.equals(dataTypes.get(i))) {
- pstmt.setBigDecimal(i + 1, null);
- } else if (Boolean.class.equals(dataTypes.get(i))) {
- pstmt.setNull(i + 1, Types.BOOLEAN);
- } else if (Byte.class.equals(dataTypes.get(i))) {
- pstmt.setNull(i + 1, Types.SMALLINT);
- } else if (Date.class.equals(dataTypes.get(i))) {
- pstmt.setDate(i + 1, null);
- } else if (Double.class.equals(dataTypes.get(i))) {
- pstmt.setNull(i + 1, Types.DOUBLE);
- } else if (Float.class.equals(dataTypes.get(i))) {
- pstmt.setNull(i + 1, Types.FLOAT);
- } else if (Integer.class.equals(dataTypes.get(i))) {
- pstmt.setNull(i + 1, Types.INTEGER);
- } else if (Long.class.equals(dataTypes.get(i))) {
- pstmt.setNull(i + 1, Types.BIGINT);
- } else if (Short.class.equals(dataTypes.get(i))) {
- pstmt.setNull(i + 1, Types.SMALLINT);
- } else if (String.class.equals(dataTypes.get(i))) {
- pstmt.setString(i + 1, null);
- } else if (Time.class.equals(dataTypes.get(i))) {
- pstmt.setTime(i + 1, null);
- } else if (Timestamp.class.equals(dataTypes.get(i))) {
- pstmt.setTimestamp(i + 1, null);
+ 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)) {
}
throw new SQLException("Data type not supported by SQLContainer: "
- + parameters.get(i).getClass().toString());
+ + dataType.getClass().toString());
}
}
--- /dev/null
+/*
+ * 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.generator;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper;
+
+/**
+ *
+ * @author Vaadin Ltd
+ */
+public class StatementHelperTest {
+
+ @Test(expected = SQLException.class)
+ public void testSetValueNullParameter() throws SQLException {
+ StatementHelper helper = new StatementHelper();
+ helper.addParameterValue(null, Object.class);
+ PreparedStatement statement = EasyMock
+ .createMock(PreparedStatement.class);
+ // should throw SQLException, not NPE
+ helper.setParameterValuesToStatement(statement);
+ }
+
+ @Test
+ public void testSetByteArrayValue() throws SQLException {
+ StatementHelper helper = new StatementHelper();
+ helper.addParameterValue(null, byte[].class);
+ PreparedStatement statement = EasyMock
+ .createMock(PreparedStatement.class);
+ // should not throw SQLException
+ helper.setParameterValuesToStatement(statement);
+
+ EasyMock.replay(statement);
+ statement.setBytes(1, null);
+ }
+}