mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
+1
-1
@@ -77,7 +77,7 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
if (visitor instanceof MySqlASTVisitor) {
|
||||
accept0((MySqlASTVisitor) visitor);
|
||||
} else {
|
||||
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
|
||||
super.accept0(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -129,7 +129,7 @@ public class MySqlDeleteStatement extends SQLDeleteStatement {
|
||||
if (visitor instanceof MySqlASTVisitor) {
|
||||
accept0((MySqlASTVisitor) visitor);
|
||||
} else {
|
||||
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
|
||||
super.accept0(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -103,7 +103,7 @@ public class MySqlInsertStatement extends SQLInsertStatement {
|
||||
if (visitor instanceof MySqlASTVisitor) {
|
||||
accept0((MySqlASTVisitor) visitor);
|
||||
} else {
|
||||
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
|
||||
super.accept0(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ public class MySqlUpdateStatement extends SQLUpdateStatement implements MySqlSta
|
||||
if (visitor instanceof MySqlASTVisitor) {
|
||||
accept0((MySqlASTVisitor) visitor);
|
||||
} else {
|
||||
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
|
||||
super.accept0(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -799,8 +799,10 @@ public class OracleExprParser extends SQLExprParser {
|
||||
}
|
||||
}
|
||||
|
||||
over.setOrderBy(parseOrderBy());
|
||||
if (over.getOrderBy() != null) {
|
||||
final SQLOrderBy orderBy = parseOrderBy();
|
||||
if (orderBy != null) {
|
||||
over.setOrderBy(orderBy);
|
||||
|
||||
OracleAnalyticWindowing windowing = null;
|
||||
if (lexer.identifierEquals(FnvHash.Constants.ROWS)) {
|
||||
lexer.nextToken();
|
||||
@@ -815,12 +817,9 @@ public class OracleExprParser extends SQLExprParser {
|
||||
if (windowing != null) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CURRENT)) {
|
||||
lexer.nextToken();
|
||||
if (lexer.stringVal().equalsIgnoreCase("ROW")) {
|
||||
lexer.nextToken();
|
||||
windowing.setExpr(new SQLIdentifierExpr("CURRENT ROW"));
|
||||
over.setWindowing(windowing);
|
||||
}
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
accept(Token.ROW);
|
||||
windowing.setExpr(new SQLIdentifierExpr("CURRENT ROW"));
|
||||
over.setWindowing(windowing);
|
||||
}
|
||||
if (lexer.identifierEquals(FnvHash.Constants.UNBOUNDED)) {
|
||||
lexer.nextToken();
|
||||
@@ -830,6 +829,46 @@ public class OracleExprParser extends SQLExprParser {
|
||||
} else {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
} else if (lexer.token() == Token.BETWEEN) {
|
||||
lexer.nextToken();
|
||||
SQLExpr beginExpr;
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CURRENT)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.ROW);
|
||||
beginExpr = new SQLIdentifierExpr("CURRENT ROW");
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.UNBOUNDED)) {
|
||||
lexer.nextToken();
|
||||
if (lexer.stringVal().equalsIgnoreCase("PRECEDING")) {
|
||||
lexer.nextToken();
|
||||
beginExpr = new SQLIdentifierExpr("UNBOUNDED PRECEDING");
|
||||
} else {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
} else {
|
||||
beginExpr = relational();
|
||||
}
|
||||
|
||||
accept(Token.AND);
|
||||
SQLExpr endExpr;
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CURRENT)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.ROW);
|
||||
endExpr = new SQLIdentifierExpr("CURRENT ROW");
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.UNBOUNDED)) {
|
||||
lexer.nextToken();
|
||||
if (lexer.stringVal().equalsIgnoreCase("PRECEDING")) {
|
||||
lexer.nextToken();
|
||||
endExpr = new SQLIdentifierExpr("UNBOUNDED PRECEDING");
|
||||
} else {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
} else {
|
||||
endExpr = relational();
|
||||
}
|
||||
|
||||
SQLExpr expr = new SQLBetweenExpr(null, beginExpr, endExpr);
|
||||
windowing.setExpr(expr);
|
||||
} else {
|
||||
SQLExpr expr = this.expr();
|
||||
windowing.setExpr(expr);
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ public class SQLServerRollbackStatement extends SQLRollbackStatement implements
|
||||
if (visitor instanceof SQLServerASTVisitor) {
|
||||
accept0((SQLServerASTVisitor) visitor);
|
||||
} else {
|
||||
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
|
||||
super.accept0(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -480,12 +480,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
quote = true;
|
||||
}
|
||||
|
||||
if (quote) {
|
||||
print('(');
|
||||
printExpr(testExpr);
|
||||
print(')');
|
||||
} else {
|
||||
printExpr(testExpr);
|
||||
if (testExpr != null) {
|
||||
if (quote) {
|
||||
print('(');
|
||||
printExpr(testExpr);
|
||||
print(')');
|
||||
} else {
|
||||
printExpr(testExpr);
|
||||
}
|
||||
}
|
||||
|
||||
if (x.isNot()) {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alibaba.druid.sql.refactor;
|
||||
package com.alibaba.druid.bvt.sql.refactor;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.alibaba.druid.bvt.sql.refactor;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ClearSchema_0 extends TestCase {
|
||||
public void test_insert_0() throws Exception {
|
||||
String sql = "INSERT INTO testdb.Websites (name, country)\n" +
|
||||
"SELECT app_name, country FROM testdb.apps;";
|
||||
|
||||
SQLStatement stmt = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL).get(0);
|
||||
|
||||
SQLASTVisitor v = new SQLASTVisitorAdapter() {
|
||||
@Override
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
if (SQLUtils.replaceInParent(x, new SQLIdentifierExpr(x.getName()))) {
|
||||
return false;
|
||||
}
|
||||
return super.visit(x);
|
||||
}
|
||||
};
|
||||
stmt.accept(v);
|
||||
|
||||
assertEquals("INSERT INTO Websites (name, country)\n" +
|
||||
"SELECT app_name, country\n" +
|
||||
"FROM apps;", stmt.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user