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 method to KiwiJdbc to get trimmed string values or null when blank #1061

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
15 changes: 15 additions & 0 deletions src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,21 @@ public static String stringOrNullIfBlank(ResultSet rs, String columnName) throws
return stringOrNullIfBlank(rs, columnName, StringTrimOption.PRESERVE);
}

/**
* Returns a String from the specified column in the {@link ResultSet}, trimming leading and trailing
* whitespace. When the database value is {@code NULL} or contains only whitespace, returns {@code null}.
*
* @param rs the ResultSet
* @param columnName the date column name
* @return the String with leading and trailing whitespace removed, or {@code null} if the
* column was blank or {@code NULL}
* @throws SQLException if there is any error getting the value from the database
*/
@Nullable
public static String trimmedStringOrNullIfBlank(ResultSet rs, String columnName) throws SQLException {
return stringOrNullIfBlank(rs, columnName, StringTrimOption.REMOVE);
}

/**
* Enum representing options for trimming strings.
*/
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,43 @@ void shouldReturn_ExactStringValue_FromResultSet(String phrase) throws SQLExcept
}
}

@Nested
class TrimmedStringOrNullIfBlank {

@ParameterizedTest
@BlankStringSource
void shouldReturnNull_WhenValue_IsBlank(String value) throws SQLException {
var resultSet = newMockResultSet();
when(resultSet.getString(anyString())).thenReturn(value);

assertThat(KiwiJdbc.trimmedStringOrNullIfBlank(resultSet, "comment")).isNull();

verify(resultSet).getString("comment");
verifyNoMoreInteractions(resultSet);
}

@ParameterizedTest
@ValueSource(strings = {
"alice",
"Sphinx of black quartz, judge my vow",
" that was a pangram",
"and so is this... ",
" The five boxing\r\nwizards jump quickly ",
" and also this one \r\n ",
"Pack my box\r\nwith five dozen\r\nliquor jugs"
})
void shouldReturn_TrimmedStringValue_FromResultSet(String phrase) throws SQLException {
var resultSet = newMockResultSet();
when(resultSet.getString(anyString())).thenReturn(phrase);

var trimmed = phrase.strip();
assertThat(KiwiJdbc.trimmedStringOrNullIfBlank(resultSet, "phrase")).isEqualTo(trimmed);

verify(resultSet).getString("phrase");
verifyNoMoreInteractions(resultSet);
}
}

@Nested
class StringOrNullIfBlankWithTrimOption {

Expand Down