1.使用jsqlparser解析
- 引入jsqlparser包:
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.6</version>
</dependency>
- 使用工具类解析
public static Map<String, Object> sqlParser(String sql) {
Map<String, Object> result = new HashMap<>();
if (StringUtils.isEmpty(sql)) {
return result;
}
try {
String operation = "";
Set<String> tableList = new HashSet<>();
Statement statement = CCJSqlParserUtil.parse(new StringReader(sql));
if (statement instanceof Select) {
operation = "SELECT";
Select select = (Select) statement;
} else if (statement instanceof Delete) {
operation = "DELETE";
Delete delete = (Delete) statement;
Table table = delete.getTable();
String tableName = table.getName();
tableList.add(tableName);
} else if (statement instanceof Update) {
operation = "UPDATE";
Update update = (Update) statement;
Table table = update.getTable();
String tableName = table.getName();
tableList.add(tableName);
} else if (statement instanceof Insert) {
operation = "INSERT";
Insert insert = (Insert) statement;
Table table = insert.getTable();
String tableName = table.getName();
tableList.add(tableName);
} else if (statement instanceof Merge) {
operation = "Merge";
Merge merge = (Merge) statement;
Table table = merge.getTable();
String tableName = table.getName();
tableList.add(tableName);
}
result.put("operation", operation);
result.put("tableList", tableList);
return result;
} catch (JSQLParserException e) {
log.error("SQL解析异常,语句为{},原因:", sql, e);
return result;
}
}
public static void getSelectTables(Set<String> result, SelectBody selectBody) {
if (selectBody instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectBody;
FromItem fromItem = plainSelect.getFromItem();
if (fromItem instanceof Table) {
Table table = (Table) fromItem;
String tableName = table.getName();
result.add(tableName);
}
else if (fromItem instanceof SubSelect) {
SubSelect subSelect = (SubSelect) fromItem;
getSelectTables(result, subSelect.getSelectBody());
}
List<Join> joinList = plainSelect.getJoins();
if (joinList != null) {
for (Join joinJoin : joinList) {
FromItem rightFromItem = joinJoin.getRightItem();
if (!(rightFromItem instanceof Table)) {
continue;
}
Table table = (Table) rightFromItem;
String tableName = table.getName();
result.add(tableName);
}
}
}
else if (selectBody instanceof SetOperationList) {
SetOperationList setOperationList = (SetOperationList) selectBody;
List<SelectBody> selectBodyList = setOperationList.getSelects();
selectBodyList.forEach(e -> getSelectTables(result, e));
}
}
2.使用Druid解析
- 引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
- 工具方法解析
public static Map<String, Object> parser(String sql) {
Map<String, Object> result = new HashMap<>();
MySqlStatementParser parser = new MySqlStatementParser(sql);
parser.setKeepComments(false);
SQLStatement stmt = parser.parseStatement();
if (stmt instanceof SQLCreateProcedureStatement) {
SQLCreateProcedureStatement procStmt = (SQLCreateProcedureStatement) stmt;
SQLStatement blockStmt = procStmt.getBlock();
if (blockStmt instanceof SQLBlockStatement) {
SQLBlockStatement sqlBlockStatement = (SQLBlockStatement) blockStmt;
List<SQLStatement> sqlStatementList = sqlBlockStatement.getStatementList();
for (SQLStatement statement : sqlStatementList) {
List<String> tableList = getTableList(statement);
}
}
}
return result;
}
public static List<String> getTableList(SQLStatement statement) {
List<String> result = new ArrayList<>();
if (statement instanceof SQLSelectStatement) {
SQLSelectStatement sqlSelectStatement = (SQLSelectStatement) statement;
SQLSelect sqlSelect = sqlSelectStatement.getSelect();
SQLSelectQuery sqlSelectQuery = sqlSelect.getQuery();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mySqlSelectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
SQLTableSource sqlTableSource = mySqlSelectQueryBlock.getFrom();
result.add(sqlTableSource.toString());
}
}
return result;
}