FreeRDP
Loading...
Searching...
No Matches
HistoryDatabase.java
1/* Room database for connection history storage */
2
3package com.freerdp.freerdpcore.data;
4
5import android.content.Context;
6
7import androidx.annotation.NonNull;
8import androidx.room.Database;
9import androidx.room.Room;
10import androidx.room.RoomDatabase;
11import androidx.room.migration.Migration;
12import androidx.sqlite.db.SupportSQLiteDatabase;
13
14@Database(entities = { HistoryEntity.class }, version = 2, exportSchema = false)
15public abstract class HistoryDatabase extends RoomDatabase
16{
17 private static final String DB_NAME = "history.db";
18
19 private static volatile HistoryDatabase instance;
20
21 public abstract HistoryDao historyDao();
22
23 public static HistoryDatabase getInstance(Context context)
24 {
25 if (instance == null)
26 {
27 synchronized (HistoryDatabase.class)
28 {
29 if (instance == null)
30 {
31 instance = Room.databaseBuilder(context.getApplicationContext(),
32 HistoryDatabase.class, DB_NAME)
33 .addMigrations(MIGRATION_1_2)
34 .fallbackToDestructiveMigration()
35 .build();
36 }
37 }
38 }
39 return instance;
40 }
41
42 // v1: item TEXT PRIMARY KEY, timestamp INTEGER (both nullable — old SQLiteOpenHelper schema)
43 // v2: item TEXT NOT NULL PRIMARY KEY, timestamp INTEGER NOT NULL (Room schema)
44 private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
45 @Override public void migrate(@NonNull SupportSQLiteDatabase db)
46 {
47 db.execSQL("CREATE TABLE IF NOT EXISTS `quick_connect_history_new` ("
48 + "`item` TEXT NOT NULL, "
49 + "`timestamp` INTEGER NOT NULL DEFAULT 0, "
50 + "PRIMARY KEY(`item`))");
51 db.execSQL("INSERT INTO `quick_connect_history_new` (item, timestamp) "
52 + "SELECT item, IFNULL(timestamp, 0) FROM `quick_connect_history`");
53 db.execSQL("DROP TABLE `quick_connect_history`");
54 db.execSQL("ALTER TABLE `quick_connect_history_new` RENAME TO `quick_connect_history`");
55 }
56 };
57}