db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kathey Marsden <kmarsdende...@sbcglobal.net>
Subject Is this an embedded bug? scale is not adjusted if target column is real or double
Date Fri, 12 Oct 2007 15:42:19 GMT
For embedded,

public void *setObject*(int parameterIndex,
                      Object <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html>
x,
                      int targetSqlType)


and

public void *setObject*(int parameterIndex,
                      Object <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html>
x,
                      int targetSqlType,
                      int scale)
With a scale specified as zero 


do not adjust the scale when the value is inserted into a double or real 
column.  Client will truncate the fractional digits. I think this is a 
bug in embedded, but am not totally sure, so thought I would verify on 
the list before filing a bug.  Below is a program to test and the output 
with embedded and client.

Thanks for confirming whether embedded or client is showing the correct 
behavior.

Thanks

Kathey



Embedded
        PASS: got expected value:123.00
        FAIL:expected:123 but got:123.45
        FAIL:expected:123 but got:123.45
        PASS: got expected value:123
        PASS: got expected value:123.00
        FAIL:expected:123 but got:123.45
        FAIL:expected:123 but got:123.45
        PASS: got expected value:123
client
        PASS: got expected value:123.00
        PASS: got expected value:123.0
        PASS: got expected value:123.0
        PASS: got expected value:123
        PASS: got expected value:123.00
        PASS: got expected value:123.0
        PASS: got expected value:123.0
        PASS: got expected value:123


import java.sql.*;
import java.math.BigDecimal;

public class ZeroScaleTest {

    public static void main(String[] args) throws Exception {
    System.out.println("Embedded");
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    Connection conn = 
DriverManager.getConnection("jdbc:derby:wombat;create=true");
    testZeroScale(conn);
   
    System.out.println("client");
    Class.forName("org.apache.derby.jdbc.ClientDriver");
     conn = 
DriverManager.getConnection("jdbc:derby://localhost:1527/wombatns;create=true");
    testZeroScale(conn);
    }

    private static void testZeroScale(Connection conn) throws SQLException{
    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE TAB (dc DECIMAL(10,2), db double, r 
real, i int)");
        PreparedStatement ps = conn.prepareStatement("INSERT INTO TAB 
VALUES(?,?,?,?)");
        BigDecimal value = new BigDecimal("123.45");

        // Test with setObject and scale of 0
        ps.setObject(1, value,java.sql.Types.DECIMAL,0);
        ps.setObject(2, value,java.sql.Types.DECIMAL,0);
        ps.setObject(3, value,java.sql.Types.DECIMAL,0);
        ps.setObject(4, value,java.sql.Types.DECIMAL,0);
        ps.executeUpdate();
       
        // Test with setObject and type with no scale.
        // should default to scale 0
        ps.setObject(1, value,java.sql.Types.DECIMAL);
        ps.setObject(2, value,java.sql.Types.DECIMAL);
        ps.setObject(3, value,java.sql.Types.DECIMAL);
        ps.setObject(4, value,java.sql.Types.DECIMAL);
        ps.executeUpdate();
   
    // We expect the scale to be adjusted
    ResultSet rs = s.executeQuery("SELECT * FROM DERBY_2073_TAB");
    BigDecimal expectedValue = new BigDecimal("123");
    rs.next();
    assertCompares(expectedValue, rs.getBigDecimal(1));
    assertCompares(expectedValue, rs.getBigDecimal(2));
    assertCompares(expectedValue, rs.getBigDecimal(3));
    assertCompares(expectedValue, rs.getBigDecimal(4));
    rs.next();
    assertCompares(expectedValue, rs.getBigDecimal(1));
    assertCompares(expectedValue, rs.getBigDecimal(2));
    assertCompares(expectedValue, rs.getBigDecimal(3));
    assertCompares(expectedValue, rs.getBigDecimal(4));
    s.executeUpdate("DROP TABLE TAB");
    }


    private static void assertCompares(BigDecimal 
expectedValue,BigDecimal value)
    {
    if (expectedValue.compareTo(value)== 0)
        System.out.println("\tPASS: got expected value:" + value);
    else
        System.out.println("\tFAIL:expected:" + expectedValue + " but 
got:" + value);
    }
}



Mime
View raw message