mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved odps sql formatter
This commit is contained in:
@@ -255,11 +255,12 @@ public class SQLUtils {
|
||||
if (i > 0) {
|
||||
visitor.println(";");
|
||||
}
|
||||
|
||||
List<String> comments = stmt.getBeforeCommentsDirect();
|
||||
if (comments != null){
|
||||
for(String comment : comments) {
|
||||
visitor.println(comment);
|
||||
{
|
||||
List<String> comments = stmt.getBeforeCommentsDirect();
|
||||
if (comments != null){
|
||||
for(String comment : comments) {
|
||||
visitor.println(comment);
|
||||
}
|
||||
}
|
||||
}
|
||||
stmt.accept(visitor);
|
||||
@@ -270,6 +271,17 @@ public class SQLUtils {
|
||||
visitor.println();
|
||||
visitor.print(";");
|
||||
}
|
||||
|
||||
List<String> comments = stmt.getAfterCommentsDirect();
|
||||
if (comments != null){
|
||||
for (int j = 0; j < comments.size(); ++j) {
|
||||
String comment = comments.get(j);
|
||||
if (j != 0) {
|
||||
visitor.println();
|
||||
}
|
||||
visitor.print(comment);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,11 +37,14 @@ public interface SQLObject {
|
||||
Map<String, Object> getAttributesDirect();
|
||||
|
||||
void addBeforeComment(String comment);
|
||||
void addBeforeComment(List<String> comments);
|
||||
|
||||
List<String> getBeforeCommentsDirect();
|
||||
|
||||
void addAfterComment(String comment);
|
||||
|
||||
void addAfterComment(List<String> comments);
|
||||
|
||||
List<String> getAfterCommentsDirect();
|
||||
|
||||
boolean hasBeforeComment();
|
||||
|
||||
@@ -111,6 +111,10 @@ public abstract class SQLObjectImpl implements SQLObject {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addBeforeComment(String comment) {
|
||||
if (comment == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (attributes == null) {
|
||||
attributes = new HashMap<String, Object>(1);
|
||||
}
|
||||
@@ -124,6 +128,20 @@ public abstract class SQLObjectImpl implements SQLObject {
|
||||
comments.add(comment);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addBeforeComment(List<String> comments) {
|
||||
if (attributes == null) {
|
||||
attributes = new HashMap<String, Object>(1);
|
||||
}
|
||||
|
||||
List<String> attrComments = (List<String>) attributes.get("format.before_comment");
|
||||
if (attrComments == null) {
|
||||
attributes.put("format.before_comment", comments);
|
||||
} else {
|
||||
attrComments.addAll(comments);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getBeforeCommentsDirect() {
|
||||
if (attributes == null) {
|
||||
@@ -148,6 +166,20 @@ public abstract class SQLObjectImpl implements SQLObject {
|
||||
comments.add(comment);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addAfterComment(List<String> comments) {
|
||||
if (attributes == null) {
|
||||
attributes = new HashMap<String, Object>(1);
|
||||
}
|
||||
|
||||
List<String> attrComments = (List<String>) attributes.get("format.after_comment");
|
||||
if (attrComments == null) {
|
||||
attributes.put("format.after_comment", comments);
|
||||
} else {
|
||||
attrComments.addAll(comments);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getAfterCommentsDirect() {
|
||||
if (attributes == null) {
|
||||
|
||||
@@ -16,5 +16,5 @@
|
||||
package com.alibaba.druid.sql.ast;
|
||||
|
||||
public interface SQLStatement extends SQLObject {
|
||||
|
||||
String getDbType();
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLCommentStatement extends SQLObjectImpl implements SQLStatement {
|
||||
public class SQLCommentStatement extends SQLStatementImpl implements SQLStatement {
|
||||
|
||||
public static enum Type {
|
||||
TABLE, COLUMN
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
private String dbType;
|
||||
|
||||
public SQLInsertStatement(){
|
||||
|
||||
@@ -85,4 +86,13 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,12 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLHint;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLSelect extends SQLObjectImpl {
|
||||
@@ -118,4 +121,16 @@ public class SQLSelect extends SQLObjectImpl {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
SQLObject parent = this.getParent();
|
||||
if (parent instanceof SQLStatement) {
|
||||
String dbType = ((SQLStatement) parent).getDbType();
|
||||
|
||||
if (dbType != null) {
|
||||
return SQLUtils.toSQLString(this, dbType);
|
||||
}
|
||||
}
|
||||
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,9 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLSelectItem extends SQLObjectImpl {
|
||||
|
||||
private SQLExpr expr;
|
||||
private String alias;
|
||||
private boolean connectByRoot = false;
|
||||
protected SQLExpr expr;
|
||||
protected String alias;
|
||||
protected boolean connectByRoot = false;
|
||||
|
||||
public SQLSelectItem(){
|
||||
|
||||
|
||||
@@ -106,11 +106,11 @@ public class MySqlLexer extends Lexer {
|
||||
bufPos++;
|
||||
}
|
||||
|
||||
stringVal = subString(mark, bufPos);
|
||||
stringVal = subString(mark - 1, bufPos + 1);
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
|
||||
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
|
||||
@@ -452,7 +452,7 @@ public class MySqlLexer extends Lexer {
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ public class MySqlLexer extends Lexer {
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
|
||||
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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.sql.dialect.odps.ast;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
|
||||
|
||||
public interface OdpsObject extends SQLObject {
|
||||
void accept0(OdpsASTVisitor visitor);
|
||||
}
|
||||
@@ -20,13 +20,13 @@ import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public abstract class OdpsObjectImpl extends SQLObjectImpl {
|
||||
public abstract class OdpsObjectImpl extends SQLObjectImpl implements OdpsObject {
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((OdpsASTVisitor) visitor);
|
||||
}
|
||||
|
||||
protected abstract void accept0(OdpsASTVisitor visitor);
|
||||
public abstract void accept0(OdpsASTVisitor visitor);
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toOdpsString(this);
|
||||
|
||||
@@ -40,7 +40,7 @@ public abstract class OdpsStatisticClause extends OdpsObjectImpl {
|
||||
|
||||
public static class NullValue extends ColumnStatisticClause {
|
||||
@Override
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
public void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, column);
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public abstract class OdpsStatisticClause extends OdpsObjectImpl {
|
||||
|
||||
public static class ColumnSum extends ColumnStatisticClause {
|
||||
@Override
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
public void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, column);
|
||||
}
|
||||
@@ -60,7 +60,7 @@ public abstract class OdpsStatisticClause extends OdpsObjectImpl {
|
||||
|
||||
public static class ColumnMin extends ColumnStatisticClause {
|
||||
@Override
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
public void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, column);
|
||||
}
|
||||
@@ -70,7 +70,7 @@ public abstract class OdpsStatisticClause extends OdpsObjectImpl {
|
||||
|
||||
public static class ColumnMax extends ColumnStatisticClause {
|
||||
@Override
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
public void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, column);
|
||||
}
|
||||
@@ -94,7 +94,7 @@ public abstract class OdpsStatisticClause extends OdpsObjectImpl {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
public void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, expr);
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public abstract class OdpsStatisticClause extends OdpsObjectImpl {
|
||||
public static class TableCount extends OdpsStatisticClause {
|
||||
|
||||
@Override
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
public void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
|
||||
}
|
||||
|
||||
@@ -19,8 +19,10 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class OdpsUDTFSQLSelectItem extends SQLSelectItem {
|
||||
public class OdpsUDTFSQLSelectItem extends SQLSelectItem implements OdpsObject {
|
||||
|
||||
private final List<String> aliasList = new ArrayList<String>();
|
||||
|
||||
@@ -36,4 +38,15 @@ public class OdpsUDTFSQLSelectItem extends SQLSelectItem {
|
||||
return aliasList;
|
||||
}
|
||||
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((OdpsASTVisitor) visitor);
|
||||
}
|
||||
|
||||
public void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.expr);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsUDTFSQLSelectItem;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
@@ -49,8 +48,8 @@ public class OdpsExprParser extends SQLExprParser {
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
public OdpsExprParser(String sql, Lexer.CommentHandler commentHandler){
|
||||
this(new OdpsLexer(sql, commentHandler));
|
||||
public OdpsExprParser(String sql, boolean skipComments, boolean keepComments){
|
||||
this(new OdpsLexer(sql, skipComments, keepComments));
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
@@ -107,7 +106,7 @@ public class OdpsExprParser extends SQLExprParser {
|
||||
SQLSelectItem item = new SQLSelectItem(expr, alias);
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
item.addAfterComment(lexer.commentVal());
|
||||
item.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
return item;
|
||||
|
||||
@@ -53,6 +53,13 @@ public class OdpsLexer extends Lexer {
|
||||
super.keywods = DEFAULT_ODPS_KEYWORDS;
|
||||
}
|
||||
|
||||
public OdpsLexer(String input, boolean skipComment, boolean keepComments){
|
||||
super(input, skipComment);
|
||||
this.skipComment = skipComment;
|
||||
this.keepComments = keepComments;
|
||||
super.keywods = DEFAULT_ODPS_KEYWORDS;
|
||||
}
|
||||
|
||||
public OdpsLexer(String input, CommentHandler commentHandler){
|
||||
super(input, commentHandler);
|
||||
super.keywods = DEFAULT_ODPS_KEYWORDS;
|
||||
@@ -107,7 +114,7 @@ public class OdpsLexer extends Lexer {
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +164,7 @@ public class OdpsLexer extends Lexer {
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
endOfComment = isEOF();
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.commentVal());
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
accept(Token.SELECT);
|
||||
|
||||
@@ -48,9 +48,8 @@ import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class OdpsStatementParser extends SQLStatementParser {
|
||||
public OdpsStatementParser(String sql){
|
||||
super (new OdpsLexer(sql), JdbcConstants.ODPS);
|
||||
super (new OdpsLexer(sql, true, true), JdbcConstants.ODPS);
|
||||
this.exprParser = new OdpsExprParser(this.lexer);
|
||||
this.lexer.setCommentHandler(commentCallBack);
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
@@ -231,8 +230,8 @@ public class OdpsStatementParser extends SQLStatementParser {
|
||||
public OdpsInsert parseOdpsInsert() {
|
||||
OdpsInsert insert = new OdpsInsert();
|
||||
|
||||
if (lexer.hasComment()) {
|
||||
insert.addBeforeComment(lexer.commentVal());
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
insert.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
SQLSelectParser selectParser = createSQLSelectParser();
|
||||
@@ -323,11 +322,22 @@ public class OdpsStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
public SQLStatement parseSet() {
|
||||
List<String> comments = null;
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
comments = lexer.readAndResetComments();
|
||||
}
|
||||
|
||||
accept(Token.SET);
|
||||
|
||||
if (identifierEquals("LABEL")) {
|
||||
lexer.nextToken();
|
||||
OdpsSetLabelStatement stmt = new OdpsSetLabelStatement();
|
||||
|
||||
if (comments != null) {
|
||||
stmt.addBeforeComment(comments);
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
stmt.setLabel(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
accept(Token.TO);
|
||||
@@ -351,6 +361,10 @@ public class OdpsStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
} else {
|
||||
SQLSetStatement stmt = new SQLSetStatement(getDbType());
|
||||
|
||||
if (comments != null) {
|
||||
stmt.addBeforeComment(comments);
|
||||
}
|
||||
|
||||
parseAssignItems(stmt.getItems(), stmt);
|
||||
|
||||
|
||||
@@ -144,10 +144,14 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
println();
|
||||
print(") ");
|
||||
print(x.getFrom().getAlias());
|
||||
println();
|
||||
}
|
||||
|
||||
for (OdpsInsert insert : x.getItems()) {
|
||||
println();
|
||||
for (int i = 0; i < x.getItems().size(); ++i) {
|
||||
OdpsInsert insert = x.getItems().get(i);
|
||||
if (i != 0) {
|
||||
println();
|
||||
}
|
||||
insert.accept(this);
|
||||
}
|
||||
return false;
|
||||
@@ -327,15 +331,30 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
public boolean visit(OdpsUDTFSQLSelectItem x) {
|
||||
x.getExpr().accept(this);
|
||||
|
||||
print(" AS (");
|
||||
println();
|
||||
print("AS (");
|
||||
|
||||
int aliasSize = x.getAliasList().size();
|
||||
if (aliasSize > 5) {
|
||||
incrementIndent();
|
||||
println();
|
||||
}
|
||||
|
||||
for (int i = 0; i < x.getAliasList().size(); ++i) {
|
||||
for (int i = 0; i < aliasSize; ++i) {
|
||||
if (i != 0) {
|
||||
print(", ");
|
||||
if (aliasSize > 5) {
|
||||
println(",");
|
||||
} else {
|
||||
print(", ");
|
||||
}
|
||||
}
|
||||
print(x.getAliasList().get(i));
|
||||
}
|
||||
|
||||
|
||||
if (aliasSize > 5) {
|
||||
decrementIndent();
|
||||
println();
|
||||
}
|
||||
print(")");
|
||||
|
||||
return false;
|
||||
|
||||
+9
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
|
||||
public class OracleSetTransactionStatement extends OracleSQLObjectImpl implements OracleStatement {
|
||||
|
||||
private boolean readOnly = false;
|
||||
private String dbType;
|
||||
|
||||
private SQLExpr name;
|
||||
|
||||
@@ -49,4 +50,12 @@ public class OracleSetTransactionStatement extends OracleSQLObjectImpl implement
|
||||
this.readOnly = readOnly;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ public class OracleLexer extends Lexer {
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,7 +278,7 @@ public class OracleLexer extends Lexer {
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
endOfComment = isEOF();
|
||||
return;
|
||||
|
||||
+9
@@ -26,6 +26,8 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
public class SQLServerBlockStatement extends SQLServerObjectImpl implements SQLServerStatement {
|
||||
|
||||
private List<SQLStatement> statementList = new ArrayList<SQLStatement>();
|
||||
|
||||
private String dbType;
|
||||
|
||||
public List<SQLStatement> getStatementList() {
|
||||
return statementList;
|
||||
@@ -43,4 +45,11 @@ public class SQLServerBlockStatement extends SQLServerObjectImpl implements SQL
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -27,6 +27,8 @@ public class SQLServerCommitStatement extends SQLServerObjectImpl implements SQL
|
||||
private SQLExpr transactionName;
|
||||
|
||||
private SQLExpr delayedDurability;
|
||||
|
||||
private String dbType;
|
||||
|
||||
public boolean isWork() {
|
||||
return work;
|
||||
@@ -59,4 +61,12 @@ public class SQLServerCommitStatement extends SQLServerObjectImpl implements SQL
|
||||
public void setDelayedDurability(SQLExpr delayedDurability) {
|
||||
this.delayedDurability = delayedDurability;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -26,6 +26,8 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
public class SQLServerDeclareStatement extends SQLServerObjectImpl implements SQLServerStatement {
|
||||
|
||||
protected List<SQLServerDeclareItem> items = new ArrayList<SQLServerDeclareItem>();
|
||||
|
||||
private String dbType;
|
||||
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
@@ -43,4 +45,11 @@ public class SQLServerDeclareStatement extends SQLServerObjectImpl implements SQ
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -29,6 +29,7 @@ public class SQLServerExecStatement extends SQLServerObjectImpl implements SQLSe
|
||||
private SQLName returnStatus;
|
||||
private SQLName moduleName;
|
||||
private List<SQLExpr> parameters = new ArrayList<SQLExpr>();
|
||||
private String dbType;
|
||||
|
||||
public SQLName getModuleName() {
|
||||
return moduleName;
|
||||
@@ -58,4 +59,12 @@ public class SQLServerExecStatement extends SQLServerObjectImpl implements SQLSe
|
||||
public void setReturnStatus(SQLName returnStatus) {
|
||||
this.returnStatus = returnStatus;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -29,6 +29,7 @@ public class SQLServerIfStatement extends SQLServerObjectImpl implements SQLServ
|
||||
private SQLExpr condition;
|
||||
private List<SQLStatement> statements = new ArrayList<SQLStatement>();
|
||||
private Else elseItem;
|
||||
private String dbType;
|
||||
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
@@ -86,4 +87,11 @@ public class SQLServerIfStatement extends SQLServerObjectImpl implements SQLServ
|
||||
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
+9
-1
@@ -24,7 +24,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
public class SQLServerSetStatement extends SQLServerObjectImpl implements SQLServerStatement {
|
||||
|
||||
private SQLAssignItem item = new SQLAssignItem();
|
||||
|
||||
private String dbType;
|
||||
|
||||
public SQLServerSetStatement(){
|
||||
}
|
||||
@@ -53,4 +53,12 @@ public class SQLServerSetStatement extends SQLServerObjectImpl implements SQLSer
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
public class SQLServerSetTransactionIsolationLevelStatement extends SQLServerObjectImpl implements SQLServerStatement {
|
||||
|
||||
private String level;
|
||||
private String dbType;
|
||||
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
@@ -37,4 +38,11 @@ public class SQLServerSetTransactionIsolationLevelStatement extends SQLServerObj
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -30,6 +30,8 @@ public class SQLServerWaitForStatement extends SQLServerObjectImpl implements SQ
|
||||
private SQLStatement statement;
|
||||
|
||||
private SQLExpr timeout;
|
||||
|
||||
private String dbType;
|
||||
|
||||
@Override
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
@@ -74,4 +76,11 @@ public class SQLServerWaitForStatement extends SQLServerObjectImpl implements SQ
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ public class SQLServerLexer extends Lexer {
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ public class SQLServerLexer extends Lexer {
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
endOfComment = isEOF();
|
||||
|
||||
|
||||
@@ -35,7 +35,9 @@ import static com.alibaba.druid.sql.parser.Token.RPAREN;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
@@ -57,7 +59,7 @@ public class Lexer {
|
||||
|
||||
protected String stringVal;
|
||||
|
||||
protected String commentVal;
|
||||
protected List<String> comments = new ArrayList<String>(2);
|
||||
|
||||
protected boolean skipComment = true;
|
||||
|
||||
@@ -288,6 +290,7 @@ public class Lexer {
|
||||
public final void nextToken() {
|
||||
bufPos = 0;
|
||||
hasComment = false;
|
||||
comments = null;
|
||||
|
||||
for (;;) {
|
||||
if (isWhitespace(ch)) {
|
||||
@@ -803,7 +806,7 @@ public class Lexer {
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
|
||||
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
|
||||
@@ -851,7 +854,7 @@ public class Lexer {
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
if (keepComments) {
|
||||
commentVal = stringVal;
|
||||
addComment(stringVal);
|
||||
}
|
||||
|
||||
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
|
||||
@@ -1017,8 +1020,26 @@ public class Lexer {
|
||||
return stringVal;
|
||||
}
|
||||
|
||||
public final String commentVal() {
|
||||
return commentVal;
|
||||
public final String readAndResetCommentValue() {
|
||||
if (comments.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String val = comments.get(0);
|
||||
|
||||
this.hasComment = false;
|
||||
this.comments.clear();
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
public final List<String> readAndResetComments() {
|
||||
List<String> comments = this.comments;
|
||||
|
||||
this.hasComment = false;
|
||||
this.comments = null;
|
||||
|
||||
return comments;
|
||||
}
|
||||
|
||||
private boolean isOperator(char ch) {
|
||||
@@ -1171,4 +1192,10 @@ public class Lexer {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void addComment(String comment) {
|
||||
if (comments == null) {
|
||||
comments = new ArrayList<String>(2);
|
||||
}
|
||||
comments.add(stringVal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1180,7 +1180,7 @@ public class SQLExprParser extends SQLParser {
|
||||
for (;;) {
|
||||
if (lexer.token() == Token.AND || lexer.token() == Token.AMPAMP) {
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
expr.addAfterComment(lexer.commentVal());
|
||||
expr.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -162,7 +162,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock();
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.commentVal());
|
||||
queryBlock.addBeforeComment(lexer.readAndResetCommentValue());
|
||||
}
|
||||
|
||||
accept(Token.SELECT);
|
||||
@@ -243,7 +243,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
if (lexer.hasComment() && lexer.isKeepComments() //
|
||||
&& lexer.token() != Token.INSERT // odps multi-insert
|
||||
) {
|
||||
where.addAfterComment(lexer.commentVal());
|
||||
where.addAfterComment(lexer.readAndResetCommentValue());
|
||||
}
|
||||
|
||||
queryBlock.setWhere(where);
|
||||
@@ -341,7 +341,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
SQLTableSource tableSrc = parseTableSourceRest(tableReference);
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
tableSrc.addAfterComment(lexer.commentVal());
|
||||
tableSrc.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
return tableSrc;
|
||||
|
||||
@@ -103,10 +103,6 @@ public class SQLStatementParser extends SQLParser {
|
||||
|
||||
protected int parseValuesSize = 3;
|
||||
|
||||
protected List<String> comments;
|
||||
|
||||
protected CommentCallback commentCallBack = new CommentCallback();
|
||||
|
||||
public SQLStatementParser(String sql){
|
||||
this(sql, null);
|
||||
}
|
||||
@@ -147,42 +143,23 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
|
||||
public void parseStatementList(List<SQLStatement> statementList, int max) {
|
||||
List<String> comments = null;
|
||||
if (this.comments != null) {
|
||||
comments = this.comments;
|
||||
this.comments = null;
|
||||
}
|
||||
|
||||
int size_original = statementList.size();
|
||||
|
||||
for (;;) {
|
||||
boolean isFirstStmt = statementList.size() == size_original + 1;
|
||||
if (isFirstStmt && comments != null && lexer.isKeepComments()) {
|
||||
SQLStatement firstStmt = statementList.get(size_original);
|
||||
for (String comment : comments) {
|
||||
firstStmt.addBeforeComment(comment);
|
||||
}
|
||||
comments = null;
|
||||
}
|
||||
|
||||
if (max != -1) {
|
||||
if (statementList.size() >= max) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.EOF) {
|
||||
return;
|
||||
}
|
||||
if (lexer.token() == Token.END) {
|
||||
if (lexer.token() == Token.EOF || lexer.token() == Token.END) {
|
||||
if (lexer.isKeepComments() && lexer.hasComment() && statementList.size() > 0) {
|
||||
SQLStatement stmt = statementList.get(statementList.size() - 1);
|
||||
stmt.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.SEMI) {
|
||||
size_original = statementList.size();
|
||||
this.comments = null;
|
||||
lexer.nextToken();
|
||||
comments = this.comments;
|
||||
if (lexer.isKeepComments()) {
|
||||
SQLStatement stmt = statementList.get(statementList.size() - 1);
|
||||
stmt.getAttributes().put("format.semi", Boolean.TRUE);
|
||||
@@ -1988,22 +1965,4 @@ public class SQLStatementParser extends SQLParser {
|
||||
public void setParseValuesSize(int parseValuesSize) {
|
||||
this.parseValuesSize = parseValuesSize;
|
||||
}
|
||||
|
||||
class CommentCallback implements Lexer.CommentHandler {
|
||||
|
||||
@Override
|
||||
public boolean handle(Token lastToken, String comment) {
|
||||
addComment(comment);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void addComment(String comment) {
|
||||
if (comments == null) {
|
||||
comments = new ArrayList<String>();
|
||||
}
|
||||
comments.add(comment);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user