Package org.greenrobot.greendao
Class AbstractDao<T,K>
- java.lang.Object
-
- org.greenrobot.greendao.AbstractDao<T,K>
-
- Type Parameters:
T- Entity typeK- Primary key (PK) type; use Void if entity does not have exactly one PK
public abstract class AbstractDao<T,K> extends java.lang.ObjectBase class for all DAOs: Implements entity operations like insert, load, delete, and query.This class is thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description protected DaoConfigconfigprotected Databasedbprotected IdentityScope<K,T>identityScopeprotected IdentityScopeLong<T>identityScopeLongprotected booleanisStandardSQLiteprotected intpkOrdinalprotected AbstractDaoSessionsessionprotected TableStatementsstatements
-
Constructor Summary
Constructors Constructor Description AbstractDao(DaoConfig config)AbstractDao(DaoConfig config, AbstractDaoSession daoSession)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidassertSinglePk()protected voidattachEntity(K key, T entity, boolean lock)Attaches the entity to the identity scope.protected voidattachEntity(T entity)Sub classes with relations additionally set the DaoMaster here.protected abstract voidbindValues(android.database.sqlite.SQLiteStatement stmt, T entity)Binds the entity's values to the statement.protected abstract voidbindValues(DatabaseStatement stmt, T entity)Binds the entity's values to the statement.longcount()voiddelete(T entity)Deletes the given entity from the database.voiddeleteAll()voiddeleteByKey(K key)Deletes an entity with the given PK from the database.voiddeleteByKeyInTx(java.lang.Iterable<K> keys)Deletes all entities with the given keys in the database using a transaction.voiddeleteByKeyInTx(K... keys)Deletes all entities with the given keys in the database using a transaction.voiddeleteInTx(java.lang.Iterable<T> entities)Deletes the given entities in the database using a transaction.voiddeleteInTx(T... entities)Deletes the given entities in the database using a transaction.booleandetach(T entity)Detaches an entity from the identity scope (session).voiddetachAll()Detaches all entities (of type T) from the identity scope (session).java.lang.String[]getAllColumns()DatabasegetDatabase()Gets the SQLiteDatabase for custom database access.protected abstract KgetKey(T entity)Returns the value of the primary key, if the entity has a single primary key, or, if not, null.protected KgetKeyVerified(T entity)SeegetKey(Object), but guarantees that the returned key is never null (throws if null).java.lang.String[]getNonPkColumns()java.lang.String[]getPkColumns()PropertygetPkProperty()Property[]getProperties()AbstractDaoSessiongetSession()java.lang.StringgetTablename()protected abstract booleanhasKey(T entity)Returns true if the entity is not null, and has a non-null key, which is also != 0.longinsert(T entity)Insert an entity into the table associated with a concrete DAO.voidinsertInTx(java.lang.Iterable<T> entities)Inserts the given entities in the database using a transaction.voidinsertInTx(java.lang.Iterable<T> entities, boolean setPrimaryKey)Inserts the given entities in the database using a transaction.voidinsertInTx(T... entities)Inserts the given entities in the database using a transaction.longinsertOrReplace(T entity)Insert an entity into the table associated with a concrete DAO.voidinsertOrReplaceInTx(java.lang.Iterable<T> entities)Inserts or replaces the given entities in the database using a transaction.voidinsertOrReplaceInTx(java.lang.Iterable<T> entities, boolean setPrimaryKey)Inserts or replaces the given entities in the database using a transaction.voidinsertOrReplaceInTx(T... entities)Inserts or replaces the given entities in the database using a transaction.longinsertWithoutSettingPk(T entity)Insert an entity into the table associated with a concrete DAO without setting key property.protected abstract booleanisEntityUpdateable()Returns true if the Entity class can be updated, e.g.Tload(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.TloadByRowId(long rowId)protected TloadCurrent(android.database.Cursor cursor, int offset, boolean lock)Internal use only.protected <O> OloadCurrentOther(AbstractDao<O,?> dao, android.database.Cursor cursor, int offset)Internal use only.protected TloadUnique(android.database.Cursor cursor)protected TloadUniqueAndCloseCursor(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 repeatableQueryobject 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 repeatableQueryobject based on the given raw SQL where you can pass any WHERE clause and arguments.protected abstract TreadEntity(android.database.Cursor cursor, int offset)Reads the values from the current position of the given cursor and returns a new entity.protected abstract voidreadEntity(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 KreadKey(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.voidrefresh(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.voidsave(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).voidsaveInTx(java.lang.Iterable<T> entities)Saves (seesave(Object)) the given entities in the database using a transaction.voidsaveInTx(T... entities)Saves (seesave(Object)) the given entities in the database using a transaction.voidupdate(T entity)protected voidupdateInsideSynchronized(T entity, android.database.sqlite.SQLiteStatement stmt, boolean lock)protected voidupdateInsideSynchronized(T entity, DatabaseStatement stmt, boolean lock)voidupdateInTx(java.lang.Iterable<T> entities)Updates the given entities in the database using a transaction.voidupdateInTx(T... entities)Updates the given entities in the database using a transaction.protected abstract KupdateKeyAfterInsert(T entity, long rowId)Updates the entity's key if possible (only for Long PKs currently).protected voidupdateKeyAfterInsertAndAttach(T entity, long rowId, boolean lock)
-
-
-
Field Detail
-
config
protected final DaoConfig config
-
db
protected final Database db
-
isStandardSQLite
protected final boolean isStandardSQLite
-
identityScope
protected final IdentityScope<K,T> identityScope
-
identityScopeLong
protected final IdentityScopeLong<T> identityScopeLong
-
statements
protected final TableStatements statements
-
session
protected final AbstractDaoSession session
-
pkOrdinal
protected final int pkOrdinal
-
-
Constructor Detail
-
AbstractDao
public AbstractDao(DaoConfig config)
-
AbstractDao
public AbstractDao(DaoConfig config, AbstractDaoSession daoSession)
-
-
Method Detail
-
getSession
public AbstractDaoSession getSession()
-
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 (seesave(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 (seesave(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 repeatableQueryobject 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 repeatableQueryobject 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)
-
queryBuilder
public QueryBuilder<T> queryBuilder()
-
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)
SeegetKey(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.
-
-