influxORM代码调整
This commit is contained in:
@@ -19,9 +19,4 @@ public @interface Delete {
|
||||
*/
|
||||
String value();
|
||||
|
||||
/***
|
||||
* 数据库名
|
||||
* todo... 处理成获取配置文件内的,减少开发人员编码输出
|
||||
*/
|
||||
String database();
|
||||
}
|
||||
|
||||
@@ -15,8 +15,9 @@ public @interface Select {
|
||||
|
||||
/***
|
||||
* 查询sql
|
||||
* 如果没有sql语句,则是将参数中的实体转为sql语句
|
||||
*/
|
||||
String value();
|
||||
String value() default "nosql";
|
||||
|
||||
/***
|
||||
* 返回映射的实体对象
|
||||
|
||||
@@ -22,7 +22,10 @@ public class InfluxProxyMapperFactory<T> implements FactoryBean {
|
||||
@Override
|
||||
public Object getObject() throws Exception {
|
||||
Object proxyInstance = Proxy.newProxyInstance(
|
||||
interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new ProxyMapper(new ParameterHandler(), executor, new ResultSetHandler()));
|
||||
interfaceClass.getClassLoader(),
|
||||
new Class[]{interfaceClass},
|
||||
new ProxyMapper(new ParameterHandler(), executor, new ResultSetHandler())
|
||||
);
|
||||
return proxyInstance;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.njcn.influx.base;
|
||||
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import com.njcn.influx.ano.Delete;
|
||||
import com.njcn.influx.ano.Select;
|
||||
import com.njcn.influx.ano.Insert;
|
||||
import com.njcn.influx.core.InfluxExecutor;
|
||||
import com.njcn.influx.core.ParameterHandler;
|
||||
import com.njcn.influx.core.ResultSetHandler;
|
||||
import com.njcn.influx.query.InfluxQueryWrapper;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
@@ -27,8 +29,22 @@ public class ProxyMapper implements InvocationHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
public Object invoke(Object proxy, Method method, Object[] args) {
|
||||
Annotation[] annotations = method.getAnnotations();
|
||||
//没有注解的。查看参数是否为InfluxQueryWrapper
|
||||
if (ArrayUtil.isEmpty(annotations)) {
|
||||
Parameter[] parameters = method.getParameters();
|
||||
if (args.length == 1 && args[0] instanceof InfluxQueryWrapper) {
|
||||
InfluxQueryWrapper influxQueryWrapper = (InfluxQueryWrapper) args[0];
|
||||
Class resultType = influxQueryWrapper.getResultEntity();
|
||||
List<Object> resultList = executor.select(influxQueryWrapper.generateSql(), resultType);
|
||||
//根据返回类型返回结果
|
||||
Class<?> returnType = method.getReturnType();
|
||||
return resultSetHandler.handleResultSet(resultList, returnType);
|
||||
}
|
||||
}
|
||||
|
||||
//采用注解的形式,即将sql写在注解里
|
||||
if (annotations.length == 1) {
|
||||
Annotation annotation = annotations[0];
|
||||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||||
@@ -56,11 +72,9 @@ public class ProxyMapper implements InvocationHandler {
|
||||
//拼接sql
|
||||
String sql = deleteAnnotation.value();
|
||||
Parameter[] parameters = method.getParameters();
|
||||
|
||||
String database = deleteAnnotation.database();
|
||||
sql = parameterHandler.handleParameter(parameters, args, sql);
|
||||
//执行sql
|
||||
executor.delete(sql, database);
|
||||
executor.delete(sql);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -25,15 +25,18 @@ public class InfluxDbConfig {
|
||||
@Value("${spring.influx.mapper-location}")
|
||||
private String mapperLocation;
|
||||
|
||||
@Value("${spring.influx.database}")
|
||||
private String database;
|
||||
|
||||
@Bean(name = "influxDbMapper")
|
||||
public InfluxDBMapper influxDBMapper(InfluxDB influxDb) {
|
||||
public InfluxDBMapper influxDbMapper(InfluxDB influxDb) {
|
||||
influxDb.setLogLevel(InfluxDB.LogLevel.BASIC);
|
||||
return new InfluxDBMapper(influxDb);
|
||||
}
|
||||
|
||||
@Bean(name = "influxDbExecutor")
|
||||
public InfluxExecutor executor(InfluxDB influxDb, InfluxDBMapper influxDbMapper) {
|
||||
return new InfluxExecutor(influxDb, influxDbMapper);
|
||||
return new InfluxExecutor(influxDb, influxDbMapper,database);
|
||||
}
|
||||
|
||||
@Bean(name = "proxyMapperRegister")
|
||||
|
||||
@@ -54,15 +54,23 @@ public interface InfluxDbSqlConstant {
|
||||
/**
|
||||
* “DESC ”
|
||||
*/
|
||||
String DESC = "DESC" + StrPool.C_SPACE;
|
||||
String DESC = StrPool.C_SPACE + "DESC" + StrPool.C_SPACE;
|
||||
|
||||
/**
|
||||
* “ASC ”
|
||||
*/
|
||||
String ASC = StrPool.C_SPACE + "ASC" + StrPool.C_SPACE;
|
||||
|
||||
|
||||
/**
|
||||
* " as value "
|
||||
*/
|
||||
String AS = StrPool.C_SPACE + "as" + StrPool.C_SPACE;
|
||||
String AS_VALUE = StrPool.C_SPACE + "as value" + StrPool.C_SPACE;
|
||||
|
||||
String EQ = "=";
|
||||
String QM = "'";
|
||||
String DQM = "\"";
|
||||
String LBK = "(";
|
||||
String RBK = ")";
|
||||
String GT = ">";
|
||||
@@ -73,11 +81,20 @@ public interface InfluxDbSqlConstant {
|
||||
/**
|
||||
* influxDB函数拼接
|
||||
*/
|
||||
String COUNT = "COUNT";
|
||||
String MAX = "MAX";
|
||||
String MIN = "MIN";
|
||||
String AVG = "MEAN";
|
||||
String MEDIAN = "MEDIAN";
|
||||
String MODE = "MODE";
|
||||
String SPREAD = "SPREAD";
|
||||
String SUM = "SUM";
|
||||
String BOTTOM = "BOTTOM";
|
||||
String TOP = "TOP";
|
||||
String LAST = "LAST";
|
||||
String DERIVATIVE = "DERIVATIVE";
|
||||
String NUM_95 = ",95";
|
||||
String CP95 = "PERCENTILE";
|
||||
String PERCENTILE = "PERCENTILE";
|
||||
|
||||
/**
|
||||
* “ tz('Asia/Shanghai')”
|
||||
@@ -102,6 +119,4 @@ public interface InfluxDbSqlConstant {
|
||||
String TIME = "time" + StrPool.C_SPACE;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ public class InfluxExecutor {
|
||||
|
||||
InfluxDB influxDb;
|
||||
InfluxDBMapper influxDbMapper;
|
||||
String database;
|
||||
|
||||
/***
|
||||
* 查询数据方法
|
||||
* @author hongawen
|
||||
@@ -29,7 +31,7 @@ public class InfluxExecutor {
|
||||
* @param domainClass 映射实体
|
||||
*/
|
||||
public <E> List<E> select(String sql, Class domainClass) {
|
||||
List<E> results = influxDbMapper.query(new Query(dealTimeZone(sql)), domainClass);
|
||||
List<E> results = influxDbMapper.query(new Query(dealTimeZone(sql), database), domainClass);
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -59,7 +61,6 @@ public class InfluxExecutor {
|
||||
}
|
||||
//获取数据库名和rp
|
||||
Measurement measurement = firstObj.getClass().getAnnotation(Measurement.class);
|
||||
String database = measurement.database();
|
||||
String retentionPolicy = measurement.retentionPolicy();
|
||||
BatchPoints batchPoints = BatchPoints
|
||||
.builder()
|
||||
@@ -69,8 +70,15 @@ public class InfluxExecutor {
|
||||
influxDb.write(batchPoints);
|
||||
}
|
||||
} else {
|
||||
Class<?> domainClass = obj.getClass();
|
||||
Point point = Point
|
||||
.measurementByPOJO(domainClass)
|
||||
.addFieldsFromPOJO(obj)
|
||||
.build();
|
||||
Measurement measurement = obj.getClass().getAnnotation(Measurement.class);
|
||||
String retentionPolicy = measurement.retentionPolicy();
|
||||
//插入单个
|
||||
influxDbMapper.save(obj);
|
||||
influxDb.write(database, retentionPolicy, point);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,13 +86,20 @@ public class InfluxExecutor {
|
||||
* 删除操作
|
||||
* @author hongawen
|
||||
* @param sql 数据库语句
|
||||
* @param database 数据库名称
|
||||
*/
|
||||
public void delete(String sql, String database) {
|
||||
influxDb.query(new Query(dealTimeZone(sql), database));
|
||||
public void delete(String sql) {
|
||||
influxDb.query(new Query(sql, database));
|
||||
}
|
||||
|
||||
private String dealTimeZone(String originalSql){
|
||||
return originalSql + InfluxDbSqlConstant.TZ;
|
||||
/***
|
||||
* 处理时区问题
|
||||
* @author hongawen
|
||||
* @param originalSql 原始sql
|
||||
*/
|
||||
private String dealTimeZone(String originalSql) {
|
||||
if (originalSql.contains(InfluxDbSqlConstant.TZ)) {
|
||||
return originalSql;
|
||||
}
|
||||
return originalSql.concat(InfluxDbSqlConstant.TZ);
|
||||
}
|
||||
}
|
||||
|
||||
728
src/main/java/com/njcn/influx/query/InfluxQueryWrapper.java
Normal file
728
src/main/java/com/njcn/influx/query/InfluxQueryWrapper.java
Normal file
@@ -0,0 +1,728 @@
|
||||
package com.njcn.influx.query;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.text.StrPool;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.njcn.influx.constant.InfluxDbSqlConstant;
|
||||
import lombok.Data;
|
||||
import org.influxdb.annotation.Column;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
*/
|
||||
@Data
|
||||
public class InfluxQueryWrapper {
|
||||
|
||||
/***
|
||||
* 组装后的查询语句
|
||||
*/
|
||||
private final StringBuilder sqlSelect = new StringBuilder();
|
||||
|
||||
/***
|
||||
* 需要返回的字段
|
||||
*/
|
||||
private final List<String> selectColumns = new ArrayList<>();
|
||||
|
||||
/***
|
||||
* 查询条件字段
|
||||
*/
|
||||
private final List<String> conditions = new ArrayList<>();
|
||||
|
||||
/***
|
||||
* 分组集合
|
||||
*/
|
||||
private final List<String> groupColumn = new ArrayList<>();
|
||||
|
||||
/***
|
||||
* 排序集合
|
||||
*/
|
||||
private final List<String> orderColumn = new ArrayList<>();
|
||||
|
||||
|
||||
/***
|
||||
* 查询目标表
|
||||
*/
|
||||
private Class measurement;
|
||||
|
||||
/***
|
||||
* 返回映射实体
|
||||
*/
|
||||
private Class resultEntity;
|
||||
|
||||
/***
|
||||
* 返回和查询用的同一个实体
|
||||
* @param measurement 查询目标表
|
||||
*/
|
||||
public InfluxQueryWrapper(Class measurement) {
|
||||
this.measurement = measurement;
|
||||
this.resultEntity = measurement;
|
||||
this.initSql();
|
||||
}
|
||||
|
||||
/***
|
||||
* 返回和查询用的不是同一个实体
|
||||
* @param measurement 查询目标表
|
||||
* @param resultEntity 返回映射实体
|
||||
*/
|
||||
public InfluxQueryWrapper(Class measurement, Class resultEntity) {
|
||||
this.measurement = measurement;
|
||||
this.resultEntity = resultEntity;
|
||||
this.initSql();
|
||||
}
|
||||
|
||||
/***
|
||||
* 初始化查询语句:'select '
|
||||
*/
|
||||
private void initSql() {
|
||||
this.selectColumns.clear();
|
||||
this.conditions.clear();
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 查询的结果属性
|
||||
* @author hongawen
|
||||
* @param fieldsStr 属性值
|
||||
* 输出为 select ["influxColumn" as fieldStr]的形式
|
||||
*/
|
||||
public InfluxQueryWrapper select(String... fieldsStr) {
|
||||
if (ArrayUtil.isNotEmpty(fieldsStr)) {
|
||||
StringBuilder selectFragment = new StringBuilder();
|
||||
Arrays.stream(fieldsStr).forEach(fieldStr -> {
|
||||
Field field = this.getTargetClassField(resultEntity, fieldStr);
|
||||
selectFragment.setLength(0);
|
||||
selectFragment.append(StrPool.C_SPACE)
|
||||
.append(this.getColumnName(field, fieldStr))
|
||||
.append(StrPool.C_SPACE)
|
||||
.append(InfluxDbSqlConstant.AS)
|
||||
.append(StrPool.C_SPACE)
|
||||
.append(fieldStr)
|
||||
.append(StrPool.C_SPACE);
|
||||
selectColumns.add(selectFragment.toString());
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/************常见的简单函数处理比如max/min/mean/percentile****************/
|
||||
|
||||
/***
|
||||
*
|
||||
* @author hongawen
|
||||
* @param functionName 指定函数 max min mean等
|
||||
* @param columnName 表字段名
|
||||
* @param resultName 映射名
|
||||
* @return InfluxQueryWrapper
|
||||
*/
|
||||
public InfluxQueryWrapper functionByCustom(String functionName, String columnName, String resultName) {
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 统计记录数
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出COUNT("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper count(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.COUNT +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 统计平均值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出MEAN("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper mean(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.AVG +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 统计中位数
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出MEDIAN("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper median(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.MEDIAN +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 统计指定字段的最常出现的值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出MODE("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper mode(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.MODE +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 统计指定字段最大值和最小值的差
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出SPREAD("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper spread(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.SPREAD +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 统计指定字段值求和
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出SUM("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper sum(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.SUM +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 获取指定字段最大值的集合
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出TOP("columnName",number)
|
||||
*/
|
||||
public InfluxQueryWrapper top(String columnName, int num) {
|
||||
String selectFragment = InfluxDbSqlConstant.TOP +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
StrPool.COMMA +
|
||||
num +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 获取指定字段最小值的集合
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出BOTTOM("columnName",number)
|
||||
*/
|
||||
public InfluxQueryWrapper bottom(String columnName, int num) {
|
||||
String selectFragment = InfluxDbSqlConstant.BOTTOM +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
StrPool.COMMA +
|
||||
num +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 注:该函数还需调研,暂时不要用
|
||||
* 统计指定字段邻近值的变化率
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出DERIVATIVE("columnName")
|
||||
*/
|
||||
@Deprecated
|
||||
public InfluxQueryWrapper derivative(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.DERIVATIVE +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 统计指定字段值时间戳最近的值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出LAST("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper last(String columnName) {
|
||||
String selectFragment = InfluxDbSqlConstant.LAST +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS_VALUE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 获取指定字段最大值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 MAX("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper max(String columnName) {
|
||||
return max(columnName, "value");
|
||||
}
|
||||
|
||||
/***
|
||||
* 获取指定字段最大值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param resultName 映射名称
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 MAX("columnName") as resultName
|
||||
*/
|
||||
public InfluxQueryWrapper max(String columnName, String resultName) {
|
||||
String selectFragment = InfluxDbSqlConstant.MAX +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS +
|
||||
resultName +
|
||||
StrPool.C_SPACE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 获取指定字段最小值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 MIN("columnName")
|
||||
*/
|
||||
public InfluxQueryWrapper min(String columnName) {
|
||||
return min(columnName, "value");
|
||||
}
|
||||
|
||||
/***
|
||||
* 获取指定字段最小值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param resultName 映射名称
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 MIN("columnName") as resultName
|
||||
*/
|
||||
public InfluxQueryWrapper min(String columnName, String resultName) {
|
||||
String selectFragment = InfluxDbSqlConstant.MIN +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS +
|
||||
resultName +
|
||||
StrPool.C_SPACE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 返回field key较大的百分之N的值。
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param percent 百分之percent
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 PERCENTILE("columnName",95)
|
||||
*/
|
||||
public InfluxQueryWrapper percentile(String columnName, int percent) {
|
||||
return percentile(columnName, "value", percent);
|
||||
}
|
||||
|
||||
/***
|
||||
* 返回field key较大的百分之N的值。
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param resultName 映射名称
|
||||
* @param percent 百分之percent
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 PERCENTILE("columnName",95) as resultName
|
||||
*/
|
||||
public InfluxQueryWrapper percentile(String columnName, String resultName, int percent) {
|
||||
String selectFragment = InfluxDbSqlConstant.PERCENTILE +
|
||||
InfluxDbSqlConstant.LBK +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
columnName +
|
||||
InfluxDbSqlConstant.DQM +
|
||||
StrPool.COMMA +
|
||||
percent +
|
||||
InfluxDbSqlConstant.RBK +
|
||||
InfluxDbSqlConstant.AS +
|
||||
resultName +
|
||||
StrPool.C_SPACE;
|
||||
selectColumns.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**************************自定义查询条件,比如between、>、<、=、>=、<=等等**************************/
|
||||
|
||||
/***
|
||||
* 注:influxdb
|
||||
* 在WHERE子句中单引号来表示字符串字段值。具有无引号字符串字段值或双引号字符串字段值的查询将不会返回任何数据
|
||||
* ,并且在大多数情况下也不会返回错误。
|
||||
* @author hongawen
|
||||
* @param fieldName 字段名
|
||||
* @param val1 起始值
|
||||
* @param val2 结束值
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出为:time >='2022-04-30 16:00:00' AND time <='2022-05-30 16:00:00'
|
||||
*/
|
||||
public InfluxQueryWrapper between(String fieldName, Object val1, Object val2) {
|
||||
StringBuilder selectFragment = new StringBuilder();
|
||||
Field field = this.getTargetClassField(measurement, fieldName);
|
||||
String columnName = this.getColumnName(field, fieldName);
|
||||
selectFragment.append(StrPool.C_SPACE)
|
||||
.append(columnName)
|
||||
.append(InfluxDbSqlConstant.GE);
|
||||
|
||||
if (val1 instanceof String) {
|
||||
//需要用单引号包装下
|
||||
selectFragment.append(InfluxDbSqlConstant.QM)
|
||||
.append(val1)
|
||||
.append(InfluxDbSqlConstant.QM)
|
||||
.append(InfluxDbSqlConstant.AND)
|
||||
.append(columnName)
|
||||
.append(InfluxDbSqlConstant.LE)
|
||||
.append(InfluxDbSqlConstant.QM)
|
||||
.append(val2)
|
||||
.append(InfluxDbSqlConstant.QM);
|
||||
} else {
|
||||
selectFragment.append(val1)
|
||||
.append(InfluxDbSqlConstant.AND)
|
||||
.append(columnName)
|
||||
.append(InfluxDbSqlConstant.LE)
|
||||
.append(val2);
|
||||
}
|
||||
this.conditions.add(selectFragment.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 指定字段等于某个值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param columnValue 数值
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 columnName = columnValue
|
||||
*/
|
||||
public InfluxQueryWrapper eq(String columnName, Object columnValue) {
|
||||
StringBuilder selectFragment = new StringBuilder();
|
||||
selectFragment.append(columnName)
|
||||
.append(InfluxDbSqlConstant.EQ);
|
||||
if (columnValue instanceof String) {
|
||||
//需要用单引号包装下
|
||||
selectFragment.append(InfluxDbSqlConstant.QM)
|
||||
.append(columnValue)
|
||||
.append(InfluxDbSqlConstant.QM);
|
||||
} else {
|
||||
selectFragment.append(columnValue);
|
||||
}
|
||||
conditions.add(selectFragment.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 指定字段大于某个值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param columnValue 数值
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 columnName > columnValue
|
||||
*/
|
||||
public InfluxQueryWrapper gt(String columnName, Object columnValue) {
|
||||
StringBuilder selectFragment = new StringBuilder();
|
||||
selectFragment.append(columnName)
|
||||
.append(InfluxDbSqlConstant.GT);
|
||||
if (columnValue instanceof String) {
|
||||
//需要用单引号包装下
|
||||
selectFragment.append(InfluxDbSqlConstant.QM)
|
||||
.append(columnValue)
|
||||
.append(InfluxDbSqlConstant.QM);
|
||||
} else {
|
||||
selectFragment.append(columnValue);
|
||||
}
|
||||
conditions.add(selectFragment.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 指定字段大于等于某个值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param columnValue 数值
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 columnName >= columnValue
|
||||
*/
|
||||
public InfluxQueryWrapper ge(String columnName, Object columnValue) {
|
||||
StringBuilder selectFragment = new StringBuilder();
|
||||
selectFragment.append(columnName)
|
||||
.append(InfluxDbSqlConstant.GE);
|
||||
if (columnValue instanceof String) {
|
||||
//需要用单引号包装下
|
||||
selectFragment.append(InfluxDbSqlConstant.QM)
|
||||
.append(columnValue)
|
||||
.append(InfluxDbSqlConstant.QM);
|
||||
} else {
|
||||
selectFragment.append(columnValue);
|
||||
}
|
||||
conditions.add(selectFragment.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 指定字段小于某个值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param columnValue 数值
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 columnName < columnValue
|
||||
*/
|
||||
public InfluxQueryWrapper lt(String columnName, Object columnValue) {
|
||||
StringBuilder selectFragment = new StringBuilder();
|
||||
selectFragment.append(columnName)
|
||||
.append(InfluxDbSqlConstant.LT);
|
||||
if (columnValue instanceof String) {
|
||||
//需要用单引号包装下
|
||||
selectFragment.append(InfluxDbSqlConstant.QM)
|
||||
.append(columnValue)
|
||||
.append(InfluxDbSqlConstant.QM);
|
||||
} else {
|
||||
selectFragment.append(columnValue);
|
||||
}
|
||||
conditions.add(selectFragment.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 指定字段小于等于某个值
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @param columnValue 数值
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 columnName <= columnValue
|
||||
*/
|
||||
public InfluxQueryWrapper le(String columnName, Object columnValue) {
|
||||
StringBuilder selectFragment = new StringBuilder();
|
||||
selectFragment.append(columnName)
|
||||
.append(InfluxDbSqlConstant.LE);
|
||||
if (columnValue instanceof String) {
|
||||
//需要用单引号包装下
|
||||
selectFragment.append(InfluxDbSqlConstant.QM)
|
||||
.append(columnValue)
|
||||
.append(InfluxDbSqlConstant.QM);
|
||||
} else {
|
||||
selectFragment.append(columnValue);
|
||||
}
|
||||
conditions.add(selectFragment.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/********************* [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]*********************/
|
||||
|
||||
/***
|
||||
* 指定字段分组
|
||||
* @author hongawen
|
||||
* @param columnName 表字段名
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 columnName
|
||||
*/
|
||||
public InfluxQueryWrapper groupBy(String columnName) {
|
||||
groupColumn.add(columnName);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 指定字段降序
|
||||
* @author hongawen
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 time desc
|
||||
*/
|
||||
public InfluxQueryWrapper timeDesc() {
|
||||
String selectFragment = InfluxDbSqlConstant.TIME
|
||||
+ InfluxDbSqlConstant.DESC;
|
||||
orderColumn.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/***
|
||||
* 指定字段升序
|
||||
* @author hongawen
|
||||
* @return InfluxQueryWrapper
|
||||
* 输出 time asc
|
||||
*/
|
||||
public InfluxQueryWrapper timeAsc( ) {
|
||||
String selectFragment = InfluxDbSqlConstant.TIME
|
||||
+ InfluxDbSqlConstant.ASC;
|
||||
orderColumn.add(selectFragment);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 根据配置后的实体生成对应的sql
|
||||
* @author hongawen
|
||||
* @return String 最终查询的sql语句
|
||||
*/
|
||||
public String generateSql() {
|
||||
this.sqlSelect.setLength(0);
|
||||
this.sqlSelect.append(InfluxDbSqlConstant.SELECT);
|
||||
//判断用户需要返回的属性
|
||||
if (CollectionUtil.isEmpty(selectColumns)) {
|
||||
sqlSelect.append(InfluxDbSqlConstant.ALL)
|
||||
.append(InfluxDbSqlConstant.FROM)
|
||||
.append(InfluxDbSqlConstant.DQM)
|
||||
.append(measurement.getSimpleName().toLowerCase())
|
||||
.append(InfluxDbSqlConstant.DQM);
|
||||
} else {
|
||||
//将集合处理成查询属性
|
||||
sqlSelect.append(String.join(StrPool.COMMA, selectColumns))
|
||||
.append(InfluxDbSqlConstant.FROM)
|
||||
.append(InfluxDbSqlConstant.DQM)
|
||||
.append(measurement.getSimpleName().toLowerCase())
|
||||
.append(InfluxDbSqlConstant.DQM);
|
||||
}
|
||||
|
||||
//判断是否有查询条件
|
||||
if (CollectionUtil.isNotEmpty(conditions)) {
|
||||
sqlSelect.append(InfluxDbSqlConstant.WHERE)
|
||||
.append(String.join(InfluxDbSqlConstant.AND, conditions));
|
||||
}
|
||||
|
||||
//判断是否有分组
|
||||
if (CollectionUtil.isNotEmpty(groupColumn)) {
|
||||
sqlSelect.append(InfluxDbSqlConstant.GB)
|
||||
.append(String.join(StrPool.COMMA, groupColumn));
|
||||
}
|
||||
|
||||
//判断是否有排序
|
||||
if (CollectionUtil.isNotEmpty(orderColumn)) {
|
||||
sqlSelect.append(InfluxDbSqlConstant.OB)
|
||||
.append(String.join(StrPool.COMMA, orderColumn));
|
||||
}
|
||||
|
||||
//最后拼接上时区
|
||||
sqlSelect.append(InfluxDbSqlConstant.TZ);
|
||||
|
||||
return sqlSelect.toString();
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
*
|
||||
* @author hongawen
|
||||
* @param beanClass 实体类
|
||||
* @param fieldName 属性名
|
||||
* @return Field 属性
|
||||
*/
|
||||
private Field getTargetClassField(Class<?> beanClass, String fieldName) {
|
||||
Field field = ReflectUtil.getField(beanClass, fieldName);
|
||||
if (Objects.isNull(field)) {
|
||||
throw new RuntimeException(fieldName + "在目标实体类中不存在!!!");
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 获取sql拼接的名称,存在注解名就用注解名,否则就用属性名
|
||||
* @author hongawen
|
||||
* @param field 属性
|
||||
* @param fieldName 属性名
|
||||
* @return String
|
||||
*/
|
||||
private String getColumnName(Field field, String fieldName) {
|
||||
Column column = field.getAnnotation(Column.class);
|
||||
//数据库字段
|
||||
String influxColumn;
|
||||
if (Objects.isNull(column)) {
|
||||
//没有注解,就用属性名
|
||||
influxColumn = fieldName;
|
||||
} else {
|
||||
//获取注解映射的名称
|
||||
influxColumn = column.name();
|
||||
}
|
||||
return influxColumn;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user