DBZ-3497 Handle MySQL DATE default value parser to accept timestamp
This commit is contained in:
parent
e6d1beb697
commit
5156ea0d16
@ -47,6 +47,14 @@ public class MySqlDefaultValueConverter {
|
||||
|
||||
private static final String EPOCH_DATE = "1970-01-01";
|
||||
|
||||
private static final DateTimeFormatter DATE_WITH_OPTIONAL_TIME_FORMATTER_BUILDER = new DateTimeFormatterBuilder()
|
||||
.append(DateTimeFormatter.ISO_LOCAL_DATE)
|
||||
.optionalStart()
|
||||
.appendLiteral(" ")
|
||||
.append(DateTimeFormatter.ISO_LOCAL_TIME)
|
||||
.optionalEnd()
|
||||
.toFormatter();
|
||||
|
||||
private final MySqlValueConverters converters;
|
||||
|
||||
public MySqlDefaultValueConverter(MySqlValueConverters converters) {
|
||||
@ -99,23 +107,24 @@ public Object convert(Column column, String value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string object for an object type of {@link LocalDate}.
|
||||
* Converts a string object for an object type of {@link LocalDate} or {@link LocalDateTime} in case of MySql Date type.
|
||||
* If the column definition allows null and default value is 0000-00-00, we need return null;
|
||||
* else 0000-00-00 will be replaced with 1970-01-01;
|
||||
*
|
||||
* @param column the column definition describing the {@code data} value; never null
|
||||
* @param value the string object to be converted into a {@link LocalDate} type;
|
||||
* @param value the string object to be converted into a {@link LocalDate} type or {@link LocalDateTime} in case of MySql Date type;
|
||||
* @return the converted value;
|
||||
*/
|
||||
private Object convertToLocalDate(Column column, String value) {
|
||||
final boolean zero = EPOCH_EQUIVALENT_DATE.matcher(value).matches() || "0".equals(value);
|
||||
final boolean zero = EPOCH_EQUIVALENT_DATE.matcher(value).matches() || EPOCH_EQUIVALENT_TIMESTAMP.matcher(value).matches() || "0".equals(value);
|
||||
|
||||
if (zero && column.isOptional()) {
|
||||
return null;
|
||||
}
|
||||
if (zero) {
|
||||
value = EPOCH_DATE;
|
||||
}
|
||||
return LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse(value));
|
||||
return LocalDate.from(DATE_WITH_OPTIONAL_TIME_FORMATTER_BUILDER.parse(value));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -394,7 +394,8 @@ public void parseDateDefaultValue() {
|
||||
" E date NOT NULL DEFAULT '9999-09-09'," +
|
||||
" F date NOT NULL DEFAULT '1111-11-11'," +
|
||||
" G date NOT NULL DEFAULT '2018-08-31'," +
|
||||
" H date NOT NULL DEFAULT 0" +
|
||||
" H date NOT NULL DEFAULT '2050-01-01 00:00:00'," +
|
||||
" I date NOT NULL DEFAULT 0" +
|
||||
");";
|
||||
parser.parse(sql, tables);
|
||||
Table table = tables.forTable(new TableId(null, null, "DATE_TABLE"));
|
||||
@ -405,7 +406,8 @@ public void parseDateDefaultValue() {
|
||||
assertThat(table.columnWithName("E").defaultValue()).isEqualTo(Date.from(ZonedDateTime.of(9999, 9, 9, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()));
|
||||
assertThat(table.columnWithName("F").defaultValue()).isEqualTo(Date.from(ZonedDateTime.of(1111, 11, 11, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()));
|
||||
assertThat(table.columnWithName("G").defaultValue()).isEqualTo(Date.from(ZonedDateTime.of(2018, 8, 31, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()));
|
||||
assertThat(table.columnWithName("H").defaultValue()).isEqualTo((Date.from(Instant.ofEpochMilli(0))));
|
||||
assertThat(table.columnWithName("H").defaultValue()).isEqualTo(Date.from(ZonedDateTime.of(2050, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant()));
|
||||
assertThat(table.columnWithName("I").defaultValue()).isEqualTo((Date.from(Instant.ofEpochMilli(0))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user