mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser refactor
This commit is contained in:
@@ -14,7 +14,6 @@ import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
@@ -111,11 +110,6 @@ public class MSSQLServerMappingVisitor extends SQLServerASTVisitorAdapter implem
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAllColumnExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
|
||||
@@ -30,7 +30,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.L
|
||||
|
||||
public class MappingVisitorUtils {
|
||||
|
||||
private static final String MAPPING_VAR_INDEX = "mapping.varIndex";
|
||||
private static final String MAPPING_VALUE = "mapping.value";
|
||||
private static final String MAPPING_PROPERTY = "mapping.property";
|
||||
private static final String MAPPING_ENTITY = "mapping.entity";
|
||||
@@ -252,14 +251,9 @@ public class MappingVisitorUtils {
|
||||
}
|
||||
|
||||
if (expr instanceof SQLVariantRefExpr) {
|
||||
Map<String, Object> attributes = expr.getAttributes();
|
||||
Integer varIndex = (Integer) attributes.get(MAPPING_VAR_INDEX);
|
||||
if (varIndex == null) {
|
||||
varIndex = visitor.getAndIncrementVariantIndex();
|
||||
expr.putAttribute(MAPPING_VAR_INDEX, varIndex);
|
||||
}
|
||||
int varIndex = ((SQLVariantRefExpr) expr).getIndex();
|
||||
|
||||
if (visitor.getParameters().size() > varIndex) {
|
||||
if (varIndex != -1 && visitor.getParameters().size() > varIndex) {
|
||||
Object parameter = visitor.getParameters().get(varIndex);
|
||||
expr.putAttribute(MAPPING_VALUE, parameter);
|
||||
}
|
||||
@@ -371,16 +365,6 @@ public class MappingVisitorUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLVariantRefExpr x) {
|
||||
Map<String, Object> attributes = x.getAttributes();
|
||||
Integer varIndex = (Integer) attributes.get(MAPPING_VAR_INDEX);
|
||||
if (varIndex == null) {
|
||||
varIndex = visitor.getAndIncrementVariantIndex();
|
||||
x.putAttribute(MAPPING_VAR_INDEX, varIndex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, MySqlSelectQueryBlock x) {
|
||||
Integer maxLimit = visitor.getEngine().getMaxLimit();
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
@@ -110,11 +109,6 @@ public class MySqlMappingVisitor extends MySqlASTVisitorAdapter implements Mappi
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAllColumnExpr x) {
|
||||
|
||||
@@ -14,7 +14,6 @@ import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
@@ -113,11 +112,6 @@ public class OracleMappingVisitor extends OracleASTVisitorAdapter implements Map
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAllColumnExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
|
||||
@@ -26,10 +26,12 @@ public class SQLVariantRefExpr extends SQLExprImpl {
|
||||
|
||||
private boolean global = false;
|
||||
|
||||
private int index = -1;
|
||||
|
||||
public SQLVariantRefExpr(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
public SQLVariantRefExpr(String name, boolean global){
|
||||
this.name = name;
|
||||
this.global = global;
|
||||
@@ -39,6 +41,14 @@ public class SQLVariantRefExpr extends SQLExprImpl {
|
||||
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@@ -82,6 +82,8 @@ public class Lexer {
|
||||
*/
|
||||
private boolean allowComment = true;
|
||||
|
||||
private int varIndex = -1;
|
||||
|
||||
public Lexer(String input){
|
||||
this(input, true);
|
||||
}
|
||||
@@ -93,6 +95,10 @@ public class Lexer {
|
||||
public void setAllowComment(boolean allowComment) {
|
||||
this.allowComment = allowComment;
|
||||
}
|
||||
|
||||
public int nextVarIndex() {
|
||||
return ++varIndex;
|
||||
}
|
||||
|
||||
private static class SavePoint {
|
||||
|
||||
@@ -185,7 +191,7 @@ public class Lexer {
|
||||
public final Token token() {
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
public String info() {
|
||||
return this.token + " " + this.stringVal();
|
||||
}
|
||||
@@ -507,11 +513,11 @@ public class Lexer {
|
||||
token = LITERAL_CHARS;
|
||||
break;
|
||||
} else {
|
||||
if (!hasSpecial) {
|
||||
System.arraycopy(buf, np + 1, sbuf, 0, sp);
|
||||
hasSpecial = true;
|
||||
}
|
||||
putChar('\'');
|
||||
if (!hasSpecial) {
|
||||
System.arraycopy(buf, np + 1, sbuf, 0, sp);
|
||||
hasSpecial = true;
|
||||
}
|
||||
putChar('\'');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,7 +356,9 @@ public class SQLExprParser extends SQLParser {
|
||||
break;
|
||||
case QUES:
|
||||
lexer.nextToken();
|
||||
sqlExpr = new SQLVariantRefExpr("?");
|
||||
SQLVariantRefExpr quesVarRefExpr = new SQLVariantRefExpr("?");
|
||||
quesVarRefExpr.setIndex(lexer.nextVarIndex());
|
||||
sqlExpr = quesVarRefExpr;
|
||||
break;
|
||||
case LEFT:
|
||||
sqlExpr = new SQLIdentifierExpr("LEFT");
|
||||
|
||||
@@ -4,8 +4,6 @@ import java.util.List;
|
||||
|
||||
public interface SQLEvalVisitor extends SQLASTVisitor {
|
||||
|
||||
public static final String EVAL_VAR_INDEX = "eval.varIndex";
|
||||
|
||||
public static final String EVAL_VALUE = "eval.value";
|
||||
|
||||
List<Object> getParameters();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.alibaba.druid.sql.visitor;
|
||||
|
||||
import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE;
|
||||
import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VAR_INDEX;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
@@ -104,16 +103,12 @@ public class SQLEvalVisitorUtils {
|
||||
|
||||
Map<String, Object> attributes = x.getAttributes();
|
||||
|
||||
Integer varIndex = (Integer) attributes.get(EVAL_VAR_INDEX);
|
||||
if (varIndex == null) {
|
||||
varIndex = visitor.incrementAndGetVariantIndex();
|
||||
x.putAttribute(EVAL_VAR_INDEX, varIndex);
|
||||
}
|
||||
int varIndex = x.getIndex();
|
||||
|
||||
if (visitor.getParameters().size() > 0) {
|
||||
if (varIndex != -1 && visitor.getParameters().size() > varIndex) {
|
||||
boolean containsValue = attributes.containsKey(EVAL_VALUE);
|
||||
if (!containsValue) {
|
||||
Object value = visitor.getParameters().get(varIndex.intValue());
|
||||
Object value = visitor.getParameters().get(varIndex);
|
||||
attributes.put(EVAL_VALUE, value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user