1 /* <lambda>null2 * 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 package androidx.room 17 18 import androidx.annotation.RestrictTo 19 import androidx.sqlite.db.SupportSQLiteStatement 20 21 /** 22 * Implementations of this class knows how to insert a particular entity. 23 * 24 * This is an internal library class and all of its implementations are auto-generated. 25 * 26 * @param T The type parameter of the entity to be inserted 27 * @constructor Creates an InsertionAdapter that can insert the entity type T into the given 28 * database. 29 */ 30 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) // used in generated code 31 @Deprecated("No longer used by generated code.", ReplaceWith("EntityInsertAdapter")) 32 abstract class EntityInsertionAdapter<T>(database: RoomDatabase) : SharedSQLiteStatement(database) { 33 /** 34 * Binds the entity into the given statement. 35 * 36 * @param statement The SQLite statement that prepared for the query returned from 37 * createInsertQuery. 38 * @param entity The entity of type T. 39 */ 40 protected abstract fun bind(statement: SupportSQLiteStatement, entity: T) 41 42 /** 43 * Inserts the entity into the database. 44 * 45 * @param entity The entity to insert 46 */ 47 fun insert(entity: T) { 48 val stmt: SupportSQLiteStatement = acquire() 49 try { 50 bind(stmt, entity) 51 stmt.executeInsert() 52 } finally { 53 release(stmt) 54 } 55 } 56 57 /** 58 * Inserts the given entities into the database. 59 * 60 * @param entities Entities to insert 61 */ 62 fun insert(entities: Array<out T>) { 63 val stmt: SupportSQLiteStatement = acquire() 64 try { 65 entities.forEach { entity -> 66 bind(stmt, entity) 67 stmt.executeInsert() 68 } 69 } finally { 70 release(stmt) 71 } 72 } 73 74 /** 75 * Inserts the given entities into the database. 76 * 77 * @param entities Entities to insert 78 */ 79 fun insert(entities: Iterable<T>) { 80 val stmt: SupportSQLiteStatement = acquire() 81 try { 82 entities.forEach { entity -> 83 bind(stmt, entity) 84 stmt.executeInsert() 85 } 86 } finally { 87 release(stmt) 88 } 89 } 90 91 /** 92 * Inserts the given entity into the database and returns the row id. 93 * 94 * @param entity The entity to insert 95 * @return The SQLite row id or -1 if no row is inserted 96 */ 97 fun insertAndReturnId(entity: T): Long { 98 val stmt: SupportSQLiteStatement = acquire() 99 return try { 100 bind(stmt, entity) 101 stmt.executeInsert() 102 } finally { 103 release(stmt) 104 } 105 } 106 107 /** 108 * Inserts the given entities into the database and returns the row ids. 109 * 110 * @param entities Entities to insert 111 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 112 */ 113 fun insertAndReturnIdsArray(entities: Collection<T>): LongArray { 114 val stmt: SupportSQLiteStatement = acquire() 115 return try { 116 val result = LongArray(entities.size) 117 entities.forEachIndexed { index, entity -> 118 bind(stmt, entity) 119 result[index] = stmt.executeInsert() 120 } 121 result 122 } finally { 123 release(stmt) 124 } 125 } 126 127 /** 128 * Inserts the given entities into the database and returns the row ids. 129 * 130 * @param entities Entities to insert 131 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 132 */ 133 fun insertAndReturnIdsArray(entities: Array<out T>): LongArray { 134 val stmt: SupportSQLiteStatement = acquire() 135 return try { 136 val result = LongArray(entities.size) 137 entities.forEachIndexed { index, entity -> 138 bind(stmt, entity) 139 result[index] = stmt.executeInsert() 140 } 141 result 142 } finally { 143 release(stmt) 144 } 145 } 146 147 /** 148 * Inserts the given entities into the database and returns the row ids. 149 * 150 * @param entities Entities to insert 151 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 152 */ 153 fun insertAndReturnIdsArrayBox(entities: Collection<T>): Array<out Long> { 154 val stmt: SupportSQLiteStatement = acquire() 155 val iterator = entities.iterator() 156 return try { 157 val result = 158 Array(entities.size) { 159 val entity = iterator.next() 160 bind(stmt, entity) 161 stmt.executeInsert() 162 } 163 result 164 } finally { 165 release(stmt) 166 } 167 } 168 169 /** 170 * Inserts the given entities into the database and returns the row ids. 171 * 172 * @param entities Entities to insert 173 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 174 */ 175 fun insertAndReturnIdsArrayBox(entities: Array<out T>): Array<out Long> { 176 val stmt: SupportSQLiteStatement = acquire() 177 val iterator = entities.iterator() 178 return try { 179 val result = 180 Array(entities.size) { 181 val entity = iterator.next() 182 bind(stmt, entity) 183 stmt.executeInsert() 184 } 185 result 186 } finally { 187 release(stmt) 188 } 189 } 190 191 /** 192 * Inserts the given entities into the database and returns the row ids. 193 * 194 * @param entities Entities to insert 195 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 196 */ 197 fun insertAndReturnIdsList(entities: Array<out T>): List<Long> { 198 val stmt: SupportSQLiteStatement = acquire() 199 return try { 200 buildList { 201 entities.forEach { entity -> 202 bind(stmt, entity) 203 add(stmt.executeInsert()) 204 } 205 } 206 } finally { 207 release(stmt) 208 } 209 } 210 211 /** 212 * Inserts the given entities into the database and returns the row ids. 213 * 214 * @param entities Entities to insert 215 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1 216 */ 217 fun insertAndReturnIdsList(entities: Collection<T>): List<Long> { 218 val stmt: SupportSQLiteStatement = acquire() 219 return try { 220 buildList { 221 entities.forEach { entity -> 222 bind(stmt, entity) 223 add(stmt.executeInsert()) 224 } 225 } 226 } finally { 227 release(stmt) 228 } 229 } 230 } 231