Skip to content

Commit

Permalink
Add methods to KiwiJdbc to convert java.sql.Date to LocalDate
Browse files Browse the repository at this point in the history
Add two localDateOrNullFromDate methods, one that accepts a
ResultSet and colum name, and one that accepts a java.sql.Date.
They are both annotated with the Checker Nullable annotation to
indicate null can be returned, and also that the java.sql.Date
argument can be null as well.

Closes #1044
  • Loading branch information
sleberknight committed Oct 7, 2023
1 parent a3e28e2 commit b2a19d0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;

import lombok.experimental.UtilityClass;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -118,6 +120,30 @@ public static LocalDateTime localDateTimeFromTimestamp(Timestamp timestamp) {
return isNull(timestamp) ? null : timestamp.toLocalDateTime();
}

/**
* Returns a {@link LocalDate} from the specified {@link java.sql.Date} column in the given {@link ResultSet}.
*
* @param rs the ResultSet
* @param columnName the date column name
* @return the converted LocalDate or {@code null} if the column was {@code NULL}
* @throws SQLException if there is problem getting the date
*/
@Nullable
public static LocalDate localDateOrNullFromDate(ResultSet rs, String columnName) throws SQLException {
return localDateOrNullFromDate(rs.getDate(columnName));
}

/**
* Returns a {@link LocalDateTime} from the given {@link java.sql.Date}.
*
* @param date the date to convert
* @return the converted LocalDate or {@code null} if the date is {@code null}
*/
@Nullable
public static LocalDate localDateOrNullFromDate(java.sql.@Nullable Date date) {
return isNull(date) ? null : date.toLocalDate();
}

/**
* Returns a {@link ZonedDateTime} in UTC from the specified column in the {@link ResultSet}.
*
Expand Down
48 changes: 47 additions & 1 deletion src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand All @@ -19,7 +20,9 @@
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -194,6 +197,49 @@ void shouldReturnNull_WhenResultSet_ReturnsNullTimestamp() throws SQLException {
}
}

@Nested
class LocalDateFromJavaSqlDate {

@Test
void shouldConvertFromDate() {
var originalDate = LocalDate.of(2023, Month.APRIL, 1);
var date = java.sql.Date.valueOf(originalDate);

assertThat(KiwiJdbc.localDateOrNullFromDate(date)).isEqualTo(originalDate);
}

@Test
void shouldReturnNull_WhenGivenNullDate() {
assertThat(KiwiJdbc.localDateOrNullFromDate(null)).isNull();
}

@Test
void shouldConvertFromResultSet() throws SQLException {
var originalDate = LocalDate.of(2023, Month.SEPTEMBER, 8);
var date = java.sql.Date.valueOf(originalDate);

var resultSet = newMockResultSet();
when(resultSet.getDate(anyString())).thenReturn(date);

assertThat(KiwiJdbc.localDateOrNullFromDate(resultSet, "date_of_birth"))
.isEqualTo(originalDate);

verify(resultSet).getDate("date_of_birth");
verifyNoMoreInteractions(resultSet);
}

@Test
void shouldReturnNull_WhenResultSet_ReturnsNullDate() throws SQLException {
var resultSet = newMockResultSet();
when(resultSet.getDate(anyString())).thenReturn(null);

assertThat(KiwiJdbc.localDateOrNullFromDate(resultSet, "expiration_date")).isNull();

verify(resultSet).getDate("expiration_date");
verifyNoMoreInteractions(resultSet);
}
}

@Nested
class UtcZonedDateTimeFromTimestamp {

Expand Down Expand Up @@ -638,4 +684,4 @@ private static ResultSet newMockResultSet() {
private PreparedStatement newMockPreparedStatement() {
return mock(PreparedStatement.class);
}
}
}

0 comments on commit b2a19d0

Please sign in to comment.