1、新增com.alibaba:druid依赖
2、动态规则类新增sql解析方法,发送dynamicTaskInfos时对sql进行解析。 3、ResponseResult返回响应信息修改为中文 4、动态任务运行状态转变为运行中时,dynamicTaskInfos生成的查询mapper语句修改
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
package com.realtime.protection.configuration.entity.task;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Data
|
||||
public class DynamicTaskInfo {
|
||||
|
||||
@@ -45,10 +55,76 @@ public class DynamicTaskInfo {
|
||||
@JsonProperty("event_type")
|
||||
private String eventType;
|
||||
|
||||
@JsonProperty("log_rule_id")
|
||||
private Long logRuleId;
|
||||
// @JsonProperty("event_ids")
|
||||
// private List<Long> eventIds;
|
||||
//
|
||||
// @JsonProperty("c_inoutid")
|
||||
// private List<Long> inoutId;
|
||||
//
|
||||
// @JsonProperty("c_netnum")
|
||||
// private List<Long> netNum;
|
||||
|
||||
@JsonProperty("conditions")
|
||||
private HashMap<String,String> conditions = new HashMap<>();
|
||||
|
||||
// @JsonProperty("eq_condition")
|
||||
// private HashMap<String,String> eqConditions = new HashMap<>();;
|
||||
|
||||
@JsonProperty("table_name")
|
||||
private List<String> tableNames = new ArrayList<>();
|
||||
|
||||
@JsonProperty("bw_sql")
|
||||
private String bwSql;
|
||||
|
||||
@JsonProperty("select_columns")
|
||||
private List<String> selectColumns = new ArrayList<>();
|
||||
|
||||
// 从防护对象列表中获取
|
||||
@JsonProperty("protect_objects")
|
||||
private List<SimpleProtectObject> protectObjects;
|
||||
|
||||
public void parseSql() {
|
||||
String bwSql = this.bwSql;
|
||||
//解析SQL语句
|
||||
SQLStatementParser parser = new MySqlStatementParser(bwSql);
|
||||
// 使用Parser解析生成AST,这里SQLStatement就是AST
|
||||
SQLStatement sqlStatement = parser.parseStatement();
|
||||
|
||||
// 检查是否是SELECT语句
|
||||
if (sqlStatement instanceof SQLSelectStatement) {
|
||||
// 创建访问者
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
sqlStatement.accept(visitor);
|
||||
|
||||
// 进一步细化,确保只获取SELECT子句的字段
|
||||
SQLSelectStatement selectStatement = (SQLSelectStatement) sqlStatement;
|
||||
SQLSelect select = selectStatement.getSelect();
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery();
|
||||
|
||||
queryBlock.getSelectList().forEach(item -> this.selectColumns.add(item.toString()));
|
||||
|
||||
Map<TableStat.Name, TableStat> tables = visitor.getTables();
|
||||
for (TableStat.Name t : tables.keySet()) {
|
||||
this.tableNames.add(t.getName());
|
||||
}
|
||||
//获取where条件
|
||||
List<TableStat.Condition> conditions = visitor.getConditions();
|
||||
conditions.forEach(condition -> {
|
||||
// log.info("解析sql后的查询条件:{}", condition.getColumn().getName());
|
||||
if (condition.getOperator().equals("IN")){
|
||||
this.conditions.put(condition.getColumn().getName(),condition.getValues().toString());
|
||||
}
|
||||
if (condition.getOperator().equals("=")){
|
||||
this.conditions.put(condition.getColumn().getName(),condition.getValues().toString());
|
||||
}
|
||||
});
|
||||
// log.info("解析sql后的查询条件:{}",conditions);
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("不是SELECT查询语句,不进行字段解析。");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class ResponseResult implements Serializable {
|
||||
}
|
||||
|
||||
public static ResponseResult ok() {
|
||||
return new ResponseResult(200, "request succeed");
|
||||
return new ResponseResult(200, "请求成功");
|
||||
}
|
||||
|
||||
public static ResponseResult ok(String message) {
|
||||
@@ -49,11 +49,11 @@ public class ResponseResult implements Serializable {
|
||||
}
|
||||
|
||||
public static ResponseResult error() {
|
||||
return new ResponseResult(500, "request failed");
|
||||
return new ResponseResult(500, "请求失败");
|
||||
}
|
||||
|
||||
public static ResponseResult invalid() {
|
||||
return new ResponseResult(400, "invalid request");
|
||||
return new ResponseResult(400, "请求无效");
|
||||
}
|
||||
|
||||
public static ResponseResult invalid(String message) {
|
||||
@@ -61,7 +61,7 @@ public class ResponseResult implements Serializable {
|
||||
}
|
||||
|
||||
public static ResponseResult unAuthorized() {
|
||||
return new ResponseResult(401, "UnAuthorized User");
|
||||
return new ResponseResult(401, "未授权用户");
|
||||
}
|
||||
|
||||
public static ResponseResult error(String message) {
|
||||
|
||||
Reference in New Issue
Block a user