This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 4019e74c46a Optimize the timezone support of
PostgreSQLDateValueParser. (#32226)
4019e74c46a is described below
commit 4019e74c46ac47e50e24ef0131c09b4b8a18ce22
Author: Cong Hu <[email protected]>
AuthorDate: Tue Jul 23 18:40:32 2024 +0800
Optimize the timezone support of PostgreSQLDateValueParser. (#32226)
---
.../text/impl/PostgreSQLDateValueParser.java | 23 +++++++++++++++++++++-
.../text/impl/PostgreSQLDateValueParserTest.java | 9 ++++++---
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/text/impl/PostgreSQLDateValueParser.java
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/text/impl/PostgreSQLDateValueParser.java
index 7f94c7a2625..5e6eabe25e5 100644
---
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/text/impl/PostgreSQLDateValueParser.java
+++
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/text/impl/PostgreSQLDateValueParser.java
@@ -18,16 +18,37 @@
package
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.text.impl;
import
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.text.PostgreSQLTextValueParser;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException;
+import org.postgresql.jdbc.TimestampUtils;
import java.sql.Date;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
/**
* Date value parser of PostgreSQL.
*/
public final class PostgreSQLDateValueParser implements
PostgreSQLTextValueParser<Date> {
+ private static final DateTimeFormatter POSTGRESQL_DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern(
+ "yyyy-MM-dd[ ][G][ ][XXXXX][XXX][X]");
+
@Override
public Date parse(final String value) {
- return Date.valueOf(value);
+ try {
+ return
Date.valueOf(LocalDate.from(POSTGRESQL_DATE_TIME_FORMATTER.parse(value)));
+ } catch (final DateTimeParseException ignored) {
+ return fallbackToPostgreSQLTimestampUtils(value);
+ }
+ }
+
+ private static Date fallbackToPostgreSQLTimestampUtils(final String value)
{
+ try {
+ return new TimestampUtils(false, null).toDate(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/text/impl/PostgreSQLDateValueParserTest.java
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/text/impl/PostgreSQLDateValueParserTest.java
index a2683083d5b..12cdda7e287 100644
---
a/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/text/impl/PostgreSQLDateValueParserTest.java
+++
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/text/impl/PostgreSQLDateValueParserTest.java
@@ -19,9 +19,9 @@ package
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.ex
import org.junit.jupiter.api.Test;
+import java.sql.Date;
import java.time.LocalDate;
-import java.time.ZoneId;
-import java.util.Date;
+import java.time.ZoneOffset;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@@ -30,6 +30,9 @@ class PostgreSQLDateValueParserTest {
@Test
void assertParse() {
- assertThat(new PostgreSQLDateValueParser().parse("2020-01-01"),
is(Date.from(LocalDate.of(2020, 1,
1).atStartOfDay(ZoneId.systemDefault()).toInstant())));
+ assertThat(new PostgreSQLDateValueParser().parse("2020-01-01"),
is(Date.valueOf(LocalDate.of(2020, 1, 1))));
+ assertThat(new PostgreSQLDateValueParser().parse("2020-01-01 +08"),
is(Date.valueOf(LocalDate.of(2020, 1,
1).atStartOfDay(ZoneOffset.of("+8")).toLocalDate())));
+ assertThat(new PostgreSQLDateValueParser().parse("2020-01-01 +08:00"),
is(Date.valueOf(LocalDate.of(2020, 1,
1).atStartOfDay(ZoneOffset.of("+8")).toLocalDate())));
+ assertThat(new PostgreSQLDateValueParser().parse("2020-01-01
+08:00:00"), is(Date.valueOf(LocalDate.of(2020, 1,
1).atStartOfDay(ZoneOffset.of("+8")).toLocalDate())));
}
}