1 /*
<lambda>null2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package androidx.room
17 
18 import androidx.annotation.RestrictTo
19 import androidx.room.util.getTotalChangedRows
20 import androidx.sqlite.SQLiteConnection
21 import androidx.sqlite.SQLiteStatement
22 
23 /**
24  * Implementations of this class know how to delete or update a particular entity.
25  *
26  * This is an library class and all of its implementations are auto-generated.
27  *
28  * @param T The type parameter of the entity to be deleted
29  * @constructor Creates a DeletionOrUpdateAdapter that can delete or update the entity type T on the
30  *   given database.
31  */
32 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) // used in generated code
33 abstract class EntityDeleteOrUpdateAdapter<T> {
34     /**
35      * Create the deletion or update query
36      *
37      * @return An SQL query that can delete or update instances of T.
38      */
39     protected abstract fun createQuery(): String
40 
41     /**
42      * Binds the entity into the given statement.
43      *
44      * @param statement The SQLite statement that prepared for the query returned from createQuery.
45      * @param entity The entity of type T.
46      */
47     protected abstract fun bind(statement: SQLiteStatement, entity: T)
48 
49     /**
50      * Deletes or updates the given entities in the database and returns the affected row count.
51      *
52      * @param entity The entity to delete or update
53      * @return The number of affected rows
54      */
55     fun handle(connection: SQLiteConnection, entity: T?): Int {
56         if (entity == null) return 0
57         connection.prepare(createQuery()).use { stmt ->
58             bind(stmt, entity)
59             stmt.step()
60         }
61         return getTotalChangedRows(connection)
62     }
63 
64     /**
65      * Deletes or updates the given entities in the database and returns the affected row count.
66      *
67      * @param entities Entities to delete or update
68      * @return The number of affected rows
69      */
70     fun handleMultiple(connection: SQLiteConnection, entities: Iterable<T?>?): Int {
71         if (entities == null) return 0
72         var total = 0
73         connection.prepare(createQuery()).use { stmt ->
74             for (entity in entities) {
75                 if (entity == null) continue
76                 bind(stmt, entity)
77                 stmt.step()
78                 stmt.reset()
79                 total += getTotalChangedRows(connection)
80             }
81         }
82         return total
83     }
84 
85     /**
86      * Deletes or updates the given entities in the database and returns the affected row count.
87      *
88      * @param entities Entities to delete or update
89      * @return The number of affected rows
90      */
91     fun handleMultiple(connection: SQLiteConnection, entities: Array<out T?>?): Int {
92         if (entities == null) return 0
93         var total = 0
94         connection.prepare(createQuery()).use { stmt ->
95             for (entity in entities) {
96                 if (entity == null) continue
97                 bind(stmt, entity)
98                 stmt.step()
99                 stmt.reset()
100                 total += getTotalChangedRows(connection)
101             }
102         }
103         return total
104     }
105 }
106