Search in sources :

Example 1 with Expression

use of io.requery.query.Expression in project requery by requery.

the class DefaultOutput method appendColumnExpression.

private void appendColumnExpression(Expression expression) {
    switch(expression.getExpressionType()) {
        case ATTRIBUTE:
            Attribute attribute = (Attribute) expression;
            qb.attribute(attribute);
            break;
        default:
            if (expression instanceof RowExpression) {
                RowExpression collection = (RowExpression) expression;
                qb.openParenthesis();
                qb.commaSeparated(collection.getExpressions(), new QueryBuilder.Appender<Expression<?>>() {

                    @Override
                    public void append(QueryBuilder qb, Expression<?> value) {
                        appendColumnForSelect(value);
                    }
                });
                qb.closeParenthesis().space();
            } else {
                qb.append(expression.getName()).space();
            }
            break;
    }
}
Also used : QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) Expression(io.requery.query.Expression) NamedExpression(io.requery.query.NamedExpression) RowExpression(io.requery.query.RowExpression) RowExpression(io.requery.query.RowExpression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 2 with Expression

use of io.requery.query.Expression 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 Expression

use of io.requery.query.Expression 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)

Example 4 with Expression

use of io.requery.query.Expression in project requery by requery.

the class GroupByGenerator method write.

@Override
public void write(final Output output, GroupByElement query) {
    QueryBuilder qb = output.builder();
    Set<Expression<?>> groupBy = query.getGroupByExpressions();
    if (groupBy != null && groupBy.size() > 0) {
        qb.keyword(GROUP, BY);
        qb.commaSeparated(groupBy, new QueryBuilder.Appender<Expression<?>>() {

            @Override
            public void append(QueryBuilder qb, Expression<?> value) {
                output.appendColumn(value);
            }
        });
        if (query.getHavingElements() != null) {
            qb.keyword(HAVING);
            for (HavingConditionElement<?> clause : query.getHavingElements()) {
                output.appendConditional(clause);
            }
        }
    }
}
Also used : Expression(io.requery.query.Expression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 5 with Expression

use of io.requery.query.Expression in project requery by requery.

the class InsertGenerator method write.

@Override
public void write(final Output output, QueryElement<?> query) {
    Map<Expression<?>, Object> values = query.updateValues();
    InsertType insertType = query.insertType();
    QueryBuilder qb = output.builder();
    qb.keyword(INSERT, INTO);
    output.appendTables();
    if (values.isEmpty()) {
        if (insertType == InsertType.VALUES) {
            qb.keyword(DEFAULT, VALUES);
        }
    } else {
        qb.openParenthesis().commaSeparated(values.entrySet(), new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {

            @Override
            public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
                Expression<?> key = value.getKey();
                switch(key.getExpressionType()) {
                    case ATTRIBUTE:
                        Attribute attribute = (Attribute) key;
                        if (attribute.isGenerated()) {
                            throw new IllegalStateException();
                        }
                        qb.attribute(attribute);
                        break;
                    default:
                        qb.append(key.getName()).space();
                        break;
                }
            }
        }).closeParenthesis().space();
        if (insertType == InsertType.VALUES) {
            qb.keyword(VALUES).openParenthesis().commaSeparated(values.entrySet(), new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {

                @Override
                public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
                    output.appendConditionValue(value.getKey(), value.getValue());
                }
            }).closeParenthesis();
        } else {
            output.appendQuery(query.subQuery());
        }
    }
}
Also used : Attribute(io.requery.meta.Attribute) QueryBuilder(io.requery.sql.QueryBuilder) Expression(io.requery.query.Expression) InsertType(io.requery.query.element.InsertType) Map(java.util.Map)

Aggregations

Expression (io.requery.query.Expression)14 QueryBuilder (io.requery.sql.QueryBuilder)9 Attribute (io.requery.meta.Attribute)6 NamedExpression (io.requery.query.NamedExpression)4 RowExpression (io.requery.query.RowExpression)4 QueryAttribute (io.requery.meta.QueryAttribute)3 Function (io.requery.query.function.Function)2 Collection (java.util.Collection)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 EntityProxy (io.requery.proxy.EntityProxy)1 AliasedExpression (io.requery.query.AliasedExpression)1 Condition (io.requery.query.Condition)1 OrderingExpression (io.requery.query.OrderingExpression)1 Tuple (io.requery.query.Tuple)1 InsertType (io.requery.query.element.InsertType)1 QueryElement (io.requery.query.element.QueryElement)1 QueryWrapper (io.requery.query.element.QueryWrapper)1 Case (io.requery.query.function.Case)1 WHERE (io.requery.sql.Keyword.WHERE)1