Skip to content

Commit

Permalink
Add support for java.sql.Date for IntervalShardingAlgorithm (#22495)
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian authored Dec 2, 2022
1 parent 2bf753d commit a8ab167
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ private String getDateTimeText(final Comparable<?> endpoint) {
if (endpoint instanceof TemporalAccessor) {
return dateTimeFormatter.format((TemporalAccessor) endpoint);
}
if (endpoint instanceof java.sql.Date) {
return dateTimeFormatter.format(((java.sql.Date) endpoint).toLocalDate().atStartOfDay(ZoneId.systemDefault()));
}
if (endpoint instanceof Date) {
return dateTimeFormatter.format(((Date) endpoint).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.Before;
import org.junit.Test;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -448,4 +449,13 @@ public void assertIntegerInJDBCType() {
new RangeShardingValue<>("t_order", "create_time", DATA_NODE_INFO, Range.closed("04", "10")));
assertThat(actualAsMonthString.size(), is(4));
}

@Test
public void assertDateInSqlDate() {
Collection<String> actualAsLocalDate = shardingAlgorithmByJDBCDate.doSharding(availableTablesForJDBCDateDataSources,
new RangeShardingValue<>("t_order", "create_time", DATA_NODE_INFO,
Range.closed(new Date(LocalDate.of(2021, 6, 15).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()),
new Date(LocalDate.of(2021, 7, 31).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()))));
assertThat(actualAsLocalDate.size(), is(24));
}
}

0 comments on commit a8ab167

Please sign in to comment.