Skip to content

Commit

Permalink
LocalDateTime support (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Jun 15, 2021
1 parent afca180 commit 7b08fb3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.xream.sqli.parser.BeanElement;
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.parser.Parser;
import io.xream.sqli.support.TimestampSupport;
import io.xream.sqli.support.TimeSupport;
import io.xream.sqli.util.EnumUtil;
import io.xream.sqli.util.SqliStringUtil;

Expand Down Expand Up @@ -182,7 +182,7 @@ default void filter(List<Bb> bbList, Mappable mappable) {
} else {
BeanElement be = parsed.getElement(arr[1]);
if (be != null) {
TimestampSupport.testNumberValueToDate(be.getClz(), bb);
TimeSupport.testWriteNumberValueToTime(be.getClz(), bb);
if (bb.getValue() == null)
ite.remove();
}
Expand All @@ -202,7 +202,7 @@ default void filter(List<Bb> bbList, Mappable mappable) {
} else {
BeanElement be = parsed.getElement(key);
if (be != null) {
TimestampSupport.testNumberValueToDate(be.getClz(), bb);
TimeSupport.testWriteNumberValueToTime(be.getClz(), bb);
if (bb.getValue() == null)
ite.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.parser.Parser;
import io.xream.sqli.support.ResultMapSingleSourceSupport;
import io.xream.sqli.support.TimestampSupport;
import io.xream.sqli.support.TimeSupport;
import io.xream.sqli.util.EnumUtil;
import io.xream.sqli.util.SqliJsonUtil;
import io.xream.sqli.util.SqliStringUtil;
Expand Down Expand Up @@ -233,7 +233,7 @@ private void concatRefresh(StringBuilder sb, Parsed parsed, RefreshCondition ref
throw new ParsingException("can not find the property " + key + " of " + parsed.getClzName());
}

TimestampSupport.testNumberValueToDate(be.getClz(), bb);
TimeSupport.testWriteNumberValueToTime(be.getClz(), bb);

if (SqliStringUtil.isNullOrEmpty(String.valueOf(bb.getValue()))
|| BaseTypeFilter.isBaseType(key, bb.getValue(), parsed)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,35 @@
/**
* @Author Sim
*/
public final class TimestampSupport {
public final class TimeSupport {

public static boolean testNumberValueToDate(Class clzz, Bb bb){
if (clzz == LocalDateTime.class) {
public static Object afterReadTime(Class propertyType, Object obj) {
if (obj instanceof LocalDateTime) {
if (propertyType == Date.class) {
Instant instant = ((LocalDateTime)obj).atZone(ZoneId.systemDefault()).toInstant();
obj = Date.from(instant);
}else if (propertyType == Timestamp.class) {
Instant instant = ((LocalDateTime)obj).atZone(ZoneId.systemDefault()).toInstant();
obj = Timestamp.from(instant);
}else if (propertyType == LocalDate.class) {
obj = ((LocalDateTime)obj).toLocalDate();
}
} else if (obj instanceof Timestamp) {
if (propertyType == LocalDateTime.class) {
obj = Instant.ofEpochMilli(((Timestamp)obj).getTime())
.atZone(ZoneId.systemDefault()).toLocalDateTime();
}else if (propertyType == Date.class) {
obj = new Date(((Timestamp)obj).getTime());
}else if (propertyType == LocalDate.class) {
obj = Instant.ofEpochMilli(((Timestamp)obj).getTime())
.atZone(ZoneId.systemDefault()).toLocalDate();
}
}
return obj;
}

public static boolean testWriteNumberValueToTime(Class propertyType, Bb bb){
if (propertyType == LocalDateTime.class) {
Object v = bb.getValue();
if (v instanceof Long || v instanceof Integer) {
if (Long.valueOf(v.toString()) == 0){
Expand All @@ -44,7 +69,7 @@ public static boolean testNumberValueToDate(Class clzz, Bb bb){
}
}
return true;
}else if (clzz == Date.class) {
}else if (propertyType == Date.class) {
Object v = bb.getValue();
if (v instanceof Long || v instanceof Integer) {
if (Long.valueOf(v.toString()) == 0){
Expand All @@ -54,7 +79,7 @@ public static boolean testNumberValueToDate(Class clzz, Bb bb){
}
}
return true;
} else if (clzz == Timestamp.class) {
} else if (propertyType == Timestamp.class) {
Object v = bb.getValue();
if (v instanceof Long || v instanceof Integer) {
if (Long.valueOf(v.toString()) == 0){
Expand All @@ -64,7 +89,7 @@ public static boolean testNumberValueToDate(Class clzz, Bb bb){
}
}
return true;
} else if (clzz == LocalDate.class) {
} else if (propertyType == LocalDate.class) {
Object v = bb.getValue();
if (v instanceof Long || v instanceof Integer) {
if (Long.valueOf(v.toString()) == 0){
Expand All @@ -76,8 +101,7 @@ public static boolean testNumberValueToDate(Class clzz, Bb bb){
}
return true;
}

return false;
return false;
}

private static long toLongValue(Object v){
Expand All @@ -88,6 +112,4 @@ private static long toLongValue(Object v){
}
}



}
34 changes: 7 additions & 27 deletions sqli-dialect/src/main/java/io/xream/sqli/dialect/MySqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
import io.xream.sqli.builder.SqlScript;
import io.xream.sqli.parser.BeanElement;
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.support.TimeSupport;
import io.xream.sqli.util.EnumUtil;
import io.xream.sqli.util.SqliJsonUtil;
import io.xream.sqli.util.SqliStringUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
Expand Down Expand Up @@ -86,29 +85,10 @@ public Object mappingToObject( Object obj, BeanElement element) {
return new BigDecimal(String.valueOf(obj));
} else if (ec == double.class || ec == Double.class) {
return Double.valueOf(obj.toString());
} else if (obj instanceof LocalDateTime) {
if (ec == Date.class) {
Instant instant = ((LocalDateTime)obj).atZone(ZoneId.systemDefault()).toInstant();
obj = Date.from(instant);
}else if (ec == Timestamp.class) {
Instant instant = ((LocalDateTime)obj).atZone(ZoneId.systemDefault()).toInstant();
obj = Timestamp.from(instant);
}else if (ec == LocalDate.class) {
obj = ((LocalDateTime)obj).toLocalDate();
}
} else if (obj instanceof Timestamp) {
if (ec == LocalDateTime.class) {
obj = Instant.ofEpochMilli(((Timestamp)obj).getTime())
.atZone(ZoneId.systemDefault()).toLocalDateTime();
}else if (ec == Date.class) {
obj = new Date(((Timestamp)obj).getTime());
}else if (ec == LocalDate.class) {
obj = Instant.ofEpochMilli(((Timestamp)obj).getTime())
.atZone(ZoneId.systemDefault()).toLocalDate();
}
} else {
return TimeSupport.afterReadTime(ec,obj);
}

return obj;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.xream.sqli.exception.PersistenceException;
import io.xream.sqli.parser.BeanElement;
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.support.TimeSupport;
import io.xream.sqli.util.EnumUtil;
import io.xream.sqli.util.SqliExceptionUtil;
import io.xream.sqli.util.SqliJsonUtil;
Expand All @@ -34,10 +35,6 @@
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

public class OracleDialect implements Dialect {
Expand Down Expand Up @@ -156,34 +153,15 @@ public Object mappingToObject(Object obj, BeanElement element) {
}
}else if (obj instanceof BigDecimal) {
return toBigDecimal(ec, obj);

} else if (obj instanceof Timestamp && ec == Date.class) {
Timestamp ts = (Timestamp) obj;
return new Date(ts.getTime());
}else if (obj instanceof LocalDateTime) {
if (ec == Date.class) {
Instant instant = ((LocalDateTime)obj).atZone(ZoneId.systemDefault()).toInstant();
obj = Date.from(instant);
}else if (ec == Timestamp.class) {
Instant instant = ((LocalDateTime)obj).atZone(ZoneId.systemDefault()).toInstant();
obj = Timestamp.from(instant);
}else if (ec == LocalDate.class) {
obj = ((LocalDateTime)obj).toLocalDate();
}
}else if (obj instanceof Timestamp) {
if (ec == LocalDateTime.class) {
obj = Instant.ofEpochMilli(((Timestamp)obj).getTime())
.atZone(ZoneId.systemDefault()).toLocalDateTime();
}else if (ec == Date.class) {
obj = new Date(((Timestamp)obj).getTime());
}else if (ec == LocalDate.class) {
obj = Instant.ofEpochMilli(((Timestamp)obj).getTime())
.atZone(ZoneId.systemDefault()).toLocalDate();
}
}else if (EnumUtil.isEnum(ec)) {
} else if (EnumUtil.isEnum(ec)) {
return EnumUtil.deserialize(ec, obj.toString());
} else {
return TimeSupport.afterReadTime(ec,obj);
}
return obj;

}

@Override
Expand Down

0 comments on commit 7b08fb3

Please sign in to comment.