From 2826f64abe713ffc277210f627092fbdd3cd9e27 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Sun, 17 Aug 2014 20:55:15 +0300 Subject: [PATCH] Handle byte[] type in StatementHelper and correct SQLException (#13305). Change-Id: I235ebe6250b9c03bbea393df7e8eb8823d119a2c --- .../query/generator/StatementHelper.java | 54 ++++++++++--------- .../generator/StatementHelperTest.java | 54 +++++++++++++++++++ 2 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 server/tests/src/com/vaadin/data/util/sqlcontainer/generator/StatementHelperTest.java diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java index ed849b51ec..379b319207 100644 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java +++ b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java @@ -119,30 +119,34 @@ public class StatementHelper implements Serializable { 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)) { @@ -150,7 +154,7 @@ public class StatementHelper implements Serializable { } throw new SQLException("Data type not supported by SQLContainer: " - + parameters.get(i).getClass().toString()); + + dataType.getClass().toString()); } } diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/generator/StatementHelperTest.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/generator/StatementHelperTest.java new file mode 100644 index 0000000000..f1eb365c45 --- /dev/null +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/generator/StatementHelperTest.java @@ -0,0 +1,54 @@ +/* + * 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); + } +} -- 2.39.5