Merge pull request #922 from wenshao/master

improved sql formatter
This commit is contained in:
高铁
2015-07-27 14:02:57 +08:00
9 changed files with 153 additions and 7 deletions
@@ -253,7 +253,20 @@ public class SQLUtils {
SQLStatement stmt = statementList.get(i);
if (i > 0) {
visitor.println(";");
visitor.print(";");
SQLStatement preStmt = statementList.get(i - 1);
List<String> comments = preStmt.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);
}
}
visitor.println();
}
{
List<String> comments = stmt.getBeforeCommentsDirect();
@@ -268,9 +281,9 @@ public class SQLUtils {
if (i == statementList.size() - 1) {
Boolean semi = (Boolean) stmt.getAttribute("format.semi");
if (semi != null && semi.booleanValue()) {
if (stmt.hasAfterComment()) {
visitor.println();
}
// if (stmt.hasAfterComment()) {
// visitor.println();
// }
visitor.print(";");
}
@@ -139,6 +139,7 @@ public class OdpsLexer extends Lexer {
for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
line++;
bufPos += 2;
scanChar();
break;
@@ -150,6 +151,7 @@ public class OdpsLexer extends Lexer {
}
if (ch == '\n') {
line++;
scanChar();
bufPos++;
break;
@@ -77,6 +77,8 @@ public class Lexer {
protected boolean endOfComment = false;
protected boolean keepComments = false;
protected int line = 0;
public Lexer(String input){
this(input, null);
@@ -293,6 +295,10 @@ public class Lexer {
for (;;) {
if (isWhitespace(ch)) {
if (ch == '\n') {
line++;
}
scanChar();
continue;
}
@@ -831,6 +837,7 @@ public class Lexer {
for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
line++;
scanChar();
break;
}
@@ -839,6 +846,7 @@ public class Lexer {
}
if (ch == '\n') {
line++;
scanChar();
break;
}
@@ -1185,4 +1193,8 @@ public class Lexer {
}
comments.add(stringVal);
}
public int getLine() {
return line;
}
}
@@ -159,9 +159,15 @@ public class SQLStatementParser extends SQLParser {
}
if (lexer.token() == Token.SEMI) {
int line0 = lexer.getLine();
lexer.nextToken();
if (lexer.isKeepComments()) {
int line1 = lexer.getLine();
if (lexer.isKeepComments() && statementList.size() > 0) {
SQLStatement stmt = statementList.get(statementList.size() - 1);
if (line1 - line0 <= 1) {
stmt.addAfterComment(lexer.readAndResetComments());
}
stmt.getAttributes().put("format.semi", Boolean.TRUE);
}
continue;