mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved parser performance.
This commit is contained in:
+18
-3
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
@@ -32,16 +33,30 @@ import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReference
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class SQLServerExprParser extends SQLExprParser {
|
||||
public final static String[] AGGREGATE_FUNCTIONS;
|
||||
|
||||
public final static String[] AGGREGATE_FUNCTIONS = { "AVG", "COUNT", "MAX", "MIN", "ROW_NUMBER", "STDDEV", "SUM" };
|
||||
public final static long[] AGGREGATE_FUNCTIONS_CODES;
|
||||
|
||||
static {
|
||||
String[] strings = { "AVG", "COUNT", "MAX", "MIN", "ROW_NUMBER", "STDDEV", "SUM" };
|
||||
AGGREGATE_FUNCTIONS_CODES = FNVUtils.fnv_64_lower(strings, true);
|
||||
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
|
||||
for (String str : strings) {
|
||||
long hash = FNVUtils.fnv_64_lower(str);
|
||||
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
|
||||
AGGREGATE_FUNCTIONS[index] = str;
|
||||
}
|
||||
}
|
||||
|
||||
public SQLServerExprParser(Lexer lexer){
|
||||
super(lexer);
|
||||
this.dbType = JdbcConstants.SQL_SERVER;
|
||||
this.aggregateFunctions = AGGREGATE_FUNCTIONS;
|
||||
this.aggregateFunctionHashCodes = AGGREGATE_FUNCTIONS_CODES;
|
||||
}
|
||||
|
||||
public SQLServerExprParser(String sql){
|
||||
@@ -143,7 +158,7 @@ public class SQLServerExprParser extends SQLExprParser {
|
||||
}
|
||||
|
||||
protected SQLServerOutput parserOutput() {
|
||||
if (identifierEquals("OUTPUT")) {
|
||||
if (lexer.identifierEquals("OUTPUT")) {
|
||||
lexer.nextToken();
|
||||
SQLServerOutput output = new SQLServerOutput();
|
||||
|
||||
@@ -221,7 +236,7 @@ public class SQLServerExprParser extends SQLExprParser {
|
||||
column.setDefaultExpr(new SQLNullExpr());
|
||||
} else {
|
||||
accept(Token.FOR);
|
||||
identifierEquals("REPLICATION ");
|
||||
acceptIdentifier("REPLICATION ");
|
||||
identity.setNotForReplication(true);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-9
@@ -54,28 +54,28 @@ public class SQLServerSelectParser extends SQLSelectParser {
|
||||
if (lexer.token() == Token.FOR) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (identifierEquals("BROWSE")) {
|
||||
if (lexer.identifierEquals("BROWSE")) {
|
||||
lexer.nextToken();
|
||||
select.setForBrowse(true);
|
||||
} else if (identifierEquals("XML")) {
|
||||
} else if (lexer.identifierEquals("XML")) {
|
||||
lexer.nextToken();
|
||||
|
||||
for (;;) {
|
||||
if (identifierEquals("AUTO") //
|
||||
|| identifierEquals("TYPE") //
|
||||
|| identifierEquals("XMLSCHEMA") //
|
||||
if (lexer.identifierEquals("AUTO") //
|
||||
|| lexer.identifierEquals("TYPE") //
|
||||
|| lexer.identifierEquals("XMLSCHEMA") //
|
||||
) {
|
||||
select.getForXmlOptions().add(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
} else if (identifierEquals("ELEMENTS")) {
|
||||
} else if (lexer.identifierEquals("ELEMENTS")) {
|
||||
lexer.nextToken();
|
||||
if (identifierEquals("XSINIL")) {
|
||||
if (lexer.identifierEquals("XSINIL")) {
|
||||
lexer.nextToken();
|
||||
select.getForXmlOptions().add("ELEMENTS XSINIL");
|
||||
} else {
|
||||
select.getForXmlOptions().add("ELEMENTS");
|
||||
}
|
||||
} else if (identifierEquals("PATH")) {
|
||||
} else if (lexer.identifierEquals("PATH")) {
|
||||
SQLExpr xmlPath = this.exprParser.expr();
|
||||
select.setXmlPath(xmlPath);
|
||||
} else {
|
||||
@@ -94,7 +94,7 @@ public class SQLServerSelectParser extends SQLSelectParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (identifierEquals("OFFSET")) {
|
||||
if (lexer.identifierEquals("OFFSET")) {
|
||||
lexer.nextToken();
|
||||
SQLExpr offset = this.expr();
|
||||
|
||||
|
||||
+23
-23
@@ -64,7 +64,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (identifierEquals("EXEC") || identifierEquals("EXECUTE")) {
|
||||
if (lexer.identifierEquals("EXEC") || lexer.identifierEquals("EXECUTE")) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLServerExecStatement execStmt = new SQLServerExecStatement();
|
||||
@@ -109,7 +109,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (identifierEquals("WAITFOR")) {
|
||||
if (lexer.identifierEquals("WAITFOR")) {
|
||||
statementList.add(this.parseWaitFor());
|
||||
return true;
|
||||
}
|
||||
@@ -353,34 +353,34 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
public SQLStatement parseSet() {
|
||||
accept(Token.SET);
|
||||
|
||||
if (identifierEquals("TRANSACTION")) {
|
||||
if (lexer.identifierEquals("TRANSACTION")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("ISOLATION");
|
||||
acceptIdentifier("LEVEL");
|
||||
|
||||
SQLServerSetTransactionIsolationLevelStatement stmt = new SQLServerSetTransactionIsolationLevelStatement();
|
||||
|
||||
if (identifierEquals("READ")) {
|
||||
if (lexer.identifierEquals("READ")) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (identifierEquals("UNCOMMITTED")) {
|
||||
if (lexer.identifierEquals("UNCOMMITTED")) {
|
||||
stmt.setLevel("READ UNCOMMITTED");
|
||||
lexer.nextToken();
|
||||
} else if (identifierEquals("COMMITTED")) {
|
||||
} else if (lexer.identifierEquals("COMMITTED")) {
|
||||
stmt.setLevel("READ COMMITTED");
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal() + ", " + lexer.info());
|
||||
}
|
||||
} else if (identifierEquals("SERIALIZABLE")) {
|
||||
} else if (lexer.identifierEquals("SERIALIZABLE")) {
|
||||
stmt.setLevel("SERIALIZABLE");
|
||||
lexer.nextToken();
|
||||
} else if (identifierEquals("SNAPSHOT")) {
|
||||
} else if (lexer.identifierEquals("SNAPSHOT")) {
|
||||
stmt.setLevel("SNAPSHOT");
|
||||
lexer.nextToken();
|
||||
} else if (identifierEquals("REPEATABLE")) {
|
||||
} else if (lexer.identifierEquals("REPEATABLE")) {
|
||||
lexer.nextToken();
|
||||
if (identifierEquals("READ")) {
|
||||
if (lexer.identifierEquals("READ")) {
|
||||
stmt.setLevel("REPEATABLE READ");
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
@@ -393,20 +393,20 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
if (identifierEquals("STATISTICS")) {
|
||||
if (lexer.identifierEquals("STATISTICS")) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLServerSetStatement stmt = new SQLServerSetStatement();
|
||||
|
||||
if (identifierEquals("IO") || identifierEquals("XML") || identifierEquals("PROFILE")
|
||||
|| identifierEquals("TIME")) {
|
||||
if (lexer.identifierEquals("IO") || lexer.identifierEquals("XML") || lexer.identifierEquals("PROFILE")
|
||||
|| lexer.identifierEquals("TIME")) {
|
||||
stmt.getItem().setTarget(new SQLIdentifierExpr("STATISTICS " + lexer.stringVal().toUpperCase()));
|
||||
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.ON) {
|
||||
stmt.getItem().setValue(new SQLIdentifierExpr("ON"));
|
||||
lexer.nextToken();
|
||||
} else if (identifierEquals("OFF")) {
|
||||
} else if (lexer.identifierEquals("OFF")) {
|
||||
stmt.getItem().setValue(new SQLIdentifierExpr("OFF"));
|
||||
lexer.nextToken();
|
||||
}
|
||||
@@ -425,7 +425,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
if (lexer.token() == Token.ON) {
|
||||
stmt.getItem().setValue(new SQLIdentifierExpr("ON"));
|
||||
lexer.nextToken();
|
||||
} else if (identifierEquals("OFF")) {
|
||||
} else if (lexer.identifierEquals("OFF")) {
|
||||
stmt.getItem().setValue(new SQLIdentifierExpr("OFF"));
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
@@ -462,7 +462,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
public SQLStatement parseBlock() {
|
||||
accept(Token.BEGIN);
|
||||
|
||||
if (identifierEquals("TRANSACTION") || identifierEquals("TRAN")) {
|
||||
if (lexer.identifierEquals("TRANSACTION") || lexer.identifierEquals("TRAN")) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLStartTransactionStatement startTrans = new SQLStartTransactionStatement();
|
||||
@@ -487,12 +487,12 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
SQLCommitStatement stmt = new SQLCommitStatement();
|
||||
|
||||
if (identifierEquals("WORK")) {
|
||||
if (lexer.identifierEquals("WORK")) {
|
||||
lexer.nextToken();
|
||||
stmt.setWork(true);
|
||||
}
|
||||
|
||||
if (identifierEquals("TRAN") || identifierEquals("TRANSACTION")) {
|
||||
if (lexer.identifierEquals("TRAN") || lexer.identifierEquals("TRANSACTION")) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.VARIANT) {
|
||||
stmt.setTransactionName(this.exprParser.expr());
|
||||
@@ -517,12 +517,12 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
SQLServerRollbackStatement stmt = new SQLServerRollbackStatement();
|
||||
|
||||
if (identifierEquals("WORK")) {
|
||||
if (lexer.identifierEquals("WORK")) {
|
||||
lexer.nextToken();
|
||||
stmt.setWork(true);
|
||||
}
|
||||
|
||||
if (identifierEquals("TRAN") || identifierEquals("TRANSACTION")) {
|
||||
if (lexer.identifierEquals("TRAN") || lexer.identifierEquals("TRANSACTION")) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.VARIANT) {
|
||||
stmt.setName(this.exprParser.expr());
|
||||
@@ -539,19 +539,19 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
SQLServerWaitForStatement stmt = new SQLServerWaitForStatement();
|
||||
|
||||
if (identifierEquals("DELAY")) {
|
||||
if (lexer.identifierEquals("DELAY")) {
|
||||
lexer.nextToken();
|
||||
stmt.setDelay(this.exprParser.expr());
|
||||
}
|
||||
|
||||
if (identifierEquals("TIME")) {
|
||||
if (lexer.identifierEquals("TIME")) {
|
||||
lexer.nextToken();
|
||||
stmt.setTime(this.exprParser.expr());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
if (identifierEquals("TIMEOUT")) {
|
||||
if (lexer.identifierEquals("TIMEOUT")) {
|
||||
lexer.nextToken();
|
||||
stmt.setTimeout(this.exprParser.expr());
|
||||
}
|
||||
|
||||
+7
-7
@@ -111,12 +111,12 @@ public class MSSQLServerExportParameterVisitor extends SQLServerOutputVisitor im
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
if(requireParameterizedOutput){
|
||||
return super.visit(x);
|
||||
}
|
||||
ExportParameterVisitorUtils.exportParameter(this.parameters, x);
|
||||
return true;
|
||||
}
|
||||
// public boolean visit(SQLBinaryOpExpr x) {
|
||||
// if(requireParameterizedOutput){
|
||||
// return super.visit(x);
|
||||
// }
|
||||
// ExportParameterVisitorUtils.exportParameter(this.parameters, x);
|
||||
// return true;
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
+8
@@ -107,6 +107,9 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLServerTop x) {
|
||||
boolean parameterized = this.parameterized;
|
||||
this.parameterized = false;
|
||||
|
||||
print0(ucase ? "TOP " : "top ");
|
||||
|
||||
boolean paren = false;
|
||||
@@ -126,6 +129,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
print0(ucase ? " PERCENT" : " percent");
|
||||
}
|
||||
|
||||
this.parameterized = parameterized;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -265,6 +269,9 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLColumnDefinition x) {
|
||||
boolean parameterized = this.parameterized;
|
||||
this.parameterized = false;
|
||||
|
||||
x.getName().accept(this);
|
||||
|
||||
if (x.getDataType() != null) {
|
||||
@@ -292,6 +299,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
}
|
||||
}
|
||||
|
||||
this.parameterized = parameterized;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user