AppDatabase.kt 2.83 KB
Newer Older
1
2
3
4
5
package com.frostnerd.smokescreen.database

import androidx.room.Database
import androidx.room.RoomDatabase
import com.frostnerd.smokescreen.database.dao.CachedResponseDao
6
import com.frostnerd.smokescreen.database.dao.DnsQueryDao
7
import com.frostnerd.smokescreen.database.dao.DnsRuleDao
8
import com.frostnerd.smokescreen.database.dao.HostSourceDao
9
import com.frostnerd.smokescreen.database.entities.CachedResponse
10
import com.frostnerd.smokescreen.database.entities.DnsQuery
11
import com.frostnerd.smokescreen.database.entities.DnsRule
12
import com.frostnerd.smokescreen.database.entities.HostSource
13
import com.frostnerd.smokescreen.database.repository.CachedResponseRepository
14
import com.frostnerd.smokescreen.database.repository.DnsQueryRepository
15
import com.frostnerd.smokescreen.database.repository.DnsRuleRepository
Daniel Wolf's avatar
Daniel Wolf committed
16
import com.frostnerd.smokescreen.database.repository.HostSourceRepository
17

Daniel Wolf's avatar
Daniel Wolf committed
18
19
/*
 * Copyright (C) 2019 Daniel Wolf (Ch4t4r)
20
 *
Daniel Wolf's avatar
Daniel Wolf committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact the developer at daniel.wolf@frostnerd.com.
35
36
 */

37
@Database(entities = [CachedResponse::class, DnsQuery::class, DnsRule::class, HostSource::class], version = AppDatabase.currentVersion)
38
abstract class AppDatabase : RoomDatabase() {
39
    companion object {
Daniel Wolf's avatar
Daniel Wolf committed
40
        const val currentVersion:Int = 12
41
42
    }

43
    abstract fun cachedResponseDao(): CachedResponseDao
44
    abstract fun dnsQueryDao():DnsQueryDao
45
    abstract fun dnsRuleDao():DnsRuleDao
46
    abstract fun hostSourceDao():HostSourceDao
47
48

    fun cachedResponseRepository() = CachedResponseRepository(cachedResponseDao())
49
    fun dnsQueryRepository() = DnsQueryRepository(dnsQueryDao())
50
    fun dnsRuleRepository() = DnsRuleRepository(dnsRuleDao())
Daniel Wolf's avatar
Daniel Wolf committed
51
    fun hostSourceRepository() = HostSourceRepository(hostSourceDao())
52
53
54
55
56

    fun recreateDnsRuleIndizes() {
        this.openHelper.writableDatabase.apply {
            runInTransaction {
                execSQL("DROP INDEX `index_DnsRule_importedFrom`")
57
                execSQL("DROP INDEX `index_DnsRule_host_type_stagingType`")
58
59

                execSQL("CREATE  INDEX `index_DnsRule_importedFrom` ON `DnsRule` (`importedFrom`)")
60
                execSQL("CREATE UNIQUE INDEX `index_DnsRule_host_type_stagingType` ON `DnsRule` (`host`, `type`, `stagingType`)")
61
62
63
            }
        }
    }
64
}