Search in sources :

Example 11 with Expression

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

the class UpdateGenerator method write.

@Override
public void write(final Output output, Map<Expression<?>, Object> values) {
    QueryBuilder qb = output.builder();
    qb.keyword(UPDATE);
    output.appendTables();
    qb.keyword(SET);
    int index = 0;
    for (Map.Entry<Expression<?>, Object> entry : values.entrySet()) {
        if (index > 0) {
            qb.append(",");
        }
        output.appendColumn(entry.getKey());
        output.appendOperator(Operator.EQUAL);
        output.appendConditionValue(entry.getKey(), entry.getValue());
        index++;
    }
}
Also used : Expression(io.requery.query.Expression) QueryBuilder(io.requery.sql.QueryBuilder) Map(java.util.Map)

Example 12 with Expression

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

the class UpsertMergeGenerator method appendUsing.

protected void appendUsing(final Output writer, final Map<Expression<?>, Object> values) {
    QueryBuilder qb = writer.builder();
    qb.openParenthesis().keyword(VALUES).openParenthesis().commaSeparated(values.keySet(), new QueryBuilder.Appender<Expression>() {

        @Override
        public void append(QueryBuilder qb, Expression expression) {
            qb.append("?");
            writer.parameters().add(expression, values.get(expression));
        }
    }).closeParenthesis().closeParenthesis().space().keyword(AS).append(alias).openParenthesis().commaSeparatedExpressions(values.keySet()).closeParenthesis().space();
}
Also used : Expression(io.requery.query.Expression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 13 with Expression

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

the class UpsertMergeGenerator method write.

@Override
public void write(Output output, Map<Expression<?>, Object> values) {
    QueryBuilder qb = output.builder();
    // TODO only supporting 1 type for now
    Type<?> type = null;
    for (Expression<?> expression : values.keySet()) {
        if (expression.getExpressionType() == ExpressionType.ATTRIBUTE) {
            Attribute attribute = (Attribute) expression;
            type = attribute.getDeclaringType();
            break;
        }
    }
    if (type == null) {
        throw new IllegalStateException();
    }
    qb.keyword(MERGE).keyword(INTO).tableName(type.getName()).keyword(USING);
    appendUsing(output, values);
    qb.keyword(ON).openParenthesis();
    int count = 0;
    Set<? extends Attribute<?, ?>> attributes = type.getKeyAttributes();
    if (attributes.isEmpty()) {
        attributes = type.getAttributes();
    }
    for (Attribute<?, ?> attribute : attributes) {
        if (count > 0) {
            qb.keyword(Keyword.AND);
        }
        qb.aliasAttribute(type.getName(), attribute);
        qb.append(" = ");
        qb.aliasAttribute(alias, attribute);
        count++;
    }
    qb.closeParenthesis().space();
    // update fragment
    LinkedHashSet<Attribute<?, ?>> updates = new LinkedHashSet<>();
    for (Expression<?> expression : values.keySet()) {
        if (expression.getExpressionType() == ExpressionType.ATTRIBUTE) {
            Attribute attribute = (Attribute) expression;
            if (!attribute.isKey()) {
                updates.add(attribute);
            }
        }
    }
    qb.keyword(WHEN, MATCHED, THEN, UPDATE, SET).commaSeparated(updates, new QueryBuilder.Appender<Attribute<?, ?>>() {

        @Override
        public void append(QueryBuilder qb, Attribute<?, ?> value) {
            qb.attribute(value);
            qb.append(" = " + alias + "." + value.getName());
        }
    }).space();
    // insert fragment
    qb.keyword(WHEN, NOT, MATCHED, THEN, INSERT).openParenthesis().commaSeparatedExpressions(values.keySet()).closeParenthesis().space().keyword(VALUES).openParenthesis().commaSeparated(values.keySet(), new QueryBuilder.Appender<Expression<?>>() {

        @Override
        public void append(QueryBuilder qb, Expression<?> value) {
            qb.aliasAttribute(alias, (Attribute) value);
        }
    }).closeParenthesis();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Attribute(io.requery.meta.Attribute) Expression(io.requery.query.Expression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 14 with Expression

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

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