diff --git a/src/main/java/com/njcn/influx/ano/IgnoreData.java b/src/main/java/com/njcn/influx/ano/IgnoreData.java new file mode 100644 index 0000000..430b3d0 --- /dev/null +++ b/src/main/java/com/njcn/influx/ano/IgnoreData.java @@ -0,0 +1,22 @@ +package com.njcn.influx.ano; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 判断为空时,是否取消存储该条记录 + * @author hongawen + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface IgnoreData { + + /** + * 用于传参 + * @return boolean 参数内容 + */ + boolean value(); + +} diff --git a/src/main/java/com/njcn/influx/core/InfluxExecutor.java b/src/main/java/com/njcn/influx/core/InfluxExecutor.java index fd4c556..9b1ccda 100644 --- a/src/main/java/com/njcn/influx/core/InfluxExecutor.java +++ b/src/main/java/com/njcn/influx/core/InfluxExecutor.java @@ -3,6 +3,7 @@ package com.njcn.influx.core; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.text.StrPool; import cn.hutool.core.util.ObjectUtil; +import com.njcn.influx.ano.IgnoreData; import com.njcn.influx.constant.InfluxDbSqlConstant; import com.njcn.influx.query.InfluxQueryWrapper; import com.njcn.influx.utils.InstantUtil; @@ -52,7 +53,7 @@ public class InfluxExecutor { List fields = new ArrayList<>(Arrays.asList(declaredFields)); //映射父级内部私有属性 Class superclass = domainClass.getSuperclass(); - if(ObjectUtil.isNotNull(superclass)){ + if (ObjectUtil.isNotNull(superclass)) { Field[] superDeclaredFields = superclass.getDeclaredFields(); List superFields = new ArrayList<>(Arrays.asList(superDeclaredFields)); fields.addAll(superFields); @@ -68,28 +69,33 @@ public class InfluxExecutor { for (QueryResult.Series series : seriesList) { obj = domainClass.newInstance(); Map tags = series.getTags(); - if(CollectionUtil.isNotEmpty(tags)){ + if (CollectionUtil.isNotEmpty(tags)) { //处理tag分组数据 for (String columnName : tags.keySet()) { for (Field declaredField : fields) { if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) { //获取属性定义的类型 declaredField.setAccessible(true); + //判断是否过滤该条记录,当数据返回 null时 + IgnoreData ignoreAnnotation = declaredField.getAnnotation(IgnoreData.class); + if (Objects.nonNull(ignoreAnnotation) && ignoreAnnotation.value() && Objects.isNull(tags.get(columnName))) { + continue; + } //时间格式 if (declaredField.getType() == Instant.class) { - declaredField.set(obj, InstantUtil.stringToInstant(tags.get(columnName).toString().replace("+08:00", "Z"))); + declaredField.set(obj, InstantUtil.stringToInstant(tags.get(columnName).replace("+08:00", "Z"))); //字符串 } else if (declaredField.getType() == String.class) { - declaredField.set(obj, tags.get(columnName).toString()); + declaredField.set(obj, tags.get(columnName)); //浮点双精度 } else if (declaredField.getType() == Double.class) { - declaredField.set(obj, Double.parseDouble(tags.get(columnName).toString())); + declaredField.set(obj, Double.parseDouble(tags.get(columnName))); //浮点 } else if (declaredField.getType() == Float.class) { - declaredField.set(obj, Float.parseFloat(tags.get(columnName).toString())); + declaredField.set(obj, Float.parseFloat(tags.get(columnName))); //整型 } else if (declaredField.getType() == Integer.class) { - declaredField.set(obj, Integer.parseInt(tags.get(columnName).toString())); + declaredField.set(obj, Integer.parseInt(tags.get(columnName))); } } } @@ -107,6 +113,12 @@ public class InfluxExecutor { if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) { //获取属性定义的类型 declaredField.setAccessible(true); + //判断是否过滤该条记录,当数据返回 null时 + IgnoreData ignoreAnnotation = declaredField.getAnnotation(IgnoreData.class); + if (Objects.nonNull(ignoreAnnotation) && ignoreAnnotation.value() && Objects.isNull(columnValue.get(i))) { + object = null; + continue; + } //时间格式 if (declaredField.getType() == Instant.class) { declaredField.set(object, InstantUtil.stringToInstant(columnValue.get(i).toString().replace("+08:00", "Z"))); @@ -126,7 +138,9 @@ public class InfluxExecutor { } } } - results.add((E) object); + if (Objects.nonNull(object)) { + results.add((E) object); + } } } } diff --git a/src/main/java/com/njcn/influx/core/ParameterHandler.java b/src/main/java/com/njcn/influx/core/ParameterHandler.java index 6f1e61d..4be7e76 100644 --- a/src/main/java/com/njcn/influx/core/ParameterHandler.java +++ b/src/main/java/com/njcn/influx/core/ParameterHandler.java @@ -29,8 +29,13 @@ public class ParameterHandler { } if (parameterType == String.class) { - sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", "'" + args[i] + "'"); - sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString()); + if (!args[i].toString().contains("select") && !args[i].toString().contains("SELECT")) { + sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", "'" + args[i] + "'"); + sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString()); + }else{ + sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", args[i].toString()); + sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString()); + } } else { sql = sql.replaceAll("\\#\\{" + parameterName + "\\}", args[i].toString()); sql = sql.replaceAll("\\$\\{" + parameterName + "\\}", args[i].toString());