|
@@ -1,4 +1,4 @@
|
|
|
-package org.es.sql.dsl.parser.sql;
|
|
|
|
|
|
|
+package org.es.sql.parser.sql;
|
|
|
|
|
|
|
|
import com.alibaba.druid.sql.ast.SQLExpr;
|
|
import com.alibaba.druid.sql.ast.SQLExpr;
|
|
|
import com.alibaba.druid.sql.ast.expr.*;
|
|
import com.alibaba.druid.sql.ast.expr.*;
|
|
@@ -6,29 +6,31 @@ import com.google.common.collect.ArrayListMultimap;
|
|
|
import com.google.common.collect.ListMultimap;
|
|
import com.google.common.collect.ListMultimap;
|
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Lists;
|
|
|
import org.apache.lucene.search.join.ScoreMode;
|
|
import org.apache.lucene.search.join.ScoreMode;
|
|
|
-import org.es.sql.dsl.bean.AtomQuery;
|
|
|
|
|
-import org.es.sql.dsl.bean.SQLCondition;
|
|
|
|
|
-import org.es.sql.dsl.enums.SQLBoolOperator;
|
|
|
|
|
-import org.es.sql.dsl.enums.SQLConditionType;
|
|
|
|
|
-import org.es.sql.dsl.exception.ElasticSql2DslException;
|
|
|
|
|
-import org.es.sql.dsl.listener.ParseActionListener;
|
|
|
|
|
-import org.es.sql.dsl.parser.query.exact.BetweenAndAtomQueryParser;
|
|
|
|
|
-import org.es.sql.dsl.parser.query.exact.BinaryAtomQueryParser;
|
|
|
|
|
-import org.es.sql.dsl.parser.query.exact.InListAtomQueryParser;
|
|
|
|
|
-import org.es.sql.dsl.parser.query.method.MethodInvocation;
|
|
|
|
|
-import org.es.sql.dsl.parser.query.method.fulltext.FullTextAtomQueryParser;
|
|
|
|
|
-//import org.es.sql.dsl.parser.query.method.script.ScriptAtomQueryParser;
|
|
|
|
|
-import org.es.sql.dsl.parser.query.method.term.TermLevelAtomQueryParser;
|
|
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
|
|
+import org.es.sql.bean.AtomQuery;
|
|
|
|
|
+import org.es.sql.bean.SQLCondition;
|
|
|
|
|
+import org.es.sql.bean.SQLArgs;
|
|
|
|
|
+import org.es.sql.enums.SQLBoolOperator;
|
|
|
|
|
+import org.es.sql.enums.SQLConditionType;
|
|
|
|
|
+import org.es.sql.exception.ElasticSql2DslException;
|
|
|
|
|
+import org.es.sql.listener.ParseActionListener;
|
|
|
|
|
+import org.es.sql.parser.query.exact.BetweenAndAtomQueryParser;
|
|
|
|
|
+import org.es.sql.parser.query.exact.BinaryAtomQueryParser;
|
|
|
|
|
+import org.es.sql.parser.query.exact.InListAtomQueryParser;
|
|
|
|
|
+import org.es.sql.parser.query.method.MethodInvocation;
|
|
|
|
|
+import org.es.sql.parser.query.method.fulltext.FullTextAtomQueryParser;
|
|
|
|
|
+import org.es.sql.parser.query.method.term.TermLevelAtomQueryParser;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
+//import org.es.sql.parser.query.method.script.ScriptAtomQueryParser;
|
|
|
|
|
+
|
|
|
public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
|
|
|
|
|
private final TermLevelAtomQueryParser termLevelAtomQueryParser;
|
|
private final TermLevelAtomQueryParser termLevelAtomQueryParser;
|
|
|
-// private final ScriptAtomQueryParser scriptAtomQueryParser;
|
|
|
|
|
|
|
+ // private final ScriptAtomQueryParser scriptAtomQueryParser;
|
|
|
private final FullTextAtomQueryParser fullTextAtomQueryParser;
|
|
private final FullTextAtomQueryParser fullTextAtomQueryParser;
|
|
|
private final BinaryAtomQueryParser binaryQueryParser;
|
|
private final BinaryAtomQueryParser binaryQueryParser;
|
|
|
private final InListAtomQueryParser inListQueryParser;
|
|
private final InListAtomQueryParser inListQueryParser;
|
|
@@ -44,17 +46,17 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
// scriptAtomQueryParser = new ScriptAtomQueryParser();
|
|
// scriptAtomQueryParser = new ScriptAtomQueryParser();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected BoolQueryBuilder parseQueryConditionExpr(SQLExpr conditionExpr, String queryAs, Object[] sqlArgs) {
|
|
|
|
|
- SQLCondition sqlCondition = recursiveParseQueryCondition(conditionExpr, queryAs, sqlArgs);
|
|
|
|
|
- SQLBoolOperator operator = sqlCondition.getOperator();
|
|
|
|
|
|
|
+ protected BoolQueryBuilder parseQueryConditionExpr(SQLExpr conditionExpr, String queryAs, SQLArgs SQLArgs) {
|
|
|
|
|
+ SQLCondition SQLCondition = recursiveParseQueryCondition(conditionExpr, queryAs, SQLArgs);
|
|
|
|
|
+ SQLBoolOperator operator = SQLCondition.getOperator();
|
|
|
|
|
|
|
|
- if (SQLConditionType.Atom == sqlCondition.getSQLConditionType()) {
|
|
|
|
|
|
|
+ if (SQLConditionType.Atom == SQLCondition.getSQLConditionType()) {
|
|
|
operator = SQLBoolOperator.AND;
|
|
operator = SQLBoolOperator.AND;
|
|
|
}
|
|
}
|
|
|
- return mergeAtomQuery(sqlCondition.getQueryList(), operator);
|
|
|
|
|
|
|
+ return mergeAtomQuery(SQLCondition.getQueryList(), operator);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private SQLCondition recursiveParseQueryCondition(SQLExpr conditionExpr, String queryAs, Object[] sqlArgs) {
|
|
|
|
|
|
|
+ private SQLCondition recursiveParseQueryCondition(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();
|
|
@@ -62,8 +64,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 = recursiveParseQueryCondition(binOpExpr.getLeft(), queryAs, SQLArgs);
|
|
|
|
|
+ SQLCondition rightCondition = recursiveParseQueryCondition(binOpExpr.getRight(), queryAs, SQLArgs);
|
|
|
|
|
|
|
|
List<AtomQuery> mergedQueryList = Lists.newArrayList();
|
|
List<AtomQuery> mergedQueryList = Lists.newArrayList();
|
|
|
combineQueryBuilder(mergedQueryList, leftCondition, operator);
|
|
combineQueryBuilder(mergedQueryList, leftCondition, operator);
|
|
@@ -73,58 +75,58 @@ 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 = recursiveParseQueryCondition(((SQLNotExpr) conditionExpr).getExpr(), queryAs, SQLArgs);
|
|
|
|
|
|
|
|
- SQLBoolOperator operator = innerSqlCondition.getOperator();
|
|
|
|
|
- if (SQLConditionType.Atom == innerSqlCondition.getSQLConditionType()) {
|
|
|
|
|
|
|
+ SQLBoolOperator operator = innerSQLCondition.getOperator();
|
|
|
|
|
+ if (SQLConditionType.Atom == innerSQLCondition.getSQLConditionType()) {
|
|
|
operator = SQLBoolOperator.AND;
|
|
operator = SQLBoolOperator.AND;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- BoolQueryBuilder boolQuery = mergeAtomQuery(innerSqlCondition.getQueryList(), operator);
|
|
|
|
|
|
|
+ BoolQueryBuilder boolQuery = mergeAtomQuery(innerSQLCondition.getQueryList(), operator);
|
|
|
boolQuery = QueryBuilders.boolQuery().mustNot(boolQuery);
|
|
boolQuery = QueryBuilders.boolQuery().mustNot(boolQuery);
|
|
|
|
|
|
|
|
return new SQLCondition(new AtomQuery(boolQuery), SQLConditionType.Atom);
|
|
return new SQLCondition(new AtomQuery(boolQuery), SQLConditionType.Atom);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- 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, Object[] 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);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
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()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void combineQueryBuilder(List<AtomQuery> combiner, SQLCondition sqlCondition, SQLBoolOperator binOperator) {
|
|
|
|
|
- if (SQLConditionType.Atom == sqlCondition.getSQLConditionType() || sqlCondition.getOperator() == binOperator) {
|
|
|
|
|
- combiner.addAll(sqlCondition.getQueryList());
|
|
|
|
|
|
|
+ private void combineQueryBuilder(List<AtomQuery> combiner, SQLCondition SQLCondition, SQLBoolOperator binOperator) {
|
|
|
|
|
+ if (SQLConditionType.Atom == SQLCondition.getSQLConditionType() || SQLCondition.getOperator() == binOperator) {
|
|
|
|
|
+ combiner.addAll(SQLCondition.getQueryList());
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- BoolQueryBuilder boolQuery = mergeAtomQuery(sqlCondition.getQueryList(), sqlCondition.getOperator());
|
|
|
|
|
|
|
+ BoolQueryBuilder boolQuery = mergeAtomQuery(SQLCondition.getQueryList(), SQLCondition.getOperator());
|
|
|
combiner.add(new AtomQuery(boolQuery));
|
|
combiner.add(new AtomQuery(boolQuery));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -143,7 +145,7 @@ public abstract class AbstractQueryConditionParser implements QueryParser {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- String nestedDocPrefix = atomQuery.getNestedQueryPathContext();
|
|
|
|
|
|
|
+ String nestedDocPrefix = atomQuery.getNestedQueryPath();
|
|
|
listMultiMap.put(nestedDocPrefix, atomQuery.getQuery());
|
|
listMultiMap.put(nestedDocPrefix, atomQuery.getQuery());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|