-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
566 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ava/org/elasticsearch/dsl/parser/query/method/AbstractFieldSpecificMethodQueryParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.elasticsearch.dsl.parser.query.method; | ||
|
||
import com.alibaba.druid.sql.ast.SQLExpr; | ||
import org.elasticsearch.dsl.bean.AtomQuery; | ||
import org.elasticsearch.dsl.bean.ElasticSqlQueryField; | ||
import org.elasticsearch.dsl.enums.QueryFieldType; | ||
import org.elasticsearch.dsl.exception.ElasticSql2DslException; | ||
import org.elasticsearch.dsl.listener.ParseActionListener; | ||
import org.elasticsearch.dsl.parser.sql.QueryFieldParser; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class AbstractFieldSpecificMethodQueryParser extends ParameterizedMethodQueryParser { | ||
|
||
protected ParseActionListener parseActionListener; | ||
|
||
public AbstractFieldSpecificMethodQueryParser(ParseActionListener parseActionListener) { | ||
this.parseActionListener = parseActionListener; | ||
} | ||
|
||
protected abstract QueryBuilder buildQuery(MethodInvocation invocation, String fieldName, Map<String, String> extraParams); | ||
|
||
protected abstract SQLExpr getFieldExpr(MethodInvocation invocation); | ||
|
||
@Override | ||
protected String getExtraParamString(MethodInvocation invocation) { | ||
//ignore extra params, subclass can override if necessary | ||
return null; | ||
} | ||
|
||
@Override | ||
protected AtomQuery parseMethodQueryWithExtraParams(MethodInvocation invocation, Map<String, String> extraParamMap) throws ElasticSql2DslException { | ||
return parseCondition(invocation, extraParamMap); | ||
} | ||
|
||
private AtomQuery parseCondition(MethodInvocation invocation, Map<String, String> extraParamMap) { | ||
QueryFieldParser queryFieldParser = new QueryFieldParser(); | ||
ElasticSqlQueryField queryField = queryFieldParser.parseConditionQueryField(getFieldExpr(invocation), invocation.getQueryAs()); | ||
|
||
AtomQuery atomQuery = null; | ||
if (queryField.getQueryFieldType() == QueryFieldType.RootDocField || queryField.getQueryFieldType() == QueryFieldType.InnerDocField) { | ||
QueryBuilder originalQuery = buildQuery(invocation, queryField.getQueryFieldFullName(), extraParamMap); | ||
atomQuery = new AtomQuery(originalQuery); | ||
} | ||
|
||
if (queryField.getQueryFieldType() == QueryFieldType.NestedDocField) { | ||
QueryBuilder originalQuery = buildQuery(invocation, queryField.getQueryFieldFullName(), extraParamMap); | ||
atomQuery = new AtomQuery(originalQuery, queryField.getNestedDocContextPath()); | ||
} | ||
|
||
if (atomQuery == null) { | ||
throw new ElasticSql2DslException(String.format("[syntax error] query condition field can not support type[%s]", queryField.getQueryFieldType())); | ||
} | ||
|
||
onAtomMethodQueryConditionParse(queryField, invocation.getSqlArgs()); | ||
|
||
return atomQuery; | ||
} | ||
|
||
|
||
private void onAtomMethodQueryConditionParse(ElasticSqlQueryField paramName, Object[] parameters) { | ||
try { | ||
parseActionListener.onAtomMethodQueryConditionParse(paramName, parameters); | ||
} | ||
catch (Exception ex) { | ||
try { | ||
parseActionListener.onFailure(ex); | ||
} | ||
catch (Exception exp) { | ||
//ignore; | ||
} | ||
} | ||
} | ||
} |
104 changes: 0 additions & 104 deletions
104
...va/org/elasticsearch/dsl/parser/query/method/AbstractFieldSpecifiedMethodQueryParser.java
This file was deleted.
Oops, something went wrong.
11 changes: 5 additions & 6 deletions
11
...e/src/main/java/org/elasticsearch/dsl/parser/query/method/CheckableMethodQueryParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
package org.elasticsearch.dsl.parser.query.method; | ||
|
||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; | ||
import org.elasticsearch.dsl.bean.AtomQuery; | ||
import org.elasticsearch.dsl.exception.ElasticSql2DslException; | ||
|
||
public abstract class CheckableMethodQueryParser implements MethodQueryParser { | ||
|
||
abstract void checkQueryMethod(SQLMethodInvokeExpr matchQueryExpr, String queryAs, Object[] sqlArgs); | ||
protected abstract void checkQueryMethod(MethodInvocation invocation) throws ElasticSql2DslException; | ||
|
||
abstract AtomQuery parseMethodQueryExpr(SQLMethodInvokeExpr matchQueryExpr, String queryAs, Object[] sqlArgs); | ||
protected abstract AtomQuery parseMethodQueryWithCheck(MethodInvocation invocation) throws ElasticSql2DslException; | ||
|
||
@Override | ||
public AtomQuery parseAtomMethodQuery(SQLMethodInvokeExpr methodExpr, String queryAs, Object[] sqlArgs) throws ElasticSql2DslException { | ||
checkQueryMethod(methodExpr, queryAs, sqlArgs); | ||
return parseMethodQueryExpr(methodExpr, queryAs, sqlArgs); | ||
public AtomQuery parseAtomMethodQuery(MethodInvocation invocation) throws ElasticSql2DslException { | ||
checkQueryMethod(invocation); | ||
return parseMethodQueryWithCheck(invocation); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...-query-core/src/main/java/org/elasticsearch/dsl/parser/query/method/MethodInvocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.elasticsearch.dsl.parser.query.method; | ||
|
||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; | ||
|
||
public class MethodInvocation { | ||
private SQLMethodInvokeExpr matchQueryExpr; | ||
private String queryAs; | ||
private Object[] sqlArgs; | ||
|
||
public MethodInvocation(SQLMethodInvokeExpr matchQueryExpr, String queryAs, Object[] sqlArgs) { | ||
this.matchQueryExpr = matchQueryExpr; | ||
this.queryAs = queryAs; | ||
this.sqlArgs = sqlArgs; | ||
} | ||
|
||
public SQLMethodInvokeExpr getMatchQueryExpr() { | ||
return matchQueryExpr; | ||
} | ||
|
||
public String getQueryAs() { | ||
return queryAs; | ||
} | ||
|
||
public Object[] getSqlArgs() { | ||
return sqlArgs; | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
...query-core/src/main/java/org/elasticsearch/dsl/parser/query/method/MethodQueryParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
package org.elasticsearch.dsl.parser.query.method; | ||
|
||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; | ||
import org.elasticsearch.dsl.bean.AtomQuery; | ||
import org.elasticsearch.dsl.exception.ElasticSql2DslException; | ||
|
||
public interface MethodQueryParser { | ||
boolean isMatchMethodInvocation(MethodInvocation invocation); | ||
|
||
boolean isMethodOf(SQLMethodInvokeExpr methodExpr); | ||
|
||
AtomQuery parseAtomMethodQuery(SQLMethodInvokeExpr methodExpr, String queryAs, Object[] sqlArgs) throws ElasticSql2DslException; | ||
AtomQuery parseAtomMethodQuery(MethodInvocation invocation) throws ElasticSql2DslException; | ||
} |
57 changes: 57 additions & 0 deletions
57
...c/main/java/org/elasticsearch/dsl/parser/query/method/ParameterizedMethodQueryParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.elasticsearch.dsl.parser.query.method; | ||
|
||
import com.google.common.collect.Maps; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.elasticsearch.dsl.bean.AtomQuery; | ||
import org.elasticsearch.dsl.exception.ElasticSql2DslException; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public abstract class ParameterizedMethodQueryParser extends CheckableMethodQueryParser { | ||
|
||
private static final String COMMA = ","; | ||
|
||
private static final String COLON = ":"; | ||
|
||
protected abstract String getExtraParamString(MethodInvocation invocation); | ||
|
||
protected abstract AtomQuery parseMethodQueryWithExtraParams( | ||
MethodInvocation invocation, Map<String, String> extraParamMap) throws ElasticSql2DslException; | ||
|
||
@Override | ||
protected AtomQuery parseMethodQueryWithCheck(MethodInvocation invocation) { | ||
Map<String, String> extraParamMap = buildExtraParamMap(invocation); | ||
return parseMethodQueryWithExtraParams(invocation, extraParamMap); | ||
} | ||
|
||
private Map<String, String> buildExtraParamMap(MethodInvocation invocation) { | ||
String extraParamString = getExtraParamString(invocation); | ||
|
||
if (StringUtils.isBlank(extraParamString)) { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
Map<String, String> extraParamMap = Maps.newHashMap(); | ||
for (String paramPair : extraParamString.split(COMMA)) { | ||
String[] paramPairArr = paramPair.split(COLON); | ||
if (paramPairArr.length == 2) { | ||
extraParamMap.put(paramPairArr[0].trim(), paramPairArr[1].trim()); | ||
} | ||
else { | ||
throw new ElasticSql2DslException("Failed to parse query method extra param string!"); | ||
} | ||
} | ||
return extraParamMap; | ||
} | ||
|
||
protected Boolean isExtraParamsString(String extraParams) { | ||
for (String paramPair : extraParams.split(COMMA)) { | ||
String[] paramPairArr = paramPair.split(COLON); | ||
if (paramPairArr.length != 2) { | ||
return Boolean.FALSE; | ||
} | ||
} | ||
return Boolean.TRUE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.