Search in sources :

Example 1 with QueryWrapper

use of io.requery.query.element.QueryWrapper in project requery by requery.

the class DefaultOutput method appendFromExpression.

private void appendFromExpression(Expression expression) {
    if (expression.getExpressionType() == ExpressionType.QUERY) {
        QueryWrapper wrapper = (QueryWrapper) expression;
        String alias = wrapper.unwrapQuery().getAlias();
        if (alias == null) {
            throw new IllegalStateException("query in 'from' expression must have an alias");
        }
        qb.openParenthesis();
        appendQuery(wrapper);
        qb.closeParenthesis().space();
        qb.append(alias).space();
    } else {
        qb.append(expression.getName());
    }
}
Also used : QueryWrapper(io.requery.query.element.QueryWrapper)

Example 2 with QueryWrapper

use of io.requery.query.element.QueryWrapper in project requery by requery.

the class DefaultOutput method appendOperation.

private void appendOperation(Condition condition, int depth) {
    Object leftOperand = condition.getLeftOperand();
    if (leftOperand instanceof Expression) {
        final Expression<?> expression = (Expression<?>) condition.getLeftOperand();
        appendColumn(expression);
        Object value = condition.getRightOperand();
        appendOperator(condition.getOperator());
        if (value instanceof Collection && (condition.getOperator() == Operator.IN || condition.getOperator() == Operator.NOT_IN)) {
            Collection collection = (Collection) value;
            qb.openParenthesis();
            qb.commaSeparated(collection, new QueryBuilder.Appender() {

                @Override
                public void append(QueryBuilder qb, Object value) {
                    appendConditionValue(expression, value);
                }
            });
            qb.closeParenthesis();
        } else if (value instanceof Object[]) {
            Object[] values = (Object[]) value;
            if (condition.getOperator() == Operator.BETWEEN) {
                Object begin = values[0];
                Object end = values[1];
                appendConditionValue(expression, begin);
                qb.keyword(AND);
                appendConditionValue(expression, end);
            } else {
                for (Object o : values) {
                    appendConditionValue(expression, o);
                }
            }
        } else if (value instanceof QueryWrapper) {
            QueryWrapper wrapper = (QueryWrapper) value;
            qb.openParenthesis();
            appendQuery(wrapper);
            qb.closeParenthesis().space();
        } else if (value instanceof Condition) {
            appendOperation((Condition) value, depth + 1);
        } else if (value != null) {
            appendConditionValue(expression, value);
        }
    } else if (leftOperand instanceof Condition) {
        if (depth > 0) {
            qb.openParenthesis();
        }
        appendOperation((Condition) leftOperand, depth + 1);
        appendOperator(condition.getOperator());
        Object value = condition.getRightOperand();
        if (value instanceof Condition) {
            appendOperation((Condition) value, depth + 1);
        } else {
            throw new IllegalStateException();
        }
        if (depth > 0) {
            qb.closeParenthesis().space();
        }
    } else {
        throw new IllegalStateException("unknown start expression type " + leftOperand);
    }
}
Also used : Condition(io.requery.query.Condition) Expression(io.requery.query.Expression) NamedExpression(io.requery.query.NamedExpression) RowExpression(io.requery.query.RowExpression) QueryWrapper(io.requery.query.element.QueryWrapper) Collection(java.util.Collection) QueryBuilder(io.requery.sql.QueryBuilder)

Aggregations

QueryWrapper (io.requery.query.element.QueryWrapper)2 Condition (io.requery.query.Condition)1 Expression (io.requery.query.Expression)1 NamedExpression (io.requery.query.NamedExpression)1 RowExpression (io.requery.query.RowExpression)1 QueryBuilder (io.requery.sql.QueryBuilder)1 Collection (java.util.Collection)1