You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by kr...@apache.org on 2010/09/08 17:26:40 UTC

svn commit: r995089 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/

Author: kristwaa
Date: Wed Sep  8 15:26:40 2010
New Revision: 995089

URL: http://svn.apache.org/viewvc?rev=995089&view=rev
Log:
DERBY-1938: Add support for setObject(<arg>, null) 

Allow calling the two-argument PreparedStatement.setObject method with null
to set a column value in the database to SQL NULL. The recommended way for
maximum portability is to use the three-argument setObject method or the
setNull method.

Patch file: derby-1938-1b-reworked_patch.diff


Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ParameterMappingTest.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java?rev=995089&r1=995088&r2=995089&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java Wed Sep  8 15:26:40 2010
@@ -1358,8 +1358,12 @@ public class PreparedStatement extends S
                 if ( paramType == java.sql.Types.JAVA_OBJECT )
                 {
                     setUDTX( parameterIndex, x );
-                }
-                else if (x instanceof String) {
+                } else if (x == null) {
+                    // DERBY-1938: Allow setting Java null also when the
+                    //      column type isn't specified explicitly by the
+                    //      user. Maps Java null to SQL NULL.
+                    setNull(parameterIndex, paramType);
+                } else if (x instanceof String) {
                     setString(parameterIndex, (String) x);
                 } else if (x instanceof Integer) {
                     setInt(parameterIndex, ((Integer) x).intValue());

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java?rev=995089&r1=995088&r2=995089&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java Wed Sep  8 15:26:40 2010
@@ -1208,11 +1208,18 @@ public abstract class EmbedPreparedState
 		// overview, that passing a untyped null into setObject() is not allowed.
 		// JCC disallows this, basically SQL can not handle a untyped NULL.
 		// Section 25.1.6 (Third edition), 24.1.5 (Second Edition)
+        //
+        // However, the following update was made to the JDBC API:
+        // "Note: Not all databases allow for a non-typed Null to be sent to
+        // the backend. For maximum portability, the setNull or the
+        // setObject(int parameterIndex, Object x, int sqlType) method should
+        // be used instead of setObject(int parameterIndex, Object x)."
+        // Based on the above sentence, passing null is now allowed by Derby.
+        // See DERBY-1938 for details.
 
 		if (x == null) {
-			//setNull(parameterIndex, colType);
-			//return;
-			throw dataTypeConversion(parameterIndex, "null");
+            setNull(parameterIndex, colType);
+            return;
 		}
 		
 		if (colType == Types.JAVA_OBJECT) {

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ParameterMappingTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ParameterMappingTest.java?rev=995089&r1=995088&r2=995089&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ParameterMappingTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ParameterMappingTest.java Wed Sep  8 15:26:40 2010
@@ -3052,51 +3052,8 @@ public class ParameterMappingTest extend
                 judge_setXXX(worked, sqleResult, 14, type);
         }
 
-        // setObject(null)
-        {
-            s.execute("DELETE FROM PM.TYPE_AS");
-
-            SQLException sqleResult = null;
-            boolean worked;
-            try {
-                // should never work!
-                // setObject(null)
-                psi.setObject(1, null);
-                psi.executeUpdate();
-                getValidValue(psq, jdbcTypes[type], "setObject");
-
-                worked = true;
-
-            } catch (SQLException sqle) {
-                sqleResult = sqle;
-                worked = false;
-            }
-            if (worked)
-                fail("FAIL: setObject(null) not valid");
-
-        }
-        {
-            s.execute("DELETE FROM PM.TYPE_AS");
-
-            SQLException sqleResult = null;
-            boolean worked;
-            try {
-                // should never work!
-                // setObject(null) as batch
-                psi.setObject(1, null);
-                psi.addBatch();
-                psi.executeBatch();
-                getValidValue(psq, jdbcTypes[type], "setObject");
-
-                worked = true;
-
-            } catch (SQLException sqle) {
-                sqleResult = sqle;
-                worked = false;
-            }
-            if (worked)
-                fail("FAIL: setObject(1,null) did not throw exception");
-        }
+        // DERBY-1938: Test setObject with null and no type specification.
+        setXXX_setObjectNullNoTypeSpec(s, psi, psq, type);
 
         setXXX_setObject(s, psi, psq, type, validString[type], "java.lang.String", 0);
         
@@ -3202,6 +3159,37 @@ public class ParameterMappingTest extend
         }
     }
 
+    /**
+     * Passes Java null to the setObject-call, expecting the driver to set the
+     * column value to SQL NULL.
+     * <p>
+     * This behavior was allowed/introduced by DERBY-1938.
+     *
+     * @param s statement used for auxiliary tasks
+     * @param psi statement used for insert
+     * @param psq statement used for query (retrieving inserted value)
+     * @param type the type of the column
+     */
+    private static void setXXX_setObjectNullNoTypeSpec(
+            Statement s, PreparedStatement psi, PreparedStatement psq,
+            int type) throws SQLException, IOException {
+        // setObject(null) - see DERBY-1938
+        s.execute("DELETE FROM PM.TYPE_AS");
+
+        // setObject(null)
+        psi.setObject(1, null);
+        psi.executeUpdate();
+        getValidValue(psq, jdbcTypes[type], "setObject");
+
+        s.execute("DELETE FROM PM.TYPE_AS");
+
+        // setObject(null) as batch
+        psi.setObject(1, null);
+        psi.addBatch();
+        psi.executeBatch();
+        getValidValue(psq, jdbcTypes[type], "setObject");
+    }
+
     private static void unexpectedException(SQLException sqle) {
 
         fail("FAIL unexpected exception - ");