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

Add nextOrThrow method to KiwiJdbc to advance a ResultSet or throw exception #1074

Closed
sleberknight opened this issue Nov 16, 2023 · 0 comments · Fixed by #1077
Closed

Add nextOrThrow method to KiwiJdbc to advance a ResultSet or throw exception #1074

sleberknight opened this issue Nov 16, 2023 · 0 comments · Fixed by #1077
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

Add a simple nextOrThrow(ResultSet) method to KiwiJdbc which advances a ResultSet, or throws an IllegalStateException if the ResultSet#next method returns false. 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:

// after executing a query to find a 'user' record by primary key...
rs.next();

// if above returns false, nothing happens until...

// use the ResultSet to map into an object, e.g.
var user = User.builder()
    .id(rs.getLong("id"))    // ...an exception is thrown here!
    .username(rs.getString("username"))
    //  set more properties...
    .build();

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.

// after executing a query to find a 'user' record by primary key...
nextOrThrow(rs, "user not found with id {}", id);

// use the ResultSet to map into an object, e.g.
var user = User.builder()
    .id(rs.getLong("id"))
    .username(rs.getString("username"))
    //  set more properties...
    .build();

Now an exception is thrown by nextOrThrow with a nicer error message: "user not found with id: 42"

@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Nov 16, 2023
@sleberknight sleberknight self-assigned this Nov 18, 2023
@sleberknight sleberknight added this to the 3.1.1 milestone Nov 18, 2023
sleberknight added a commit that referenced this issue Nov 18, 2023
* Add three overloaded nextOrThrow methods to KiwiJdbc
* The different variants allow for a default message, a custom
  message, and a message template with arguments

Closes #1074
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant