|  |  |  | 
|---|
|  |  |  | import com.example.firstapp.database.entity.KeywordEntity | 
|---|
|  |  |  | import com.example.firstapp.database.entity.Msg | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | import com.example.firstapp.database.dao.ReminderDao | 
|---|
|  |  |  | import com.example.firstapp.database.dao.ReminderRecordDao | 
|---|
|  |  |  | import com.example.firstapp.database.entity.Reminder | 
|---|
|  |  |  | import com.example.firstapp.database.entity.ReminderRecord | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Database( | 
|---|
|  |  |  | entities = [ Msg::class, Code::class, KeywordEntity::class], | 
|---|
|  |  |  | //    views = [LogsDetail::class], | 
|---|
|  |  |  | version = 20, | 
|---|
|  |  |  | entities = [ | 
|---|
|  |  |  | Msg::class, | 
|---|
|  |  |  | Code::class, | 
|---|
|  |  |  | KeywordEntity::class, | 
|---|
|  |  |  | Reminder::class, | 
|---|
|  |  |  | ReminderRecord::class | 
|---|
|  |  |  | ], | 
|---|
|  |  |  | views = [ | 
|---|
|  |  |  | CourierStat::class, | 
|---|
|  |  |  | DailyStat::class | 
|---|
|  |  |  | ], | 
|---|
|  |  |  | version = 21, | 
|---|
|  |  |  | exportSchema = false | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | @TypeConverters(ConvertersDate::class) | 
|---|
|  |  |  | 
|---|
|  |  |  | abstract fun msgDao(): MsgDao | 
|---|
|  |  |  | abstract fun codeDao(): CodeDao | 
|---|
|  |  |  | abstract fun keywordDao(): KeywordDao | 
|---|
|  |  |  | abstract fun reminderDao(): ReminderDao | 
|---|
|  |  |  | abstract fun reminderRecordDao(): ReminderRecordDao | 
|---|
|  |  |  |  | 
|---|
|  |  |  | companion object { | 
|---|
|  |  |  | @Volatile | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }).addMigrations( | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MIGRATION_MSG, | 
|---|
|  |  |  | MIGRATION_20_21 | 
|---|
|  |  |  | ) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /*if (BuildConfig.DEBUG) { | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return builder.build() | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | """) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 创建 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, | 
|---|
|  |  |  | //                ); | 
|---|
|  |  |  | //                """) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private val MIGRATION_20_21 = object : Migration(20, 21) { | 
|---|
|  |  |  | override fun migrate(database: SupportSQLiteDatabase) { | 
|---|
|  |  |  | // 创建 reminders 表 | 
|---|
|  |  |  | database.execSQL( | 
|---|
|  |  |  | """ | 
|---|
|  |  |  | CREATE TABLE IF NOT EXISTS reminders ( | 
|---|
|  |  |  | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | 
|---|
|  |  |  | categoryId INTEGER NOT NULL, | 
|---|
|  |  |  | categoryName TEXT NOT NULL, | 
|---|
|  |  |  | notificationMethod TEXT NOT NULL, | 
|---|
|  |  |  | isEnabled INTEGER NOT NULL DEFAULT 1, | 
|---|
|  |  |  | createdAt INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000) | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | """ | 
|---|
|  |  |  | ) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 创建 reminder_records 表 | 
|---|
|  |  |  | database.execSQL( | 
|---|
|  |  |  | """ | 
|---|
|  |  |  | CREATE TABLE IF NOT EXISTS reminder_records ( | 
|---|
|  |  |  | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | 
|---|
|  |  |  | categoryId INTEGER NOT NULL, | 
|---|
|  |  |  | categoryName TEXT NOT NULL, | 
|---|
|  |  |  | content TEXT NOT NULL, | 
|---|
|  |  |  | notificationMethod TEXT NOT NULL, | 
|---|
|  |  |  | status INTEGER NOT NULL DEFAULT 0, | 
|---|
|  |  |  | createdAt INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000) | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | """ | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|