Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDBC driver getBoolean fails on null value #8677

Closed
escabe opened this issue Nov 26, 2018 · 1 comment
Closed

JDBC driver getBoolean fails on null value #8677

escabe opened this issue Nov 26, 2018 · 1 comment
Assignees
Labels
Milestone

Comments

@escabe
Copy link

escabe commented Nov 26, 2018

OrientDB Version: latest git version (commit d6fb8ec)

Java Version: 1.8.0_121-b13

OS: Windows 10

Expected behavior

I have a class myV with a property myBool which is of type Boolean. There are two records of type myV, the first record contains has a value for myBool (false) and the second record has myBool = null.

When I retrieve these records through the JDBC interface, I believe that I should be able to write:

// Execute query
Statement s = conn.createStatement();
ResultSet r = s.executeQuery("SELECT myBool FROM myV");
// Obtain meta data
ResultSetMetaData m = r.getMetaData();
int nCols = m.getColumnCount();
// Get the column types once and store them in an array
int colTypes[] = new int[nCols];
for (int i=0;i<nCols;i++) {
    colTypes[i] = m.getColumnType(i+1);
}

// Go through all rows and columns
while(r.next()) {
    for (int i=0;i<nCols;i++) {
        switch (colTypes[i]) {
            case java.sql.Types.BOOLEAN:
                boolean b = r.getBoolean(i+1);
                System.out.println(b);
                break;
            default:
                Object obj = r.getObject(i+1);
                System.out.println(obj);
                break;
        }
    }
}
r.close();
s.close();

There are three important assumptions/points in this code:

  1. I only have to determine the types of the columns once; I mean this cannot change per row, right?

  2. If the type is BOOLEAN, I should retrieve the value with getBoolean.

  3. In my real code I have case statements for other types as well, not shown here, plus, also, shown here, always a fallback which uses getObject, which should always work.

Actual behavior

This errors out with

Exception in thread "main" java.sql.SQLException: An error occurred during the retrieval of the boolean value at column 'myBool' ---> {"@type":"d","@version":0,"myBool":null}
	at com.orientechnologies.orient.jdbc.OrientJdbcResultSet.getBoolean(OrientJdbcResultSet.java:379)
	at com.orientechnologies.orient.jdbc.OrientJdbcResultSet.getBoolean(OrientJdbcResultSet.java:369)
	at Main.main(Main.java:21)
Caused by: java.lang.NullPointerException
	at com.orientechnologies.orient.jdbc.OrientJdbcResultSet.getBoolean(OrientJdbcResultSet.java:375)
	... 2 more

The problem here is that getBoolean fails when the actual data is null.

Note by the way, that if I do not "cache" the column types and use m.getColumnType inside the while-loop, that getColumnType will return 0 once the second row is reached; which gets me into the default case which then works correctly. But the type of a column should not be changing when going through the rows, right?

Proposed fix

For now, I actually made a change to the JDBC driver, updated getBoolean to use Boolean object instead of boolean primitive and return false if value was null:

public boolean getBoolean(String columnLabel) throws SQLException {
  try {
    Boolean r = result.getProperty(columnLabel);
    if (r==null) {
      lastReadWasNull = true;
      return false;
    } else {
        return r;
    }
  } catch (Exception e) {
    throw new SQLException(
        "An error occurred during the retrieval of the boolean value at column '" + columnLabel + "' ---> " + result.toElement()
            .toJSON(), e);
  }

}
@luigidellaquila
Copy link
Member

Hi @escabe

I just pushed a fix, it will be released with v 3.0.12

Thanks

Luigi

@luigidellaquila luigidellaquila added this to the 3.0.12 milestone Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants