Skip to content

Commit

Permalink
refactor sql parser & support tableMapping. issue alibaba#1432
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Sep 24, 2016
1 parent 2e478b1 commit a26e4c5
Show file tree
Hide file tree
Showing 33 changed files with 423 additions and 170 deletions.
20 changes: 19 additions & 1 deletion src/main/java/com/alibaba/druid/sql/SQLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.sql.ast.SQLExpr;
Expand Down Expand Up @@ -263,10 +264,18 @@ public static String toSQLString(List<SQLStatement> statementList, String dbType
}

public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters) {
return toSQLString(statementList, dbType, parameters, null);
return toSQLString(statementList, dbType, parameters, null, null);
}

public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters, FormatOption option) {
return toSQLString(statementList, dbType, parameters, option, null);
}

public static String toSQLString(List<SQLStatement> statementList
, String dbType
, List<Object> parameters
, FormatOption option
, Map<String, String> tableMapping) {
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType);
if (parameters != null) {
Expand All @@ -278,6 +287,10 @@ public static String toSQLString(List<SQLStatement> statementList, String dbType
}
visitor.setUppCase(option.isUppCase());

if (tableMapping != null) {
visitor.setTableMapping(tableMapping);
}

for (int i = 0; i < statementList.size(); i++) {
SQLStatement stmt = statementList.get(i);

Expand Down Expand Up @@ -645,5 +658,10 @@ public void setUppCase(boolean val) {
this.ucase = val;
}
}

public static String refactor(String dbType, String sql, Map<String, String> tableMapping) {
List<SQLStatement> stmtList = parseStatements(sql, dbType);
return SQLUtils.toSQLString(stmtList, dbType, null, null, tableMapping);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public boolean visit(MySqlCreateTableStatement x) {
print0(ucase ? "IF NOT EXISTS " : "if not exists ");
}

x.getName().accept(this);
printTableSourceExpr(x.getName());

if (x.getLike() != null) {
print0(ucase ? " LIKE " : " like ");
Expand Down Expand Up @@ -2659,7 +2659,7 @@ public void endVisit(MySqlIgnoreIndexHint x) {
}

public boolean visit(SQLExprTableSource x) {
x.getExpr().accept(this);
printTableSourceExpr(x.getExpr());

if (x.getAlias() != null) {
print(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.alibaba.druid.sql.dialect.mysql.visitor;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ public boolean visit(OracleSelectSubqueryTableSource x) {
public boolean visit(OracleSelectTableReference x) {
if (x.isOnly()) {
print0(ucase ? "ONLY (" : "only (");
x.getExpr().accept(this);
printTableSourceExpr(x.getExpr());

if (x.getPartition() != null) {
print(' ');
Expand All @@ -628,7 +628,7 @@ public boolean visit(OracleSelectTableReference x) {

print(')');
} else {
x.getExpr().accept(this);
printTableSourceExpr(x.getExpr());

if (x.getPartition() != null) {
print(' ');
Expand Down Expand Up @@ -1285,27 +1285,11 @@ public boolean visit(OracleInsertStatement x) {

x.getTableSource().accept(this);

if (x.getColumns().size() > 0) {
incrementIndent();
println();
print('(');
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
if (i != 0) {
if (i % 5 == 0) {
println();
}
print0(", ");
}
x.getColumns().get(i).accept(this);
}
print(')');
decrementIndent();
}
printInsertColumns(x.getColumns());

if (x.getValues() != null) {
println();
print0(ucase ? "VALUES" : "values");
println();
print0(ucase ? "VALUES " : "values ");
x.getValues().accept(this);
} else {
if (x.getQuery() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public boolean visit(PGDeleteStatement x) {
print0(ucase ? "ONLY " : "only ");
}

x.getTableName().accept(this);
printTableSourceExpr(x.getTableName());

if (x.getAlias() != null) {
print0(ucase ? " AS " : " as ");
Expand Down Expand Up @@ -337,22 +337,7 @@ public boolean visit(PGInsertStatement x) {

x.getTableSource().accept(this);

if (x.getColumns().size() > 0) {
incrementIndent();
println();
print('(');
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
if (i != 0) {
if (i % 5 == 0) {
println();
}
print0(", ");
}
x.getColumns().get(i).accept(this);
}
print(')');
decrementIndent();
}
printInsertColumns(x.getColumns());

if (x.getValues() != null) {
println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,7 @@ public boolean visit(SQLServerInsertStatement x) {

x.getTableSource().accept(this);

if (x.getColumns().size() > 0) {
incrementIndent();
println();
print('(');
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
if (i != 0) {
if (i % 5 == 0) {
println();
}
print0(", ");
}

x.getColumns().get(i).accept(this);
}
print(')');
decrementIndent();
}
printInsertColumns(x.getColumns());

if (x.getOutput() != null) {
println();
Expand All @@ -177,8 +161,7 @@ public boolean visit(SQLServerInsertStatement x) {

if (x.getValuesList().size() != 0) {
println();
print0(ucase ? "VALUES" : "values");
println();
print0(ucase ? "VALUES " : "values ");
for (int i = 0, size = x.getValuesList().size(); i < size; ++i) {
if (i != 0) {
print(',');
Expand Down Expand Up @@ -256,7 +239,7 @@ public void endVisit(SQLServerUpdateStatement x) {
}

public boolean visit(SQLExprTableSource x) {
x.getExpr().accept(this);
printTableSourceExpr(x.getExpr());

if (x.getAlias() != null) {
print(' ');
Expand Down
103 changes: 69 additions & 34 deletions src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import java.sql.Clob;
import java.sql.NClob;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;

import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.*;
Expand All @@ -40,17 +38,19 @@
public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements PrintableVisitor {

protected final Appendable appender;
private String indent = "\t";
private int indentCount = 0;
private boolean prettyFormat = true;
protected boolean ucase = true;
protected int selectListNumberOfLine = 5;
private String indent = "\t";
private int indentCount = 0;
private boolean prettyFormat = true;
protected boolean ucase = true;
protected int selectListNumberOfLine = 5;

protected boolean groupItemSingleLine = false;
protected boolean groupItemSingleLine = false;

protected List<Object> parameters;
protected List<Object> parameters;

protected String dbType;
protected String dbType;

protected Map<String, String> tableMapping;

public SQLASTOutputVisitor(Appendable appender){
this.appender = appender;
Expand All @@ -64,6 +64,17 @@ public int getParametersSize() {
return this.parameters.size();
}

public void addTableMapping(String srcTable, String destTable) {
if (tableMapping != null) {
tableMapping = new HashMap<String, String>();
}
tableMapping.put(srcTable, destTable);
}

public void setTableMapping(Map<String, String> tableMapping) {
this.tableMapping = tableMapping;
}

public List<Object> getParameters() {
if (parameters == null) {
parameters = new ArrayList<Object>();
Expand Down Expand Up @@ -772,8 +783,22 @@ public boolean visit(SQLSelectOrderByItem x) {
return false;
}

protected void printTableSourceExpr(SQLExpr expr) {
if (tableMapping != null && expr instanceof SQLName) {
String tableName = expr.toString();
String destTableName = tableMapping.get(tableName);
if (destTableName != null) {
tableName = destTableName;
print0(tableName);
return;
}
}

expr.accept(this);
}

public boolean visit(SQLExprTableSource x) {
x.getExpr().accept(this);
printTableSourceExpr(x.getExpr());

if (x.getAlias() != null) {
print(' ');
Expand Down Expand Up @@ -984,10 +1009,10 @@ public boolean visit(SQLDeleteStatement x) {

if (from == null) {
print0(ucase ? "DELETE FROM " : "delete from ");
x.getTableName().accept(this);
printTableSourceExpr(x.getTableName());
} else {
print0(ucase ? "DELETE " : "delete ");
x.getTableName().accept(this);
printTableSourceExpr(x.getTableName());
print0(ucase ? " FROM " : " from ");
from.accept(this);
}
Expand Down Expand Up @@ -1019,19 +1044,42 @@ public boolean visit(SQLInsertStatement x) {

x.getTableSource().accept(this);

if (x.getColumns().size() > 0) {
incrementIndent();
printInsertColumns(x.getColumns());

if (!x.getValuesList().isEmpty()) {
println();
print0(ucase ? "VALUES " : "values ");
printAndAccept(x.getValuesList(), ", ");
} else {
if (x.getQuery() != null) {
println();
x.getQuery().setParent(x);
x.getQuery().accept(this);
}
}

return false;
}

protected void printInsertColumns(List<SQLExpr> columns) {
final int size = columns.size();
if (size > 0) {
if (size > 5) {
incrementIndent();
println();
} else {
print(' ');
}
print('(');
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
for (int i = 0; i < size; ++i) {
if (i != 0) {
if (i % 5 == 0) {
println();
}
print0(", ");
}

SQLExpr column = x.getColumns().get(i);
SQLExpr column = columns.get(i);
column.accept(this);

String dataType = (String) column.getAttribute("dataType");
Expand All @@ -1041,23 +1089,10 @@ public boolean visit(SQLInsertStatement x) {
}
}
print(')');
decrementIndent();
}

if (!x.getValuesList().isEmpty()) {
println();
print0(ucase ? "VALUES" : "values");
println();
printAndAccept(x.getValuesList(), ", ");
} else {
if (x.getQuery() != null) {
println();
x.getQuery().setParent(x);
x.getQuery().accept(this);
if (size > 5) {
decrementIndent();
}
}

return false;
}

public boolean visit(SQLUpdateSetItem x) {
Expand Down Expand Up @@ -1101,7 +1136,7 @@ public boolean visit(SQLCreateTableStatement x) {
print0(ucase ? "LOCAL TEMPORARY " : "local temporary ");
}

x.getName().accept(this);
printTableSourceExpr(x.getName());

int size = x.getTableElementList().size();

Expand Down

0 comments on commit a26e4c5

Please sign in to comment.