1/*
2 * Copyright (C) 2016 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
17package androidx.room;
18
19import androidx.annotation.RestrictTo;
20import androidx.sqlite.db.SupportSQLiteStatement;
21
22/**
23 * Implementations of this class knows how to delete or update a particular entity.
24 * <p>
25 * This is an internal library class and all of its implementations are auto-generated.
26 *
27 * @param <T> The type parameter of the entity to be deleted
28 * @hide
29 */
30@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
31@SuppressWarnings({"WeakerAccess", "unused"})
32public abstract class EntityDeletionOrUpdateAdapter<T> extends SharedSQLiteStatement {
33    /**
34     * Creates a DeletionOrUpdateAdapter that can delete or update the entity type T on the given
35     * database.
36     *
37     * @param database The database to delete / update the item in.
38     */
39    public EntityDeletionOrUpdateAdapter(RoomDatabase database) {
40        super(database);
41    }
42
43    /**
44     * Create the deletion or update query
45     *
46     * @return An SQL query that can delete or update instances of T.
47     */
48    @Override
49    protected abstract String createQuery();
50
51    /**
52     * Binds the entity into the given statement.
53     *
54     * @param statement The SQLite statement that prepared for the query returned from
55     *                  createQuery.
56     * @param entity    The entity of type T.
57     */
58    protected abstract void bind(SupportSQLiteStatement statement, T entity);
59
60    /**
61     * Deletes or updates the given entities in the database and returns the affected row count.
62     *
63     * @param entity The entity to delete or update
64     * @return The number of affected rows
65     */
66    public final int handle(T entity) {
67        final SupportSQLiteStatement stmt = acquire();
68        try {
69            bind(stmt, entity);
70            return stmt.executeUpdateDelete();
71        } finally {
72            release(stmt);
73        }
74    }
75
76    /**
77     * Deletes or updates the given entities in the database and returns the affected row count.
78     *
79     * @param entities Entities to delete or update
80     * @return The number of affected rows
81     */
82    public final int handleMultiple(Iterable<T> entities) {
83        final SupportSQLiteStatement stmt = acquire();
84        try {
85            int total = 0;
86            for (T entity : entities) {
87                bind(stmt, entity);
88                total += stmt.executeUpdateDelete();
89            }
90            return total;
91        } finally {
92            release(stmt);
93        }
94    }
95
96    /**
97     * Deletes or updates the given entities in the database and returns the affected row count.
98     *
99     * @param entities Entities to delete or update
100     * @return The number of affected rows
101     */
102    public final int handleMultiple(T[] entities) {
103        final SupportSQLiteStatement stmt = acquire();
104        try {
105            int total = 0;
106            for (T entity : entities) {
107                bind(stmt, entity);
108                total += stmt.executeUpdateDelete();
109            }
110            return total;
111        } finally {
112            release(stmt);
113        }
114    }
115}
116