|
|
@@ -8,67 +8,77 @@ import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
-import org.elasticsearch.dsl.ElasticSqlIdentifier;
|
|
|
+import org.elasticsearch.dsl.bean.ElasticSqlQueryField;
|
|
|
+import org.elasticsearch.dsl.bean.ElasticSqlQueryFields;
|
|
|
+import org.elasticsearch.dsl.enums.QueryFieldType;
|
|
|
import org.elasticsearch.dsl.exception.ElasticSql2DslException;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
public class ElasticSqlIdentifierHelper {
|
|
|
|
|
|
+ public static ElasticSqlQueryField parseSqlQueryField(SQLExpr queryFieldExpr, String queryAs,
|
|
|
+ SQLFlatFieldFunc flatFieldFunc, SQLNestedFieldFunc nestedFieldFunc) {
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 字段标识符处理
|
|
|
*
|
|
|
- * @param propertyNameExpr 待处理字段表达式
|
|
|
- * @param queryAsAlias 文档类型别名
|
|
|
- * @param singlePropertyFunc 非内嵌类型处理策略(inner/property)
|
|
|
- * @param pathPropertyFunc 内嵌类型处理逻辑(nested)
|
|
|
+ * @param propertyNameExpr 待处理字段表达式
|
|
|
+ * @param queryAsAlias 文档类型别名
|
|
|
+ * @param flatFieldFunc 非内嵌类型处理策略(inner/property)
|
|
|
+ * @param nestedFieldFunc 内嵌类型处理逻辑(nested)
|
|
|
* @return 标识符类型
|
|
|
*/
|
|
|
- public static ElasticSqlIdentifier parseSqlIdentifier(final SQLExpr propertyNameExpr, final String queryAsAlias,
|
|
|
- final ElasticSqlSinglePropertyFunc singlePropertyFunc,
|
|
|
- final ElasticSqlPathPropertyFunc pathPropertyFunc) {
|
|
|
- final StringBuilder propertyPathBuilder = new StringBuilder();
|
|
|
- final StringBuilder propertyNameBuilder = new StringBuilder();
|
|
|
+ public static ElasticSqlQueryField parseSqlIdentifier(SQLExpr propertyNameExpr, String queryAsAlias,
|
|
|
+ SQLFlatFieldFunc flatFieldFunc,
|
|
|
+ SQLNestedFieldFunc nestedFieldFunc) {
|
|
|
+ StringBuilder propertyPathBuilder = new StringBuilder();
|
|
|
+ StringBuilder propertyNameBuilder = new StringBuilder();
|
|
|
if (propertyNameExpr instanceof SQLMethodInvokeExpr) {
|
|
|
//如果指定是inner doc类型
|
|
|
SQLMethodInvokeExpr innerObjExpr = (SQLMethodInvokeExpr) propertyNameExpr;
|
|
|
if (ElasticSqlMethodInvokeHelper.INNER_DOC_METHOD.equalsIgnoreCase(innerObjExpr.getMethodName())) {
|
|
|
ElasticSqlMethodInvokeHelper.checkInnerDocMethod(innerObjExpr);
|
|
|
|
|
|
- ElasticSqlIdentifierHelper.parseSqlIdf(innerObjExpr.getParameters().get(0), queryAsAlias, new ElasticSqlSinglePropertyFunc() {
|
|
|
+ ElasticSqlIdentifierHelper.parseSqlIdf(innerObjExpr.getParameters().get(0), queryAsAlias, new SQLFlatFieldFunc() {
|
|
|
@Override
|
|
|
- public void parse(String propertyName) {
|
|
|
+ public void parse(String flatFieldName) {
|
|
|
throw new ElasticSql2DslException("[syntax error] The arg of method inner_doc must contain property reference path");
|
|
|
}
|
|
|
- }, new ElasticSqlPathPropertyFunc() {
|
|
|
+ }, new SQLNestedFieldFunc() {
|
|
|
@Override
|
|
|
- public void parse(String propertyPath, String propertyName) {
|
|
|
- propertyPathBuilder.append(propertyPath);
|
|
|
- propertyNameBuilder.append(propertyName);
|
|
|
- singlePropertyFunc.parse(String.format("%s.%s", propertyPath, propertyName));
|
|
|
+ public void parse(String nestedDocPath, String fieldName) {
|
|
|
+ propertyPathBuilder.append(nestedDocPath);
|
|
|
+ propertyNameBuilder.append(fieldName);
|
|
|
+ flatFieldFunc.parse(String.format("%s.%s", nestedDocPath, fieldName));
|
|
|
}
|
|
|
});
|
|
|
- return new ElasticSqlIdentifier(propertyPathBuilder.toString(), propertyNameBuilder.toString(), ElasticSqlIdentifier.IdentifierType.InnerDocProperty);
|
|
|
+ //return new ElasticSqlQueryField(propertyPathBuilder.toString(), propertyNameBuilder.toString(), QueryFieldType.InnerDocField);
|
|
|
+ return ElasticSqlQueryFields.newInnerDocQueryField(propertyPathBuilder.toString(), propertyNameBuilder.toString());
|
|
|
}
|
|
|
|
|
|
//如果执行是nested doc类型
|
|
|
if (ElasticSqlMethodInvokeHelper.NESTED_DOC_METHOD.equalsIgnoreCase(innerObjExpr.getMethodName())) {
|
|
|
ElasticSqlMethodInvokeHelper.checkNestedDocMethod(innerObjExpr);
|
|
|
|
|
|
- ElasticSqlIdentifierHelper.parseSqlIdf(innerObjExpr.getParameters().get(0), queryAsAlias, new ElasticSqlSinglePropertyFunc() {
|
|
|
+ ElasticSqlIdentifierHelper.parseSqlIdf(innerObjExpr.getParameters().get(0), queryAsAlias, new SQLFlatFieldFunc() {
|
|
|
@Override
|
|
|
- public void parse(String propertyName) {
|
|
|
+ public void parse(String flatFieldName) {
|
|
|
throw new ElasticSql2DslException("[syntax error] The arg of method nested_doc must contain property reference path");
|
|
|
}
|
|
|
- }, new ElasticSqlPathPropertyFunc() {
|
|
|
+ }, new SQLNestedFieldFunc() {
|
|
|
@Override
|
|
|
- public void parse(String propertyPath, String propertyName) {
|
|
|
- propertyPathBuilder.append(propertyPath);
|
|
|
- propertyNameBuilder.append(propertyName);
|
|
|
- pathPropertyFunc.parse(propertyPath, propertyName);
|
|
|
+ public void parse(String nestedDocPath, String fieldName) {
|
|
|
+ propertyPathBuilder.append(nestedDocPath);
|
|
|
+ propertyNameBuilder.append(fieldName);
|
|
|
+ nestedFieldFunc.parse(nestedDocPath, fieldName);
|
|
|
}
|
|
|
});
|
|
|
- return new ElasticSqlIdentifier(propertyPathBuilder.toString(), propertyNameBuilder.toString(), ElasticSqlIdentifier.IdentifierType.NestedDocProperty);
|
|
|
+ //return new ElasticSqlQueryField(propertyPathBuilder.toString(), propertyNameBuilder.toString(), QueryFieldType.NestedDocField);
|
|
|
+ return ElasticSqlQueryFields.newNestedDocQueryField(propertyPathBuilder.toString(), propertyNameBuilder.toString());
|
|
|
}
|
|
|
|
|
|
throw new ElasticSql2DslException("[syntax error] Sql identifier method only support nested_doc and inner_doc");
|
|
|
@@ -76,33 +86,36 @@ public class ElasticSqlIdentifierHelper {
|
|
|
|
|
|
//默认按照inner doc或者property name来处理
|
|
|
final List<Boolean> isInnerDocProperty = Lists.newLinkedList();
|
|
|
- ElasticSqlIdentifierHelper.parseSqlIdf(propertyNameExpr, queryAsAlias, new ElasticSqlSinglePropertyFunc() {
|
|
|
+ ElasticSqlIdentifierHelper.parseSqlIdf(propertyNameExpr, queryAsAlias, new SQLFlatFieldFunc() {
|
|
|
@Override
|
|
|
- public void parse(String propertyName) {
|
|
|
+ public void parse(String flatFieldName) {
|
|
|
isInnerDocProperty.add(Boolean.FALSE);
|
|
|
- propertyNameBuilder.append(propertyName);
|
|
|
- singlePropertyFunc.parse(propertyName);
|
|
|
+ propertyNameBuilder.append(flatFieldName);
|
|
|
+ flatFieldFunc.parse(flatFieldName);
|
|
|
}
|
|
|
- }, new ElasticSqlPathPropertyFunc() {
|
|
|
+ }, new SQLNestedFieldFunc() {
|
|
|
@Override
|
|
|
- public void parse(String propertyPath, String propertyName) {
|
|
|
+ public void parse(String nestedDocPath, String fieldName) {
|
|
|
isInnerDocProperty.add(Boolean.TRUE);
|
|
|
- propertyPathBuilder.append(propertyPath);
|
|
|
- propertyNameBuilder.append(propertyName);
|
|
|
- singlePropertyFunc.parse(String.format("%s.%s", propertyPath, propertyName));
|
|
|
+ propertyPathBuilder.append(nestedDocPath);
|
|
|
+ propertyNameBuilder.append(fieldName);
|
|
|
+ flatFieldFunc.parse(String.format("%s.%s", nestedDocPath, fieldName));
|
|
|
}
|
|
|
});
|
|
|
if (CollectionUtils.isNotEmpty(isInnerDocProperty)) {
|
|
|
if (isInnerDocProperty.get(0)) {
|
|
|
- return new ElasticSqlIdentifier(propertyPathBuilder.toString(), propertyNameBuilder.toString(), ElasticSqlIdentifier.IdentifierType.InnerDocProperty);
|
|
|
+ //return new ElasticSqlQueryField(propertyPathBuilder.toString(), propertyNameBuilder.toString(), QueryFieldType.InnerDocField);
|
|
|
+ return ElasticSqlQueryFields.newInnerDocQueryField(propertyPathBuilder.toString(), propertyNameBuilder.toString());
|
|
|
}
|
|
|
- return new ElasticSqlIdentifier(propertyNameBuilder.toString());
|
|
|
+ //return new ElasticSqlQueryField(propertyNameBuilder.toString());
|
|
|
+ return ElasticSqlQueryFields.newRootDocQueryField(propertyNameBuilder.toString());
|
|
|
}
|
|
|
- return new ElasticSqlIdentifier(propertyPathBuilder.toString(), propertyNameBuilder.toString(), ElasticSqlIdentifier.IdentifierType.MatchAllField);
|
|
|
+ //return new ElasticSqlQueryField(propertyPathBuilder.toString(), propertyNameBuilder.toString(), QueryFieldType.MatchAllField);
|
|
|
+ return ElasticSqlQueryFields.newMatchAllField(propertyPathBuilder.toString());
|
|
|
}
|
|
|
|
|
|
private static void parseSqlIdf(final SQLExpr propertyNameExpr, final String queryAsAlias,
|
|
|
- final ElasticSqlSinglePropertyFunc singlePropertyFunc, final ElasticSqlPathPropertyFunc pathPropertyFunc) {
|
|
|
+ final SQLFlatFieldFunc singlePropertyFunc, final SQLNestedFieldFunc pathPropertyFunc) {
|
|
|
//查询所有字段
|
|
|
if (propertyNameExpr instanceof SQLAllColumnExpr) {
|
|
|
return;
|
|
|
@@ -141,13 +154,13 @@ public class ElasticSqlIdentifierHelper {
|
|
|
|
|
|
|
|
|
@FunctionalInterface
|
|
|
- public interface ElasticSqlSinglePropertyFunc {
|
|
|
- void parse(String propertyName);
|
|
|
+ public interface SQLFlatFieldFunc {
|
|
|
+ void parse(String flatFieldName);
|
|
|
}
|
|
|
|
|
|
@FunctionalInterface
|
|
|
- public interface ElasticSqlPathPropertyFunc {
|
|
|
- void parse(String propertyPath, String propertyName);
|
|
|
+ public interface SQLNestedFieldFunc {
|
|
|
+ void parse(String nestedDocPath, String fieldName);
|
|
|
}
|
|
|
|
|
|
}
|