diff --git a/src/main/java/com/njcn/influx/query/InfluxQueryWrapper.java b/src/main/java/com/njcn/influx/query/InfluxQueryWrapper.java index c588e84..fdcfb26 100644 --- a/src/main/java/com/njcn/influx/query/InfluxQueryWrapper.java +++ b/src/main/java/com/njcn/influx/query/InfluxQueryWrapper.java @@ -53,6 +53,11 @@ public class InfluxQueryWrapper { */ private Class measurement; + /*** + * 不确定查询表名时,动态指定 + */ + private String measurementName; + /*** * 返回映射实体 */ @@ -79,6 +84,19 @@ public class InfluxQueryWrapper { this.initSql(); } + /*** + * 返回和查询用的不是同一个实体 + * 注意:该构建方法查询的参数必须使用string传递,禁止lambda传递 + * @param measurement 查询目标表 + * @param resultEntity 返回映射实体 + */ + public InfluxQueryWrapper(String measurement, Class resultEntity) { + this.measurementName = measurement; + this.measurement = null; + this.resultEntity = resultEntity; + this.initSql(); + } + /*** * 初始化查询语句 */ @@ -116,6 +134,32 @@ public class InfluxQueryWrapper { } +// /*** +// * 查询的结果属性 +// * @author hongawen +// * @param fieldsStr 属性值 +// * 输出为 select ["influxColumn" as fieldStr]的形式 +// */ +// @SafeVarargs +// public InfluxQueryWrapper select(String... fieldsStr) { +// if (ArrayUtil.isNotEmpty(fieldsStr)) { +// StringBuilder selectFragment = new StringBuilder(); +// Arrays.stream(fieldsStr).forEach(fieldStr -> { +// selectFragment.setLength(0); +// selectFragment.append(StrPool.C_SPACE) +// .append(fieldStr) +// .append(StrPool.C_SPACE) +// .append(InfluxDbSqlConstant.AS) +// .append(StrPool.C_SPACE) +// .append(this.getColumnName(resultEntity, LambdaUtil.columnToString(fieldStr))) +// .append(StrPool.C_SPACE); +// selectColumns.add(selectFragment.toString()); +// }); +// } +// return this; +// } + + /************常见的简单函数处理比如max/min/mean/percentile****************/ /*** @@ -209,6 +253,7 @@ public class InfluxQueryWrapper { selectColumns.add(selectFragment); return this; } + public InfluxQueryWrapper mean(String columnName) { String selectFragment = InfluxDbSqlConstant.AVG + InfluxDbSqlConstant.LBK + @@ -302,6 +347,7 @@ public class InfluxQueryWrapper { selectColumns.add(selectFragment); return this; } + public InfluxQueryWrapper mode(String columnName) { String selectFragment = InfluxDbSqlConstant.MODE + InfluxDbSqlConstant.LBK + @@ -393,6 +439,7 @@ public class InfluxQueryWrapper { selectColumns.add(selectFragment); return this; } + public InfluxQueryWrapper sum(String columnName) { String selectFragment = InfluxDbSqlConstant.SUM + InfluxDbSqlConstant.LBK + @@ -429,6 +476,7 @@ public class InfluxQueryWrapper { selectColumns.add(selectFragment); return this; } + public InfluxQueryWrapper top(String columnName, int num) { String selectFragment = InfluxDbSqlConstant.TOP + InfluxDbSqlConstant.LBK + @@ -504,6 +552,7 @@ public class InfluxQueryWrapper { selectColumns.add(selectFragment); return this; } + @Deprecated public InfluxQueryWrapper derivative(String columnName) { String selectFragment = InfluxDbSqlConstant.DERIVATIVE + @@ -548,7 +597,7 @@ public class InfluxQueryWrapper { return this; } - public InfluxQueryWrapper last(String columnName,String resultName) { + public InfluxQueryWrapper last(String columnName, String resultName) { String selectFragment = InfluxDbSqlConstant.LAST + InfluxDbSqlConstant.LBK + InfluxDbSqlConstant.DQM + @@ -558,7 +607,8 @@ public class InfluxQueryWrapper { InfluxDbSqlConstant.AS + resultName + - StrPool.C_SPACE;; + StrPool.C_SPACE; + ; selectColumns.add(selectFragment); return this; } @@ -641,6 +691,7 @@ public class InfluxQueryWrapper { selectColumns.add(selectFragment); return this; } + public InfluxQueryWrapper max(String columnName) { String selectFragment = InfluxDbSqlConstant.MAX + InfluxDbSqlConstant.LBK + @@ -678,7 +729,6 @@ public class InfluxQueryWrapper { } - /*** * 批量获取指定字段最大值 * @author hongawen @@ -808,6 +858,7 @@ public class InfluxQueryWrapper { selectColumns.add(selectFragment); return this; } + public InfluxQueryWrapper percentile(String columnName, int percent) { String selectFragment = InfluxDbSqlConstant.PERCENTILE + InfluxDbSqlConstant.LBK + @@ -930,6 +981,7 @@ public class InfluxQueryWrapper { conditions.add(selectFragment.toString()); return this; } + public InfluxQueryWrapper ne(String columnName, Object columnValue) { StringBuilder selectFragment = new StringBuilder(); selectFragment.append(columnName) @@ -1129,7 +1181,7 @@ public class InfluxQueryWrapper { return this; } - public InfluxQueryWrapper or( String columnName, List columnValues) { + public InfluxQueryWrapper or(String columnName, List columnValues) { if (CollectionUtil.isEmpty(columnValues)) { throw new RuntimeException("查询数值集合为空,请校验!"); } @@ -1255,16 +1307,26 @@ public class InfluxQueryWrapper { if (CollectionUtil.isEmpty(selectColumns)) { sqlSelect.append(InfluxDbSqlConstant.ALL) .append(InfluxDbSqlConstant.FROM) - .append(InfluxDbSqlConstant.DQM) - .append(((Measurement) measurement.getAnnotation(Measurement.class)).name()) .append(InfluxDbSqlConstant.DQM); + if (Objects.isNull(this.measurement)) { + sqlSelect.append(measurementName) + .append(InfluxDbSqlConstant.DQM); + } else { + sqlSelect.append(((Measurement) measurement.getAnnotation(Measurement.class)).name()) + .append(InfluxDbSqlConstant.DQM); + } } else { //将集合处理成查询属性 sqlSelect.append(String.join(StrPool.COMMA, selectColumns)) .append(InfluxDbSqlConstant.FROM) - .append(InfluxDbSqlConstant.DQM) - .append(((Measurement) measurement.getAnnotation(Measurement.class)).name()) .append(InfluxDbSqlConstant.DQM); + if (Objects.isNull(this.measurement)) { + sqlSelect.append(measurementName) + .append(InfluxDbSqlConstant.DQM); + } else { + sqlSelect.append(((Measurement) measurement.getAnnotation(Measurement.class)).name()) + .append(InfluxDbSqlConstant.DQM); + } } //判断是否有查询条件