8 Dao methods
Marcel van der Heide edited this page 2023-01-16 13:21:41 +01:00

Dao methods

In RoomEx Dao's come with many default methods to work with Room and relationships.
The methods displayed in this article are available when using ICrud<T> or any of its subinterfaces on your DaoEx class. These methods are also available with Dao's generated with the @Autodao as these classes inherit the ICrud<T> interface. Without the ICrud<T> interface, or any of its subinterfaces, you still get the same methods, just with a slightly different name.

...WithChildren methods

Methods ending on "WithChildren" are used to not only process the parent entity, but also the relation between the parent and its children. Using the recursively argument children, and children of these children, will also be processed together with its relations.

Creating data

These methods are used to insert new records in the database.

suspend fun insert(entity: T): Long
suspend fun insertWithChildren(entity: T, recursively: Boolean = false): Long
suspend fun insertWithChildren(entity: T, recursively: Boolean = false, vararg fields: String): Long

Arguments

Name Type Default Description
entity T (RoomExEntity) - The entity to insert in the database.
recursively Boolean false If its children, and children of these children, should also be inserted and processed.
fields vararg String - Defines which relation fields should be processed. If no field is given, every relation property will be processed.

Reading data

These methods are used to fetch records from the database.

suspend fun exists(id: Long): Boolean
suspend fun getById(id: Long): T?
suspend fun getAll(): MutableList<T>
suspend fun getByIdWithChildren(id: Long, recursively: Boolean = false, vararg fields: String) : T?

Arguments

Name Type Default Description
id Long - The id of the entity to read from the database.
recursively Boolean false If its children, and children of these children, should also be fetched from the database.
fields vararg String - Defines which relation fields should be processed. If no field is given, every relation property will be processed.



suspend fun loadChildren(entity: T, recursively: Boolean = false) : T
suspend fun loadChildren(entity: T, recursively: Boolean = false, vararg fields : String) : T

Arguments

Name Type Default Description
entity T (RoomExEntity) - The entity of which its children should be loaded/fetched from the database.
recursively Boolean false If its children, and children of these children, should also be fetched from the database.
fields vararg String - Defines which relation fields should be processed. If no field is given, every relation property will be processed.

Updating data

These methods are used to update existing records in the database. Children with the insertCascade flag set on its relation will be inserted.

suspend fun update(entity: T)
suspend fun updateWithChildren(entity: T, removeIfNull: Boolean = false, removeFromList: Boolean = true, recursively: Boolean = false)
suspend fun updateWithChildren(entity: T, removeIfNull: Boolean = false, removeFromList: Boolean = true, recursively: Boolean = false, vararg fields: String)

Arguments

Name Type Default Description
entity T (RoomExEntity) - The entity to update in the database.
recursively Boolean false If its children, and children of these children, should also be updated and processed.
fields vararg String - Defines which relation fields should be processed. If no field is given, every relation property will be processed.

Deleting data

These methods are used to delete existing records in the database.

suspend fun delete(entity: T): Int
suspend fun delete(id: Long): Int
suspend fun deleteWithChildren(entity: T, recursively: Boolean = false, vararg fields : String): Int
suspend fun deleteChildren(entity: T, recursively: Boolean = false, clearFromEntity: Boolean = false, vararg fields : String)

Arguments

Name Type Default Description
entity T (RoomExEntity) - The entity to delete from the database.
recursively Boolean false If its children, and children of these children, should also be inserted and processed.
clearFromEntity Boolean false Defines if the deleted children should be removed from entity instance.
fields vararg String - Defines which relation fields should be processed. If no field is given, every relation property will be processed.

Manual joining

These methods are used to manually add or remove joins between 2 entities.

suspend fun addJoin(entity: T, child: RoomExEntity, fieldName: String)
suspend fun addJoins(entity: T, children: List<RoomExEntity>, fieldName: String)

Arguments

Name Type Default Description
entity T (RoomExEntity) - The parent entity of the new join.
child RoomExEntity - The child entity of the new join.
children List<RoomExEntity> - The child entities of the new joins.
fields vararg String - Defines to which property the new join belongs.



suspend fun removeJoin(entity: T, child: RoomExEntity, fieldName: String)
suspend fun removeJoins(entity: T, children: List<RoomExEntity>, fieldName: String)
suspend fun removeJoinsIfNull(entity: T, vararg fields : String)

Arguments

Name Type Default Description
entity T (RoomExEntity) - The parent entity of join to remove.
child RoomExEntity - The child entity of the join to remove.
children List<RoomExEntity> - The child entities of the joins to remove.
fields vararg String - Defines to which property the existing join belongs.