mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -199,7 +199,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
if ((!isParameterized()) && isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printComment(x.getBeforeCommentsDirect(), "\n");
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
|
||||
@@ -20,9 +20,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.*;
|
||||
@@ -74,7 +72,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
|
||||
if (this.isPrettyFormat() && x.hasBodyBeforeComment()) {
|
||||
print(' ');
|
||||
printComment(x.getBodyBeforeCommentsDirect(), "");
|
||||
printlnComment(x.getBodyBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
incrementIndent();
|
||||
@@ -88,7 +86,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
}
|
||||
if (this.isPrettyFormat() && element.hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(element.getAfterCommentsDirect(), "\n");
|
||||
printlnComment(element.getAfterCommentsDirect());
|
||||
}
|
||||
|
||||
if (i != size - 1) {
|
||||
@@ -121,7 +119,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
}
|
||||
if (this.isPrettyFormat() && column.hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(column.getAfterCommentsDirect(), "\n");
|
||||
printlnComment(column.getAfterCommentsDirect());
|
||||
}
|
||||
|
||||
if (i != partitionSize - 1) {
|
||||
@@ -204,8 +202,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
@Override
|
||||
public boolean visit(OdpsInsert x) {
|
||||
if (x.hasBeforeComment()) {
|
||||
printComment(x.getBeforeCommentsDirect(), "\n");
|
||||
println();
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
if (x.isOverwrite()) {
|
||||
print0(ucase ? "INSERT OVERWRITE TABLE " : "insert overwrite table ");
|
||||
@@ -238,31 +235,31 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printSelectList(List<SQLSelectItem> selectList) {
|
||||
incrementIndent();
|
||||
for (int i = 0, size = selectList.size(); i < size; ++i) {
|
||||
SQLSelectItem selectItem = selectList.get(i);
|
||||
|
||||
if (i != 0) {
|
||||
SQLSelectItem preSelectItem = selectList.get(i - 1);
|
||||
if (preSelectItem.hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(preSelectItem.getAfterCommentsDirect(), "\n");
|
||||
}
|
||||
|
||||
println();
|
||||
print0(", ");
|
||||
}
|
||||
|
||||
selectItem.accept(this);
|
||||
|
||||
if (i == selectList.size() - 1 && selectItem.hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(selectItem.getAfterCommentsDirect(), "\n");
|
||||
}
|
||||
}
|
||||
decrementIndent();
|
||||
}
|
||||
// protected void printSelectList(List<SQLSelectItem> selectList) {
|
||||
// incrementIndent();
|
||||
// for (int i = 0, size = selectList.size(); i < size; ++i) {
|
||||
// SQLSelectItem selectItem = selectList.get(i);
|
||||
//
|
||||
// if (i != 0) {
|
||||
// SQLSelectItem preSelectItem = selectList.get(i - 1);
|
||||
// if (preSelectItem.hasAfterComment()) {
|
||||
// print(' ');
|
||||
// printlnComment(preSelectItem.getAfterCommentsDirect());
|
||||
// }
|
||||
//
|
||||
// println();
|
||||
// print0(", ");
|
||||
// }
|
||||
//
|
||||
// selectItem.accept(this);
|
||||
//
|
||||
// if (i == selectList.size() - 1 && selectItem.hasAfterComment()) {
|
||||
// print(' ');
|
||||
// printlnComments(selectItem.getAfterCommentsDirect());
|
||||
// }
|
||||
// }
|
||||
// decrementIndent();
|
||||
// }
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSubqueryTableSource x) {
|
||||
@@ -414,8 +411,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
@Override
|
||||
public boolean visit(OdpsSelectQueryBlock x) {
|
||||
if (isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printComment(x.getBeforeCommentsDirect(), "\n");
|
||||
println();
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
@@ -453,7 +449,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
x.getWhere().accept(this);
|
||||
if (x.getWhere().hasAfterComment() && isPrettyFormat()) {
|
||||
print(' ');
|
||||
printComment(x.getWhere().getAfterCommentsDirect(), "\n");
|
||||
printlnComment(x.getWhere().getAfterCommentsDirect());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,8 @@ public class OracleLexer extends Lexer {
|
||||
|
||||
public OracleLexer(String input){
|
||||
super(input);
|
||||
this.skipComment = true;
|
||||
this.keepComments = true;
|
||||
super.keywods = DEFAULT_ORACLE_KEYWORDS;
|
||||
}
|
||||
|
||||
@@ -280,7 +282,7 @@ public class OracleLexer extends Lexer {
|
||||
bufPos++;
|
||||
}
|
||||
|
||||
stringVal = subString(mark + 1, bufPos);
|
||||
stringVal = subString(mark, ch != EOI ? bufPos : bufPos + 1);
|
||||
token = Token.LINE_COMMENT;
|
||||
commentCount++;
|
||||
if (keepComments) {
|
||||
|
||||
@@ -247,6 +247,10 @@ public class OracleSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
OracleSelectQueryBlock queryBlock = new OracleSelectQueryBlock();
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
lexer.nextToken();
|
||||
|
||||
|
||||
@@ -243,8 +243,9 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelect x) {
|
||||
if (x.getWithSubQuery() != null) {
|
||||
x.getWithSubQuery().accept(this);
|
||||
SQLWithSubqueryClause with = x.getWithSubQuery();
|
||||
if (with != null) {
|
||||
with.accept(this);
|
||||
println();
|
||||
}
|
||||
|
||||
@@ -381,6 +382,10 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
}
|
||||
|
||||
public boolean visit(OracleSelectQueryBlock x) {
|
||||
if (isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
|
||||
if (x.getHintsSize() > 0) {
|
||||
@@ -803,9 +808,8 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(')');
|
||||
}
|
||||
println();
|
||||
print0(ucase ? "AS" : "as");
|
||||
println();
|
||||
|
||||
print0(ucase ? " AS " : " as ");
|
||||
print('(');
|
||||
incrementIndent();
|
||||
println();
|
||||
|
||||
Reference in New Issue
Block a user