Hi all,

We found a case where metadata from a resultset declares a column class (a 
"java.lang.Double") but a value happens to not match (it is a 
"java.math.BigDecimal"). We base some logic on these column classes (like 
sorting in UI), and our code breaks (class cast exceptions).

The culprit seems to be "IFNULL(MyDoubleColumn, 0.0)" which changes the 
type of the value that is returned. Either the meta data is wrong, or the 
IFNULL function should have returned a compatible value.

We are using H2 1.4.190.

I attach a simple self-contained example. Please let me know if you need 
further information.

Cheers,
-Christopher

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.
package test.h2ifnull;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Properties;

public class H2IfNullTest {

    public static void main(String[] args) throws Throwable {
        Properties properties = new Properties();
        String jdbcConnectionString = "jdbc:h2:mem:xxx";
        Connection conn = DriverManager.getConnection(jdbcConnectionString, properties);
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        stmt.execute("CREATE TABLE TestTable (MyDoubleColumn DOUBLE)");
        stmt.execute("INSERT INTO TestTable VALUES(1.2)");
        stmt.execute("INSERT INTO TestTable VALUES(null)");
        stmt.execute("SELECT IFNULL(MyDoubleColumn, 0.0) AS MyXXXXColumn FROM TestTable");
        final ResultSet rs = stmt.getResultSet();
        ResultSetMetaData metaData = rs.getMetaData();
        int columnCount = metaData.getColumnCount();
        for(int i=0; i<columnCount; i++) {
            String columnName = metaData.getColumnName(i + 1);
            String columnClassName = metaData.getColumnClassName(i + 1);
            System.out.println("Column \"" + columnName + "\" is a \"" + columnClassName + "\"");
        }
        System.out.println("MyXXXXColumn actual value classes:");
        while(rs.next()) {
            System.out.println("  " + rs.getObject(1).getClass().getName());
        }
        stmt.close();
        conn.close();
    }

}

Reply via email to