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 com.android.launcher3.provider;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.Binder;
23 import android.os.Process;
24 
25 import com.android.launcher3.LauncherSettings.Favorites;
26 import com.android.launcher3.pm.UserCache;
27 import com.android.launcher3.util.IntArray;
28 
29 /**
30  * A set of utility methods for Launcher DB used for DB updates and migration.
31  */
32 public class LauncherDbUtils {
33 
queryIntArray(boolean distinct, SQLiteDatabase db, String tableName, String columnName, String selection, String groupBy, String orderBy)34     public static IntArray queryIntArray(boolean distinct, SQLiteDatabase db, String tableName,
35             String columnName, String selection, String groupBy, String orderBy) {
36         IntArray out = new IntArray();
37         try (Cursor c = db.query(distinct, tableName, new String[] { columnName }, selection, null,
38                 groupBy, null, orderBy, null)) {
39             while (c.moveToNext()) {
40                 out.add(c.getInt(0));
41             }
42         }
43         return out;
44     }
45 
tableExists(SQLiteDatabase db, String tableName)46     public static boolean tableExists(SQLiteDatabase db, String tableName) {
47         try (Cursor c = db.query(true, "sqlite_master", new String[] {"tbl_name"},
48                 "tbl_name = ?", new String[] {tableName},
49                 null, null, null, null, null)) {
50             return c.getCount() > 0;
51         }
52     }
53 
dropTable(SQLiteDatabase db, String tableName)54     public static void dropTable(SQLiteDatabase db, String tableName) {
55         db.execSQL("DROP TABLE IF EXISTS " + tableName);
56     }
57 
58     /** Copy fromTable in fromDb to toTable in toDb. */
copyTable(SQLiteDatabase fromDb, String fromTable, SQLiteDatabase toDb, String toTable, Context context)59     public static void copyTable(SQLiteDatabase fromDb, String fromTable, SQLiteDatabase toDb,
60             String toTable, Context context) {
61         long userSerial = UserCache.INSTANCE.get(context).getSerialNumberForUser(
62                 Process.myUserHandle());
63         dropTable(toDb, toTable);
64         Favorites.addTableToDb(toDb, userSerial, false, toTable);
65         if (fromDb != toDb) {
66             toDb.execSQL("ATTACH DATABASE '" + fromDb.getPath() + "' AS from_db");
67             toDb.execSQL(
68                     "INSERT INTO " + toTable + " SELECT * FROM from_db." + fromTable);
69             toDb.execSQL("DETACH DATABASE 'from_db'");
70         } else {
71             toDb.execSQL("INSERT INTO " + toTable + " SELECT * FROM " + fromTable);
72         }
73     }
74 
75     /**
76      * Utility class to simplify managing sqlite transactions
77      */
78     public static class SQLiteTransaction extends Binder implements AutoCloseable {
79         private final SQLiteDatabase mDb;
80 
SQLiteTransaction(SQLiteDatabase db)81         public SQLiteTransaction(SQLiteDatabase db) {
82             mDb = db;
83             db.beginTransaction();
84         }
85 
commit()86         public void commit() {
87             mDb.setTransactionSuccessful();
88         }
89 
90         @Override
close()91         public void close() {
92             mDb.endTransaction();
93         }
94 
getDb()95         public SQLiteDatabase getDb() {
96             return mDb;
97         }
98     }
99 }
100