Search in sources :

Example 11 with QueryBuilder

use of io.requery.sql.QueryBuilder 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 QueryBuilder

use of io.requery.sql.QueryBuilder 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 QueryBuilder

use of io.requery.sql.QueryBuilder 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 QueryBuilder

use of io.requery.sql.QueryBuilder in project requery by requery.

the class WhereGenerator method write.

@Override
public void write(Output output, WhereElement query) {
    QueryBuilder qb = output.builder();
    ExistsElement<?> whereExists = query.getWhereExistsElement();
    if (whereExists != null) {
        qb.keyword(WHERE);
        if (whereExists.isNotExists()) {
            qb.keyword(NOT);
        }
        qb.keyword(EXISTS);
        qb.openParenthesis();
        Supplier<?> wrapper = whereExists.getQuery();
        output.appendQuery((QueryWrapper) wrapper);
        qb.closeParenthesis().space();
    } else if (query.getWhereElements() != null && query.getWhereElements().size() > 0) {
        qb.keyword(WHERE);
        for (WhereConditionElement<?> w : query.getWhereElements()) {
            output.appendConditional(w);
        }
    }
}
Also used : WhereConditionElement(io.requery.query.element.WhereConditionElement) QueryBuilder(io.requery.sql.QueryBuilder)

Example 15 with QueryBuilder

use of io.requery.sql.QueryBuilder 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

QueryBuilder (io.requery.sql.QueryBuilder)15 Expression (io.requery.query.Expression)9 Attribute (io.requery.meta.Attribute)3 Map (java.util.Map)3 NamedExpression (io.requery.query.NamedExpression)2 RowExpression (io.requery.query.RowExpression)2 Cursor (android.database.Cursor)1 QueryAttribute (io.requery.meta.QueryAttribute)1 Condition (io.requery.query.Condition)1 OrderingExpression (io.requery.query.OrderingExpression)1 InsertType (io.requery.query.element.InsertType)1 QueryWrapper (io.requery.query.element.QueryWrapper)1 WhereConditionElement (io.requery.query.element.WhereConditionElement)1 ResultSet (java.sql.ResultSet)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1