DnsRuleDao.kt 3.4 KB
Newer Older
1
2
package com.frostnerd.smokescreen.database.dao

3
import androidx.room.*
4
import com.frostnerd.smokescreen.database.converters.DnsTypeConverter
5
import com.frostnerd.smokescreen.database.entities.DnsRule
6
import org.minidns.record.Record
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

/*
 * Copyright (C) 2019 Daniel Wolf (Ch4t4r)
 * 
 * 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.
 */
@Dao
27
@TypeConverters(DnsTypeConverter::class)
28
29
interface DnsRuleDao {

30
31
32
    @Update
    fun update(dnsRule: DnsRule)

33
34
35
    @Insert
    fun insert(dnsRule: DnsRule)

36
37
38
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertIgnore(dnsRule: DnsRule):Long

39
40
    @Query("DELETE FROM DnsRule WHERE importedFrom IS NULL")
    fun deleteAllUserRules()
41

42
43
44
45
46
47
48
49
50
51
52
    @Query("UPDATE DnsRule SET stagingType=1 WHERE importedFrom IS NOT NULL")
    fun markNonUserRulesForDeletion()

    @Query("DELETE FROM DnsRule WHERE stagingType=1")
    fun deleteMarkedRules()

    @Query("DELETE FROM DnsRule WHERE stagingType=2")
    fun deleteStagedRules()

    @Query("UPDATE DnsRule SET stagingType=NULL")
    fun commitStaging()
53
54

    @Insert
55
    fun insertAll(rules: Collection<DnsRule>)
56

57
58
59
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAllIgnoreConflict(rules: Collection<DnsRule>)

60
    @Query("SELECT COUNT(*) FROM DnsRule")
61
    fun getCount(): Long
62

63
64
65
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom IS NULL")
    fun getUserCount():Long

Daniel Wolf's avatar
Daniel Wolf committed
66
67
68
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom IS NOT NULL")
    fun getNonUserCount(): Long

69
    @Query("SELECT CASE WHEN :type=28 THEN IFNULL(ipv6Target, target) ELSE target END FROM DnsRule WHERE host=:host AND (type = :type OR type=255) AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1) AND (importedFrom IS NOT NULL OR :useUserRules=1) LIMIT 1")
70
    fun findRuleTarget(host: String, type: Record.TYPE, useUserRules:Boolean): String?
71
72
73

    @Query("DELETE FROM DnsRule WHERE importedFrom=:sourceId")
    fun deleteAllFromSource(sourceId: Long)
74
75
76
77
78
79
80

    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL ORDER BY host LIMIT :limit OFFSET :offset")
    fun getAllUserRules(offset:Int, limit:Int):List<DnsRule>

    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL ORDER BY host")
    fun getAllUserRules():List<DnsRule>

Daniel Wolf's avatar
Daniel Wolf committed
81
82
83
    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL ORDER BY host LIMIT :limit OFFSET :offset")
    fun getAllNonUserRules(offset:Int, limit:Int):List<DnsRule>

84
85
    @Delete
    fun remove(rule:DnsRule)
86
87
88

    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom=:hostSourceId")
    fun getCountForHostSource(hostSourceId:Long):Int
89
90
91
92

    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL AND host=:host AND type=:type AND stagingType=2 LIMIT 1")
    fun getNonUserRule(host:String, type:Record.TYPE):DnsRule?

93
}