Commit dde084f5 by 郑冰晶

优化异常告警

parent 6b608d56
package com.secoo.mall.datasource.security.visitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
public class ParameterVisitor extends MySqlASTVisitorAdapter {
}
package com.secoo.mall.datasource.security;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlSelectIntoStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.secoo.mall.datasource.security.rule.ColumnRule;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class SQLParserTest {
public static void testVisitor(){
// String sql = "select u.name,a.age from t_user u,t_account a where u.id = a.id and t_account.id > 12 and a.age >?;";
// String sql = "select secooStoreDB.t_sequence.`code` from secooStoreDB.t_sequence,secooStoreDB.t_store_fail_mq where secooStoreDB.t_sequence.`code`=secooStoreDB.t_store_fail_mq.topic";
// String sql = "delete from t_sequence";
// String sql = "INSERT INTO `secooStoreDB`.`t_sequence`(`t_sequence`.`name`, `current_value`, `increment`, `code`) VALUES (?, 4551, 1, 2),('t_store_category', 65015, 1, 1);";
// String sql = "INSERT INTO `secooStoreDB`.`t_sequence` VALUES ('t_store', 4551, 1, 2),('t_store_category', 65015, 1, 1)";
// String sql = "update t_user set name=?,age=10 where id = id and id > 12";
// String sql = "update t_user u,t_account a set u.name=?,a.age=10 where u.id = a.id and u.id > 12 and a.age >?";
// String sql = "update t_user u,t_account a set t_user.name=?,a.age=10 where u.id = a.id and u.id > 12 and a.age >?";
String sql = "update t_user u,t_account a set secooStoreDB.t_account.name=?,a.age=10 where secooStoreDB.t_account.id = a.id and u.id > 12 and a.age >?";
// 解析sql
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql,"mysql");
for (SQLStatement stmt : stmtList) {
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
System.out.println(visitor.getParameters());
if (stmt instanceof SQLSelectStatement) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
System.out.println(visitor.getColumns());
System.out.println(selectStmt.getSelect());
System.out.println("---");
}else if(stmt instanceof MySqlDeleteStatement){
MySqlDeleteStatement deleteStmt = (MySqlDeleteStatement) stmt;
System.out.println("---");
}else if (stmt instanceof MySqlInsertStatement) {
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
String tableName = insertStmt.getTableName().getSimpleName();
int valuesSize = insertStmt.getValuesList().size();
Collection<TableStat.Column> columns = visitor.getColumns();
int columnSize = columns.size();
for (TableStat.Column column : columns) {
System.out.println(column.getTable());
}
System.out.println("---");
}else if(stmt instanceof MySqlUpdateStatement){
MySqlUpdateStatement updateStmt = (MySqlUpdateStatement) stmt;
for(SQLUpdateSetItem item :updateStmt.getItems()){
if (item.getValue() instanceof SQLVariantRefExpr) {
SQLVariantRefExpr sqlVariantRefExpr = (SQLVariantRefExpr) item.getValue();
if(item.getColumn() instanceof SQLIdentifierExpr){
SQLIdentifierExpr sqlIdentifierExpr = (SQLIdentifierExpr) item.getColumn();
System.out.println(sqlIdentifierExpr.getResolvedOwnerObject().toString());
}else if(item.getColumn() instanceof SQLPropertyExpr){
SQLPropertyExpr columnSqlPropertyExpr = (SQLPropertyExpr) item.getColumn();
if(columnSqlPropertyExpr.getOwner() instanceof SQLIdentifierExpr){
SQLIdentifierExpr tableSQLIdentifierExpr = (SQLIdentifierExpr) columnSqlPropertyExpr.getOwner();
System.out.println(tableSQLIdentifierExpr.getName());
System.out.println(columnSqlPropertyExpr.getResolvedOwnerObject().toString());
}else{
SQLPropertyExpr tableSqlPropertyExpr = (SQLPropertyExpr) columnSqlPropertyExpr.getOwner();
System.out.println(tableSqlPropertyExpr.getName());
}
}
}
}
System.out.println("---");
}else{
}
}
/*String cipherText = "定时任务";
AESEncryptAlgorithm aesEncryptAlgorithm = new AESEncryptAlgorithm();
aesEncryptAlgorithm.getProps().setProperty(AESEncryptAlgorithm.ENCRYPT_KEY,"123");
aesEncryptAlgorithm.init();
try {
aesEncryptAlgorithm.decrypt(cipherText);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
System.out.println("");*/
}
public static void main(String[] args) {
testVisitor();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment