mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sqlserver parser
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2014 Alibaba.com All right reserved. This software is the
|
||||
* confidential and proprietary information of Alibaba.com ("Confidential
|
||||
* Information"). You shall not disclose such Confidential Information and shall
|
||||
* use it only in accordance with the terms of the license agreement you entered
|
||||
* into with Alibaba.com.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
|
||||
public class SQLServerOutput extends SQLServerObjectImpl {
|
||||
|
||||
protected SQLExprTableSource into;
|
||||
|
||||
protected final List<SQLExpr> columns = new ArrayList<SQLExpr>();
|
||||
|
||||
protected final List<SQLSelectItem> selectList = new ArrayList<SQLSelectItem>();
|
||||
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
this.acceptChild(visitor, selectList);
|
||||
this.acceptChild(visitor, into);
|
||||
this.acceptChild(visitor, columns);
|
||||
}
|
||||
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExprTableSource getInto() {
|
||||
return into;
|
||||
}
|
||||
|
||||
public void setInto(SQLExprTableSource into) {
|
||||
this.into = into;
|
||||
}
|
||||
|
||||
public List<SQLExpr> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public List<SQLSelectItem> getSelectList() {
|
||||
return selectList;
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -20,6 +20,8 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerObject;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
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;
|
||||
|
||||
@@ -29,6 +31,10 @@ public class SQLServerInsertStatement extends SQLInsertStatement implements SQLS
|
||||
|
||||
private boolean defaultValues;
|
||||
|
||||
private SQLServerTop top;
|
||||
|
||||
private SQLServerOutput output;
|
||||
|
||||
public ValuesClause getValues() {
|
||||
if (valuesList.size() == 0) {
|
||||
return null;
|
||||
@@ -56,8 +62,10 @@ public class SQLServerInsertStatement extends SQLInsertStatement implements SQLS
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
this.acceptChild(visitor, getTop());
|
||||
this.acceptChild(visitor, getTableSource());
|
||||
this.acceptChild(visitor, getColumns());
|
||||
this.acceptChild(visitor, getOutput());
|
||||
this.acceptChild(visitor, getValuesList());
|
||||
this.acceptChild(visitor, getQuery());
|
||||
}
|
||||
@@ -73,4 +81,20 @@ public class SQLServerInsertStatement extends SQLInsertStatement implements SQLS
|
||||
this.defaultValues = defaultValues;
|
||||
}
|
||||
|
||||
public SQLServerOutput getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
public void setOutput(SQLServerOutput output) {
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
public SQLServerTop getTop() {
|
||||
return top;
|
||||
}
|
||||
|
||||
public void setTop(SQLServerTop top) {
|
||||
this.top = top;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-2
@@ -17,6 +17,7 @@ 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.SQLServerOutput;
|
||||
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;
|
||||
@@ -24,8 +25,9 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLServerStatement {
|
||||
|
||||
private SQLServerTop top;
|
||||
private SQLTableSource from;
|
||||
private SQLServerTop top;
|
||||
private SQLTableSource from;
|
||||
private SQLServerOutput output;
|
||||
|
||||
public SQLServerTop getTop() {
|
||||
return top;
|
||||
@@ -43,6 +45,14 @@ public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLS
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public SQLServerOutput getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
public void setOutput(SQLServerOutput output) {
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
this.accept0((SQLServerASTVisitor) visitor);
|
||||
@@ -54,6 +64,7 @@ public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLS
|
||||
acceptChild(visitor, top);
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, items);
|
||||
acceptChild(visitor, output);
|
||||
acceptChild(visitor, from);
|
||||
acceptChild(visitor, where);
|
||||
}
|
||||
|
||||
@@ -15,13 +15,19 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
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;
|
||||
@@ -133,6 +139,54 @@ public class SQLServerExprParser extends SQLExprParser {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected SQLServerOutput parserOutput() {
|
||||
if (identifierEquals("OUTPUT")) {
|
||||
lexer.nextToken();
|
||||
SQLServerOutput output = new SQLServerOutput();
|
||||
|
||||
final List<SQLSelectItem> selectList = output.getSelectList();
|
||||
for (;;) {
|
||||
final SQLSelectItem selectItem = parseSelectItem();
|
||||
selectList.add(selectItem);
|
||||
|
||||
if (lexer.token() != Token.COMMA) {
|
||||
break;
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.INTO) {
|
||||
lexer.nextToken();
|
||||
output.setInto(new SQLExprTableSource(this.name()));
|
||||
if (lexer.token() == (Token.LPAREN)) {
|
||||
lexer.nextToken();
|
||||
this.exprList(output.getColumns(), output);
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected SQLSelectItem parseSelectItem() {
|
||||
SQLExpr expr;
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
expr = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextTokenComma();
|
||||
|
||||
if (lexer.token() != Token.COMMA) {
|
||||
expr = this.primaryRest(expr);
|
||||
expr = this.exprRest(expr);
|
||||
}
|
||||
} else {
|
||||
expr = this.expr();
|
||||
}
|
||||
final String alias = as();
|
||||
return new SQLSelectItem(expr, alias);
|
||||
}
|
||||
|
||||
protected SQLColumnDefinition createColumnDefinition() {
|
||||
SQLColumnDefinition column = new SQLServerColumnDefinition();
|
||||
|
||||
+18
-2
@@ -26,6 +26,7 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerExecStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement;
|
||||
@@ -93,6 +94,11 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
protected void parseInsert0(SQLInsertInto insert, boolean acceptSubQuery) {
|
||||
SQLServerInsertStatement insertStatement = (SQLServerInsertStatement) insert;
|
||||
|
||||
SQLServerTop top = this.getExprParser().parseTop();
|
||||
if (top != null) {
|
||||
insertStatement.setTop(top);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.INTO) {
|
||||
lexer.nextToken();
|
||||
@@ -107,7 +113,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
parseInsert0_hinits(insertStatement);
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
if (lexer.token() == Token.IDENTIFIER && !lexer.stringVal().equalsIgnoreCase("OUTPUT")) {
|
||||
insertStatement.setAlias(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
}
|
||||
@@ -117,6 +123,11 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
this.exprParser.exprList(insertStatement.getColumns(), insertStatement);
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
SQLServerOutput output = this.getExprParser().parserOutput();
|
||||
if (output != null) {
|
||||
insertStatement.setOutput(output);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.VALUES) {
|
||||
lexer.nextToken();
|
||||
@@ -168,6 +179,11 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
udpateStatement.setTableSource(tableSource);
|
||||
|
||||
parseUpdateSet(udpateStatement);
|
||||
|
||||
SQLServerOutput output = this.getExprParser().parserOutput();
|
||||
if (output != null) {
|
||||
udpateStatement.setOutput(output);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.FROM) {
|
||||
lexer.nextToken();
|
||||
@@ -182,7 +198,7 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
return udpateStatement;
|
||||
}
|
||||
|
||||
|
||||
public SQLServerExprParser getExprParser() {
|
||||
return (SQLServerExprParser) exprParser;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
@@ -67,4 +68,8 @@ public interface SQLServerASTVisitor extends SQLASTVisitor {
|
||||
boolean visit(SQLServerSetStatement x);
|
||||
|
||||
void endVisit(SQLServerSetStatement x);
|
||||
|
||||
boolean visit(SQLServerOutput x);
|
||||
|
||||
void endVisit(SQLServerOutput x);
|
||||
}
|
||||
|
||||
+11
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
@@ -128,4 +129,14 @@ public class SQLServerASTVisitorAdapter extends SQLASTVisitorAdapter implements
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLServerOutput x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLServerOutput x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+64
-5
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition.Identity;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
@@ -95,7 +96,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
|
||||
boolean paren = false;
|
||||
|
||||
if (x.getParent() instanceof SQLServerUpdateStatement) {
|
||||
if (x.getParent() instanceof SQLServerUpdateStatement || x.getParent() instanceof SQLServerInsertStatement) {
|
||||
paren = true;
|
||||
print("(");
|
||||
}
|
||||
@@ -131,8 +132,16 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLServerInsertStatement x) {
|
||||
print("INSERT INTO ");
|
||||
print("INSERT ");
|
||||
|
||||
if (x.getTop() != null) {
|
||||
x.getTop().setParent(x);
|
||||
x.getTop().accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
print("INTO ");
|
||||
|
||||
x.getTableSource().accept(this);
|
||||
|
||||
if (x.getColumns().size() > 0) {
|
||||
@@ -152,6 +161,12 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
print(")");
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
if (x.getOutput() != null) {
|
||||
println();
|
||||
x.getOutput().setParent(x);
|
||||
x.getOutput().accept(this);
|
||||
}
|
||||
|
||||
if (x.getValuesList().size() != 0) {
|
||||
println();
|
||||
@@ -170,7 +185,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
println();
|
||||
x.getQuery().accept(this);
|
||||
}
|
||||
|
||||
|
||||
if (x.isDefaultValues()) {
|
||||
print(" DEFAULT VALUES");
|
||||
}
|
||||
@@ -202,6 +217,12 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
}
|
||||
x.getItems().get(i).accept(this);
|
||||
}
|
||||
|
||||
if (x.getOutput() != null) {
|
||||
println();
|
||||
x.getOutput().setParent(x);
|
||||
x.getOutput().accept(this);
|
||||
}
|
||||
|
||||
if (x.getFrom() != null) {
|
||||
println();
|
||||
@@ -333,7 +354,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLServerSetTransactionIsolationLevelStatement x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -348,6 +369,44 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLServerSetStatement x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLServerOutput x) {
|
||||
print("OUTPUT ");
|
||||
printSelectList(x.getSelectList());
|
||||
|
||||
if (x.getInto() != null) {
|
||||
incrementIndent();
|
||||
println();
|
||||
print("INTO ");
|
||||
x.getInto().accept(this);
|
||||
|
||||
if (x.getColumns().size() > 0) {
|
||||
incrementIndent();
|
||||
println();
|
||||
print("(");
|
||||
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
|
||||
if (i != 0) {
|
||||
if (i % 5 == 0) {
|
||||
println();
|
||||
}
|
||||
print(", ");
|
||||
}
|
||||
|
||||
x.getColumns().get(i).accept(this);
|
||||
}
|
||||
print(")");
|
||||
decrementIndent();
|
||||
}
|
||||
}
|
||||
decrementIndent();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLServerOutput x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition.Identity;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
@@ -157,4 +158,14 @@ public class SQLServerSchemaStatVisitor extends SchemaStatVisitor implements SQL
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLServerOutput x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLServerOutput x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user