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:
@@ -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