Java Code Examples for com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr#getParameters()

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr#getParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldMaker.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private static Field makeFilterMethodField(SQLMethodInvokeExpr filterMethod, String alias) throws SqlParseException {
    List<SQLExpr> parameters = filterMethod.getParameters();
    int parametersSize = parameters.size();
    if (parametersSize != 1 && parametersSize != 2) {
        throw new SqlParseException("filter group by field should only have one or 2 parameters filter(Expr) or filter(name,Expr)");
    }
    String filterAlias = filterMethod.getMethodName();
    SQLExpr exprToCheck = null;
    if (parametersSize == 1) {
        exprToCheck = parameters.get(0);
        filterAlias = "filter(" + exprToCheck.toString().replaceAll("\n", " ") + ")";
    }
    if (parametersSize == 2) {
        filterAlias = Util.extendedToString(parameters.get(0));
        exprToCheck = parameters.get(1);
    }
    Where where = Where.newInstance();
    new WhereParser(new SqlParser()).parseWhere(exprToCheck, where);
    if (where.getWheres().size() == 0)
        throw new SqlParseException("unable to parse filter where.");
    List<KVValue> methodParameters = new ArrayList<>();
    methodParameters.add(new KVValue("where", where));
    methodParameters.add(new KVValue("alias", filterAlias + "@FILTER"));
    return new MethodField("filter", methodParameters, null, alias);
}
 
Example 2
Source File: JoinParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private String getMethodInvokeFieldName(SQLSelectItem item){
	SQLMethodInvokeExpr invoke = (SQLMethodInvokeExpr)item.getExpr();
	List<SQLExpr> itemExprs = invoke.getParameters();
	for(SQLExpr itemExpr:itemExprs){
		if (itemExpr instanceof SQLPropertyExpr) {
			return itemExpr.toString();//字段别名
		}
	}
	return item.toString();
}
 
Example 3
Source File: WhereParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private Object[] getMethodValuesWithSubQueries(SQLMethodInvokeExpr method) throws SqlParseException {
    List<Object> values = new ArrayList<>();
    for (SQLExpr innerExpr : method.getParameters()) {
        if (innerExpr instanceof SQLQueryExpr) {
            Select select = sqlParser.parseSelect((MySqlSelectQueryBlock) ((SQLQueryExpr) innerExpr).getSubQuery().getQuery());
            values.add(new SubQueryExpression(select));
        } else if (innerExpr instanceof SQLTextLiteralExpr) {
            values.add(((SQLTextLiteralExpr) innerExpr).getText());
        } else {
            values.add(innerExpr);
        }

    }
    return values.toArray();
}
 
Example 4
Source File: ScriptFilter.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public boolean tryParseFromMethodExpr(SQLMethodInvokeExpr expr) throws SqlParseException {
    if (!expr.getMethodName().toLowerCase().equals("script")) {
        return false;
    }
    List<SQLExpr> methodParameters = expr.getParameters();
    if (methodParameters.size() == 0) {
        return false;
    }
    script = Util.extendedToString(methodParameters.get(0));

    if (methodParameters.size() == 1) {
        return true;
    }

    args = new HashMap<>();
    for (int i = 1; i < methodParameters.size(); i++) {

        SQLExpr innerExpr = methodParameters.get(i);
        if (!(innerExpr instanceof SQLBinaryOpExpr)) {
            return false;
        }
        SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) innerExpr;
        if (!binaryOpExpr.getOperator().getName().equals("=")) {
            return false;
        }

        SQLExpr right = binaryOpExpr.getRight();
        Object value = Util.expr2Object(right);
        String key = Util.extendedToString(binaryOpExpr.getLeft());
        if(key.equals("script_type")){
            parseAndUpdateScriptType(value.toString());
        }
        else {
            args.put(key, value);
        }

    }
    return true;
}
 
Example 5
Source File: ChildrenType.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public boolean tryFillFromExpr(SQLExpr expr) throws SqlParseException {
    if (!(expr instanceof SQLMethodInvokeExpr)) return false;
    SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) expr;

    String methodName = method.getMethodName();

    if (!methodName.toLowerCase().equals("children")) return false;

    List<SQLExpr> parameters = method.getParameters();

    if (parameters.size() != 2)
        throw new SqlParseException("on children object only allowed 2 parameters (type, field)/(type, conditions...) ");

    String type = Util.extendedToString(parameters.get(0));
    this.childType = type;
    
    SQLExpr secondParameter = parameters.get(1);
    if(secondParameter instanceof SQLTextLiteralExpr || secondParameter instanceof SQLIdentifierExpr || secondParameter instanceof SQLPropertyExpr) {
        this.field = Util.extendedToString(secondParameter);
        this.simple = true;
    } else {
        Where where = Where.newInstance();
        new WhereParser(new SqlParser()).parseWhere(secondParameter,where);
        if(where.getWheres().size() == 0)
            throw new SqlParseException("unable to parse filter where.");
        this.where = where;
        simple = false;
    }
    
    return true;
}
 
Example 6
Source File: MysqlMethodInvocationHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private SQLExpr invokeAddDate(SQLMethodInvokeExpr expr, boolean negative) throws SQLNonTransientException {
    List<SQLExpr> parameters = expr.getParameters();
    if (parameters.size() != 2) {
        throwSyntaxError(expr);
    }
    SQLExpr p1 = parameters.get(0);
    SQLExpr p2 = parameters.get(1);
    if (p1 instanceof SQLMethodInvokeExpr) {
        p1 = doInvoke((SQLMethodInvokeExpr) p1);
    }
    if (p1 instanceof SQLCharExpr) {
        String time = ((SQLCharExpr) p1).getText();
        Integer delta = null;
        String unit = null;
        if (p2 instanceof SQLIntegerExpr) {
            delta = (Integer) ((SQLIntegerExpr) p2).getNumber();
            unit = "DAY";
        } else if (p2 instanceof MySqlIntervalExpr) {
            SQLIntegerExpr value = (SQLIntegerExpr) ((MySqlIntervalExpr) p2).getValue();
            delta = (Integer) value.getNumber();
            unit = ((MySqlIntervalExpr) p2).getUnit().name();
        } else {
            throwSyntaxError(p2);
        }
        try {
            Date date = DateUtils.parseDate(time, SUPPORT_PATTERNS);
            Date result;
            delta = negative ? -delta : delta;
            if ("MONTH".equals(unit)) {
                result = DateUtils.addMonths(date, delta);
            } else if ("DAY".equals(unit)) {
                result = DateUtils.addDays(date, delta);
            } else if ("HOUR".equals(unit)) {
                result = DateUtils.addHours(date, delta);
            } else if ("MINUTE".equals(unit)) {
                result = DateUtils.addMinutes(date, delta);
            } else if ("SECOND".equals(unit)) {
                result = DateUtils.addSeconds(date, delta);
            } else {
                return null;
            }
            String ret = DateFormatUtils.format(result, "yyyy-MM-dd HH:mm:ss");
            return new SQLIdentifierExpr(ret);
        } catch (ParseException e) {
            LOGGER.error("",e);
        }
    }
    return null;
}
 
Example 7
Source File: NestedType.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public boolean tryFillFromExpr(SQLExpr expr) throws SqlParseException {
    if (!(expr instanceof SQLMethodInvokeExpr)) return false;
    SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) expr;
    String methodNameLower = method.getMethodName().toLowerCase();
    if (!(methodNameLower.equals("nested") || methodNameLower.equals("reverse_nested"))) return false;

    reverse = methodNameLower.equals("reverse_nested");

    List<SQLExpr> parameters = method.getParameters();
    int size = parameters.size();
    if (size != 3 && size != 2 && size != 1)
        throw new SqlParseException("on nested object only allowed 3 parameters (path,conditions..,inner_hits) or 2 parameters (field,path)/(path,conditions..) or 1 parameter (field) ");

    // inner_hits
    if (size == 3) {
        this.innerHits = Util.extendedToString(parameters.remove(--size));
    }

    String field = Util.extendedToString(parameters.get(0));
    this.field = field;
    if (size == 1) {
        //calc path myself..
        if (!field.contains(".")) {
            if (!reverse)
                throw new SqlParseException("nested should contain . on their field name");
            else {
                this.path = null;
                this.simple = true;
            }
        } else {
            int lastDot = field.lastIndexOf(".");
            this.path = field.substring(0, lastDot);
            this.simple = true;

        }

    } else if (size == 2) {
        SQLExpr secondParameter = parameters.get(1);
        if(secondParameter instanceof SQLTextLiteralExpr || secondParameter instanceof SQLIdentifierExpr || secondParameter instanceof SQLPropertyExpr) {

            String pathString = Util.extendedToString(secondParameter);
            if(pathString.equals(""))
                this.path = null;
            else
                this.path = pathString;
            this.simple = true;
        }
        else {
            this.path = field;
            Where where = Where.newInstance();
            new WhereParser(new SqlParser()).parseWhere(secondParameter,where);
            if(where.getWheres().size() == 0)
                throw new SqlParseException("unable to parse filter where.");
            this.where = where;
            simple = false;
        }
    }

    return true;
}
 
Example 8
Source File: Paramer.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public static Paramer parseParamer(SQLMethodInvokeExpr method) throws SqlParseException {
	Paramer instance = new Paramer();
	List<SQLExpr> parameters = method.getParameters();
       for (SQLExpr expr : parameters) {
           if (expr instanceof SQLCharExpr) {
               if (instance.value == null) {
                   instance.value = ((SQLCharExpr) expr).getText();
               } else {
                   instance.analysis = ((SQLCharExpr) expr).getText();
               }
           } else if (expr instanceof SQLNumericLiteralExpr) {
               instance.boost = ((SQLNumericLiteralExpr) expr).getNumber().floatValue();
           } else if (expr instanceof SQLBinaryOpExpr) {
               SQLBinaryOpExpr sqlExpr = (SQLBinaryOpExpr) expr;
               switch (Util.expr2Object(sqlExpr.getLeft()).toString()) {
                   case "query":
                       instance.value = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "analyzer":
                       instance.analysis = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "boost":
                       instance.boost = Float.parseFloat(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;
                   case "slop":
                       instance.slop = Integer.parseInt(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;

                   case "fields":
                       int index;
                       for (String f : Strings.splitStringByCommaToArray(Util.expr2Object(sqlExpr.getRight()).toString())) {
                           index = f.lastIndexOf('^');
                           if (-1 < index) {
                               instance.fieldsBoosts.put(f.substring(0, index), Float.parseFloat(f.substring(index + 1)));
                           } else {
                               instance.fieldsBoosts.put(f, 1.0F);
                           }
                       }
                       break;
                   case "type":
                       instance.type = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "tie_breaker":
                       instance.tieBreaker = Float.parseFloat(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;
                   case "operator":
                       instance.operator = Operator.fromString(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;

                   case "default_field":
                       instance.defaultField = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;

                   case "in_order":
                       instance.inOrder = Boolean.valueOf(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;
                   case "clauses":
                       instance.clauses = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "minimum_should_match":
                       instance.minimumShouldMatch = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;

                   default:
                       break;
               }
           }
       }

	return instance;
}