improved sql parser.

This commit is contained in:
wenshao
2017-06-04 21:30:23 +08:00
parent de4bfb0ae3
commit 21ae9b5722
63 changed files with 1049 additions and 350 deletions
@@ -479,21 +479,7 @@ public class SQLUtils {
}
public static List<SQLExpr> split(SQLBinaryOpExpr x) {
List<SQLExpr> groupList = new ArrayList<SQLExpr>();
groupList.add(x.getRight());
SQLExpr left = x.getLeft();
for (;;) {
if (left instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) left).getOperator() == x.getOperator()) {
SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left;
groupList.add(binaryLeft.getRight());
left = binaryLeft.getLeft();
} else {
groupList.add(left);
break;
}
}
return groupList;
return SQLBinaryOpExpr.split(x);
}
public static String translateOracleToMySql(String sql) {
@@ -789,5 +775,40 @@ public class SQLUtils {
return new SQLUnaryExpr(SQLUnaryOperator.Not, expr);
}
public static String normalize(String name) {
if (name.length() > 2) {
char c0 = name.charAt(0);
char x0 = name.charAt(name.length() - 1);
if ((c0 == '"' && x0 == '"') || (c0 == '`' && x0 == '`')) {
name = name.substring(1, name.length() - 1);
}
}
return name;
}
public static boolean isValue(SQLExpr expr) {
if (expr instanceof SQLLiteralExpr) {
return true;
}
if (expr instanceof SQLVariantRefExpr) {
return true;
}
if (expr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) expr;
SQLBinaryOperator op = binaryOpExpr.getOperator();
if (op == SQLBinaryOperator.Add
|| op == SQLBinaryOperator.Subtract
|| op == SQLBinaryOperator.Multiply) {
return isValue(binaryOpExpr.getLeft())
&& isValue(binaryOpExpr.getRight());
}
}
return false;
}
}