mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add mapping support
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.alibaba.druid.sql;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
|
||||
|
||||
public class SQLUtils {
|
||||
|
||||
public static String toMySqlString(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
sqlObject.accept(new MySqlOutputVisitor(out));
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
}
|
||||
|
||||
public static String toOracleString(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
sqlObject.accept(new OracleOutputVisitor(out));
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
}
|
||||
|
||||
public static String toPGString(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
sqlObject.accept(new PGOutputVisitor(out));
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,8 +27,10 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
|
||||
public SQLExprTableSource(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public SQLExprTableSource(SQLExpr expr){
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public SQLExpr getExpr() {
|
||||
return this.expr;
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
|
||||
public class MySqlMappingVisitor extends MySqlASTVisitorAdapter {
|
||||
|
||||
private final Map<String, SQLExpr> mapping = new HashMap<String, SQLExpr>();
|
||||
|
||||
public void addMapping(String column, String targetExpr) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -85,14 +85,22 @@ import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
|
||||
public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
protected Appendable appender;
|
||||
private String indent = "\t";
|
||||
private int indentCount = 0;
|
||||
protected final Appendable appender;
|
||||
private String indent = "\t";
|
||||
private int indentCount = 0;
|
||||
|
||||
public SQLASTOutputVisitor(Appendable appender){
|
||||
this.appender = appender;
|
||||
}
|
||||
|
||||
public int getIndentCount() {
|
||||
return indentCount;
|
||||
}
|
||||
|
||||
public Appendable getAppender() {
|
||||
return appender;
|
||||
}
|
||||
|
||||
public void decrementIndent() {
|
||||
this.indentCount -= 1;
|
||||
}
|
||||
@@ -213,8 +221,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
if (x.getLeft() instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr left = (SQLBinaryOpExpr) x.getLeft();
|
||||
boolean leftRational = left.getOperator() == SQLBinaryOperator.BooleanAnd
|
||||
|| left.getOperator() == SQLBinaryOperator.BooleanOr;
|
||||
|
||||
|| left.getOperator() == SQLBinaryOperator.BooleanOr;
|
||||
|
||||
if (left.getOperator().priority > x.getOperator().priority) {
|
||||
if (leftRational) {
|
||||
incrementIndent();
|
||||
@@ -222,7 +230,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
print('(');
|
||||
left.accept(this);
|
||||
print(')');
|
||||
|
||||
|
||||
if (leftRational) {
|
||||
decrementIndent();
|
||||
}
|
||||
@@ -257,11 +265,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
if (rightRational) {
|
||||
incrementIndent();
|
||||
}
|
||||
|
||||
|
||||
print('(');
|
||||
right.accept(this);
|
||||
print(')');
|
||||
|
||||
|
||||
if (rightRational) {
|
||||
decrementIndent();
|
||||
}
|
||||
@@ -985,5 +993,5 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ public interface SQLASTVisitor {
|
||||
|
||||
boolean visit(SQLExistsExpr x);
|
||||
|
||||
boolean visit(SQLIdentifierExpr astNode);
|
||||
boolean visit(SQLIdentifierExpr x);
|
||||
|
||||
boolean visit(SQLInListExpr x);
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLIdentifierExpr astNode) {
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user