improved sql parser. for issue #1979

This commit is contained in:
wenshao
2017-09-17 17:10:55 +08:00
parent ff9364cf12
commit b54a987cc8
5 changed files with 115 additions and 5 deletions
@@ -23,8 +23,8 @@ import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerStatementImpl;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
public class SQLServerSetStatement extends SQLServerStatementImpl implements SQLServerStatement {
private SQLAssignItem item = new SQLAssignItem();
private Option option;
private SQLAssignItem item = new SQLAssignItem();
public SQLServerSetStatement(){
}
@@ -38,6 +38,9 @@ public class SQLServerSetStatement extends SQLServerStatementImpl implements SQL
}
public void setItem(SQLAssignItem item) {
if (item != null) {
item.setParent(this);
}
this.item = item;
}
@@ -46,6 +49,14 @@ public class SQLServerSetStatement extends SQLServerStatementImpl implements SQL
item.output(buf);
}
public Option getOption() {
return option;
}
public void setOption(Option option) {
this.option = option;
}
@Override
public void accept0(SQLServerASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -53,4 +64,13 @@ public class SQLServerSetStatement extends SQLServerStatementImpl implements SQL
}
visitor.endVisit(this);
}
public void set(SQLExpr target, SQLExpr value) {
SQLAssignItem assignItem = new SQLAssignItem(target, value);
setItem(assignItem);
}
public static enum Option {
IDENTITY_INSERT
}
}
@@ -362,7 +362,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
public SQLStatement parseSet() {
accept(Token.SET);
if (lexer.identifierEquals("TRANSACTION")) {
if (lexer.identifierEquals(FnvHash.Constants.TRANSACTION)) {
lexer.nextToken();
acceptIdentifier("ISOLATION");
acceptIdentifier("LEVEL");
@@ -402,7 +402,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
return stmt;
}
if (lexer.identifierEquals("STATISTICS")) {
if (lexer.identifierEquals(FnvHash.Constants.STATISTICS)) {
lexer.nextToken();
SQLServerSetStatement stmt = new SQLServerSetStatement();
@@ -415,7 +415,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
if (lexer.token() == Token.ON) {
stmt.getItem().setValue(new SQLIdentifierExpr("ON"));
lexer.nextToken();
} else if (lexer.identifierEquals("OFF")) {
} else if (lexer.identifierEquals(FnvHash.Constants.OFF)) {
stmt.getItem().setValue(new SQLIdentifierExpr("OFF"));
lexer.nextToken();
}
@@ -423,6 +423,25 @@ public class SQLServerStatementParser extends SQLStatementParser {
return stmt;
}
if (lexer.identifierEquals(FnvHash.Constants.IDENTITY_INSERT)) {
SQLServerSetStatement stmt = new SQLServerSetStatement();
stmt.setOption(SQLServerSetStatement.Option.IDENTITY_INSERT);
lexer.nextToken();
SQLName table = this.exprParser.name();
if (lexer.token() == Token.ON) {
SQLExpr value = new SQLIdentifierExpr("ON");
stmt.set(table, value);
lexer.nextToken();
} else if (lexer.identifierEquals(FnvHash.Constants.OFF)) {
SQLExpr value = new SQLIdentifierExpr("OFF");
stmt.set(table, value);
lexer.nextToken();
}
return stmt;
}
if (lexer.token() == Token.VARIANT) {
SQLSetStatement stmt = new SQLSetStatement(getDbType());
parseAssignItems(stmt.getItems(), stmt);
@@ -350,6 +350,13 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
@Override
public boolean visit(SQLServerSetStatement x) {
print0(ucase ? "SET " : "set ");
SQLServerSetStatement.Option option = x.getOption();
if (option != null) {
print(option.name());
print(' ');
}
SQLAssignItem item = x.getItem();
item.getTarget().accept(this);
print(' ');
@@ -36,6 +36,25 @@ public final class FnvHash {
return hash;
}
public static long fnv1a_64(String input, int offset, int end) {
if (input == null) {
return 0;
}
if (input.length() < end) {
end = input.length();
}
long hash = BASIC;
for (int i = offset; i < end; ++i) {
char c = input.charAt(i);
hash ^= c;
hash *= PRIME;
}
return hash;
}
public static long fnv1a_64(char[] chars) {
if (chars == null) {
return 0;