mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sql server parser
This commit is contained in:
+3
-3
@@ -26,13 +26,13 @@ public class SQLServerSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Top top;
|
||||
private SQLServerTop top;
|
||||
|
||||
public Top getTop() {
|
||||
public SQLServerTop getTop() {
|
||||
return top;
|
||||
}
|
||||
|
||||
public void setTop(Top top) {
|
||||
public void setTop(SQLServerTop top) {
|
||||
this.top = top;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ package com.alibaba.druid.sql.dialect.sqlserver.ast;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
|
||||
public class Top extends SQLServerObjectImpl {
|
||||
public class SQLServerTop extends SQLServerObjectImpl {
|
||||
|
||||
/**
|
||||
*
|
||||
+16
-6
@@ -18,24 +18,33 @@ package com.alibaba.druid.sql.dialect.sqlserver.ast.stmt;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLServerStatement {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SQLTableSource from;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SQLServerTop top;
|
||||
private SQLTableSource from;
|
||||
|
||||
public SQLServerTop getTop() {
|
||||
return top;
|
||||
}
|
||||
|
||||
public void setTop(SQLServerTop top) {
|
||||
this.top = top;
|
||||
}
|
||||
|
||||
public SQLTableSource getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
|
||||
public void setFrom(SQLTableSource from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
this.accept0((SQLServerASTVisitor) visitor);
|
||||
@@ -44,6 +53,7 @@ public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLS
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, top);
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, items);
|
||||
acceptChild(visitor, from);
|
||||
@@ -51,5 +61,5 @@ public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLS
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
@@ -106,4 +107,27 @@ public class SQLServerExprParser extends SQLExprParser {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public SQLServerTop parseTop() {
|
||||
if (lexer.token() == Token.TOP) {
|
||||
SQLServerTop top = new SQLServerTop();
|
||||
lexer.nextToken();
|
||||
|
||||
boolean paren = false;
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
paren = true;
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
top.setExpr(primary());
|
||||
|
||||
if (paren) {
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
return top;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
|
||||
import static com.alibaba.druid.sql.parser.LayoutCharacters.EOI;
|
||||
import static com.alibaba.druid.sql.parser.Token.IDENTIFIER;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -142,4 +143,35 @@ public class SQLServerLexer extends Lexer {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
protected void scanLBracket() {
|
||||
mark = pos;
|
||||
|
||||
if (buf == null) {
|
||||
buf = new char[32];
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (isEOF()) {
|
||||
lexError("unclosed.str.lit");
|
||||
return;
|
||||
}
|
||||
|
||||
ch = charAt(++pos);
|
||||
|
||||
if (ch == ']') {
|
||||
scanChar();
|
||||
token = IDENTIFIER;
|
||||
break;
|
||||
}
|
||||
|
||||
if (bufPos == buf.length) {
|
||||
putChar(ch);
|
||||
} else {
|
||||
buf[bufPos++] = ch;
|
||||
}
|
||||
}
|
||||
|
||||
stringVal = subString(mark, bufPos + 2);
|
||||
}
|
||||
}
|
||||
|
||||
+11
-2
@@ -16,11 +16,13 @@
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelect;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.Top;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
@@ -74,7 +76,7 @@ public class SQLServerSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.TOP) {
|
||||
Top top = new Top();
|
||||
SQLServerTop top = new SQLServerTop();
|
||||
lexer.nextToken();
|
||||
top.setExpr(createExprParser().primary());
|
||||
queryBlock.setTop(top);
|
||||
@@ -82,6 +84,13 @@ public class SQLServerSelectParser extends SQLSelectParser {
|
||||
|
||||
parseSelectList(queryBlock);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.INTO) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLTableSource into = this.parseTableSource();
|
||||
queryBlock.setInto((SQLExprTableSource) into);
|
||||
}
|
||||
|
||||
parseFrom(queryBlock);
|
||||
|
||||
|
||||
+21
-13
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertInto;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerUpdateStatement;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
@@ -36,7 +37,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
public SQLServerStatementParser(String sql){
|
||||
super(new SQLServerExprParser(sql));
|
||||
}
|
||||
|
||||
|
||||
public SQLSelectParser createSQLSelectParser() {
|
||||
return new SQLServerSelectParser(this.exprParser);
|
||||
}
|
||||
@@ -44,7 +45,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
public SQLServerStatementParser(Lexer lexer){
|
||||
super(new SQLServerExprParser(lexer));
|
||||
}
|
||||
|
||||
|
||||
public boolean parseStatementListDialect(List<SQLStatement> statementList) {
|
||||
if (lexer.token() == Token.WITH) {
|
||||
SQLStatement stmt = parseSelect();
|
||||
@@ -54,7 +55,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public SQLStatement parseInsert() {
|
||||
SQLServerInsertStatement insertStatement = new SQLServerInsertStatement();
|
||||
|
||||
@@ -68,7 +69,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
protected void parseInsert0(SQLInsertInto insert, boolean acceptSubQuery) {
|
||||
SQLServerInsertStatement insertStatement = (SQLServerInsertStatement) insert;
|
||||
|
||||
|
||||
if (lexer.token() == Token.INTO) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -115,29 +116,32 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
insertStatement.setQuery(queryExpr.getSubQuery());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected SQLServerUpdateStatement createUpdateStatement() {
|
||||
return new SQLServerUpdateStatement();
|
||||
}
|
||||
|
||||
|
||||
public SQLUpdateStatement parseUpdateStatement() {
|
||||
SQLServerUpdateStatement udpateStatement = createUpdateStatement();
|
||||
|
||||
if (lexer.token() == Token.UPDATE) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLTableSource tableSource = this.exprParser.createSelectParser().parseTableSource();
|
||||
udpateStatement.setTableSource(tableSource);
|
||||
accept(Token.UPDATE);
|
||||
|
||||
SQLServerTop top = this.getExprParser().parseTop();
|
||||
if (top != null) {
|
||||
udpateStatement.setTop(top);
|
||||
}
|
||||
|
||||
SQLTableSource tableSource = this.exprParser.createSelectParser().parseTableSource();
|
||||
udpateStatement.setTableSource(tableSource);
|
||||
|
||||
parseUpdateSet(udpateStatement);
|
||||
|
||||
|
||||
if (lexer.token() == Token.FROM) {
|
||||
lexer.nextToken();
|
||||
SQLTableSource from = this.exprParser.createSelectParser().parseTableSource();
|
||||
udpateStatement.setFrom(from);
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == (Token.WHERE)) {
|
||||
lexer.nextToken();
|
||||
udpateStatement.setWhere(this.exprParser.expr());
|
||||
@@ -145,4 +149,8 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
return udpateStatement;
|
||||
}
|
||||
|
||||
public SQLServerExprParser getExprParser() {
|
||||
return (SQLServerExprParser) exprParser;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -16,7 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.Top;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerUpdateStatement;
|
||||
@@ -28,9 +28,9 @@ public interface SQLServerASTVisitor extends SQLASTVisitor {
|
||||
|
||||
void endVisit(SQLServerSelectQueryBlock x);
|
||||
|
||||
boolean visit(Top x);
|
||||
boolean visit(SQLServerTop x);
|
||||
|
||||
void endVisit(Top x);
|
||||
void endVisit(SQLServerTop x);
|
||||
|
||||
boolean visit(SQLServerObjectReferenceExpr x);
|
||||
|
||||
|
||||
+3
-3
@@ -16,7 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.Top;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerUpdateStatement;
|
||||
@@ -35,12 +35,12 @@ public class SQLServerASTVisitorAdapter extends SQLASTVisitorAdapter implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(Top x) {
|
||||
public boolean visit(SQLServerTop x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(Top x) {
|
||||
public void endVisit(SQLServerTop x) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
+29
-4
@@ -17,7 +17,7 @@ package com.alibaba.druid.sql.dialect.sqlserver.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.Top;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerUpdateStatement;
|
||||
@@ -42,9 +42,16 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
|
||||
if (x.getTop() != null) {
|
||||
x.getTop().accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
printSelectList(x.getSelectList());
|
||||
|
||||
if (x.getInto() != null) {
|
||||
println();
|
||||
print("INTO ");
|
||||
x.getInto().accept(this);
|
||||
}
|
||||
|
||||
if (x.getFrom() != null) {
|
||||
println();
|
||||
@@ -73,15 +80,27 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(Top x) {
|
||||
public boolean visit(SQLServerTop x) {
|
||||
print("TOP ");
|
||||
|
||||
boolean paren = false;
|
||||
|
||||
if (x.getParent() instanceof SQLServerUpdateStatement) {
|
||||
paren = true;
|
||||
print("(");
|
||||
}
|
||||
|
||||
x.getExpr().accept(this);
|
||||
print(" ");
|
||||
|
||||
if (paren) {
|
||||
print(")");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(Top x) {
|
||||
public void endVisit(SQLServerTop x) {
|
||||
|
||||
}
|
||||
|
||||
@@ -148,6 +167,12 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
@Override
|
||||
public boolean visit(SQLServerUpdateStatement x) {
|
||||
print("UPDATE ");
|
||||
|
||||
if (x.getTop() != null) {
|
||||
x.getTop().setParent(x);
|
||||
x.getTop().accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
x.getTableSource().accept(this);
|
||||
|
||||
|
||||
+3
-3
@@ -20,7 +20,7 @@ import java.util.Map;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.Top;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerUpdateStatement;
|
||||
@@ -47,12 +47,12 @@ public class SQLServerSchemaStatVisitor extends SchemaStatVisitor implements SQL
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(Top x) {
|
||||
public boolean visit(SQLServerTop x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(Top x) {
|
||||
public void endVisit(SQLServerTop x) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -251,8 +251,7 @@ public class Lexer {
|
||||
token = RPAREN;
|
||||
return;
|
||||
case '[':
|
||||
scanChar();
|
||||
token = LBRACKET;
|
||||
scanLBracket();
|
||||
return;
|
||||
case ']':
|
||||
scanChar();
|
||||
@@ -375,6 +374,11 @@ public class Lexer {
|
||||
|
||||
}
|
||||
|
||||
protected void scanLBracket() {
|
||||
scanChar();
|
||||
token = LBRACKET;
|
||||
}
|
||||
|
||||
private final void scanOperator() {
|
||||
switch (ch) {
|
||||
case '+':
|
||||
|
||||
@@ -631,7 +631,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
if (x.getGroupBy() != null) {
|
||||
print(" ");
|
||||
println();
|
||||
x.getGroupBy().accept(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerSelectTest10 extends TestCase {
|
||||
|
||||
public void test_isEmpty() throws Exception {
|
||||
String sql = "SELECT c.FirstName, c.LastName, e.Title, a.AddressLine1, a.City" + //
|
||||
" , sp.Name AS [State/Province], a.PostalCode" + //
|
||||
" INTO dbo.EmployeeAddresses" + //
|
||||
" FROM Person.Contact AS c" + //
|
||||
" JOIN HumanResources.Employee AS e ON e.ContactID = c.ContactID" + //
|
||||
" JOIN HumanResources.EmployeeAddress AS ea ON ea.EmployeeID = e.EmployeeID" + //
|
||||
" JOIN Person.Address AS a on a.AddressID = ea.AddressID" + //
|
||||
" JOIN Person.StateProvince as sp ON sp.StateProvinceID = a.StateProvinceID;";
|
||||
|
||||
String expect = "SELECT c.FirstName, c.LastName, e.Title, a.AddressLine1, a.City" + //
|
||||
"\n\t, sp.Name AS [State/Province], a.PostalCode" + //
|
||||
"\nINTO dbo.EmployeeAddresses" + //
|
||||
"\nFROM Person.Contact c" + //
|
||||
"\n\tJOIN HumanResources.Employee e ON e.ContactID = c.ContactID" + //
|
||||
"\n\tJOIN HumanResources.EmployeeAddress ea ON ea.EmployeeID = e.EmployeeID" + //
|
||||
"\n\tJOIN Person.Address a ON a.AddressID = ea.AddressID" + //
|
||||
"\n\tJOIN Person.StateProvince sp ON sp.StateProvinceID = a.StateProvinceID";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class SQLServerSelectTest5 extends TestCase {
|
||||
String sql = "SELECT name, password_hash FROM [master].[sys].sql_logins";
|
||||
|
||||
String expect = "SELECT name, password_hash\n" + //
|
||||
"FROM master.sys.sql_logins";
|
||||
"FROM [master].[sys].sql_logins";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerSelectTest8 extends TestCase {
|
||||
|
||||
public void test_isEmpty() throws Exception {
|
||||
String sql = "SELECT Location.Distance (@p)" + " FROM Cities;";
|
||||
|
||||
String expect = "SELECT Location.Distance(@p)" + //
|
||||
"\nFROM Cities";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerSelectTest9 extends TestCase {
|
||||
|
||||
public void test_isEmpty() throws Exception {
|
||||
String sql = "SELECT AVG(UnitPrice) AS 'Average Price' " + //
|
||||
"FROM Sales.SalesOrderDetail;";
|
||||
|
||||
String expect = "SELECT AVG(UnitPrice) AS 'Average Price'" + //
|
||||
"\nFROM Sales.SalesOrderDetail";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,8 @@ public class SQLServerUpdateTest extends TestCase {
|
||||
"\nFROM reg_student_charge_item a, (SELECT a.FId, CASE WHEN SUM(b.FChargeAmount) IS NULL THEN 0 ELSE SUM(b.FChargeAmount) END AS amount" +
|
||||
"\n\t\tFROM reg_student_charge_item a" +
|
||||
"\n\t\t\tLEFT JOIN reg_student_charge_daybook b ON a.FId = b.FChargeItemId" +
|
||||
"\n\t\tWHERE a.FId = ? GROUP BY a.FId" +
|
||||
"\n\t\tWHERE a.FId = ?" +
|
||||
"\n\t\tGROUP BY a.FId" +
|
||||
"\n\t\t) b" +
|
||||
"\nWHERE a.FId = b.FId" +
|
||||
"\nAND a.FId = ?";
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest1 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE dbo.Table2 " + //
|
||||
"SET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB " + //
|
||||
"FROM dbo.Table2 " + //
|
||||
" INNER JOIN dbo.Table1 " + //
|
||||
" ON (dbo.Table2.ColA = dbo.Table1.ColA);";
|
||||
|
||||
String expect = "UPDATE dbo.Table2" + //
|
||||
"\nSET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB" + //
|
||||
"\nFROM dbo.Table2" + //
|
||||
"\n\tINNER JOIN dbo.Table1 ON dbo.Table2.ColA = dbo.Table1.ColA";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest2 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE Cities " + //
|
||||
"SET Location = CONVERT(Point, '12.3:46.2') " + //
|
||||
"WHERE Name = 'Anchorage';";
|
||||
|
||||
String expect = "UPDATE Cities" + //
|
||||
"\nSET Location = CONVERT(Point, '12.3:46.2')" + //
|
||||
"\nWHERE Name = 'Anchorage'";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest3 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE Sales.SalesPerson " + //
|
||||
"SET SalesYTD = SalesYTD + " + //
|
||||
" (SELECT SUM(so.SubTotal) " + //
|
||||
" FROM Sales.SalesOrderHeader AS so" + //
|
||||
" WHERE so.OrderDate = (SELECT MAX(OrderDate)" + //
|
||||
" FROM Sales.SalesOrderHeader AS so2" + //
|
||||
" WHERE so2.SalesPersonID = " + //
|
||||
" so.SalesPersonID)" + //
|
||||
" AND Sales.SalesPerson.SalesPersonID = so.SalesPersonID" + //
|
||||
" GROUP BY so.SalesPersonID);";
|
||||
|
||||
String expect = "UPDATE Sales.SalesPerson" +
|
||||
"\nSET SalesYTD = SalesYTD + (" +
|
||||
"\n\tSELECT SUM(so.SubTotal)" +
|
||||
"\n\tFROM Sales.SalesOrderHeader so" +
|
||||
"\n\tWHERE so.OrderDate = (" +
|
||||
"\n\t\t\tSELECT MAX(OrderDate)" +
|
||||
"\n\t\t\tFROM Sales.SalesOrderHeader so2" +
|
||||
"\n\t\t\tWHERE so2.SalesPersonID = so.SalesPersonID" +
|
||||
"\n\t\t\t)" +
|
||||
"\n\t\tAND Sales.SalesPerson.SalesPersonID = so.SalesPersonID" +
|
||||
"\n\tGROUP BY so.SalesPersonID" +
|
||||
"\n\t)";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest4 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE TOP (10) HumanResources.Employee " +
|
||||
"SET VacationHours = VacationHours * 1.25 ;";
|
||||
|
||||
String expect = "UPDATE TOP (10) HumanResources.Employee" +
|
||||
"\nSET VacationHours = VacationHours * 1.25";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user