Search in sources :

Example 1 with Condition

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

Example 2 with Condition

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

the class DefaultOutput method appendConditional.

@Override
public void appendConditional(LogicalElement element) {
    LogicalOperator op = element.getOperator();
    if (op != null) {
        switch(op) {
            case AND:
                qb.keyword(AND);
                break;
            case OR:
                qb.keyword(OR);
                break;
        }
    }
    Condition condition = element.getCondition();
    boolean nested = condition.getRightOperand() instanceof Condition;
    if (nested) {
        qb.openParenthesis();
    }
    appendOperation(condition, 0);
    if (nested) {
        qb.closeParenthesis().space();
    }
}
Also used : Condition(io.requery.query.Condition) LogicalOperator(io.requery.query.element.LogicalOperator)

Aggregations

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