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();
|
||||
|
||||
@@ -257,7 +257,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
SQLSelectItem selectItem = selectList.get(i);
|
||||
SQLExpr selectItemExpr = selectItem.getExpr();
|
||||
if (i != 0) {
|
||||
if (selectItemExpr instanceof SQLMethodInvokeExpr) {
|
||||
SQLSelectItem preSelectItem = selectList.get(i - 1);
|
||||
if (preSelectItem.getAfterCommentsDirect() != null) {
|
||||
lineItemCount = 0;
|
||||
println();
|
||||
} else if (selectItemExpr instanceof SQLMethodInvokeExpr) {
|
||||
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) selectItemExpr;
|
||||
int paramCount = methodInvokeExpr.getParameters().size();
|
||||
lineItemCount += paramCount;
|
||||
@@ -280,6 +284,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
} else {
|
||||
selectItem.accept(this);
|
||||
}
|
||||
|
||||
if (selectItem.hasAfterComment()) {
|
||||
print(' ');
|
||||
printlnComment(selectItem.getAfterCommentsDirect());
|
||||
}
|
||||
}
|
||||
decrementIndent();
|
||||
}
|
||||
@@ -405,12 +414,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
if (isPrettyFormat() && item.hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(item.getAfterCommentsDirect(), "\n");
|
||||
printlnComment(item.getAfterCommentsDirect());
|
||||
}
|
||||
|
||||
if (i != groupList.size() - 1 && isPrettyFormat() && item.getParent().hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(item.getParent().getAfterCommentsDirect(), "\n");
|
||||
printlnComment(item.getParent().getAfterCommentsDirect());
|
||||
}
|
||||
|
||||
boolean printOpSpace = true;
|
||||
@@ -478,7 +487,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
if (x.getRight().hasAfterComment() && isPrettyFormat()) {
|
||||
print(' ');
|
||||
printlnComments(x.getRight().getAfterCommentsDirect());
|
||||
printlnComment(x.getRight().getAfterCommentsDirect());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1240,7 +1249,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
if (isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printComment(x.getBeforeCommentsDirect(), "\n");
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
@@ -1461,7 +1470,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
if (isPrettyFormat() && x.hasAfterComment()) {
|
||||
print(' ');
|
||||
printComment(x.getAfterCommentsDirect(), "\n");
|
||||
printlnComment(x.getAfterCommentsDirect());
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -2620,12 +2629,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLWithSubqueryClause x) {
|
||||
print0(ucase ? "WITH" : "with");
|
||||
print0(ucase ? "WITH " : "with ");
|
||||
if (x.getRecursive() == Boolean.TRUE) {
|
||||
print0(ucase ? " RECURSIVE" : " recursive");
|
||||
print0(ucase ? "RECURSIVE " : " recursive ");
|
||||
}
|
||||
incrementIndent();
|
||||
println();
|
||||
printlnAndAccept(x.getEntries(), ", ");
|
||||
decrementIndent();
|
||||
return false;
|
||||
@@ -2640,9 +2648,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(')');
|
||||
}
|
||||
println();
|
||||
print0(ucase ? "AS" : "as");
|
||||
println();
|
||||
print(' ');
|
||||
print0(ucase ? "AS " : "as ");
|
||||
print('(');
|
||||
incrementIndent();
|
||||
println();
|
||||
@@ -3267,13 +3274,13 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printComment(List<String> comments, String seperator) {
|
||||
protected void printlnComment(List<String> comments) {
|
||||
if (comments != null) {
|
||||
for (int i = 0; i < comments.size(); ++i) {
|
||||
if (i != 0) {
|
||||
print0(seperator);
|
||||
}
|
||||
String comment = comments.get(i);
|
||||
if (i != 0 && comment.startsWith("--")) {
|
||||
println();
|
||||
}
|
||||
print0(comment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,19 +25,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLArrayExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCastExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLSequenceExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
@@ -516,6 +504,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
continue;
|
||||
}
|
||||
Object value = SQLEvalVisitorUtils.eval(getDbType(), item, parameters, false);
|
||||
if (value == SQLEvalVisitor.EVAL_VALUE_NULL) {
|
||||
value = null;
|
||||
}
|
||||
condition.getValues().add(value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user