db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1062743 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ client/org/apache/derby/client/net/ drda/org/apache/derby/impl/drda/ engine/org/apache/derby/iapi/types/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi...
Date Mon, 24 Jan 2011 11:53:39 GMT
Author: kahatlen
Date: Mon Jan 24 11:53:38 2011
New Revision: 1062743

URL: http://svn.apache.org/viewvc?rev=1062743&view=rev
Log:
DERBY-4965: Boolean to char conversion results in integer

Made JDBC level conversion from boolean to character types result in
"true" and "false" instead of "1" and "0".

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/DatabaseMetaData.java
    db/derby/code/trunk/java/client/org/apache/derby/client/net/NetStatementRequest.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ParameterMappingTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NullIfTest.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java?rev=1062743&r1=1062742&r2=1062743&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java Mon Jan
24 11:53:38 2011
@@ -99,9 +99,41 @@ final class CrossConverters {
 
     // Convert from boolean source to target type.
     // In support of PS.setBoolean().
-    // See differences.html for DNC setBoolean() semantics.
     final Object setObject(int targetType, boolean source) throws SqlException {
-        return setObject(targetType, (short) (source ? 1 : 0));
+        short numVal = source ? (short) 1 : 0;
+        switch (targetType) {
+        case Types.BIT:
+        case Types.BOOLEAN:
+            return Boolean.valueOf(source);
+
+        case Types.SMALLINT:
+            return new Short(numVal);
+
+        case Types.INTEGER:
+            return new Integer(numVal);
+
+        case Types.BIGINT:
+            return new Long(numVal);
+
+        case Types.REAL:
+            return new Float(numVal);
+
+        case Types.DOUBLE:
+            return new Double(numVal);
+
+        case Types.DECIMAL:
+            return java.math.BigDecimal.valueOf(numVal);
+
+        case Types.CHAR:
+        case Types.VARCHAR:
+        case Types.LONGVARCHAR:
+            return String.valueOf(source);
+
+        default:
+            throw new SqlException(agent_.logWriter_,
+                new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
+                "boolean", Types.getTypeString(targetType));
+        }
     }
 
     // Convert from byte source to target type

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/DatabaseMetaData.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/DatabaseMetaData.java?rev=1062743&r1=1062742&r2=1062743&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/DatabaseMetaData.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/DatabaseMetaData.java Mon Jan
24 11:53:38 2011
@@ -96,6 +96,14 @@ public abstract class DatabaseMetaData i
     /** True if the server supports boolean values */
     private boolean supportsBooleanValues_;
 
+    /**
+     * True if the server supports transport of boolean parameter values as
+     * booleans. If false, boolean values used as parameters in prepared
+     * statements will be transported as smallints to preserve backwards
+     * compatibility. See DERBY-4965.
+     */
+    private boolean supportsBooleanParameterTransport_;
+
     //---------------------constructors/finalizer---------------------------------
 
     protected DatabaseMetaData(Agent agent, Connection connection, ProductLevel productLevel)
{
@@ -2332,6 +2340,9 @@ public abstract class DatabaseMetaData i
 
         supportsBooleanValues_ =
                 productLevel_.greaterThanOrEqualTo(10, 7, 0);
+
+        supportsBooleanParameterTransport_ =
+                productLevel_.greaterThanOrEqualTo(10, 8, 0);
     }
 
     /**
@@ -2388,6 +2399,14 @@ public abstract class DatabaseMetaData i
         return supportsBooleanValues_;
     }
 
+    /**
+     * Check if the server accepts receiving booleans as parameter values.
+     * @return true if the server supports this
+     */
+    final public boolean serverSupportsBooleanParameterTransport() {
+        return supportsBooleanParameterTransport_;
+    }
+
     //------------helper methods for meta data info call methods------------------
 
 

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/net/NetStatementRequest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/net/NetStatementRequest.java?rev=1062743&r1=1062742&r2=1062743&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/NetStatementRequest.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/NetStatementRequest.java Mon
Jan 24 11:53:38 2011
@@ -721,6 +721,9 @@ public class NetStatementRequest extends
                         }
                         break;
 
+                    case DRDAConstants.DRDA_TYPE_NBOOLEAN:
+                        write1Byte(((Short) inputs[i]).shortValue());
+                        break;
                     case DRDAConstants.DRDA_TYPE_NINTEGER:
                         writeIntFdocaData(((Integer) inputs[i]).intValue());
                         break;
@@ -1217,8 +1220,10 @@ public class NetStatementRequest extends
                     lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NINTEGER;
                     lidAndLengths[i][1] = 4;
                     break;
+                case java.sql.Types.BIT:
                 case java.sql.Types.BOOLEAN:
-                    if ( netAgent_.netConnection_.serverSupportsBooleanValues() )
+                    if ( netAgent_.netConnection_.databaseMetaData_.
+                            serverSupportsBooleanParameterTransport() )
                     {
                         lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NBOOLEAN;
                         lidAndLengths[i][1] = 1;
@@ -1231,7 +1236,6 @@ public class NetStatementRequest extends
                     break;
                 case java.sql.Types.SMALLINT:
                 case java.sql.Types.TINYINT:
-                case java.sql.Types.BIT:
                     // lid: PROTOCOL_TYPE_NSMALL,  length override: 2
                     // dataFormat: Short
                     lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NSMALL;

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java?rev=1062743&r1=1062742&r2=1062743&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java Mon Jan 24
11:53:38 2011
@@ -4635,6 +4635,14 @@ class DRDAConnThread extends Thread {
 
 		switch (drdaType)
 		{
+            case DRDAConstants.DRDA_TYPE_NBOOLEAN:
+            {
+                boolean paramVal = (reader.readByte() == 1);
+                if (SanityManager.DEBUG)
+                    trace("boolean parameter value is: " + paramVal);
+                ps.setBoolean(i+1, paramVal);
+                break;
+            }
 			case DRDAConstants.DRDA_TYPE_NSMALL:
 			{
 				short paramVal = (short) reader.readShort(getByteOrder());

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java?rev=1062743&r1=1062742&r2=1062743&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java Mon Jan 24 11:53:38
2011
@@ -1420,8 +1420,7 @@ readingLoop:
 
     public void setValue(boolean theValue) throws StandardException
     {
-        // match JCC.
-        setValue(theValue ? "1" : "0");
+        setValue(Boolean.toString(theValue));
     }
 
     public void setValue(int theValue)  throws StandardException

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=1062743&r1=1062742&r2=1062743&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
Mon Jan 24 11:53:38 2011
@@ -3564,9 +3564,8 @@ public class ParameterMappingTest extend
 
     private static void checkValidStringValue(String method, String s) {
         s = s.trim();
-        if ("setBoolean".equals(method) ||
-                "setObject(java.lang.Boolean)".equals(method) )
-            assertEquals("1",s);
+        if (isBooleanMethod(method))
+            assertEquals("true", s);
         else if ("setBytes".equals(method) ||
                 ("setObject(byte[])".equals(method)))
             assertEquals("EncodedString: > 1027 ",s.substring(0,22));

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java?rev=1062743&r1=1062742&r2=1062743&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
Mon Jan 24 11:53:38 2011
@@ -1752,6 +1752,72 @@ public class BooleanValuesTest  extends 
         }
     }
 
+    /**
+     * Verify fix for DERBY-4965 - conversion from boolean to char results
+     * in 1/0 instead of true/false.
+     */
+    public void test_4965() throws SQLException {
+        String[] stringTypes = { "CHAR(10)", "VARCHAR(10)", "LONG VARCHAR" };
+        for (int i = 0; i < stringTypes.length; i++) {
+            PreparedStatement ps = prepareStatement(
+                    "values cast(? as " + stringTypes[i] + ")");
+
+            // Test setBoolean()
+            ps.setBoolean(1, true);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "true");
+            ps.setBoolean(1, false);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "false");
+
+            // Test setObject(int, Object)
+            ps.setObject(1, Boolean.TRUE);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "true");
+            ps.setObject(1, Boolean.FALSE);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "false");
+
+            // Test setObject(int, Object, int)
+            int[] targetTypes = {
+                Types.BIT, Types.BOOLEAN,
+                Types.CHAR, Types.VARCHAR, Types.LONGVARCHAR,
+            };
+            for (int j = 0; j < targetTypes.length; j++) {
+                ps.setObject(1, Boolean.TRUE, targetTypes[j]);
+                JDBC.assertSingleValueResultSet(ps.executeQuery(), "true");
+                ps.setObject(1, Boolean.FALSE, targetTypes[j]);
+                JDBC.assertSingleValueResultSet(ps.executeQuery(), "false");
+            }
+        }
+
+        String[] intTypes = { "SMALLINT", "INTEGER", "BIGINT" };
+        for (int i = 0; i < intTypes.length; i++) {
+            PreparedStatement ps = prepareStatement(
+                    "values cast(? as " + intTypes[i] + ")");
+
+            // Test setBoolean()
+            ps.setBoolean(1, true);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "1");
+            ps.setBoolean(1, false);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "0");
+
+            // Test setObject(int, Object)
+            ps.setObject(1, Boolean.TRUE);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "1");
+            ps.setObject(1, Boolean.FALSE);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "0");
+
+            // Test setObject(int, Object, int)
+            int[] targetTypes = {
+                Types.BIT, Types.BOOLEAN,
+                Types.SMALLINT, Types.INTEGER, Types.BIGINT,
+            };
+            for (int j = 0; j < targetTypes.length; j++) {
+                ps.setObject(1, Boolean.TRUE, targetTypes[j]);
+                JDBC.assertSingleValueResultSet(ps.executeQuery(), "1");
+                ps.setObject(1, Boolean.FALSE, targetTypes[j]);
+                JDBC.assertSingleValueResultSet(ps.executeQuery(), "0");
+            }
+        }
+    }
+
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // SQL ROUTINES

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NullIfTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NullIfTest.java?rev=1062743&r1=1062742&r2=1062743&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NullIfTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NullIfTest.java
Mon Jan 24 11:53:38 2011
@@ -88,8 +88,8 @@ public class NullIfTest extends BaseJDBC
             /*DECIMAL(10,5)*/ {"1","1",null,"1"},
             /*REAL*/ {"1.0","1.0",null,"1.0"},
             /*DOUBLE*/ {"1.0","1.0",null,"1.0"},
-            /*CHAR(60)*/ {"1","1","1","1"},
-            /*VARCHAR(60)*/ {"1","1","1","1"},
+            /*CHAR(60)*/ {"true","true","true","true"},
+            /*VARCHAR(60)*/ {"true","true","true","true"},
             /*LONG VARCHAR*/ {"Exception","Exception","Exception","Exception"},
             /*CHAR(60) FOR BIT DATA*/ {null,null,null,null},
             /*VARCHAR(60) FOR BIT DATA*/ {null,null,null,null},
@@ -109,8 +109,8 @@ public class NullIfTest extends BaseJDBC
             /*DECIMAL(10,5)*/ {"1.00000","1.00000",null,"1.00000"},
             /*REAL*/ {"1.0","1.0",null,"1.0"},
             /*DOUBLE*/ {"1.0","1.0",null,"1.0"},
-            /*CHAR(60)*/ {"1","1","1","1"},
-            /*VARCHAR(60)*/ {"1","1","1","1"},
+            /*CHAR(60)*/ {"true","true","true","true"},
+            /*VARCHAR(60)*/ {"true","true","true","true"},
             /*LONG VARCHAR*/ {"Exception","Exception","Exception","Exception"},
             /*CHAR(60) FOR BIT DATA*/ {null,null,null,null},
             /*VARCHAR(60) FOR BIT DATA*/ {null,null,null,null},



Mime
View raw message