lonyee1989 edited a comment on issue #3203: sharding is not support 
LocalDateTime type field
URL: 
https://github.com/apache/incubator-shardingsphere/issues/3203#issuecomment-540498934
 
 
   @terrymanu shardingsphere defaulf jdk version is 1.7
   
   I've extended this feature on fork and upgraded the JDK to 1.8 for 
functionality.
   
   `4.0.0-RC2-1.8`
   https://github.com/lonyee1989/incubator-shardingsphere/tree/4.0.0-RC2-1.8
   
   _Mybatis 5+ is mainly supported_
   
   ## The implementation code
   
   1. package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
   ```java
       /**
        * Convert value via expected class type.
        * 
        * @param value original value
        * @param convertType expected class type
        * @return converted value
        */
       public static Object convertValue(final Object value, final Class<?> 
convertType) {
           if (null == value) {
               return convertNullValue(convertType);
           } 
           if (value.getClass() == convertType) {
               return value;
           }
           if (LocalDateTime.class.equals(convertType)) {
               return convertLocalDateTimeValue(value, convertType);
           }
           if (LocalDate.class.equals(convertType)) {
               return convertLocalDateValue(value, convertType);
           }
           if (LocalTime.class.equals(convertType)) {
               return convertLocalTimeValue(value, convertType);
           }
   
   
           if (value instanceof Number) {
               return convertNumberValue(value, convertType);
           }
           if (value instanceof Date) {
               return convertDateValue(value, convertType);
           }
           if (value instanceof byte[]) {
               return convertByteArrayValue(value, convertType);
           }
           if (String.class.equals(convertType)) {
               return value.toString();
           } else {
               return value;
           }
       }
   
     ......
   
     private static Object convertLocalDateTimeValue(final Object value, final 
Class<?> convertType) {
           Timestamp timestamp = (Timestamp) value;
           return timestamp.toLocalDateTime();
       }
   
       private static Object convertLocalDateValue(final Object value, final 
Class<?> convertType) {
           Timestamp timestamp = (Timestamp) value;
           return timestamp.toLocalDateTime().toLocalDate();
       }
   
       private static Object convertLocalTimeValue(final Object value, final 
Class<?> convertType) {
           Timestamp timestamp = (Timestamp) value;
           return timestamp.toLocalDateTime().toLocalTime();
       }
   
   ```
   
   2. package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
   ```java
       @Override
       public <T> T getObject(final int columnIndex, final Class<T> type) 
throws SQLException {
           if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) 
|| LocalTime.class.equals(type)) {
               return (T) 
ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, 
Timestamp.class), type);
           }
           throw new SQLFeatureNotSupportedException("getObject with type");
       }
   
       @Override
       public <T> T getObject(final String columnLabel, final Class<T> type) 
throws SQLException {
           if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) 
|| LocalTime.class.equals(type)) {
               return (T) 
ResultSetUtil.convertValue(mergeResultSet.getValue(columnLabel, 
Timestamp.class), type);
           }
           throw new SQLFeatureNotSupportedException("getObject with type");
       }
   
   ```
   
   3. package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
   ```java
       @Override
       public <T> T getObject(final int columnIndex, final Class<T> type) 
throws SQLException {
           if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) 
|| LocalTime.class.equals(type)) {
               return (T) 
ResultSetUtil.convertValue(resultSet.getValue(columnIndex, Timestamp.class), 
type);
           }
           throw new SQLFeatureNotSupportedException("getObject with type");
       }
   
       @Override
       public <T> T getObject(final String columnLabel, final Class<T> type) 
throws SQLException {
           if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) 
|| LocalTime.class.equals(type)) {
               return (T) 
ResultSetUtil.convertValue(resultSet.getValue(columnLabel, Timestamp.class), 
type);
           }
           throw new SQLFeatureNotSupportedException("getObject with type");
       }
   
   ```
   
   4. package org.apache.shardingsphere.shardingjdbc.jdbc.unsupported;
   ```java
       @Override
       public <T> T getObject(final int columnIndex, final Class<T> type) 
throws SQLException {
           throw new SQLFeatureNotSupportedException("getObject with type");
       }
       
       @Override
       public <T> T getObject(final String columnLabel, final Class<T> type) 
throws SQLException {
           throw new SQLFeatureNotSupportedException("getObject with type");
       }
   ```
   
   
   

----------------------------------------------------------------
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