ElasticSqlMethodInvokeHelper.java 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package org.es.sql.dsl.helper;
  2. import com.alibaba.druid.sql.ast.SQLExpr;
  3. import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
  4. import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
  5. import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
  6. import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
  7. import com.google.common.collect.ImmutableList;
  8. import org.apache.commons.collections.CollectionUtils;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.es.sql.dsl.exception.ElasticSql2DslException;
  11. import java.util.List;
  12. public class ElasticSqlMethodInvokeHelper {
  13. public static final List<String> DATE_METHOD = ImmutableList.of("date", "to_date", "toDate");
  14. public static final List<String> AGG_TERMS_METHOD = ImmutableList.of("terms", "terms_agg");
  15. public static final List<String> AGG_RANGE_METHOD = ImmutableList.of("range", "range_agg");
  16. public static final List<String> AGG_RANGE_SEGMENT_METHOD = ImmutableList.of("segment", "segment_agg");
  17. public static final String AGG_MIN_METHOD = "min";
  18. public static final String AGG_MAX_METHOD = "max";
  19. public static final String AGG_AVG_METHOD = "avg";
  20. public static final String AGG_SUM_METHOD = "sum";
  21. public static Boolean isMethodOf(List<String> methodAlias, String method) {
  22. if (CollectionUtils.isEmpty(methodAlias)) {
  23. return Boolean.FALSE;
  24. }
  25. for (String alias : methodAlias) {
  26. if (alias.equalsIgnoreCase(method)) {
  27. return Boolean.TRUE;
  28. }
  29. }
  30. return Boolean.FALSE;
  31. }
  32. public static Boolean isMethodOf(String methodAlias, String method) {
  33. if (StringUtils.isBlank(methodAlias)) {
  34. return Boolean.FALSE;
  35. }
  36. return methodAlias.equalsIgnoreCase(method);
  37. }
  38. public static void checkTermsAggMethod(SQLMethodInvokeExpr aggInvokeExpr) {
  39. if (!isMethodOf(AGG_TERMS_METHOD, aggInvokeExpr.getMethodName())) {
  40. throw new ElasticSql2DslException("[syntax error] Sql not support method:" + aggInvokeExpr.getMethodName());
  41. }
  42. }
  43. public static void checkRangeAggMethod(SQLMethodInvokeExpr aggInvokeExpr) {
  44. if (!isMethodOf(AGG_RANGE_METHOD, aggInvokeExpr.getMethodName())) {
  45. throw new ElasticSql2DslException("[syntax error] Sql not support method:" + aggInvokeExpr.getMethodName());
  46. }
  47. }
  48. public static void checkRangeItemAggMethod(SQLMethodInvokeExpr aggInvokeExpr) {
  49. if (!isMethodOf(AGG_RANGE_SEGMENT_METHOD, aggInvokeExpr.getMethodName())) {
  50. throw new ElasticSql2DslException("[syntax error] Sql not support method:" + aggInvokeExpr.getMethodName());
  51. }
  52. }
  53. public static void checkStatAggMethod(SQLAggregateExpr statAggExpr) {
  54. if (!AGG_MIN_METHOD.equalsIgnoreCase(statAggExpr.getMethodName()) &&
  55. !AGG_MAX_METHOD.equalsIgnoreCase(statAggExpr.getMethodName()) &&
  56. !AGG_AVG_METHOD.equalsIgnoreCase(statAggExpr.getMethodName()) &&
  57. !AGG_SUM_METHOD.equalsIgnoreCase(statAggExpr.getMethodName())) {
  58. throw new ElasticSql2DslException("[syntax error] Sql not support method:" + statAggExpr.getMethodName());
  59. }
  60. }
  61. public static void checkDateMethod(SQLMethodInvokeExpr dateInvokeExpr) {
  62. if (!isMethodOf(DATE_METHOD, dateInvokeExpr.getMethodName())) {
  63. throw new ElasticSql2DslException("[syntax error] Sql not support method:" + dateInvokeExpr.getMethodName());
  64. }
  65. if (CollectionUtils.isEmpty(dateInvokeExpr.getParameters()) || dateInvokeExpr.getParameters().size() != 2) {
  66. throw new ElasticSql2DslException(String.format("[syntax error] There is no %s args method named date",
  67. dateInvokeExpr.getParameters() != null ? dateInvokeExpr.getParameters().size() : 0));
  68. }
  69. SQLExpr patternArg = dateInvokeExpr.getParameters().get(0);
  70. SQLExpr timeValArg = dateInvokeExpr.getParameters().get(1);
  71. if (!(patternArg instanceof SQLCharExpr) && !(patternArg instanceof SQLVariantRefExpr)) {
  72. throw new ElasticSql2DslException("[syntax error] The first arg of date method should be a time pattern");
  73. }
  74. if (!(timeValArg instanceof SQLCharExpr) && !(timeValArg instanceof SQLVariantRefExpr)) {
  75. throw new ElasticSql2DslException("[syntax error] The second arg of date method should be a string of time");
  76. }
  77. }
  78. }