]> source.dussan.org Git - vaadin-framework.git/commitdiff
Handle byte[] type in StatementHelper and correct SQLException (#13305).
authorDenis Anisimov <denis@vaadin.com>
Sun, 17 Aug 2014 17:55:15 +0000 (20:55 +0300)
committerVaadin Code Review <review@vaadin.com>
Wed, 20 Aug 2014 11:36:41 +0000 (11:36 +0000)
Change-Id: I235ebe6250b9c03bbea393df7e8eb8823d119a2c

server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java
server/tests/src/com/vaadin/data/util/sqlcontainer/generator/StatementHelperTest.java [new file with mode: 0644]

index ed849b51ec613af4ff434e222ebbe6cd980d67a0..379b319207e6628990b4bdcf5e853eeadfde975f 100644 (file)
@@ -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 (file)
index 0000000..f1eb365
--- /dev/null
@@ -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);
+    }
+}