diff --git a/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java b/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java index 049bbeae..f8ef1ab2 100644 --- a/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java +++ b/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java @@ -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. diff --git a/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java b/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java index 7568b1e8..02988417 100644 --- a/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java +++ b/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java @@ -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 {