You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/12/02 07:11:48 UTC

[shardingsphere] branch master updated: Add support for `java.sql.Date` for IntervalShardingAlgorithm (#22495)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new a8ab167e133 Add support for `java.sql.Date` for IntervalShardingAlgorithm (#22495)
a8ab167e133 is described below

commit a8ab167e133ba461d2459dd7ca1cb28fa50ffc1b
Author: Ling Hengqian <li...@outlook.com>
AuthorDate: Fri Dec 2 15:11:42 2022 +0800

    Add support for `java.sql.Date` for IntervalShardingAlgorithm (#22495)
---
 .../algorithm/sharding/datetime/IntervalShardingAlgorithm.java |  3 +++
 .../sharding/datetime/IntervalShardingAlgorithmTest.java       | 10 ++++++++++
 2 files changed, 13 insertions(+)

diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
index abdf13b8b7a..e6e1c1bfbb6 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
@@ -332,6 +332,9 @@ public final class IntervalShardingAlgorithm implements StandardShardingAlgorith
         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());
         }
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
index 5b51ac59b58..bfdc8d4272c 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
@@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.sql.Date;
 import java.sql.Timestamp;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -448,4 +449,13 @@ public final class IntervalShardingAlgorithmTest {
                 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));
+    }
 }