mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. for issue #1979
This commit is contained in:
+22
-2
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
+22
-3
@@ -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);
|
||||
|
||||
+7
@@ -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;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SQLServerSelectTest27 extends TestCase {
|
||||
|
||||
public void test_simple() throws Exception {
|
||||
String sql = //
|
||||
"set identity_insert tb_coupon OFF"; //
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.SQL_SERVER);
|
||||
|
||||
{
|
||||
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER);
|
||||
|
||||
Assert.assertEquals("SET IDENTITY_INSERT tb_coupon OFF", text);
|
||||
}
|
||||
{
|
||||
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
|
||||
Assert.assertEquals("set IDENTITY_INSERT tb_coupon OFF", text);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user