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