Search in sources :

Example 1 with Function

use of io.requery.query.function.Function in project requery by requery.

the class QueryElement method fromExpressions.

public Set<Expression<?>> fromExpressions() {
    if (from == null) {
        types = new LinkedHashSet<>();
        Set<? extends Expression<?>> expressions;
        switch(queryType) {
            case SELECT:
                expressions = getSelection();
                break;
            case INSERT:
            case UPDATE:
            case UPSERT:
                expressions = updates.keySet();
                break;
            default:
                expressions = Collections.emptySet();
        }
        for (Expression<?> expression : expressions) {
            if (expression instanceof AliasedExpression) {
                expression = ((AliasedExpression) expression).getInnerExpression();
            }
            if (expression instanceof Attribute) {
                Type type = ((Attribute) expression).getDeclaringType();
                types.add(type);
            } else if (expression instanceof Function) {
                Function function = (Function) expression;
                for (Object arg : function.arguments()) {
                    Type type = null;
                    if (arg instanceof Attribute) {
                        type = ((Attribute) arg).getDeclaringType();
                        types.add(type);
                    } else if (arg instanceof Class) {
                        type = model.typeOf((Class) arg);
                    }
                    if (type != null) {
                        types.add(type);
                    }
                }
            }
        }
        if (from == null) {
            from = new LinkedHashSet<>();
        }
        if (!types.isEmpty()) {
            from.addAll(types);
        }
    }
    return from;
}
Also used : Function(io.requery.query.function.Function) Type(io.requery.meta.Type) ExpressionType(io.requery.query.ExpressionType) Attribute(io.requery.meta.Attribute) AliasedExpression(io.requery.query.AliasedExpression)

Example 2 with Function

use of io.requery.query.function.Function in project requery by requery.

the class DefaultOutput method appendConditionValue.

private void appendConditionValue(Expression expression, Object value, boolean parameterize) {
    if (value instanceof QueryAttribute) {
        appendColumn((Expression<?>) value);
    } else if (value instanceof Supplier && ((Supplier) value).get() instanceof QueryAttribute) {
        appendColumn((Expression<?>) ((Supplier) value).get());
    } else if (value instanceof NamedExpression) {
        NamedExpression namedExpression = (NamedExpression) value;
        qb.append(namedExpression.getName());
    } else if (value instanceof Function) {
        appendFunction((Function) value);
    } else if (value instanceof Collection && expression.getExpressionType() == ExpressionType.ROW) {
        qb.openParenthesis();
        qb.commaSeparated((Collection) value);
        qb.closeParenthesis();
    } else {
        if (parameterize) {
            if (parameters != null) {
                parameters.add(expression, value);
            }
            qb.append("?").space();
        } else {
            if (value instanceof CharSequence) {
                qb.appendQuoted(value.toString()).space();
            } else {
                qb.append(value).space();
            }
        }
    }
}
Also used : Function(io.requery.query.function.Function) Expression(io.requery.query.Expression) NamedExpression(io.requery.query.NamedExpression) RowExpression(io.requery.query.RowExpression) NamedExpression(io.requery.query.NamedExpression) QueryAttribute(io.requery.meta.QueryAttribute) Collection(java.util.Collection) Supplier(io.requery.util.function.Supplier)

Example 3 with Function

use of io.requery.query.function.Function in project requery by requery.

the class DefaultOutput method appendFunction.

private void appendFunction(Function function) {
    if (function instanceof Case) {
        appendCaseFunction((Case) function);
    } else {
        Function.Name name = configuration.getMapping().mapFunctionName(function);
        qb.append(name.getName());
        if (function.arguments().length == 0 && name.isConstant()) {
            return;
        }
        qb.openParenthesis();
        int index = 0;
        for (Object arg : function.arguments()) {
            if (index > 0) {
                qb.comma();
            }
            if (arg instanceof Expression) {
                Expression expression = (Expression) arg;
                switch(expression.getExpressionType()) {
                    case ATTRIBUTE:
                        appendColumnForSelect(expression);
                        break;
                    case FUNCTION:
                        Function inner = (Function) arg;
                        appendFunction(inner);
                        break;
                    default:
                        qb.append(expression.getName());
                        break;
                }
            } else if (arg instanceof Class) {
                qb.append("*");
            } else {
                appendConditionValue(function.expressionForArgument(index), arg);
            }
            index++;
        }
        qb.closeParenthesis().space();
    }
}
Also used : Function(io.requery.query.function.Function) Expression(io.requery.query.Expression) NamedExpression(io.requery.query.NamedExpression) RowExpression(io.requery.query.RowExpression) Case(io.requery.query.function.Case)

Aggregations

Function (io.requery.query.function.Function)3 Expression (io.requery.query.Expression)2 NamedExpression (io.requery.query.NamedExpression)2 RowExpression (io.requery.query.RowExpression)2 Attribute (io.requery.meta.Attribute)1 QueryAttribute (io.requery.meta.QueryAttribute)1 Type (io.requery.meta.Type)1 AliasedExpression (io.requery.query.AliasedExpression)1 ExpressionType (io.requery.query.ExpressionType)1 Case (io.requery.query.function.Case)1 Supplier (io.requery.util.function.Supplier)1 Collection (java.util.Collection)1