Skip to content

Commit

Permalink
Add LocalDate, LocalTime, LocalDateTime, ZoneDateTime Converters
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauernfeind committed Jun 7, 2024
1 parent 24f6f65 commit 1bec5c3
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Stream;

Expand Down Expand Up @@ -459,6 +463,54 @@ Object convertStringLiteral(String str) {
}
};
}
if (cls == LocalDate.class) {
return new ColumnTypeConvertor() {
@Override
Object convertStringLiteral(String str) {
if (str.charAt(0) != '\'' || str.charAt(str.length() - 1) != '\'') {
throw new IllegalArgumentException(
"Instant literal not enclosed in single-quotes (\"" + str + "\")");
}
return LocalDate.parse(str.substring(1, str.length() - 1));
}
};
}
if (cls == LocalTime.class) {
return new ColumnTypeConvertor() {
@Override
Object convertStringLiteral(String str) {
if (str.charAt(0) != '\'' || str.charAt(str.length() - 1) != '\'') {
throw new IllegalArgumentException(
"Instant literal not enclosed in single-quotes (\"" + str + "\")");
}
return LocalTime.parse(str.substring(1, str.length() - 1));
}
};
}
if (cls == LocalDateTime.class) {
return new ColumnTypeConvertor() {
@Override
Object convertStringLiteral(String str) {
if (str.charAt(0) != '\'' || str.charAt(str.length() - 1) != '\'') {
throw new IllegalArgumentException(
"Instant literal not enclosed in single-quotes (\"" + str + "\")");
}
return LocalDateTime.parse(str.substring(1, str.length() - 1));
}
};
}
if (cls == ZonedDateTime.class) {
return new ColumnTypeConvertor() {
@Override
Object convertStringLiteral(String str) {
if (str.charAt(0) != '\'' || str.charAt(str.length() - 1) != '\'') {
throw new IllegalArgumentException(
"Instant literal not enclosed in single-quotes (\"" + str + "\")");
}
return ZonedDateTime.parse(str.substring(1, str.length() - 1));
}
};
}
if (cls == Object.class) {
return new ColumnTypeConvertor() {
@Override
Expand Down

0 comments on commit 1bec5c3

Please sign in to comment.