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.getLastInsertedRowId 20 import androidx.sqlite.SQLiteConnection 21 import androidx.sqlite.SQLiteStatement 22 23 /** 24 * Implementations of this class knows how to insert a particular entity. 25 * 26 * This is a library class and all of its implementations are auto-generated. 27 * 28 * @param T The type parameter of the entity to be inserted 29 * @constructor Creates an InsertionAdapter that can insert the entity type T into the given 30 * database. 31 */ 32 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) // used in generated code 33 abstract class EntityInsertAdapter<T> { 34 /** 35 * Create the query. 36 * 37 * @return The SQL query to prepare. 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 45 * createInsertQuery. 46 * @param entity The entity of type T. 47 */ 48 protected abstract fun bind(statement: SQLiteStatement, entity: T) 49 50 /** 51 * Inserts the entity into the database. 52 * 53 * @param entity The entity to insert 54 */ 55 fun insert(connection: SQLiteConnection, entity: T?) { 56 if (entity == null) return 57 connection.prepare(createQuery()).use { stmt -> 58 bind(stmt, entity) 59 stmt.step() 60 } 61 } 62 63 /** 64 * Inserts the given entities into the database. 65 * 66 * @param entities Entities to insert 67 */ 68 fun insert(connection: SQLiteConnection, entities: Array<out T?>?) { 69 if (entities == null) return 70 connection.prepare(createQuery()).use { stmt -> 71 for (entity in entities) { 72 if (entity == null) continue 73 bind(stmt, entity) 74 stmt.step() 75 stmt.reset() 76 } 77 } 78 } 79 80 /** 81 * Inserts the given entities into the database. 82 * 83 * @param entities Entities to insert 84 */ 85 fun insert(connection: SQLiteConnection, entities: Iterable<T?>?) { 86 if (entities == null) return 87 connection.prepare(createQuery()).use { stmt -> 88 for (entity in entities) { 89 if (entity == null) continue 90 bind(stmt, entity) 91 stmt.step() 92 stmt.reset() 93 } 94 } 95 } 96 97 /** 98 * Inserts the given entity into the database and returns the row id. 99 * 100 * @param entity The entity to insert 101 * @return The SQLite row id or -1 if no row is inserted 102 */ 103 fun insertAndReturnId(connection: SQLiteConnection, entity: T?): Long { 104 if (entity == null) return -1 105 connection.prepare(createQuery()).use { stmt -> 106 bind(stmt, entity) 107 stmt.step() 108 } 109 return getLastInsertedRowId(connection) 110 } 111 112 /** 113 * Inserts the given entities into the database and returns the row ids. 114 * 115 * @param entities Entities to insert 116 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 117 */ 118 fun insertAndReturnIdsArray( 119 connection: SQLiteConnection, 120 entities: Collection<T?>? 121 ): LongArray { 122 if (entities == null) return longArrayOf() 123 return connection.prepare(createQuery()).use { stmt -> 124 LongArray(entities.size) { index -> 125 val entity = entities.elementAt(index) 126 if (entity != null) { 127 bind(stmt, entity) 128 stmt.step() 129 stmt.reset() 130 getLastInsertedRowId(connection) 131 } else { 132 -1 133 } 134 } 135 } 136 } 137 138 /** 139 * Inserts the given entities into the database and returns the row ids. 140 * 141 * @param entities Entities to insert 142 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 143 */ 144 fun insertAndReturnIdsArray(connection: SQLiteConnection, entities: Array<out T?>?): LongArray { 145 if (entities == null) return longArrayOf() 146 return connection.prepare(createQuery()).use { stmt -> 147 LongArray(entities.size) { index -> 148 val entity = entities.elementAt(index) 149 if (entity != null) { 150 bind(stmt, entity) 151 stmt.step() 152 stmt.reset() 153 getLastInsertedRowId(connection) 154 } else { 155 -1 156 } 157 } 158 } 159 } 160 161 /** 162 * Inserts the given entities into the database and returns the row ids. 163 * 164 * @param entities Entities to insert 165 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 166 */ 167 fun insertAndReturnIdsArrayBox( 168 connection: SQLiteConnection, 169 entities: Collection<T?>? 170 ): Array<out Long> { 171 if (entities == null) return arrayOf() 172 return connection.prepare(createQuery()).use { stmt -> 173 Array(entities.size) { index -> 174 val entity = entities.elementAt(index) 175 if (entity != null) { 176 bind(stmt, entity) 177 stmt.step() 178 stmt.reset() 179 getLastInsertedRowId(connection) 180 } else { 181 -1 182 } 183 } 184 } 185 } 186 187 /** 188 * Inserts the given entities into the database and returns the row ids. 189 * 190 * @param entities Entities to insert 191 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 192 */ 193 fun insertAndReturnIdsArrayBox( 194 connection: SQLiteConnection, 195 entities: Array<out T?>? 196 ): Array<out Long> { 197 if (entities == null) return arrayOf() 198 return connection.prepare(createQuery()).use { stmt -> 199 Array(entities.size) { index -> 200 val entity = entities.elementAt(index) 201 if (entity != null) { 202 bind(stmt, entity) 203 stmt.step() 204 stmt.reset() 205 getLastInsertedRowId(connection) 206 } else { 207 -1 208 } 209 } 210 } 211 } 212 213 /** 214 * Inserts the given entities into the database and returns the row ids. 215 * 216 * @param entities Entities to insert 217 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 218 */ 219 fun insertAndReturnIdsList(connection: SQLiteConnection, entities: Array<out T?>?): List<Long> { 220 if (entities == null) return emptyList() 221 return buildList { 222 connection.prepare(createQuery()).use { stmt -> 223 entities.forEach { entity -> 224 if (entity != null) { 225 bind(stmt, entity) 226 stmt.step() 227 stmt.reset() 228 add(getLastInsertedRowId(connection)) 229 } else { 230 add(-1) 231 } 232 } 233 } 234 } 235 } 236 237 /** 238 * Inserts the given entities into the database and returns the row ids. 239 * 240 * @param entities Entities to insert 241 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 242 */ 243 fun insertAndReturnIdsList( 244 connection: SQLiteConnection, 245 entities: Collection<T?>? 246 ): List<Long> { 247 if (entities == null) return emptyList() 248 return buildList { 249 connection.prepare(createQuery()).use { stmt -> 250 entities.forEach { entity -> 251 if (entity != null) { 252 bind(stmt, entity) 253 stmt.step() 254 stmt.reset() 255 add(getLastInsertedRowId(connection)) 256 } else { 257 add(-1) 258 } 259 } 260 } 261 } 262 } 263 } 264