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 int-to-boolean conversion methods to KiwiJdbc #1058

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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