mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
druid 1.2.0, improved sql parser.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
package com.alibaba.druid.sql.transform;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.repository.SchemaObject;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SQLRefactorVisitor extends SQLASTVisitorAdapter {
|
||||
private int havingLevel = 0;
|
||||
private int groupByLevel = 0;
|
||||
|
||||
private char quote = '"';
|
||||
public SQLRefactorVisitor(DbType dbType) {
|
||||
this.dbType = dbType;
|
||||
|
||||
switch (dbType) {
|
||||
case mysql:
|
||||
case mariadb:
|
||||
case ads:
|
||||
quote = '`';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Long, TableMapping> tableMappings = new HashMap<Long, TableMapping>();
|
||||
|
||||
public void addMapping(TableMapping mapping) {
|
||||
tableMappings.put(mapping.getSrcTableHash(), mapping);
|
||||
}
|
||||
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
TableMapping mapping =findMapping(x);
|
||||
if (mapping == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String destTable = mapping.getDestTable();
|
||||
|
||||
x.setExpr(new SQLIdentifierExpr(quote(destTable)));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private TableMapping findMapping(SQLExprTableSource x) {
|
||||
SchemaObject schemaObject = x.getSchemaObject();
|
||||
if (schemaObject == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
long nameHashCode = FnvHash.hashCode64(schemaObject.getName());
|
||||
return tableMappings.get(nameHashCode);
|
||||
}
|
||||
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
TableMapping mapping = null;
|
||||
|
||||
if (groupByLevel > 0 || havingLevel > 0) {
|
||||
SQLSelectQueryBlock queryBlock = null;
|
||||
for (SQLObject parent = x.getParent();
|
||||
parent != null;
|
||||
parent = parent.getParent()
|
||||
) {
|
||||
if (parent instanceof SQLSelectQueryBlock) {
|
||||
queryBlock = (SQLSelectQueryBlock) parent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
boolean matchAlias = false;
|
||||
if (queryBlock != null) {
|
||||
for (SQLSelectItem item : queryBlock.getSelectList()) {
|
||||
if (item.alias_hash() == x.hashCode64()) {
|
||||
matchAlias = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchAlias) {
|
||||
SQLObject parent = x.getParent();
|
||||
if (parent instanceof SQLOrderBy
|
||||
|| parent instanceof SQLSelectGroupByClause) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (havingLevel > 0) {
|
||||
boolean agg = false;
|
||||
for (; parent != null; parent = parent.getParent()) {
|
||||
if (parent instanceof SQLSelectQueryBlock) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (parent instanceof SQLAggregateExpr) {
|
||||
agg = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!agg) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SQLObject ownerObject = x.getResolvedOwnerObject();
|
||||
if (ownerObject instanceof SQLExprTableSource) {
|
||||
mapping = findMapping((SQLExprTableSource) ownerObject);
|
||||
}
|
||||
|
||||
if (mapping == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String srcName = x.getName();
|
||||
String mappingColumn = mapping.getMappingColumn(srcName);
|
||||
if (mappingColumn != null) {
|
||||
x.setName(quote(mappingColumn));
|
||||
}
|
||||
|
||||
SQLObject parent = x.getParent();
|
||||
if (parent instanceof SQLSelectItem
|
||||
&& ((SQLSelectItem) parent).getAlias() == null) {
|
||||
((SQLSelectItem) parent).setAlias(srcName);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
groupByLevel++;
|
||||
for (SQLExpr item : x.getItems()) {
|
||||
item.accept(this);
|
||||
}
|
||||
|
||||
SQLExpr having = x.getHaving();
|
||||
if (having != null) {
|
||||
havingLevel++;
|
||||
having.accept(this);
|
||||
havingLevel--;
|
||||
}
|
||||
groupByLevel--;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
TableMapping mapping = null;
|
||||
SchemaObject schemaObject = null;
|
||||
|
||||
boolean aliasOwer = false;
|
||||
SQLObject ownerObject = x.getResolvedOwnerObject();
|
||||
if (ownerObject instanceof SQLExprTableSource) {
|
||||
SQLExprTableSource exprTableSource = (SQLExprTableSource) ownerObject;
|
||||
if (exprTableSource.getAlias() != null && x.getOwner() instanceof SQLIdentifierExpr) {
|
||||
if (FnvHash.hashCode64(exprTableSource.getAlias()) == ((SQLIdentifierExpr) x.getOwner()).nameHashCode64()) {
|
||||
aliasOwer = true;
|
||||
}
|
||||
}
|
||||
|
||||
mapping = findMapping(exprTableSource);
|
||||
schemaObject = (exprTableSource).getSchemaObject();
|
||||
}
|
||||
|
||||
if (mapping == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String srcName = x.getName();
|
||||
String mappingColumn = mapping.getMappingColumn(srcName);
|
||||
if (mappingColumn != null) {
|
||||
x.setName(quote(mappingColumn));
|
||||
}
|
||||
|
||||
SQLObject parent = x.getParent();
|
||||
if (parent instanceof SQLSelectItem
|
||||
&& ((SQLSelectItem) parent).getAlias() == null) {
|
||||
((SQLSelectItem) parent).setAlias(srcName);
|
||||
}
|
||||
|
||||
if (x.getOwner() instanceof SQLIdentifierExpr
|
||||
&& ((SQLIdentifierExpr) x.getOwner()).nameHashCode64() == mapping.getSrcTableHash()
|
||||
&& !aliasOwer
|
||||
) {
|
||||
x.setOwner(new SQLIdentifierExpr(quote(mapping.getDestTable())));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private String quote(String name) {
|
||||
char[] chars = new char[name.length() + 2];
|
||||
name.getChars(0, name.length(), chars, 1);
|
||||
chars[0] = '`';
|
||||
chars[chars.length - 1] = '`';
|
||||
return new String(chars);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.transform;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
|
||||
public interface SQLTranform {
|
||||
String getSourceDbType();
|
||||
String getTargetDbType();
|
||||
DbType getSourceDbType();
|
||||
DbType getTargetDbType();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,17 +15,19 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.transform;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
|
||||
public abstract class SQLTranformImpl implements SQLTranform {
|
||||
protected String sourceDbType;
|
||||
protected String targetDbType;
|
||||
protected DbType sourceDbType;
|
||||
protected DbType targetDbType;
|
||||
|
||||
@Override
|
||||
public String getSourceDbType() {
|
||||
public DbType getSourceDbType() {
|
||||
return sourceDbType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTargetDbType() {
|
||||
public DbType getTargetDbType() {
|
||||
return targetDbType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.alibaba.druid.sql.transform;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLType;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
import static com.alibaba.druid.sql.parser.SQLParserFeature.EnableSQLBinaryOpExprGroup;
|
||||
|
||||
/**
|
||||
* @author lijun.cailj 2020/5/8
|
||||
*/
|
||||
public class SQLUnifiedUtils {
|
||||
|
||||
public static long unifyHash(String sql, DbType type) {
|
||||
String unifySQL = unifySQL(sql, DbType.mysql);
|
||||
return FnvHash.fnv1a_64_lower(unifySQL);
|
||||
}
|
||||
|
||||
public static String unifySQL(String sql, DbType type) {
|
||||
if (StringUtils.isEmpty(sql)) {
|
||||
throw new IllegalArgumentException("sql is empty.");
|
||||
}
|
||||
|
||||
SQLType sqlType = SQLParserUtils.getSQLType(sql, DbType.mysql);
|
||||
|
||||
String parameterizeSQL = null;
|
||||
switch (sqlType) {
|
||||
case INSERT:
|
||||
case UPDATE:
|
||||
case SELECT:
|
||||
case DELETE:
|
||||
parameterizeSQL = ParameterizedOutputVisitorUtils.parameterize(sql, DbType.mysql);
|
||||
SQLStatement stmt = SQLUtils.parseSingleStatement(parameterizeSQL, DbType.mysql, EnableSQLBinaryOpExprGroup);
|
||||
stmt.accept(new SQLUnifiedVisitor());
|
||||
return SQLUtils.toMySqlString(stmt);
|
||||
default:
|
||||
return ParameterizedOutputVisitorUtils.parameterize(sql, DbType.mysql);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.alibaba.druid.sql.transform;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExprGroup;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
import com.alibaba.druid.sql.repository.SchemaRepository;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLUnifiedVisitor extends MySqlASTVisitorAdapter {
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlSelectQueryBlock x) {
|
||||
SchemaRepository repository = new SchemaRepository(DbType.mysql);
|
||||
repository.resolve(x);
|
||||
|
||||
List<SQLSelectItem> selectList = x.getSelectList();
|
||||
for (SQLSelectItem item : selectList) {
|
||||
if (item.getExpr() instanceof SQLPropertyExpr) {
|
||||
SQLPropertyExpr expr = (SQLPropertyExpr) item.getExpr();
|
||||
|
||||
SQLTableSource resolvedTableSource = expr.getResolvedTableSource();
|
||||
|
||||
if (resolvedTableSource != null) {
|
||||
String alias = resolvedTableSource.getAlias();
|
||||
if (alias != null) {
|
||||
expr.setOwner(alias);
|
||||
} else {
|
||||
expr.setOwner(resolvedTableSource.computeAlias());
|
||||
}
|
||||
}
|
||||
}
|
||||
item.setAlias(null);
|
||||
}
|
||||
|
||||
Collections.sort(selectList, new Comparator<SQLSelectItem>() {
|
||||
@Override
|
||||
public int compare(SQLSelectItem o1, SQLSelectItem o2) {
|
||||
return o1.toString().compareToIgnoreCase(o2.toString());
|
||||
}
|
||||
});
|
||||
|
||||
if (x.getFrom() != null) {
|
||||
x.getFrom().accept(this);
|
||||
}
|
||||
|
||||
if (x.getWhere() != null) {
|
||||
x.getWhere().accept(this);
|
||||
}
|
||||
|
||||
if(x.getGroupBy() != null) {
|
||||
x.getGroupBy().accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
SQLBinaryOperator operator = x.getOperator();
|
||||
if (operator == SQLBinaryOperator.BooleanOr || operator == SQLBinaryOperator.BooleanAnd) {
|
||||
SQLExpr left = x.getLeft();
|
||||
left.accept(this);
|
||||
|
||||
SQLExpr right = x.getRight();
|
||||
right.accept(this);
|
||||
|
||||
int compareResult = left.toString().compareToIgnoreCase(right.toString());
|
||||
if (compareResult > 0) {
|
||||
x.setLeft(right);
|
||||
x.setRight(left);
|
||||
}
|
||||
} else {
|
||||
SQLExpr left = x.getLeft();
|
||||
left.accept(this);
|
||||
|
||||
SQLExpr right = x.getRight();
|
||||
right.accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
SQLExpr owner = x.getOwner();
|
||||
|
||||
SQLTableSource resolvedTableSource = x.getResolvedTableSource();
|
||||
|
||||
if (resolvedTableSource != null) {
|
||||
String alias = resolvedTableSource.getAlias();
|
||||
if (alias != null) {
|
||||
x.setOwner(alias);
|
||||
} else {
|
||||
x.setOwner(resolvedTableSource.computeAlias());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryOpExprGroup x) {
|
||||
Collections.sort(x.getItems(), new Comparator<SQLExpr>() {
|
||||
@Override
|
||||
public int compare(SQLExpr o1, SQLExpr o2) {
|
||||
return o1.toString().compareToIgnoreCase(o2.toString());
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUnionQuery x) {
|
||||
SQLSelectQuery left = x.getLeft();
|
||||
SQLSelectQuery right = x.getRight();
|
||||
|
||||
left.accept(this);
|
||||
right.accept(this);
|
||||
|
||||
int compareResult = left.toString().compareToIgnoreCase(right.toString());
|
||||
if (compareResult > 0) {
|
||||
x.setLeft(right);
|
||||
x.setRight(left);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
String tablename = ((SQLName) x.getExpr()).getSimpleName();
|
||||
x.setAlias(tablename);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
Collections.sort(x.getItems(), new Comparator<SQLExpr>() {
|
||||
@Override
|
||||
public int compare(SQLExpr o1, SQLExpr o2) {
|
||||
return o1.toString().compareToIgnoreCase(o2.toString());
|
||||
}
|
||||
});
|
||||
|
||||
SQLExpr having = x.getHaving();
|
||||
if (having != null) {
|
||||
having.accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.alibaba.druid.sql.transform;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TableMapping {
|
||||
private final String srcTable;
|
||||
private final String destTable;
|
||||
private final long srcTableHash;
|
||||
|
||||
private final Map<Long, ColumnMapping> columnMappings = new HashMap<Long, ColumnMapping>();
|
||||
|
||||
public TableMapping(String srcTable, String destTable) {
|
||||
this.srcTable = SQLUtils.normalize(srcTable);
|
||||
this.destTable = SQLUtils.normalize(destTable);
|
||||
this.srcTableHash = FnvHash.hashCode64(srcTable);
|
||||
}
|
||||
|
||||
public String getSrcTable() {
|
||||
return srcTable;
|
||||
}
|
||||
|
||||
public String getDestTable() {
|
||||
return destTable;
|
||||
}
|
||||
|
||||
public long getSrcTableHash() {
|
||||
return srcTableHash;
|
||||
}
|
||||
|
||||
public String getMappingColumn(String srcColumn) {
|
||||
long hash = FnvHash.hashCode64(srcColumn);
|
||||
ColumnMapping columnMapping = columnMappings.get(hash);
|
||||
if (columnMapping == null) {
|
||||
return null;
|
||||
}
|
||||
return columnMapping.destDestColumn;
|
||||
}
|
||||
|
||||
public void addColumnMapping(String srcColumn, String destColumn) {
|
||||
ColumnMapping columnMapping = new ColumnMapping(srcColumn, destColumn);
|
||||
columnMappings.put(columnMapping.srcColumnHash, columnMapping);
|
||||
}
|
||||
|
||||
private static class ColumnMapping {
|
||||
public final String srcColumn;
|
||||
public final String destDestColumn;
|
||||
public final long srcColumnHash;
|
||||
|
||||
public ColumnMapping(String srcColumn, String destDestColumn) {
|
||||
this.srcColumn = SQLUtils.normalize(srcColumn);
|
||||
this.destDestColumn = SQLUtils.normalize(destDestColumn);
|
||||
this.srcColumnHash = FnvHash.hashCode64(srcColumn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alibaba.druid.sql.transform.function;
|
||||
|
||||
public class FunctionTransform {
|
||||
}
|
||||
Reference in New Issue
Block a user