All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.greenrobot.daocompat.query.QueryBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2011-2017 Markus Junginger, greenrobot (http://greenrobot.org)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.greenrobot.daocompat.query;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.Property;
import org.greenrobot.daocompat.AbstractDao;

import io.objectbox.query.PropertyQueryCondition;
import io.objectbox.query.PropertyQueryConditionImpl;
import io.objectbox.query.QueryBuilder.StringOrder;

public class QueryBuilder {

    public static class Condition {
        public final PropertyQueryConditionImpl queryCondition;
        public final StringOrder stringOrder;
        public Condition(PropertyQueryCondition queryCondition, StringOrder stringOrder) {
            this.queryCondition = (PropertyQueryConditionImpl) queryCondition;
            this.stringOrder = stringOrder;
        }
    }

    public static class Order {
        public final Property property;
        public final int orderFlag;
        public Order(Property property, int orderFlag) {
            this.property = property;
            this.orderFlag = orderFlag;
        }
        public Order(Property property) {
            this(property, 0);
        }
    }

    private final AbstractDao dao;

    private final List conditions;
    private final List orders;

    private StringOrder stringOrderCollation;
    private Integer limit;
    private Integer offset;

    /** For internal use by greenDAO only. */
    public static  QueryBuilder internalCreate(AbstractDao dao) {
        return new QueryBuilder<>(dao);
    }

    protected QueryBuilder(AbstractDao dao) {
        this.dao = dao;
        this.stringOrderCollation = StringOrder.CASE_INSENSITIVE;
        this.conditions = new ArrayList<>();
        this.orders = new ArrayList<>();
    }

    /**
     * @deprecated This no longer has any effect. Use one of the property condition overrides
     * to supply a string order, e.g. {@code property.eq(value, StringOrder.CASE_INSENSITIVE)}.
     */
    @Deprecated
    public QueryBuilder stringOrderCollation(StringOrder stringOrderCollation) {
        this.stringOrderCollation = stringOrderCollation;
        return this;
    }

    /**
     * Adds the given conditions to the where clause using an logical AND. To create new conditions, use the properties
     * given in the generated dao classes.
     */
    @SuppressWarnings("rawtypes") // Using raw type to avoid warnings in user query code.
    public QueryBuilder where(PropertyQueryCondition condition, PropertyQueryCondition... more) {
        conditions.add(new Condition(condition, stringOrderCollation));
        for (PropertyQueryCondition queryCondition : more) {
            conditions.add(new Condition(queryCondition, stringOrderCollation));
        }
        return this;
    }

    /** Adds the given properties to the ORDER BY section using ascending order. */
    @SuppressWarnings("rawtypes") // Using raw type to avoid warnings in user query code.
    public QueryBuilder orderAsc(Property... properties) {
        for (Property property : properties) {
            orders.add(new Order(property));
        }
        return this;
    }

    /** Adds the given properties to the ORDER BY section using descending order. */
    @SuppressWarnings("rawtypes") // Using raw type to avoid warnings in user query code.
    public QueryBuilder orderDesc(Property... properties) {
        for (Property property : properties) {
            orders.add(new Order(property, io.objectbox.query.QueryBuilder.DESCENDING));
        }
        return this;
    }

    /**
     * Limits the number of results returned by queries.
     */
    public QueryBuilder limit(int limit) {
        this.limit = limit;
        return this;
    }

    /**
     * Sets the offset for query results in combination with {@link #limit(int)}. The first {@code limit} results are
     * skipped and the total number of results will be limited by {@code limit}. You cannot use offset without limit.
     */
    public QueryBuilder offset(int offset) {
        this.offset = offset;
        return this;
    }

    /**
     * Builds a reusable query object (Query objects can be executed more efficiently than creating
     * a QueryBuilder for each execution.
     */
    public Query build() {
        Condition[] conditions = this.conditions.toArray(new Condition[0]);
        Order[] orders = this.orders.toArray(new Order[0]);
        return Query.create(dao, conditions, orders, limit, offset);
    }

    /**
     * Shorthand for {@link QueryBuilder#build() build()}.{@link Query#list() list()}; see {@link Query#list()} for
     * details. To execute a query more than once, you should build the query and keep the {@link Query} object for
     * efficiency reasons.
     */
    public List list() {
        return build().list();
    }

    /**
     * Shorthand for {@link QueryBuilder#build() build()}.{@link Query#unique() unique()}; see {@link Query#unique()}
     * for details. To execute a query more than once, you should build the query and keep the {@link Query} object for
     * efficiency reasons.
     */
    public T unique() {
        return build().unique();
    }

    /**
     * Shorthand for {@link QueryBuilder#build() build()}.{@link Query#uniqueOrThrow() uniqueOrThrow()}; see
     * {@link Query#uniqueOrThrow()} for details. To execute a query more than once, you should build the query and
     * keep
     * the {@link Query} object for efficiency reasons.
     */
    public T uniqueOrThrow() {
        return build().uniqueOrThrow();
    }

    /**
     * Shorthand for {@link QueryBuilder#build() build()}.{@link Query#count() count()}; see
     * {@link Query#uniqueOrThrow()} for details. To execute a query more than once, you should build the query and
     * keep
     * the {@link Query} object for efficiency reasons.
     */
    public long count() {
        return build().count();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy