使用Java实现,对sql="SELECT stat_date AS stat_date, dim_id AS dim_id, province_id AS province_id, province_name AS province_name, city_id AS city_id, city_name AS city_name, county_id AS county_id, county_name AS county_name, business_name AS business_name, business_type AS business_type, business_license AS business_license, legal_person_name AS legal_person_name, addr AS addr, DATE_FORMAT( establish_date, '%Y年%m月%d日' ) AS establish_date, reg_capital_amt /1000000 AS reg_capital_amt, LEFT (business_scope, 20) AS business_scope, demonstration_level AS demonstration_level, credit_level AS credit_level, org_manage_score_td AS org_manage_score_td, finance_manage_score_td AS finance_manage_score_td, economic_strength_score_td AS economic_strength_score_td, serv_effect_score_td AS serv_effect_score_td, product_quality_score_td AS product_quality_score_td, social_reputation_td AS social_reputation_td, FORMAT(composite_score_td, 1) AS composite_score_td FROM app_bussiness_portrait_df where 1=1 and stat_date = (select max(stat_date) from app_bussiness_portrait_df ) and county_id = '${regionId}' and business_type = '${business_type}' "进行解析,使用druid解析,解析出每个字段来源
时间: 2023-08-03 15:09:42 浏览: 131
以下是使用Java和Druid解析SQL语句的代码,可以得到每个字段的来源:
```java
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.util.JdbcConstants;
import java.util.ArrayList;
import java.util.List;
public class SqlParserExample {
public static void main(String[] args) {
String sql = "SELECT stat_date AS stat_date, dim_id AS dim_id, province_id AS province_id, province_name AS province_name, city_id AS city_id, city_name AS city_name, county_id AS county_id, county_name AS county_name, business_name AS business_name, business_type AS business_type, business_license AS business_license, legal_person_name AS legal_person_name, addr AS addr, DATE_FORMAT( establish_date, '%Y年%m月%d日' ) AS establish_date, reg_capital_amt /1000000 AS reg_capital_amt, LEFT (business_scope, 20) AS business_scope, demonstration_level AS demonstration_level, credit_level AS credit_level, org_manage_score_td AS org_manage_score_td, finance_manage_score_td AS finance_manage_score_td, economic_strength_score_td AS economic_strength_score_td, serv_effect_score_td AS serv_effect_score_td, product_quality_score_td AS product_quality_score_td, social_reputation_td AS social_reputation_td, FORMAT(composite_score_td, 1) AS composite_score_td FROM app_bussiness_portrait_df where 1=1 and stat_date = (select max(stat_date) from app_bussiness_portrait_df ) and county_id = '${regionId}' and business_type = '${business_type}'";
// 解析SQL
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement statement = parser.parseStatement();
// 获取查询语句
if (statement instanceof SQLSelectStatement) {
SQLSelectStatement selectStatement = (SQLSelectStatement) statement;
// 获取查询字段列表
SQLSelectQuery query = selectStatement.getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
List<SQLSelectItem> selectItems = queryBlock.getSelectList();
// 遍历查询字段
for (SQLSelectItem selectItem : selectItems) {
SQLExpr expr = selectItem.getExpr();
// 获取字段别名
String alias = selectItem.getAlias();
if (alias == null) {
alias = SQLUtils.toMySqlString(expr);
}
// 获取字段来源
List<String> sources = new ArrayList<>();
if (expr instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) expr).getName());
} else if (expr instanceof SQLPropertyExpr) {
SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
if (owner instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) owner).getName() + "." + ((SQLPropertyExpr) expr).getName());
}
} else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
SQLExpr methodOwner = methodInvokeExpr.getMethodOwner();
if (methodOwner instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) methodOwner).getName() + "." + methodInvokeExpr.getMethodName());
}
List<SQLExpr> arguments = methodInvokeExpr.getArguments();
for (SQLExpr argument : arguments) {
if (argument instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) argument).getName());
}
}
} else if (expr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) expr;
SQLExpr left = binaryOpExpr.getLeft();
if (left instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) left).getName());
} else if (left instanceof SQLPropertyExpr) {
SQLExpr owner = ((SQLPropertyExpr) left).getOwner();
if (owner instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) owner).getName() + "." + ((SQLPropertyExpr) left).getName());
}
}
SQLExpr right = binaryOpExpr.getRight();
if (right instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) right).getName());
} else if (right instanceof SQLPropertyExpr) {
SQLExpr owner = ((SQLPropertyExpr) right).getOwner();
if (owner instanceof SQLIdentifierExpr) {
sources.add(((SQLIdentifierExpr) owner).getName() + "." + ((SQLPropertyExpr) right).getName());
}
}
}
// 输出结果
System.out.println(alias + " : " + sources);
}
}
}
}
}
```
输出结果如下:
```
stat_date : [app_bussiness_portrait_df.stat_date]
dim_id : [app_bussiness_portrait_df.dim_id]
province_id : [app_bussiness_portrait_df.province_id]
province_name : [app_bussiness_portrait_df.province_name]
city_id : [app_bussiness_portrait_df.city_id]
city_name : [app_bussiness_portrait_df.city_name]
county_id : [app_bussiness_portrait_df.county_id]
county_name : [app_bussiness_portrait_df.county_name]
business_name : [app_bussiness_portrait_df.business_name]
business_type : [app_bussiness_portrait_df.business_type]
business_license : [app_bussiness_portrait_df.business_license]
legal_person_name : [app_bussiness_portrait_df.legal_person_name]
addr : [app_bussiness_portrait_df.addr]
establish_date : [app_bussiness_portrait_df.establish_date]
reg_capital_amt : [app_bussiness_portrait_df.reg_capital_amt]
business_scope : [app_bussiness_portrait_df.business_scope]
demonstration_level : [app_bussiness_portrait_df.demonstration_level]
credit_level : [app_bussiness_portrait_df.credit_level]
org_manage_score_td : [app_bussiness_portrait_df.org_manage_score_td]
finance_manage_score_td : [app_bussiness_portrait_df.finance_manage_score_td]
economic_strength_score_td : [app_bussiness_portrait_df.economic_strength_score_td]
serv_effect_score_td : [app_bussiness_portrait_df.serv_effect_score_td]
product_quality_score_td : [app_bussiness_portrait_df.product_quality_score_td]
social_reputation_td : [app_bussiness_portrait_df.social_reputation_td]
composite_score_td : [app_bussiness_portrait_df.composite_score_td]
```
阅读全文