T
- Entity typeK
- Primary key (PK) type; use Void if entity does not have exactly one PKpublic abstract class AbstractDao<T,K>
extends java.lang.Object
This class is thread-safe.
Modifier and Type | Field and Description |
---|---|
protected DaoConfig |
config |
protected Database |
db |
protected IdentityScope<K,T> |
identityScope |
protected IdentityScopeLong<T> |
identityScopeLong |
protected boolean |
isStandardSQLite |
protected int |
pkOrdinal |
protected AbstractDaoSession |
session |
protected TableStatements |
statements |
Constructor and Description |
---|
AbstractDao(DaoConfig config) |
AbstractDao(DaoConfig config,
AbstractDaoSession daoSession) |
Modifier and Type | Method and 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(DatabaseStatement stmt,
T entity)
Binds the entity's values to the statement.
|
protected abstract void |
bindValues(android.database.sqlite.SQLiteStatement 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 !
|
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. for setting the PK after insert.
|
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,
DatabaseStatement stmt,
boolean lock) |
protected void |
updateInsideSynchronized(T entity,
android.database.sqlite.SQLiteStatement 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) |
protected final DaoConfig config
protected final Database db
protected final boolean isStandardSQLite
protected final IdentityScope<K,T> identityScope
protected final IdentityScopeLong<T> identityScopeLong
protected final TableStatements statements
protected final AbstractDaoSession session
protected final int pkOrdinal
public AbstractDao(DaoConfig config)
public AbstractDao(DaoConfig config, AbstractDaoSession daoSession)
public AbstractDaoSession getSession()
public java.lang.String getTablename()
public Property[] getProperties()
public Property getPkProperty()
public java.lang.String[] getAllColumns()
public java.lang.String[] getPkColumns()
public java.lang.String[] getNonPkColumns()
public T load(K key)
key
- a PK value or nullpublic T loadByRowId(long rowId)
protected T loadUniqueAndCloseCursor(android.database.Cursor cursor)
protected T loadUnique(android.database.Cursor cursor)
public java.util.List<T> loadAll()
public boolean detach(T entity)
public void detachAll()
protected java.util.List<T> loadAllAndCloseCursor(android.database.Cursor cursor)
public void insertInTx(java.lang.Iterable<T> entities)
entities
- The entities to insert.public void insertInTx(T... entities)
entities
- The entities to insert.public void insertInTx(java.lang.Iterable<T> entities, boolean setPrimaryKey)
entities
- The entities to insert.setPrimaryKey
- if true, the PKs of the given will be set after the insert; pass false to improve
performance.public void insertOrReplaceInTx(java.lang.Iterable<T> entities, boolean setPrimaryKey)
entities
- The entities to insert.setPrimaryKey
- if true, the PKs of the given will be set after the insert; pass false to improve
performance.public void insertOrReplaceInTx(java.lang.Iterable<T> entities)
entities
- The entities to insert.public void insertOrReplaceInTx(T... entities)
entities
- The entities to insert.public long insert(T entity)
public long insertWithoutSettingPk(T entity)
Warning: This may be faster, but the entity should not be used anymore. The entity also won't be attached to identity scope.
public long insertOrReplace(T entity)
protected void updateKeyAfterInsertAndAttach(T entity, long rowId, boolean lock)
public void save(T entity)
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.
public void saveInTx(T... entities)
save(Object)
) the given entities in the database using a transaction.entities
- The entities to save.public void saveInTx(java.lang.Iterable<T> entities)
save(Object)
) the given entities in the database using a transaction.entities
- The entities to save.protected java.util.List<T> loadAllFromCursor(android.database.Cursor cursor)
protected final T loadCurrent(android.database.Cursor cursor, int offset, boolean lock)
protected final <O> O loadCurrentOther(AbstractDao<O,?> dao, android.database.Cursor cursor, int offset)
public java.util.List<T> queryRaw(java.lang.String where, java.lang.String... selectionArg)
public Query<T> queryRawCreate(java.lang.String where, java.lang.Object... selectionArg)
Query
object based on the given raw SQL where you can pass any WHERE clause and
arguments.public Query<T> queryRawCreateListArgs(java.lang.String where, java.util.Collection<java.lang.Object> selectionArg)
Query
object based on the given raw SQL where you can pass any WHERE clause and
arguments.public void deleteAll()
public void delete(T entity)
public void deleteByKey(K key)
public void deleteInTx(java.lang.Iterable<T> entities)
entities
- The entities to delete.public void deleteInTx(T... entities)
entities
- The entities to delete.public void deleteByKeyInTx(java.lang.Iterable<K> keys)
keys
- Keys of the entities to delete.public void deleteByKeyInTx(K... keys)
keys
- Keys of the entities to delete.public void refresh(T entity)
public void update(T entity)
public QueryBuilder<T> queryBuilder()
protected void updateInsideSynchronized(T entity, DatabaseStatement stmt, boolean lock)
protected void updateInsideSynchronized(T entity, android.database.sqlite.SQLiteStatement stmt, boolean lock)
protected final void attachEntity(K key, T entity, boolean lock)
key
- Needed only for identity scope, pass null if there's none.entity
- The entitiy to attachprotected void attachEntity(T entity)
entity
- The entitiy to attachpublic void updateInTx(java.lang.Iterable<T> entities)
entities
- The entities to insert.public void updateInTx(T... entities)
entities
- The entities to update.protected void assertSinglePk()
public long count()
protected K getKeyVerified(T entity)
getKey(Object)
, but guarantees that the returned key is never null (throws if null).@Experimental public RxDao<T,K> rxPlain()
rx()
@Experimental public RxDao<T,K> rx()
rxPlain()
public Database getDatabase()
protected abstract T readEntity(android.database.Cursor cursor, int offset)
protected abstract K readKey(android.database.Cursor cursor, int offset)
protected abstract void readEntity(android.database.Cursor cursor, T entity, int offset)
protected abstract void bindValues(DatabaseStatement stmt, T entity)
protected abstract void bindValues(android.database.sqlite.SQLiteStatement stmt, T entity)
protected abstract K updateKeyAfterInsert(T entity, long rowId)
protected abstract K getKey(T entity)
protected abstract boolean hasKey(T entity)
protected abstract boolean isEntityUpdateable()
Available under the Apache License, Version 2.0 - Copyright © 2011-2016 greenrobot.org. All Rights Reserved.