improved odps sql formatter

This commit is contained in:
shaojin.wensj
2015-07-20 04:41:57 +08:00
parent 8f51368245
commit a5811a4f3e
38 changed files with 415 additions and 103 deletions
@@ -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);
}
}