|
@@ -16,17 +16,19 @@ import org.es.sql.enums.SQLBoolOperator;
|
|
|
import org.es.sql.enums.SQLConditionType;
|
|
import org.es.sql.enums.SQLConditionType;
|
|
|
import org.es.sql.exception.ElasticSql2DslException;
|
|
import org.es.sql.exception.ElasticSql2DslException;
|
|
|
import org.es.sql.listener.ParseActionListener;
|
|
import org.es.sql.listener.ParseActionListener;
|
|
|
|
|
+import org.es.sql.listener.ParseActionListenerAdapter;
|
|
|
import org.es.sql.parser.query.exact.BetweenAndAtomQueryParser;
|
|
import org.es.sql.parser.query.exact.BetweenAndAtomQueryParser;
|
|
|
import org.es.sql.parser.query.exact.BinaryAtomQueryParser;
|
|
import org.es.sql.parser.query.exact.BinaryAtomQueryParser;
|
|
|
import org.es.sql.parser.query.exact.InListAtomQueryParser;
|
|
import org.es.sql.parser.query.exact.InListAtomQueryParser;
|
|
|
import org.es.sql.parser.query.method.MethodInvocation;
|
|
import org.es.sql.parser.query.method.MethodInvocation;
|
|
|
import org.es.sql.parser.query.method.fulltext.FullTextAtomQueryParser;
|
|
import org.es.sql.parser.query.method.fulltext.FullTextAtomQueryParser;
|
|
|
|
|
+import org.es.sql.parser.query.method.join.JoinAtomQueryParser;
|
|
|
import org.es.sql.parser.query.method.script.ScriptAtomQueryParser;
|
|
import org.es.sql.parser.query.method.script.ScriptAtomQueryParser;
|
|
|
import org.es.sql.parser.query.method.term.TermLevelAtomQueryParser;
|
|
import org.es.sql.parser.query.method.term.TermLevelAtomQueryParser;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
-public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
|
|
|
|
+public class BoolExpressionParser {
|
|
|
|
|
|
|
|
private final TermLevelAtomQueryParser termLevelAtomQueryParser;
|
|
private final TermLevelAtomQueryParser termLevelAtomQueryParser;
|
|
|
private final ScriptAtomQueryParser scriptAtomQueryParser;
|
|
private final ScriptAtomQueryParser scriptAtomQueryParser;
|
|
@@ -35,7 +37,13 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
private final InListAtomQueryParser inListQueryParser;
|
|
private final InListAtomQueryParser inListQueryParser;
|
|
|
private final BetweenAndAtomQueryParser betweenAndQueryParser;
|
|
private final BetweenAndAtomQueryParser betweenAndQueryParser;
|
|
|
|
|
|
|
|
- public AbstractQueryConditionParser(ParseActionListener parseActionListener) {
|
|
|
|
|
|
|
+ private final JoinAtomQueryParser joinAtomQueryParser;
|
|
|
|
|
+
|
|
|
|
|
+ public BoolExpressionParser() {
|
|
|
|
|
+ this(new ParseActionListenerAdapter());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public BoolExpressionParser(ParseActionListener parseActionListener) {
|
|
|
termLevelAtomQueryParser = new TermLevelAtomQueryParser(parseActionListener);
|
|
termLevelAtomQueryParser = new TermLevelAtomQueryParser(parseActionListener);
|
|
|
fullTextAtomQueryParser = new FullTextAtomQueryParser(parseActionListener);
|
|
fullTextAtomQueryParser = new FullTextAtomQueryParser(parseActionListener);
|
|
|
binaryQueryParser = new BinaryAtomQueryParser(parseActionListener);
|
|
binaryQueryParser = new BinaryAtomQueryParser(parseActionListener);
|
|
@@ -43,10 +51,12 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
betweenAndQueryParser = new BetweenAndAtomQueryParser(parseActionListener);
|
|
betweenAndQueryParser = new BetweenAndAtomQueryParser(parseActionListener);
|
|
|
|
|
|
|
|
scriptAtomQueryParser = new ScriptAtomQueryParser();
|
|
scriptAtomQueryParser = new ScriptAtomQueryParser();
|
|
|
|
|
+ joinAtomQueryParser = new JoinAtomQueryParser();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected BoolQueryBuilder parseQueryConditionExpr(SQLExpr conditionExpr, String queryAs, SQLArgs SQLArgs) {
|
|
|
|
|
- SQLCondition SQLCondition = recursiveParseQueryCondition(conditionExpr, queryAs, SQLArgs);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public BoolQueryBuilder parseBoolQueryExpr(SQLExpr conditionExpr, String queryAs, SQLArgs SQLArgs) {
|
|
|
|
|
+ SQLCondition SQLCondition = recursiveParseBoolQueryExpr(conditionExpr, queryAs, SQLArgs);
|
|
|
SQLBoolOperator operator = SQLCondition.getOperator();
|
|
SQLBoolOperator operator = SQLCondition.getOperator();
|
|
|
|
|
|
|
|
if (SQLConditionType.Atom == SQLCondition.getSQLConditionType()) {
|
|
if (SQLConditionType.Atom == SQLCondition.getSQLConditionType()) {
|
|
@@ -55,7 +65,7 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
return mergeAtomQuery(SQLCondition.getQueryList(), operator);
|
|
return mergeAtomQuery(SQLCondition.getQueryList(), operator);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private SQLCondition recursiveParseQueryCondition(SQLExpr conditionExpr, String queryAs, SQLArgs SQLArgs) {
|
|
|
|
|
|
|
+ private SQLCondition recursiveParseBoolQueryExpr(SQLExpr conditionExpr, String queryAs, SQLArgs SQLArgs) {
|
|
|
if (conditionExpr instanceof SQLBinaryOpExpr) {
|
|
if (conditionExpr instanceof SQLBinaryOpExpr) {
|
|
|
SQLBinaryOpExpr binOpExpr = (SQLBinaryOpExpr) conditionExpr;
|
|
SQLBinaryOpExpr binOpExpr = (SQLBinaryOpExpr) conditionExpr;
|
|
|
SQLBinaryOperator binOperator = binOpExpr.getOperator();
|
|
SQLBinaryOperator binOperator = binOpExpr.getOperator();
|
|
@@ -63,8 +73,8 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
if (SQLBinaryOperator.BooleanAnd == binOperator || SQLBinaryOperator.BooleanOr == binOperator) {
|
|
if (SQLBinaryOperator.BooleanAnd == binOperator || SQLBinaryOperator.BooleanOr == binOperator) {
|
|
|
SQLBoolOperator operator = SQLBinaryOperator.BooleanAnd == binOperator ? SQLBoolOperator.AND : SQLBoolOperator.OR;
|
|
SQLBoolOperator operator = SQLBinaryOperator.BooleanAnd == binOperator ? SQLBoolOperator.AND : SQLBoolOperator.OR;
|
|
|
|
|
|
|
|
- SQLCondition leftCondition = recursiveParseQueryCondition(binOpExpr.getLeft(), queryAs, SQLArgs);
|
|
|
|
|
- SQLCondition rightCondition = recursiveParseQueryCondition(binOpExpr.getRight(), queryAs, SQLArgs);
|
|
|
|
|
|
|
+ SQLCondition leftCondition = recursiveParseBoolQueryExpr(binOpExpr.getLeft(), queryAs, SQLArgs);
|
|
|
|
|
+ SQLCondition rightCondition = recursiveParseBoolQueryExpr(binOpExpr.getRight(), queryAs, SQLArgs);
|
|
|
|
|
|
|
|
List<AtomQuery> mergedQueryList = Lists.newArrayList();
|
|
List<AtomQuery> mergedQueryList = Lists.newArrayList();
|
|
|
combineQueryBuilder(mergedQueryList, leftCondition, operator);
|
|
combineQueryBuilder(mergedQueryList, leftCondition, operator);
|
|
@@ -74,7 +84,7 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else if (conditionExpr instanceof SQLNotExpr) {
|
|
else if (conditionExpr instanceof SQLNotExpr) {
|
|
|
- SQLCondition innerSQLCondition = recursiveParseQueryCondition(((SQLNotExpr) conditionExpr).getExpr(), queryAs, SQLArgs);
|
|
|
|
|
|
|
+ SQLCondition innerSQLCondition = recursiveParseBoolQueryExpr(((SQLNotExpr) conditionExpr).getExpr(), queryAs, SQLArgs);
|
|
|
|
|
|
|
|
SQLBoolOperator operator = innerSQLCondition.getOperator();
|
|
SQLBoolOperator operator = innerSQLCondition.getOperator();
|
|
|
if (SQLConditionType.Atom == innerSQLCondition.getSQLConditionType()) {
|
|
if (SQLConditionType.Atom == innerSQLCondition.getSQLConditionType()) {
|
|
@@ -90,32 +100,36 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
return new SQLCondition(parseAtomQueryCondition(conditionExpr, queryAs, SQLArgs), SQLConditionType.Atom);
|
|
return new SQLCondition(parseAtomQueryCondition(conditionExpr, queryAs, SQLArgs), SQLConditionType.Atom);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private AtomQuery parseAtomQueryCondition(SQLExpr sqlConditionExpr, String queryAs, SQLArgs SQLArgs) {
|
|
|
|
|
|
|
+ private AtomQuery parseAtomQueryCondition(SQLExpr sqlConditionExpr, String queryAs, SQLArgs sqlArgs) {
|
|
|
if (sqlConditionExpr instanceof SQLMethodInvokeExpr) {
|
|
if (sqlConditionExpr instanceof SQLMethodInvokeExpr) {
|
|
|
SQLMethodInvokeExpr methodQueryExpr = (SQLMethodInvokeExpr) sqlConditionExpr;
|
|
SQLMethodInvokeExpr methodQueryExpr = (SQLMethodInvokeExpr) sqlConditionExpr;
|
|
|
|
|
|
|
|
- MethodInvocation methodInvocation = new MethodInvocation(methodQueryExpr, queryAs, SQLArgs);
|
|
|
|
|
|
|
+ MethodInvocation methodInvocation = new MethodInvocation(methodQueryExpr, queryAs, sqlArgs);
|
|
|
|
|
|
|
|
if (scriptAtomQueryParser.isMatchMethodInvocation(methodInvocation)) {
|
|
if (scriptAtomQueryParser.isMatchMethodInvocation(methodInvocation)) {
|
|
|
return scriptAtomQueryParser.parseAtomMethodQuery(methodInvocation);
|
|
return scriptAtomQueryParser.parseAtomMethodQuery(methodInvocation);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (fullTextAtomQueryParser.isFulltextAtomQuery(methodInvocation)) {
|
|
if (fullTextAtomQueryParser.isFulltextAtomQuery(methodInvocation)) {
|
|
|
- return fullTextAtomQueryParser.parseFullTextAtomQuery(methodQueryExpr, queryAs, SQLArgs);
|
|
|
|
|
|
|
+ return fullTextAtomQueryParser.parseFullTextAtomQuery(methodQueryExpr, queryAs, sqlArgs);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (termLevelAtomQueryParser.isTermLevelAtomQuery(methodInvocation)) {
|
|
if (termLevelAtomQueryParser.isTermLevelAtomQuery(methodInvocation)) {
|
|
|
- return termLevelAtomQueryParser.parseTermLevelAtomQuery(methodQueryExpr, queryAs, SQLArgs);
|
|
|
|
|
|
|
+ return termLevelAtomQueryParser.parseTermLevelAtomQuery(methodQueryExpr, queryAs, sqlArgs);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (joinAtomQueryParser.isJoinAtomQuery(methodInvocation)) {
|
|
|
|
|
+ return joinAtomQueryParser.parseJoinAtomQuery(methodQueryExpr, queryAs, sqlArgs);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else if (sqlConditionExpr instanceof SQLBinaryOpExpr) {
|
|
else if (sqlConditionExpr instanceof SQLBinaryOpExpr) {
|
|
|
- return binaryQueryParser.parseBinaryQuery((SQLBinaryOpExpr) sqlConditionExpr, queryAs, SQLArgs);
|
|
|
|
|
|
|
+ return binaryQueryParser.parseBinaryQuery((SQLBinaryOpExpr) sqlConditionExpr, queryAs, sqlArgs);
|
|
|
}
|
|
}
|
|
|
else if (sqlConditionExpr instanceof SQLInListExpr) {
|
|
else if (sqlConditionExpr instanceof SQLInListExpr) {
|
|
|
- return inListQueryParser.parseInListQuery((SQLInListExpr) sqlConditionExpr, queryAs, SQLArgs);
|
|
|
|
|
|
|
+ return inListQueryParser.parseInListQuery((SQLInListExpr) sqlConditionExpr, queryAs, sqlArgs);
|
|
|
}
|
|
}
|
|
|
else if (sqlConditionExpr instanceof SQLBetweenExpr) {
|
|
else if (sqlConditionExpr instanceof SQLBetweenExpr) {
|
|
|
- return betweenAndQueryParser.parseBetweenAndQuery((SQLBetweenExpr) sqlConditionExpr, queryAs, SQLArgs);
|
|
|
|
|
|
|
+ return betweenAndQueryParser.parseBetweenAndQuery((SQLBetweenExpr) sqlConditionExpr, queryAs, sqlArgs);
|
|
|
}
|
|
}
|
|
|
throw new ElasticSql2DslException(String.format("[syntax error] Can not support query condition type[%s]", sqlConditionExpr.toString()));
|
|
throw new ElasticSql2DslException(String.format("[syntax error] Can not support query condition type[%s]", sqlConditionExpr.toString()));
|
|
|
}
|
|
}
|