Skip to content

Commit

Permalink
added DatabaseQueryBuilder.singleInt and fixed DatabaseQueryBuilder.s…
Browse files Browse the repository at this point in the history
…ingleLong
  • Loading branch information
jhannes committed Aug 9, 2024
1 parent 3f535ad commit f085702
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/main/java/org/fluentjdbc/DatabaseQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ default SingleRow<String> singleString(Connection connection, String fieldName)
return singleObject(connection, row -> row.getString(fieldName));
}

/**
* Returns a int from the specified column name
*
* @param connection Database connection
* @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
* @throws MultipleRowsReturnedException if more than one row was matched the query
*/
@Nonnull
@CheckReturnValue
default SingleRow<Integer> singleInt(Connection connection, final String fieldName) {
return singleObject(connection, row -> row.getInt(fieldName));
}

/**
* Returns a long from the specified column name
*
Expand All @@ -59,7 +72,7 @@ default SingleRow<String> singleString(Connection connection, String fieldName)
*/
@Nonnull
@CheckReturnValue
default SingleRow<Number> singleLong(Connection connection, final String fieldName) {
default SingleRow<Long> singleLong(Connection connection, final String fieldName) {
return singleObject(connection, row -> row.getLong(fieldName));
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/fluentjdbc/DatabaseTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void shouldThrowIfSingleQueryReturnsMultipleRows() {
table.insert().setField("code", 123).setField("name", "the same name").execute(connection);
table.insert().setField("code", 456).setField("name", "the same name").execute(connection);

assertThatThrownBy(() -> table.where("name", "the same name").singleLong(connection, "code"))
assertThatThrownBy(() -> table.where("name", "the same name").singleInt(connection, "code"))
.isInstanceOf(MultipleRowsReturnedException.class);
}

Expand Down

0 comments on commit f085702

Please sign in to comment.