mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sql parser
This commit is contained in:
@@ -1176,7 +1176,9 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
charType.getArguments().add(this.expr());
|
||||
SQLExpr arg = this.expr();
|
||||
arg.setParent(charType);
|
||||
charType.getArguments().add(arg);
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
@@ -1195,7 +1197,7 @@ public class SQLExprParser extends SQLParser {
|
||||
protected SQLDataType parseDataTypeRest(SQLDataType dataType) {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
exprList(dataType.getArguments());
|
||||
exprList(dataType.getArguments(), dataType);
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.visitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
@@ -118,12 +119,24 @@ public class ParameterizedOutputVisitorUtils {
|
||||
}
|
||||
|
||||
public static boolean visit(ParameterizedVisitor v, SQLIntegerExpr x) {
|
||||
SQLObject parent = x.getParent();
|
||||
|
||||
if (parent instanceof SQLDataType) {
|
||||
return SQLASTOutputVisitorUtils.visit(v, x);
|
||||
}
|
||||
|
||||
v.print('?');
|
||||
v.incrementReplaceCunt();
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(ParameterizedVisitor v, SQLNumberExpr x) {
|
||||
SQLObject parent = x.getParent();
|
||||
|
||||
if (parent instanceof SQLDataType) {
|
||||
return SQLASTOutputVisitorUtils.visit(v, x);
|
||||
}
|
||||
|
||||
v.print('?');
|
||||
v.incrementReplaceCunt();
|
||||
return false;
|
||||
|
||||
@@ -15,13 +15,10 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.visitor;
|
||||
|
||||
public interface ParameterizedVisitor extends SQLASTVisitor {
|
||||
public interface ParameterizedVisitor extends SQLPrintableVisitor {
|
||||
|
||||
int getReplaceCount();
|
||||
|
||||
void incrementReplaceCunt();
|
||||
|
||||
void print(char value);
|
||||
|
||||
void print(String text);
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
|
||||
|
||||
public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements SQLPrintableVisitor {
|
||||
|
||||
protected final Appendable appender;
|
||||
private String indent = "\t";
|
||||
@@ -485,8 +485,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public boolean visit(SQLIntegerExpr x) {
|
||||
print(x.getNumber().toString());
|
||||
return false;
|
||||
return SQLASTOutputVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
@@ -555,8 +554,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public boolean visit(SQLNumberExpr x) {
|
||||
print(x.getNumber().toString());
|
||||
return false;
|
||||
return SQLASTOutputVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.alibaba.druid.sql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
|
||||
public class SQLASTOutputVisitorUtils {
|
||||
|
||||
public static boolean visit(SQLPrintableVisitor visitor, SQLIntegerExpr x) {
|
||||
visitor.print(x.getNumber().toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(SQLPrintableVisitor visitor, SQLNumberExpr x) {
|
||||
visitor.print(x.getNumber().toString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.alibaba.druid.sql.visitor;
|
||||
|
||||
public interface SQLPrintableVisitor extends SQLASTVisitor {
|
||||
|
||||
void print(char value);
|
||||
|
||||
void print(String text);
|
||||
}
|
||||
Reference in New Issue
Block a user