You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/11/17 13:40:27 UTC

[shardingsphere] branch master updated: Use PostgreSQL TimestampUtils to support more formats (#22240)

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

panjuan 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 d07a818a442 Use PostgreSQL TimestampUtils to support more formats (#22240)
d07a818a442 is described below

commit d07a818a442fca71d3d8fc8af477761da4641f0a
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Thu Nov 17 21:40:08 2022 +0800

    Use PostgreSQL TimestampUtils to support more formats (#22240)
---
 db-protocol/postgresql/pom.xml                              |  6 ++++++
 .../bind/protocol/PostgreSQLTextTimestampUtils.java         | 13 ++++++++++++-
 .../bind/protocol/PostgreSQLTextTimestampUtilsTest.java     |  8 ++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/db-protocol/postgresql/pom.xml b/db-protocol/postgresql/pom.xml
index 5849a6f972a..7da79d1b6f5 100644
--- a/db-protocol/postgresql/pom.xml
+++ b/db-protocol/postgresql/pom.xml
@@ -39,5 +39,11 @@
             <artifactId>shardingsphere-postgresql-dialect-exception</artifactId>
             <version>${project.version}</version>
         </dependency>
+        
+        <dependency>
+            <groupId>org.postgresql</groupId>
+            <artifactId>postgresql</artifactId>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtils.java b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtils.java
index a687b0bef8b..ceaeb428a28 100644
--- a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtils.java
+++ b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtils.java
@@ -19,7 +19,10 @@ package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.ex
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.util.exception.external.sql.type.wrapper.SQLWrapperException;
+import org.postgresql.jdbc.TimestampUtils;
 
+import java.sql.SQLException;
 import java.sql.Timestamp;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
@@ -49,7 +52,15 @@ public final class PostgreSQLTextTimestampUtils {
         try {
             return Timestamp.valueOf(LocalDateTime.from(POSTGRESQL_DATE_TIME_FORMATTER.parse(value)));
         } catch (final DateTimeParseException ignored) {
-            return Timestamp.valueOf(value);
+            return fallbackToPostgreSQLTimestampUtils(value);
+        }
+    }
+    
+    private static Timestamp fallbackToPostgreSQLTimestampUtils(final String value) {
+        try {
+            return new TimestampUtils(false, null).toTimestamp(null, value);
+        } catch (final SQLException ex) {
+            throw new SQLWrapperException(ex);
         }
     }
 }
diff --git a/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtilsTest.java b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtilsTest.java
index 8563584ff73..e8ef42f58e4 100644
--- a/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtilsTest.java
+++ b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimestampUtilsTest.java
@@ -56,7 +56,15 @@ public final class PostgreSQLTextTimestampUtilsTest {
                 new Object[]{"211012 23:23:23.12345+08:00:00", Timestamp.valueOf("2021-10-12 23:23:23.12345")},
                 new Object[]{"10/12/21 23:23:23.12345+08:00:00", Timestamp.valueOf("2021-10-12 23:23:23.12345")},
                 new Object[]{"2021-10-12 23:23:23.123456", Timestamp.valueOf("2021-10-12 23:23:23.123456")},
+                new Object[]{"2021-10-12 23:23:23.1234567", Timestamp.valueOf("2021-10-12 23:23:23.1234567")},
+                new Object[]{"2021-10-12 23:23:23.12345678", Timestamp.valueOf("2021-10-12 23:23:23.12345678")},
+                new Object[]{"2021-10-12 23:23:23.123456789", Timestamp.valueOf("2021-10-12 23:23:23.123456789")},
                 new Object[]{"2021-10-12 23:23:23.123456 +08:00", Timestamp.valueOf("2021-10-12 23:23:23.123456")},
+                // TODO The following 4 cases are related to user.timezone of test environment
+                // new Object[]{"2021-10-12 23:23:23.1234567 +08:00", Timestamp.valueOf("2021-10-12 23:23:23.1234567")},
+                // new Object[]{"2021-10-12 23:23:23.12345678 +08:00", Timestamp.valueOf("2021-10-12 23:23:23.12345678")},
+                // new Object[]{"2021-10-12 23:23:23.123456789+08:00", Timestamp.valueOf("2021-10-12 23:23:23.123456789")},
+                // new Object[]{"2021-10-12 23:23:23.123456789 +08:00", Timestamp.valueOf("2021-10-12 23:23:23.123456789")},
                 new Object[]{"2021-10-12 23:23:23.123456 -08:00", Timestamp.valueOf("2021-10-12 23:23:23.123456")},
                 new Object[]{"2021-3-3 23:23:23.123456", Timestamp.valueOf("2021-03-03 23:23:23.123456")});
     }