yzeng1618 commented on code in PR #10048:
URL: https://github.com/apache/seatunnel/pull/10048#discussion_r2544127924


##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresJdbcRowConverter.java:
##########
@@ -69,6 +73,47 @@ public String converterName() {
         return DatabaseIdentifier.POSTGRESQL;
     }
 
+    @Override
+    protected void setValueToStatementByDataType(
+            Object value,
+            PreparedStatement statement,
+            SeaTunnelDataType<?> seaTunnelDataType,
+            int statementIndex,
+            @Nullable String sourceType)
+            throws SQLException {
+        if (seaTunnelDataType.getSqlType().equals(SqlType.TIMESTAMP_TZ)) {
+            if (value == null) {
+                statement.setNull(statementIndex, 
java.sql.Types.TIMESTAMP_WITH_TIMEZONE);
+                return;
+            }
+            OffsetDateTime offsetDateTime = (OffsetDateTime) value;
+            try {
+                statement.setObject(statementIndex, offsetDateTime);
+                return;
+            } catch (AbstractMethodError | SQLException e) {
+                try {
+                    PGobject timestampTzObject = new PGobject();
+                    timestampTzObject.setType("timestamptz");
+                    timestampTzObject.setValue(offsetDateTime.toString());
+                    statement.setObject(statementIndex, timestampTzObject);
+                    return;
+                } catch (SQLException pge) {
+                    try {
+                        statement.setTimestamp(
+                                statementIndex, 
Timestamp.from(offsetDateTime.toInstant()));
+                        return;
+                    } catch (SQLException se) {
+                        throw new SQLException(
+                                "Failed to set TIMESTAMP_TZ value for 
PostgreSQL using all methods",
+                                se);
+                    }
+                }
+            }
+        }

Review Comment:
   Changed to always write via PGobject("timestamptz") and removed the nested 
try‑catch blocks.



##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresJdbcRowConverter.java:
##########
@@ -316,4 +366,134 @@ public String microsecondsToIntervalFormatVal(String 
intervalVal) {
         if (seconds > 0) sb.append(seconds).append(" seconds");
         return sb.toString().trim();
     }
+
+    private OffsetDateTime getPostgresOffsetDateTime(ResultSet rs, int 
columnIndex)
+            throws SQLException {
+        // Read the value ONCE to avoid drivers returning null on subsequent 
reads
+        final Object obj;
+        try {
+            obj = rs.getObject(columnIndex);
+        } catch (SQLException e) {
+            log.debug("Failed to get object from ResultSet at column {}", 
columnIndex, e);
+            // Best-effort fallback to string, still only a single read path 
for parsing
+            final String str;
+            try {
+                str = rs.getString(columnIndex);
+            } catch (SQLException se) {
+                log.debug("Failed to get string from ResultSet at column {}", 
columnIndex, se);
+                return null;
+            }
+            return parseTimestampIfPresent(str);
+        }

Review Comment:
   Removed the fallback to getString; now we use a single getObject read path 
and let any SQLException bubble up.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to