Class QueryBuilder<T>

  • Type Parameters:
    T - Entity class to create an query for.

    public class QueryBuilder<T>
    extends java.lang.Object
    Builds custom entity queries using constraints and parameters and without SQL (QueryBuilder creates SQL for you). To acquire an QueryBuilder, use AbstractDao.queryBuilder() or AbstractDaoSession.queryBuilder(Class). Entity properties are referenced by Fields in the "Properties" inner class of the generated DAOs. This approach allows compile time checks and prevents typo errors occuring at build time.

    Example: Query for all users with the first name "Joe" ordered by their last name. (The class Properties is an inner class of UserDao and should be imported before.)
    List joes = dao.queryBuilder().where(Properties.FirstName.eq("Joe")).orderAsc(Properties.LastName).list();
    • Field Detail

      • LOG_SQL

        public static boolean LOG_SQL
        Set to true to debug the SQL.
      • LOG_VALUES

        public static boolean LOG_VALUES
        Set to see the given values.
    • Constructor Detail

      • QueryBuilder

        protected QueryBuilder​(AbstractDao<T,​?> dao)
      • QueryBuilder

        protected QueryBuilder​(AbstractDao<T,​?> dao,
                               java.lang.String tablePrefix)
    • Method Detail

      • internalCreate

        public static <T2> QueryBuilder<T2> internalCreate​(AbstractDao<T2,​?> dao)
        For internal use by greenDAO only.
      • distinct

        public QueryBuilder<T> distinct()
        Use a SELECT DISTINCT to avoid duplicate entities returned, e.g. when doing joins.
      • where

        public QueryBuilder<T> where​(WhereCondition cond,
                                     WhereCondition... condMore)
        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.
      • join

        public <J> Join<T,​J> join​(java.lang.Class<J> destinationEntityClass,
                                        Property destinationProperty)
        Expands the query to another entity type by using a JOIN. The primary key property of the primary entity for this QueryBuilder is used to match the given destinationProperty.
      • join

        public <J> Join<T,​J> join​(Property sourceProperty,
                                        java.lang.Class<J> destinationEntityClass)
        Expands the query to another entity type by using a JOIN. The given sourceProperty is used to match the primary key property of the given destinationEntity.
      • join

        public <J> Join<T,​J> join​(Property sourceProperty,
                                        java.lang.Class<J> destinationEntityClass,
                                        Property destinationProperty)
        Expands the query to another entity type by using a JOIN. The given sourceProperty is used to match the given destinationProperty of the given destinationEntity.
      • join

        public <J> Join<T,​J> join​(Join<?,​T> sourceJoin,
                                        Property sourceProperty,
                                        java.lang.Class<J> destinationEntityClass,
                                        Property destinationProperty)
        Expands the query to another entity type by using a JOIN. The given sourceJoin's property is used to match the given destinationProperty of the given destinationEntity. Note that destination entity of the given join is used as the source for the new join to add. In this way, it is possible to compose complex "join of joins" across several entities if required.
      • orderAsc

        public QueryBuilder<T> orderAsc​(Property... properties)
        Adds the given properties to the ORDER BY section using ascending order.
      • orderDesc

        public QueryBuilder<T> orderDesc​(Property... properties)
        Adds the given properties to the ORDER BY section using descending order.
      • orderCustom

        public QueryBuilder<T> orderCustom​(Property property,
                                           java.lang.String customOrderForProperty)
        Adds the given properties to the ORDER BY section using the given custom order.
      • orderRaw

        public QueryBuilder<T> orderRaw​(java.lang.String rawOrder)
        Adds the given raw SQL string to the ORDER BY section. Do not use this for standard properties: orderAsc and orderDesc are preferred.
      • append

        protected java.lang.StringBuilder append​(java.lang.StringBuilder builder,
                                                 Property property)
      • limit

        public QueryBuilder<T> limit​(int limit)
        Limits the number of results returned by queries.
      • offset

        public QueryBuilder<T> offset​(int offset)
        Sets the offset for query results in combination with limit(int). The first offset results are skipped and the total number of results will be limited by limit. You cannot use offset without limit.
      • build

        public Query<T> build()
        Builds a reusable query object (Query objects can be executed more efficiently than creating a QueryBuilder for each execution.
      • buildCursor

        public CursorQuery buildCursor()
        Builds a reusable query object for low level android.database.Cursor access. (Query objects can be executed more efficiently than creating a QueryBuilder for each execution.
      • buildDelete

        public DeleteQuery<T> buildDelete()
        Builds a reusable query object for deletion (Query objects can be executed more efficiently than creating a QueryBuilder for each execution.
      • buildCount

        public CountQuery<T> buildCount()
        Builds a reusable query object for counting rows (Query objects can be executed more efficiently than creating a QueryBuilder for each execution.
      • list

        public java.util.List<T> list()
        Shorthand for build().list(); see Query.list() for details. To execute a query more than once, you should build the query and keep the Query object for efficiency reasons.
      • unique

        public T unique()
        Shorthand for build().unique(); see Query.unique() for details. To execute a query more than once, you should build the query and keep the Query object for efficiency reasons.