Skip to content

Commit

Permalink
Add int-to-boolean conversion methods to KiwiJdbc
Browse files Browse the repository at this point in the history
* Add overloaded booleanFromInt methods. Both accept a ResultSet
  and a String columnName, but one accepts a BooleanConversionOption
  argument while the other doesn't. The one that does not accept
  a BooleanConversionOption uses the ZERO_OR_ONE option.

Closes #1046
  • Loading branch information
sleberknight committed Oct 7, 2023
1 parent ea327a4 commit eb14254
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,36 @@ public static boolean booleanFromLong(ResultSet rs, String columnName, BooleanCo
return KiwiPrimitives.booleanFromLong(rs.getLong(columnName), option);
}

/**
* Converts an int value in the specified column to a boolean. The database value must be zero, one
* or NULL.
*
* @param rs the ResultSet
* @param columnName the column name
* @return true if the database value is one, or false if it is zero or NULL
* @throws IllegalArgumentException if the value in the column is not zero, one, or NULL
* @throws SQLException if there is any error getting the value from the database
* @see #booleanFromInt(ResultSet, String, BooleanConversionOption)
*/
public static boolean booleanFromInt(ResultSet rs, String columnName) throws SQLException {
return booleanFromInt(rs, columnName, BooleanConversionOption.ZERO_OR_ONE);
}

/**
* Converts an int value in the specified column to a boolean using the given {@link BooleanConversionOption}.
*
* @param rs the ResultSet
* @param columnName the column name
* @param option how to convert the int value into a boolean
* @return the converted value, determined using the conversion option
* @throws SQLException if there is any error getting the value from the database
*/
public static boolean booleanFromInt(ResultSet rs, String columnName, BooleanConversionOption option)
throws SQLException {

return KiwiPrimitives.booleanFromInt(rs.getInt(columnName), option);
}

/**
* Sets the {@link Timestamp} value in a null-safe manner by using the {@link PreparedStatement#setNull(int, int)}
* method for {@code null} values. Uses {@link Types#TIMESTAMP} as the SQL type.
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,65 @@ void shouldConvert_WithBooleanConversionOption(long value,
}
}

@Nested
class BooleanFromInt {

@ParameterizedTest
@CsvSource(textBlock = """
1, true,
0, false
""")
void shouldConvert_WithZeroOrOneConversionOption(int value, boolean expectedResult) throws SQLException {
var resultSet = newMockResultSet();
when(resultSet.getInt(anyString())).thenReturn(value);

assertThat(KiwiJdbc.booleanFromInt(resultSet, "is_admin"))
.isEqualTo(expectedResult);

verify(resultSet).getInt("is_admin");
verifyNoMoreInteractions(resultSet);
}

@ParameterizedTest
@ValueSource(ints = { -1, 2, 4, 42 })
void shouldThrowIllegalArgument_WhenValueFromResultSet_IsNotZeroOrOne_WithZeroOrOneConversionOption(int value)
throws SQLException {

var resultSet = newMockResultSet();
when(resultSet.getInt(anyString())).thenReturn(value);

assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiJdbc.booleanFromInt(resultSet, "is_active"))
.withMessage("value must be 0 or 1, but found %d", value);

verify(resultSet).getInt("is_active");
verifyNoMoreInteractions(resultSet);
}

@ParameterizedTest
@CsvSource(textBlock = """
1, ZERO_OR_ONE, true,
1, NON_ZERO_AS_TRUE, true,
-1, NON_ZERO_AS_TRUE, true,
2, NON_ZERO_AS_TRUE, true,
1000, NON_ZERO_AS_TRUE, true,
0, ZERO_OR_ONE, false,
0, NON_ZERO_AS_TRUE, false
""")
void shouldConvert_WithBooleanConversionOption(int value,
BooleanConversionOption option,
boolean expectedResult) throws SQLException {
var resultSet = newMockResultSet();
when(resultSet.getInt(anyString())).thenReturn(value);

assertThat(KiwiJdbc.booleanFromInt(resultSet, "is_admin", option))
.isEqualTo(expectedResult);

verify(resultSet).getInt("is_admin");
verifyNoMoreInteractions(resultSet);
}
}

@Nested
class NullSafeSetInt {

Expand Down

0 comments on commit eb14254

Please sign in to comment.