cloudroam
2025-03-04 2a117297daf83b2e1a104603e9641226d5beeba3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.example.firstapp
 
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.firstapp.database.dao.CodeDao
import com.example.firstapp.database.dao.KeywordDao
import com.example.firstapp.database.dao.MsgDao
import com.example.firstapp.database.dao.ReminderDao
import com.example.firstapp.database.entity.Code
import com.example.firstapp.database.entity.KeywordEntity
import com.example.firstapp.database.entity.Msg
import com.example.firstapp.database.entity.Reminder
import com.example.firstapp.utils.DATABASE_NAME
import com.example.firstapp.utils.SettingUtils
import com.example.firstapp.utils.TAG_LIST
 
import com.example.firstapp.database.ext.ConvertersDate
import com.example.firstapp.model.CourierStat
import com.example.firstapp.model.DailyStat
 
 
@Database(
    entities = [
        Msg::class, 
        Code::class, 
        KeywordEntity::class, 
        Reminder::class
    ],
    views = [
        CourierStat::class,
        DailyStat::class
    ],
    version = 20,
    exportSchema = false
)
@TypeConverters(ConvertersDate::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun msgDao(): MsgDao
    abstract fun codeDao(): CodeDao
    abstract fun keywordDao(): KeywordDao
    abstract fun reminderDao(): ReminderDao
 
    companion object {
        @Volatile
        private var instance: AppDatabase? = null
 
        fun getInstance(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }
 
        private fun buildDatabase(context: Context): AppDatabase {
            val builder = Room.databaseBuilder(
                context.applicationContext, AppDatabase::class.java, DATABASE_NAME
            ).allowMainThreadQueries() //TODO:允许主线程访问,后面再优化
                .addCallback(object : Callback() {
                    override fun onCreate(db: SupportSQLiteDatabase) {
 
                    }
                }).addMigrations(
 
                    MIGRATION_MSG,
                )
 
            /*if (BuildConfig.DEBUG) {
                builder.setQueryCallback({ sqlQuery, bindArgs ->
                    println("SQL_QUERY: $sqlQuery\nBIND_ARGS: $bindArgs")
                }, Executors.newSingleThreadExecutor())
            }*/
 
            return builder.build()
        }
 
 
 
 
 
        private val MIGRATION_MSG = object : Migration(19, 20) {
            override fun migrate(database: SupportSQLiteDatabase) {
                //database.execSQL("Create table Msg as Select id,type,`from`,content,(case when sim_info like 'SIM1%' then '0' when sim_info like 'SIM2%' then '1' else '-1' end) as sim_slot,sim_info,sub_id,time from Logs where 1 = 1")
                database.execSQL(
                    """
                    CREATE TABLE "Msg" (
                      "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                      "type" TEXT NOT NULL DEFAULT 'sms',
                      "from" TEXT NOT NULL DEFAULT '',
                      "content" TEXT NOT NULL DEFAULT '',
                      "sim_slot" INTEGER NOT NULL DEFAULT -1,
                      "sim_info" TEXT NOT NULL DEFAULT '',
                      "sub_id" INTEGER NOT NULL DEFAULT 0,
                      "time" INTEGER NOT NULL
                    )
                """.trimIndent()
                )
 
                database.execSQL("CREATE UNIQUE INDEX \"index_Msg_id\" ON \"Msg\" ( \"id\" ASC)")
 
                // 新增 KeywordEntity 表的创建逻辑
                database.execSQL("""
                    CREATE TABLE IF NOT EXISTS `keywords` (
                        `id` INTEGER PRIMARY KEY AUTOINCREMENT, 
                        `keyword` TEXT NOT NULL, 
                        `type` TEXT NOT NULL, 
                        `isEnabled` INTEGER NOT NULL
                    )
                """)
 
                // 创建 CourierStat 视图
                database.execSQL("""
                    CREATE VIEW IF NOT EXISTS CourierStat AS
                    SELECT courierName, COUNT(*) as count 
                    FROM packages 
                    GROUP BY courierName
                """)
 
                // 创建 DailyStat 视图
                database.execSQL("""
                    CREATE VIEW IF NOT EXISTS DailyStat AS
                    SELECT date(receivedTime/1000, 'unixepoch', 'localtime') as date, 
                           COUNT(*) as count 
                    FROM packages 
                    GROUP BY date(receivedTime/1000, 'unixepoch', 'localtime')
                """)
//                database.execSQL("""
//                   CREATE TABLE   IF NOT EXISTS `reminders` (
//                    id INTEGER PRIMARY KEY AUTOINCREMENT,
//                    type TEXT NOT NULL,
//                    nickname TEXT NOT NULL,
//                    keywords TEXT NOT NULL,
//                );
//                """)
 
            }
        }
 
    }
 
}