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 package androidx.room;
     17 
     18 import androidx.annotation.RestrictTo;
     19 import androidx.sqlite.db.SupportSQLiteStatement;
     20 
     21 import java.util.concurrent.atomic.AtomicBoolean;
     22 
     23 /**
     24  * Represents a prepared SQLite state that can be re-used multiple times.
     25  * <p>
     26  * This class is used by generated code. After it is used, {@code release} must be called so that
     27  * it can be used by other threads.
     28  * <p>
     29  * To avoid re-entry even within the same thread, this class allows only 1 time access to the shared
     30  * statement until it is released.
     31  *
     32  * @hide
     33  */
     34 @SuppressWarnings({"WeakerAccess", "unused"})
     35 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
     36 public abstract class SharedSQLiteStatement {
     37     private final AtomicBoolean mLock = new AtomicBoolean(false);
     38 
     39     private final RoomDatabase mDatabase;
     40     private volatile SupportSQLiteStatement mStmt;
     41 
     42     /**
     43      * Creates an SQLite prepared statement that can be re-used across threads. If it is in use,
     44      * it automatically creates a new one.
     45      *
     46      * @param database The database to create the statement in.
     47      */
     48     public SharedSQLiteStatement(RoomDatabase database) {
     49         mDatabase = database;
     50     }
     51 
     52     /**
     53      * Create the query.
     54      *
     55      * @return The SQL query to prepare.
     56      */
     57     protected abstract String createQuery();
     58 
     59     protected void assertNotMainThread() {
     60         mDatabase.assertNotMainThread();
     61     }
     62 
     63     private SupportSQLiteStatement createNewStatement() {
     64         String query = createQuery();
     65         return mDatabase.compileStatement(query);
     66     }
     67 
     68     private SupportSQLiteStatement getStmt(boolean canUseCached) {
     69         final SupportSQLiteStatement stmt;
     70         if (canUseCached) {
     71             if (mStmt == null) {
     72                 mStmt = createNewStatement();
     73             }
     74             stmt = mStmt;
     75         } else {
     76             // it is in use, create a one off statement
     77             stmt = createNewStatement();
     78         }
     79         return stmt;
     80     }
     81 
     82     /**
     83      * Call this to get the statement. Must call {@link #release(SupportSQLiteStatement)} once done.
     84      */
     85     public SupportSQLiteStatement acquire() {
     86         assertNotMainThread();
     87         return getStmt(mLock.compareAndSet(false, true));
     88     }
     89 
     90     /**
     91      * Must call this when statement will not be used anymore.
     92      *
     93      * @param statement The statement that was returned from acquire.
     94      */
     95     public void release(SupportSQLiteStatement statement) {
     96         if (statement == mStmt) {
     97             mLock.set(false);
     98         }
     99     }
    100 }
    101