Add nextOrThrow method to KiwiJdbc to advance a ResultSet or throw exception #1074
Labels
new feature
A new feature such as a new class, method, package, group of classes, etc.
Milestone
Add a simple
nextOrThrow(ResultSet)
method toKiwiJdbc
which advances aResultSet
, or throws anIllegalStateException
if theResultSet#next
method returnsfalse
. It could also have overloads to accept a message, or a template and parameters.While trivial, this is useful in certain situations, for example when you execute a "count" query or find a record by (unique) primary key, there should always be exactly one row, and if not then something went wrong (or horribly wrong if you got an empty result set from a "count" query).
Without this (trivial) method, it's easy to see code like:
In the above, if
rs.next()
returns false, you won't see any error until later, when you attempt to access data, in the above when attempting to get the "id" field value. It's better to make the assertion and fail earlier, e.g.Now an exception is thrown by
nextOrThrow
with a nicer error message:"user not found with id: 42"
The text was updated successfully, but these errors were encountered: