Class AbstractDao<T,​K>

  • Type Parameters:
    T - Entity type
    K - Primary key (PK) type; use Void if entity does not have exactly one PK

    public abstract class AbstractDao<T,​K>
    extends java.lang.Object
    Base class for all DAOs: Implements entity operations like insert, load, delete, and query.

    This class is thread-safe.

    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void assertSinglePk()  
      protected void attachEntity​(K key, T entity, boolean lock)
      Attaches the entity to the identity scope.
      protected void attachEntity​(T entity)
      Sub classes with relations additionally set the DaoMaster here.
      protected abstract void bindValues​(android.database.sqlite.SQLiteStatement stmt, T entity)
      Binds the entity's values to the statement.
      protected abstract void bindValues​(DatabaseStatement stmt, T entity)
      Binds the entity's values to the statement.
      long count()  
      void delete​(T entity)
      Deletes the given entity from the database.
      void deleteAll()  
      void deleteByKey​(K key)
      Deletes an entity with the given PK from the database.
      void deleteByKeyInTx​(java.lang.Iterable<K> keys)
      Deletes all entities with the given keys in the database using a transaction.
      void deleteByKeyInTx​(K... keys)
      Deletes all entities with the given keys in the database using a transaction.
      void deleteInTx​(java.lang.Iterable<T> entities)
      Deletes the given entities in the database using a transaction.
      void deleteInTx​(T... entities)
      Deletes the given entities in the database using a transaction.
      boolean detach​(T entity)
      Detaches an entity from the identity scope (session).
      void detachAll()
      Detaches all entities (of type T) from the identity scope (session).
      java.lang.String[] getAllColumns()  
      Database getDatabase()
      Gets the SQLiteDatabase for custom database access.
      protected abstract K getKey​(T entity)
      Returns the value of the primary key, if the entity has a single primary key, or, if not, null.
      protected K getKeyVerified​(T entity)
      See getKey(Object), but guarantees that the returned key is never null (throws if null).
      java.lang.String[] getNonPkColumns()  
      java.lang.String[] getPkColumns()  
      Property getPkProperty()  
      Property[] getProperties()  
      AbstractDaoSession getSession()  
      java.lang.String getTablename()  
      protected abstract boolean hasKey​(T entity)
      Returns true if the entity is not null, and has a non-null key, which is also != 0.
      long insert​(T entity)
      Insert an entity into the table associated with a concrete DAO.
      void insertInTx​(java.lang.Iterable<T> entities)
      Inserts the given entities in the database using a transaction.
      void insertInTx​(java.lang.Iterable<T> entities, boolean setPrimaryKey)
      Inserts the given entities in the database using a transaction.
      void insertInTx​(T... entities)
      Inserts the given entities in the database using a transaction.
      long insertOrReplace​(T entity)
      Insert an entity into the table associated with a concrete DAO.
      void insertOrReplaceInTx​(java.lang.Iterable<T> entities)
      Inserts or replaces the given entities in the database using a transaction.
      void insertOrReplaceInTx​(java.lang.Iterable<T> entities, boolean setPrimaryKey)
      Inserts or replaces the given entities in the database using a transaction.
      void insertOrReplaceInTx​(T... entities)
      Inserts or replaces the given entities in the database using a transaction.
      long insertWithoutSettingPk​(T entity)
      Insert an entity into the table associated with a concrete DAO without setting key property.
      protected abstract boolean isEntityUpdateable()
      Returns true if the Entity class can be updated, e.g.
      T load​(K key)
      Loads the entity for the given PK.
      java.util.List<T> loadAll()
      Loads all available entities from the database.
      protected java.util.List<T> loadAllAndCloseCursor​(android.database.Cursor cursor)  
      protected java.util.List<T> loadAllFromCursor​(android.database.Cursor cursor)
      Reads all available rows from the given cursor and returns a list of entities.
      T loadByRowId​(long rowId)  
      protected T loadCurrent​(android.database.Cursor cursor, int offset, boolean lock)
      Internal use only.
      protected <O> O loadCurrentOther​(AbstractDao<O,​?> dao, android.database.Cursor cursor, int offset)
      Internal use only.
      protected T loadUnique​(android.database.Cursor cursor)  
      protected T loadUniqueAndCloseCursor​(android.database.Cursor cursor)  
      QueryBuilder<T> queryBuilder()  
      java.util.List<T> queryRaw​(java.lang.String where, java.lang.String... selectionArg)
      A raw-style query where you can pass any WHERE clause and arguments.
      Query<T> queryRawCreate​(java.lang.String where, java.lang.Object... selectionArg)
      Creates a repeatable Query object based on the given raw SQL where you can pass any WHERE clause and arguments.
      Query<T> queryRawCreateListArgs​(java.lang.String where, java.util.Collection<java.lang.Object> selectionArg)
      Creates a repeatable Query object based on the given raw SQL where you can pass any WHERE clause and arguments.
      protected abstract T readEntity​(android.database.Cursor cursor, int offset)
      Reads the values from the current position of the given cursor and returns a new entity.
      protected abstract void readEntity​(android.database.Cursor cursor, T entity, int offset)
      Reads the values from the current position of the given cursor into an existing entity.
      protected abstract K readKey​(android.database.Cursor cursor, int offset)
      Reads the key from the current position of the given cursor, or returns null if there's no single-value key.
      void refresh​(T entity)
      Resets all locally changed properties of the entity by reloading the values from the database.
      RxDao<T,​K> rx()
      The returned RxDao is a special DAO that let's you interact with Rx Observables using RX's IO scheduler for subscribeOn.
      RxDao<T,​K> rxPlain()
      The returned RxDao is a special DAO that let's you interact with Rx Observables without any Scheduler set for subscribeOn.
      void save​(T entity)
      "Saves" an entity to the database: depending on the existence of the key property, it will be inserted (key is null) or updated (key is not null).
      void saveInTx​(java.lang.Iterable<T> entities)
      Saves (see save(Object)) the given entities in the database using a transaction.
      void saveInTx​(T... entities)
      Saves (see save(Object)) the given entities in the database using a transaction.
      void update​(T entity)  
      protected void updateInsideSynchronized​(T entity, android.database.sqlite.SQLiteStatement stmt, boolean lock)  
      protected void updateInsideSynchronized​(T entity, DatabaseStatement stmt, boolean lock)  
      void updateInTx​(java.lang.Iterable<T> entities)
      Updates the given entities in the database using a transaction.
      void updateInTx​(T... entities)
      Updates the given entities in the database using a transaction.
      protected abstract K updateKeyAfterInsert​(T entity, long rowId)
      Updates the entity's key if possible (only for Long PKs currently).
      protected void updateKeyAfterInsertAndAttach​(T entity, long rowId, boolean lock)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getTablename

        public java.lang.String getTablename()
      • getProperties

        public Property[] getProperties()
      • getPkProperty

        public Property getPkProperty()
      • getAllColumns

        public java.lang.String[] getAllColumns()
      • getPkColumns

        public java.lang.String[] getPkColumns()
      • getNonPkColumns

        public java.lang.String[] getNonPkColumns()
      • load

        public T load​(K key)
        Loads the entity for the given PK.
        Parameters:
        key - a PK value or null
        Returns:
        The entity or null, if no entity matched the PK value
      • loadByRowId

        public T loadByRowId​(long rowId)
      • loadUniqueAndCloseCursor

        protected T loadUniqueAndCloseCursor​(android.database.Cursor cursor)
      • loadUnique

        protected T loadUnique​(android.database.Cursor cursor)
      • loadAll

        public java.util.List<T> loadAll()
        Loads all available entities from the database.
      • detach

        public boolean detach​(T entity)
        Detaches an entity from the identity scope (session). Subsequent query results won't return this object.
      • detachAll

        public void detachAll()
        Detaches all entities (of type T) from the identity scope (session). Subsequent query results won't return any previously loaded objects.
      • loadAllAndCloseCursor

        protected java.util.List<T> loadAllAndCloseCursor​(android.database.Cursor cursor)
      • insertInTx

        public void insertInTx​(java.lang.Iterable<T> entities)
        Inserts the given entities in the database using a transaction.
        Parameters:
        entities - The entities to insert.
      • insertInTx

        public void insertInTx​(T... entities)
        Inserts the given entities in the database using a transaction.
        Parameters:
        entities - The entities to insert.
      • insertInTx

        public void insertInTx​(java.lang.Iterable<T> entities,
                               boolean setPrimaryKey)
        Inserts the given entities in the database using a transaction. The given entities will become tracked if the PK is set.
        Parameters:
        entities - The entities to insert.
        setPrimaryKey - if true, the PKs of the given will be set after the insert; pass false to improve performance.
      • insertOrReplaceInTx

        public void insertOrReplaceInTx​(java.lang.Iterable<T> entities,
                                        boolean setPrimaryKey)
        Inserts or replaces the given entities in the database using a transaction. The given entities will become tracked if the PK is set.
        Parameters:
        entities - The entities to insert.
        setPrimaryKey - if true, the PKs of the given will be set after the insert; pass false to improve performance.
      • insertOrReplaceInTx

        public void insertOrReplaceInTx​(java.lang.Iterable<T> entities)
        Inserts or replaces the given entities in the database using a transaction.
        Parameters:
        entities - The entities to insert.
      • insertOrReplaceInTx

        public void insertOrReplaceInTx​(T... entities)
        Inserts or replaces the given entities in the database using a transaction.
        Parameters:
        entities - The entities to insert.
      • insert

        public long insert​(T entity)
        Insert an entity into the table associated with a concrete DAO.
        Returns:
        row ID of newly inserted entity
      • insertWithoutSettingPk

        public long insertWithoutSettingPk​(T entity)
        Insert an entity into the table associated with a concrete DAO without setting key property.

        Warning: This may be faster, but the entity should not be used anymore. The entity also won't be attached to identity scope.

        Returns:
        row ID of newly inserted entity
      • insertOrReplace

        public long insertOrReplace​(T entity)
        Insert an entity into the table associated with a concrete DAO.
        Returns:
        row ID of newly inserted entity
      • updateKeyAfterInsertAndAttach

        protected void updateKeyAfterInsertAndAttach​(T entity,
                                                     long rowId,
                                                     boolean lock)
      • save

        public void save​(T entity)
        "Saves" an entity to the database: depending on the existence of the key property, it will be inserted (key is null) or updated (key is not null).

        This is similar to insertOrReplace(Object), but may be more efficient, because if a key is present, it does not have to query if that key already exists.

      • saveInTx

        public void saveInTx​(T... entities)
        Saves (see save(Object)) the given entities in the database using a transaction.
        Parameters:
        entities - The entities to save.
      • saveInTx

        public void saveInTx​(java.lang.Iterable<T> entities)
        Saves (see save(Object)) the given entities in the database using a transaction.
        Parameters:
        entities - The entities to save.
      • loadAllFromCursor

        protected java.util.List<T> loadAllFromCursor​(android.database.Cursor cursor)
        Reads all available rows from the given cursor and returns a list of entities.
      • loadCurrent

        protected final T loadCurrent​(android.database.Cursor cursor,
                                      int offset,
                                      boolean lock)
        Internal use only. Considers identity scope.
      • loadCurrentOther

        protected final <O> O loadCurrentOther​(AbstractDao<O,​?> dao,
                                               android.database.Cursor cursor,
                                               int offset)
        Internal use only. Considers identity scope.
      • queryRaw

        public java.util.List<T> queryRaw​(java.lang.String where,
                                          java.lang.String... selectionArg)
        A raw-style query where you can pass any WHERE clause and arguments.
      • queryRawCreate

        public Query<T> queryRawCreate​(java.lang.String where,
                                       java.lang.Object... selectionArg)
        Creates a repeatable Query object based on the given raw SQL where you can pass any WHERE clause and arguments.
      • queryRawCreateListArgs

        public Query<T> queryRawCreateListArgs​(java.lang.String where,
                                               java.util.Collection<java.lang.Object> selectionArg)
        Creates a repeatable Query object based on the given raw SQL where you can pass any WHERE clause and arguments.
      • deleteAll

        public void deleteAll()
      • delete

        public void delete​(T entity)
        Deletes the given entity from the database. Currently, only single value PK entities are supported.
      • deleteByKey

        public void deleteByKey​(K key)
        Deletes an entity with the given PK from the database. Currently, only single value PK entities are supported.
      • deleteInTx

        public void deleteInTx​(java.lang.Iterable<T> entities)
        Deletes the given entities in the database using a transaction.
        Parameters:
        entities - The entities to delete.
      • deleteInTx

        public void deleteInTx​(T... entities)
        Deletes the given entities in the database using a transaction.
        Parameters:
        entities - The entities to delete.
      • deleteByKeyInTx

        public void deleteByKeyInTx​(java.lang.Iterable<K> keys)
        Deletes all entities with the given keys in the database using a transaction.
        Parameters:
        keys - Keys of the entities to delete.
      • deleteByKeyInTx

        public void deleteByKeyInTx​(K... keys)
        Deletes all entities with the given keys in the database using a transaction.
        Parameters:
        keys - Keys of the entities to delete.
      • refresh

        public void refresh​(T entity)
        Resets all locally changed properties of the entity by reloading the values from the database.
      • update

        public void update​(T entity)
      • updateInsideSynchronized

        protected void updateInsideSynchronized​(T entity,
                                                DatabaseStatement stmt,
                                                boolean lock)
      • updateInsideSynchronized

        protected void updateInsideSynchronized​(T entity,
                                                android.database.sqlite.SQLiteStatement stmt,
                                                boolean lock)
      • attachEntity

        protected final void attachEntity​(K key,
                                          T entity,
                                          boolean lock)
        Attaches the entity to the identity scope. Calls attachEntity(T entity).
        Parameters:
        key - Needed only for identity scope, pass null if there's none.
        entity - The entitiy to attach
      • attachEntity

        protected void attachEntity​(T entity)
        Sub classes with relations additionally set the DaoMaster here. Must be called before the entity is attached to the identity scope.
        Parameters:
        entity - The entitiy to attach
      • updateInTx

        public void updateInTx​(java.lang.Iterable<T> entities)
        Updates the given entities in the database using a transaction.
        Parameters:
        entities - The entities to insert.
      • updateInTx

        public void updateInTx​(T... entities)
        Updates the given entities in the database using a transaction.
        Parameters:
        entities - The entities to update.
      • assertSinglePk

        protected void assertSinglePk()
      • count

        public long count()
      • getKeyVerified

        protected K getKeyVerified​(T entity)
        See getKey(Object), but guarantees that the returned key is never null (throws if null).
      • rxPlain

        @Experimental
        public RxDao<T,​K> rxPlain()
        The returned RxDao is a special DAO that let's you interact with Rx Observables without any Scheduler set for subscribeOn.
        See Also:
        rx()
      • rx

        @Experimental
        public RxDao<T,​K> rx()
        The returned RxDao is a special DAO that let's you interact with Rx Observables using RX's IO scheduler for subscribeOn.
        See Also:
        rxPlain()
      • getDatabase

        public Database getDatabase()
        Gets the SQLiteDatabase for custom database access. Not needed for greenDAO entities.
      • readEntity

        protected abstract T readEntity​(android.database.Cursor cursor,
                                        int offset)
        Reads the values from the current position of the given cursor and returns a new entity.
      • readKey

        protected abstract K readKey​(android.database.Cursor cursor,
                                     int offset)
        Reads the key from the current position of the given cursor, or returns null if there's no single-value key.
      • readEntity

        protected abstract void readEntity​(android.database.Cursor cursor,
                                           T entity,
                                           int offset)
        Reads the values from the current position of the given cursor into an existing entity.
      • bindValues

        protected abstract void bindValues​(DatabaseStatement stmt,
                                           T entity)
        Binds the entity's values to the statement. Make sure to synchronize the statement outside of the method.
      • bindValues

        protected abstract void bindValues​(android.database.sqlite.SQLiteStatement stmt,
                                           T entity)
        Binds the entity's values to the statement. Make sure to synchronize the enclosing DatabaseStatement outside of the method.
      • updateKeyAfterInsert

        protected abstract K updateKeyAfterInsert​(T entity,
                                                  long rowId)
        Updates the entity's key if possible (only for Long PKs currently). This method must always return the entity's key regardless of whether the key existed before or not.
      • getKey

        protected abstract K getKey​(T entity)
        Returns the value of the primary key, if the entity has a single primary key, or, if not, null. Returns null if entity is null.
      • hasKey

        protected abstract boolean hasKey​(T entity)
        Returns true if the entity is not null, and has a non-null key, which is also != 0. entity is null.
      • isEntityUpdateable

        protected abstract boolean isEntityUpdateable()
        Returns true if the Entity class can be updated, e.g. for setting the PK after insert.