You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by st...@apache.org on 2021/04/01 15:03:21 UTC

[openjpa] 04/04: OPENJPA-2854 fix OffsetTime handling for PostgreSQL

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

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

commit c3bbb92557743928d045e1061360f3bfd48b182e
Author: Mark Struberg <st...@apache.org>
AuthorDate: Thu Apr 1 17:02:25 2021 +0200

    OPENJPA-2854 fix OffsetTime handling for PostgreSQL
    
    PostgreSQL doesn't natively support OffsetTime. While it has a column type
    time with time zone it actually only stores the time as UTC time.
---
 .../openjpa/jdbc/sql/PostgresDictionary.java       | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
index 644442a..a1d6ce9 100644
--- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
+++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
@@ -38,6 +38,8 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.OffsetDateTime;
+import java.time.OffsetTime;
+import java.time.ZoneOffset;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.HashSet;
@@ -750,6 +752,30 @@ public class PostgresDictionary extends DBDictionary {
         return rs.getObject(column, OffsetDateTime.class);
     }
 
+    /**
+     * default column type for OffsetTime is 'time with time zone'.
+     * But opposed to the name PostgreSQL internally stores those values in UTC time
+     * without any timezone.
+     */
+    @Override
+    public void setOffsetTime(PreparedStatement stmnt, int idx, OffsetTime val, Column col) throws SQLException {
+        // this is really a whacky hack somehow
+        // PostgreSQL doesn't support OffsetTime natively.
+        // The JDBC driver will automatically convert this to UTC which is the
+        // internal normalised TimeZone PostgreSQL uses.
+        LocalTime utcTime = val.withOffsetSameInstant(OffsetDateTime.now().getOffset()).toLocalTime();
+        stmnt.setTime(idx, java.sql.Time.valueOf(utcTime));
+    }
+
+    @Override
+    public OffsetTime getOffsetTime(ResultSet rs, int column) throws SQLException {
+        final java.sql.Time utcTime = rs.getTime(column);
+        if (utcTime != null) {
+            return utcTime.toLocalTime().atOffset(OffsetDateTime.now().getOffset());
+        }
+        return null;
+    }
+
     @Override
     public void setLocalDate(PreparedStatement stmnt, int idx, LocalDate val, Column col) throws SQLException {
         stmnt.setObject(idx, val);