wuchong commented on a change in pull request #9236: [FLINK-13283][jdbc] Fix 
JDBC connectors with DataTypes.DATE/TIME/TIMESTAMP support
URL: https://github.com/apache/flink/pull/9236#discussion_r308043685
 
 

 ##########
 File path: 
flink-connectors/flink-jdbc/src/main/java/org/apache/flink/api/java/io/jdbc/JDBCUtils.java
 ##########
 @@ -110,13 +117,35 @@ public static void setField(PreparedStatement upload, 
int type, Object field, in
                                                upload.setBigDecimal(index + 1, 
(java.math.BigDecimal) field);
                                                break;
                                        case java.sql.Types.DATE:
-                                               upload.setDate(index + 1, 
(java.sql.Date) field);
+                                               if (field instanceof Date) {
+                                                       upload.setDate(index + 
1, (Date) field);
+                                               } else {
+                                                       
Preconditions.checkArgument(
+                                                               field 
instanceof LocalDate,
+                                                               "Field must be 
either java.sql.Date or java.time.LocalDate");
+                                                       upload.setDate(index + 
1, java.sql.Date.valueOf((LocalDate) field));
+                                               }
                                                break;
                                        case java.sql.Types.TIME:
-                                               upload.setTime(index + 1, 
(java.sql.Time) field);
+                                               if (field instanceof Time) {
+                                                       upload.setTime(index + 
1, (Time) field);
+                                               } else {
+                                                       
Preconditions.checkArgument(
+                                                               field 
instanceof LocalTime,
+                                                               "Field must be 
either java.sql.Time or java.time.LocalTime");
+                                                       upload.setTime(index + 
1, Time.valueOf((LocalTime) field));
+                                               }
                                                break;
                                        case java.sql.Types.TIMESTAMP:
-                                               upload.setTimestamp(index + 1, 
(java.sql.Timestamp) field);
+                                               if (field instanceof Timestamp) 
{
+                                                       
upload.setTimestamp(index + 1, (Timestamp) field);
+                                               } else {
+                                                       
Preconditions.checkArgument(
+                                                               field 
instanceof LocalDateTime,
+                                                               "Field must be 
either java.sql.Timestamp or java.time.LocalDateTime");
+                                                       upload.setTimestamp(
+                                                               index + 1, 
Timestamp.valueOf((LocalDateTime) field));
+                                               }
 
 Review comment:
   I would like to use `else if` instead of `checkArgument` here. Because we 
many support more datetime type in the future (e.g. `Instant`, 
`OffsetDateTime`).
   
   ```java
   if (field instanceof Timestamp) {
                                                        
upload.setTimestamp(index + 1, (Timestamp) field);
                                                } else if (field instanceof 
LocalDateTime) {
                                                        
upload.setTimestamp(index + 1, Timestamp.valueOf((LocalDateTime) field));
                                                } else {
                                                        throw new 
IllegalArgumentException("Field must be either java.sql.Timestamp or 
java.time.LocalDateTime, but is " + field.getClass());
                                                }
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to