2019-07-28 10:44:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2024-05-02 08:26:33 +00:00
|
|
|
package androidx.room
|
2019-07-28 10:44:45 +00:00
|
|
|
|
2024-05-02 08:26:33 +00:00
|
|
|
import androidx.annotation.RestrictTo
|
|
|
|
import androidx.sqlite.db.SupportSQLiteStatement
|
2019-07-28 10:44:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementations of this class knows how to delete or update a particular entity.
|
2024-05-02 08:26:33 +00:00
|
|
|
*
|
2019-07-28 10:44:45 +00:00
|
|
|
* This is an internal library class and all of its implementations are auto-generated.
|
|
|
|
*
|
2024-05-02 08:26:33 +00:00
|
|
|
* @constructor Creates a DeletionOrUpdateAdapter that can delete or update the entity type T on the
|
|
|
|
* given database.
|
|
|
|
*
|
|
|
|
* @param T The type parameter of the entity to be deleted
|
|
|
|
*/
|
2019-07-28 10:44:45 +00:00
|
|
|
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
|
2024-05-02 08:26:33 +00:00
|
|
|
abstract class EntityDeletionOrUpdateAdapter<T> (
|
|
|
|
database: RoomDatabase
|
|
|
|
) : SharedSQLiteStatement(database) {
|
2019-07-28 10:44:45 +00:00
|
|
|
/**
|
|
|
|
* Create the deletion or update query
|
|
|
|
*
|
|
|
|
* @return An SQL query that can delete or update instances of T.
|
|
|
|
*/
|
2024-05-02 08:26:33 +00:00
|
|
|
abstract override fun createQuery(): String
|
2019-07-28 10:44:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds the entity into the given statement.
|
|
|
|
*
|
|
|
|
* @param statement The SQLite statement that prepared for the query returned from
|
2024-05-02 08:26:33 +00:00
|
|
|
* createQuery.
|
2019-07-28 10:44:45 +00:00
|
|
|
* @param entity The entity of type T.
|
|
|
|
*/
|
2024-05-02 08:26:33 +00:00
|
|
|
protected abstract fun bind(statement: SupportSQLiteStatement, entity: T)
|
2019-07-28 10:44:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes or updates the given entities in the database and returns the affected row count.
|
|
|
|
*
|
|
|
|
* @param entity The entity to delete or update
|
|
|
|
* @return The number of affected rows
|
|
|
|
*/
|
2024-05-02 08:26:33 +00:00
|
|
|
fun handle(entity: T): Int {
|
|
|
|
val stmt: SupportSQLiteStatement = acquire()
|
|
|
|
return try {
|
|
|
|
bind(stmt, entity)
|
|
|
|
stmt.executeUpdateDelete()
|
2019-07-28 10:44:45 +00:00
|
|
|
} finally {
|
2024-05-02 08:26:33 +00:00
|
|
|
release(stmt)
|
2019-07-28 10:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes or updates the given entities in the database and returns the affected row count.
|
|
|
|
*
|
|
|
|
* @param entities Entities to delete or update
|
|
|
|
* @return The number of affected rows
|
|
|
|
*/
|
2024-05-02 08:26:33 +00:00
|
|
|
fun handleMultiple(entities: Iterable<T>): Int {
|
|
|
|
val stmt: SupportSQLiteStatement = acquire()
|
|
|
|
return try {
|
|
|
|
var total = 0
|
|
|
|
entities.forEach { entity ->
|
|
|
|
bind(stmt, entity)
|
|
|
|
total += stmt.executeUpdateDelete()
|
2019-07-28 10:44:45 +00:00
|
|
|
}
|
2024-05-02 08:26:33 +00:00
|
|
|
total
|
2019-07-28 10:44:45 +00:00
|
|
|
} finally {
|
2024-05-02 08:26:33 +00:00
|
|
|
release(stmt)
|
2019-07-28 10:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes or updates the given entities in the database and returns the affected row count.
|
|
|
|
*
|
|
|
|
* @param entities Entities to delete or update
|
|
|
|
* @return The number of affected rows
|
|
|
|
*/
|
2024-05-02 08:26:33 +00:00
|
|
|
fun handleMultiple(entities: Array<out T>): Int {
|
|
|
|
val stmt: SupportSQLiteStatement = acquire()
|
|
|
|
return try {
|
|
|
|
var total = 0
|
|
|
|
entities.forEach { entity ->
|
|
|
|
bind(stmt, entity)
|
|
|
|
total += stmt.executeUpdateDelete()
|
2019-07-28 10:44:45 +00:00
|
|
|
}
|
2024-05-02 08:26:33 +00:00
|
|
|
total
|
2019-07-28 10:44:45 +00:00
|
|
|
} finally {
|
2024-05-02 08:26:33 +00:00
|
|
|
release(stmt)
|
2019-07-28 10:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|