java: Fix handling of SQL NULL in Row class.#1461
Conversation
|
It seems strange to do it this way. For one thing, it's not thread-safe to ask about the "last" call. |
|
I agree it's strange, but it's the way JDBC does it. If we don't do it that way, we would have type-conversion methods that are 99% similar to JDBC. I think going that extra 1% is worth it for two reasons: (1) it's immediately familiar to anyone who's used JDBC, and (2) we are planning to wrap this in a JDBC-compatible interface anyway, and recommend that over our low-level interface whenever possible. For users who want getters that return wrapped types (e.g. Long), they can get something equivalent to that by calling |
There was a problem hiding this comment.
Should this be 0 if null, too, to be consistent?
There was a problem hiding this comment.
I thought about that, but in my opinion the least ambiguous convention is "return null for any getter where it is possible to return null". This seems to be the convention followed by JDBC, which returns null for BigDecimal getBigDecimal().
|
Ok, I still think it's strange but it's up to you. I just have two comments which you can resolve as you wish. LGTM. |
Previously, getters that convert to primitive types (like `long getLong()`) would throw NullPointerException if the value was actually SQL NULL. To better match the expectations set by the JDBC ResultSet interface, which we intend to mimic, we now will instead return 0 when asked to convert SQL NULL to a primitive type. To distinguish between 0 and SQL NULL, the JDBC ResultSet interface provides two mechanisms, which we now support as well: 1. Call `wasNull()` after `getLong()` returns 0. 2. Call `getObject(..., Long.class)` instead of `getLong()` to get a (possibly-null) `Long` instead of a `long`.
java: Fix handling of SQL NULL in Row class.
@erzel
Previously, getters that convert to primitive types
(like
long getLong()) would throw NullPointerException if the valuewas actually SQL NULL. To better match the expectations set by the JDBC
ResultSet interface, which we intend to mimic, we now will instead
return 0 when asked to convert SQL NULL to a primitive type.
To distinguish between 0 and SQL NULL, the JDBC ResultSet interface
provides two mechanisms, which we now support as well:
wasNull()aftergetLong()returns 0.getObject(..., Long.class)instead ofgetLong()to get a (possibly-null)
Longinstead of along.See also: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html