mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved parser
This commit is contained in:
@@ -3112,7 +3112,13 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
int line = lexer.line;
|
||||
accept(Token.RPAREN);
|
||||
if (line + 1 == lexer.line
|
||||
&& lexer.hasComment()
|
||||
&& lexer.getComments().get(0).startsWith("--")) {
|
||||
inListExpr.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
} else {
|
||||
SQLExpr itemExpr = primary();
|
||||
itemExpr.setParent(inListExpr);
|
||||
|
||||
@@ -1680,6 +1680,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
primaryTableSourceRest(rightTableSource);
|
||||
|
||||
} else if (rightTableSource.getAlias() == null && !(rightTableSource instanceof SQLValuesTableSource)) {
|
||||
int line = lexer.line;
|
||||
String tableAlias;
|
||||
if (lexer.token == Token.AS) {
|
||||
lexer.nextToken();
|
||||
@@ -1696,6 +1697,12 @@ public class SQLSelectParser extends SQLParser {
|
||||
if (tableAlias != null) {
|
||||
rightTableSource.setAlias(tableAlias);
|
||||
|
||||
if (line + 1 == lexer.line
|
||||
&& lexer.hasComment()
|
||||
&& lexer.getComments().get(0).startsWith("--")) {
|
||||
rightTableSource.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
if (lexer.token == Token.LPAREN) {
|
||||
if (rightTableSource instanceof SQLSubqueryTableSource) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -168,6 +168,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
return;
|
||||
case SEMI: {
|
||||
int line0 = lexer.getLine();
|
||||
char ch = lexer.ch;
|
||||
lexer.nextToken();
|
||||
int line1 = lexer.getLine();
|
||||
|
||||
@@ -176,9 +177,16 @@ public class SQLStatementParser extends SQLParser {
|
||||
lastStmt.setAfterSemi(true);
|
||||
|
||||
if (lexer.isKeepComments()) {
|
||||
SQLStatement stmt = statementList.get(statementList.size() - 1);
|
||||
if (ch == '\n'
|
||||
&& lexer.getComments() != null
|
||||
&& !lexer.getComments().isEmpty()
|
||||
&& !(lastStmt instanceof SQLSetStatement)
|
||||
) {
|
||||
lexer.getComments().add(0, new String("\n"));
|
||||
}
|
||||
|
||||
if (line1 - line0 <= 1) {
|
||||
stmt.addAfterComment(lexer.readAndResetComments());
|
||||
lastStmt.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5395,6 +5403,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
SQLWithSubqueryClause.Entry entry = new SQLWithSubqueryClause.Entry();
|
||||
entry.setParent(withQueryClause);
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
entry.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
String alias = this.lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
entry.setAlias(alias);
|
||||
|
||||
@@ -1763,6 +1763,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
print(')');
|
||||
|
||||
List<String> afterComments = x.getAfterCommentsDirect();
|
||||
if (afterComments != null && !afterComments.isEmpty() && afterComments.get(0).startsWith("--")) {
|
||||
print(' ');
|
||||
}
|
||||
printlnComment(afterComments);
|
||||
|
||||
if (x.getHint() != null) {
|
||||
x.getHint().accept(this);
|
||||
}
|
||||
@@ -5283,6 +5289,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLWithSubqueryClause.Entry x) {
|
||||
if (isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
print0(x.getAlias());
|
||||
|
||||
if (x.getColumns().size() > 0) {
|
||||
|
||||
@@ -673,7 +673,10 @@ public class MySqlSelectTest_290 extends MysqlTest {
|
||||
"\t\tGROUP BY tran.id\n" +
|
||||
"\t) salesTeam\n" +
|
||||
"\tON salesTeam.id = tran.id\n" +
|
||||
"WHERE contract.status_code IN ('SD040501', 'SD040502')\n" +
|
||||
"WHERE contract.status_code IN ('SD040501', 'SD040502') -- <orgId> AND org.id in (?{orgId}) </orgId>\n" +
|
||||
"-- <projectId> AND tran.project_id in ( ?{projectId} )</projectId>\n" +
|
||||
"-- <startDate2>AND contract.vesting_date >= ?{startDate2} </startDate2>\n" +
|
||||
"-- <endDate2> AND contract.vesting_date <= ?{endDate2} </endDate2>\n" +
|
||||
"GROUP BY contract.id", stmt.toString());
|
||||
|
||||
System.out.println(stmt.toString());
|
||||
|
||||
@@ -13,25 +13,29 @@ public class OdpsFormatCommentTest8 extends TestCase {
|
||||
+ "\nselect * from table2;;select * from table3;" //
|
||||
+ "\n--comment2 goes here"
|
||||
;//
|
||||
Assert.assertEquals("SELECT *"
|
||||
+ "\nFROM table1;"
|
||||
+ "\n"
|
||||
+ "\n-- comment1 goes here"
|
||||
+ "\nSELECT *"
|
||||
+ "\nFROM table2;"
|
||||
+ "\n"
|
||||
+ "\nSELECT *"
|
||||
+ "\nFROM table3;-- comment2 goes here", SQLUtils.formatOdps(sql));
|
||||
Assert.assertEquals("SELECT *\n" +
|
||||
"FROM table1;\n" +
|
||||
"\n" +
|
||||
"-- comment1 goes here\n" +
|
||||
"SELECT *\n" +
|
||||
"FROM table2;\n" +
|
||||
"\n" +
|
||||
"SELECT *\n" +
|
||||
"FROM table3;\n" +
|
||||
"\n" +
|
||||
"-- comment2 goes here", SQLUtils.formatOdps(sql));
|
||||
|
||||
Assert.assertEquals("select *"
|
||||
+ "\nfrom table1;"
|
||||
+ "\n"
|
||||
+ "\n-- comment1 goes here"
|
||||
+ "\nselect *"
|
||||
+ "\nfrom table2;"
|
||||
+ "\n"
|
||||
+ "\nselect *"
|
||||
+ "\nfrom table3;-- comment2 goes here", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
Assert.assertEquals("select *\n" +
|
||||
"from table1;\n" +
|
||||
"\n" +
|
||||
"-- comment1 goes here\n" +
|
||||
"select *\n" +
|
||||
"from table2;\n" +
|
||||
"\n" +
|
||||
"select *\n" +
|
||||
"from table3;\n" +
|
||||
"\n" +
|
||||
"-- comment2 goes here", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user