Skip to content

Commit e75f72d

Browse files
committed
refactor db
1 parent abc1867 commit e75f72d

27 files changed

Lines changed: 553 additions & 884 deletions

app/src/androidTest/java/spam/blocker/service/RuleTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ class RuleTest {
8282
}
8383

8484
fun add_number_rule(r: RegexRule) {
85-
NumberRegexTable().addNewRule(ctx, r)
85+
NumberRegexTable().addNew(ctx, r)
8686
}
8787

8888
fun add_content_rule(r: RegexRule) {
89-
ContentRegexTable().addNewRule(ctx, r)
89+
ContentRegexTable().addNew(ctx, r)
9090
}
9191

9292

app/src/main/java/spam/blocker/App.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ class App : Application() {
9494
private fun listenToNewCallSMS() {
9595
val ctx = this
9696
Events.onNewCall.listen { recordId ->
97-
val record = G.callVM.table.findRecordById(this, recordId as Long)
97+
val record = G.callVM.table.findById(this, recordId as Long)
9898
if (G.callVM.isVisible(ctx, record!!))
9999
if (G.callVM.records.firstOrNull()?.id != record.id) // ugly workaround to prevent double inserting
100100
G.callVM.records.add(0, record)
101101
}
102102
Events.onNewSMS.listen { recordId ->
103-
val record = SmsTable().findRecordById(this, recordId as Long)
103+
val record = SmsTable().findById(this, recordId as Long)
104104
if (G.smsVM.isVisible(ctx, record!!))
105105
if (G.smsVM.records.firstOrNull()?.id != record.id) // ugly workaround to prevent double inserting
106106
G.smsVM.records.add(0, record)

app/src/main/java/spam/blocker/config/Config.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ abstract class PatternRules : IConfig {
666666
val tbl = table()
667667
tbl.clearAll(ctx)
668668
rules.forEach {
669-
tbl.addRuleWithId(ctx, it)
669+
tbl.addWithId(ctx, it)
670670
}
671671
}
672672
}
@@ -710,7 +710,7 @@ class ApiQuery : IConfig {
710710
val table = G.apiQueryVM.table
711711
table.clearAll(ctx)
712712
apis.forEach {
713-
table.addRecordWithId(ctx, it)
713+
table.addWithId(ctx, it)
714714
}
715715
val spf = spf.ApiQueryOptions(ctx)
716716
spf.isListCollapsed = listCollapsed
@@ -732,7 +732,7 @@ class ApiReport : IConfig {
732732
val table = G.apiReportVM.table
733733
table.clearAll(ctx)
734734
apis.forEach {
735-
table.addRecordWithId(ctx, it)
735+
table.addWithId(ctx, it)
736736
}
737737
spf.ApiReportOptions(ctx).isListCollapsed = listCollapsed
738738
}
@@ -750,7 +750,7 @@ class Bots : IConfig {
750750
override fun apply(ctx: Context) {
751751
BotTable.clearAll(ctx)
752752
bots.forEach {
753-
BotTable.addRecordWithId(ctx, it)
753+
BotTable.addWithId(ctx, it)
754754
}
755755
}
756756
}

app/src/main/java/spam/blocker/db/ApiTable.kt

Lines changed: 34 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -83,142 +83,68 @@ data class ReportApi(
8383
}
8484

8585
abstract class ApiTable(
86-
val tableName: String
87-
) {
88-
abstract fun fromCursor(it: Cursor): IApi
89-
90-
@SuppressLint("Range")
91-
fun listAll(ctx: Context, where: String = ""): List<IApi> {
92-
93-
val sql = "SELECT * FROM $tableName $where ORDER BY ${Db.COLUMN_DESC}"
94-
95-
val ret: MutableList<IApi> = mutableListOf()
96-
97-
val cursor = Db.getInstance(ctx).readableDatabase.rawQuery(sql, null)
98-
99-
cursor.use {
100-
if (it.moveToFirst()) {
101-
do {
102-
ret += fromCursor(it)
103-
} while (it.moveToNext())
104-
}
105-
return ret
106-
}
107-
}
108-
109-
abstract fun addNewRecord(ctx: Context, r: IApi): Long
110-
abstract fun addRecordWithId(ctx: Context, r: IApi)
111-
abstract fun updateById(ctx: Context, id: Long, r: IApi): Boolean
112-
113-
fun deleteById(ctx: Context, id: Long): Boolean {
114-
val sql = "DELETE FROM $tableName WHERE ${Db.COLUMN_ID} = $id"
115-
val cursor = Db.getInstance(ctx).writableDatabase.rawQuery(sql, null)
116-
117-
return cursor.use {
118-
it.moveToFirst()
119-
}
120-
}
121-
122-
fun clearAll(ctx: Context) {
123-
val db = Db.getInstance(ctx).writableDatabase
124-
val sql = "DELETE FROM $tableName"
125-
db.execSQL(sql)
86+
tableName: String
87+
) : BasicTable<IApi>(tableName) {
88+
abstract override fun fromCursor(cursor: Cursor): IApi
89+
fun listAll(ctx: Context): List<IApi> {
90+
return listAll(ctx, orderBy = Db.COLUMN_DESC)
12691
}
12792
}
12893

12994
class QueryApiTable : ApiTable(Db.TABLE_API_QUERY) {
13095
@SuppressLint("Range")
131-
override fun fromCursor(it: Cursor): IApi {
96+
override fun fromCursor(cursor: Cursor): IApi {
13297
val actionsConfig =
133-
it.getStringOrNull(it.getColumnIndex(Db.COLUMN_ACTIONS)) ?: ""
98+
cursor.getStringOrNull(cursor.getColumnIndex(Db.COLUMN_ACTIONS)) ?: ""
13499
val actions = actionsConfig.parseActions()
135100

136101
return QueryApi(
137-
id = it.getLong(it.getColumnIndex(Db.COLUMN_ID)),
138-
desc = it.getStringOrNull(it.getColumnIndex(Db.COLUMN_DESC)) ?: "",
102+
id = cursor.getLong(cursor.getColumnIndex(Db.COLUMN_ID)),
103+
desc = cursor.getStringOrNull(cursor.getColumnIndex(Db.COLUMN_DESC)) ?: "",
139104
actions = actions,
140-
enabled = it.getIntOrNull(it.getColumnIndex(Db.COLUMN_ENABLED)) == 1,
105+
enabled = cursor.getIntOrNull(cursor.getColumnIndex(Db.COLUMN_ENABLED)) == 1,
141106
)
142107
}
143108

144-
override fun addNewRecord(ctx: Context, r: IApi): Long {
145-
val db = Db.getInstance(ctx).writableDatabase
146-
val cv = ContentValues()
147-
cv.put(Db.COLUMN_DESC, r.desc)
148-
cv.put(Db.COLUMN_ACTIONS, r.actions.serialize())
149-
cv.put(Db.COLUMN_ENABLED, if (r.enabled) 1 else 0)
150-
return db.insert(tableName, null, cv)
151-
}
152-
153-
override fun addRecordWithId(ctx: Context, r: IApi) {
154-
val db = Db.getInstance(ctx).writableDatabase
155-
val cv = ContentValues()
156-
cv.put(Db.COLUMN_ID, r.id)
157-
cv.put(Db.COLUMN_DESC, r.desc)
158-
cv.put(Db.COLUMN_ACTIONS, r.actions.serialize())
159-
cv.put(Db.COLUMN_ENABLED, if (r.enabled) 1 else 0)
160-
db.insert(tableName, null, cv)
161-
}
162-
163-
override fun updateById(ctx: Context, id: Long, r: IApi): Boolean {
164-
val db = Db.getInstance(ctx).writableDatabase
109+
override fun toContentValues(item: IApi, includeId: Boolean): ContentValues {
165110
val cv = ContentValues()
166-
cv.put(Db.COLUMN_DESC, r.desc)
167-
cv.put(Db.COLUMN_ACTIONS, r.actions.serialize())
168-
cv.put(Db.COLUMN_ENABLED, if (r.enabled) 1 else 0)
169-
170-
return db.update(tableName, cv, "${Db.COLUMN_ID} = $id", null) >= 0
111+
if (includeId) {
112+
cv.put(Db.COLUMN_ID, item.id)
113+
}
114+
cv.put(Db.COLUMN_DESC, item.desc)
115+
cv.put(Db.COLUMN_ACTIONS, item.actions.serialize())
116+
cv.put(Db.COLUMN_ENABLED, if (item.enabled) 1 else 0)
117+
return cv
171118
}
172119
}
173120

174121
class ReportApiTable : ApiTable(Db.TABLE_API_REPORT) {
175122
@SuppressLint("Range")
176-
override fun fromCursor(it: Cursor): IApi {
123+
override fun fromCursor(cursor: Cursor): IApi {
177124
val actionsConfig =
178-
it.getStringOrNull(it.getColumnIndex(Db.COLUMN_ACTIONS)) ?: ""
125+
cursor.getStringOrNull(cursor.getColumnIndex(Db.COLUMN_ACTIONS)) ?: ""
179126
val actions = actionsConfig.parseActions()
180127

181128
return ReportApi(
182-
id = it.getLong(it.getColumnIndex(Db.COLUMN_ID)),
183-
desc = it.getStringOrNull(it.getColumnIndex(Db.COLUMN_DESC)) ?: "",
129+
id = cursor.getLong(cursor.getColumnIndex(Db.COLUMN_ID)),
130+
desc = cursor.getStringOrNull(cursor.getColumnIndex(Db.COLUMN_DESC)) ?: "",
184131
actions = actions,
185-
enabled = it.getIntOrNull(it.getColumnIndex(Db.COLUMN_ENABLED)) == 1,
132+
enabled = cursor.getIntOrNull(cursor.getColumnIndex(Db.COLUMN_ENABLED)) == 1,
186133
// `true` if `1` or `null`(old version doesn't have this field)
187-
autoReportTypes = it.getIntOrNull(it.getColumnIndex(Db.COLUMN_AUTO_REPORT_TYPES)) ?: AutoReportTypes.DefaultTypes
134+
autoReportTypes = cursor.getIntOrNull(cursor.getColumnIndex(Db.COLUMN_AUTO_REPORT_TYPES)) ?: AutoReportTypes.DefaultTypes
188135
)
189136
}
190137

191-
override fun addNewRecord(ctx: Context, r: IApi): Long {
192-
val db = Db.getInstance(ctx).writableDatabase
193-
val cv = ContentValues()
194-
cv.put(Db.COLUMN_DESC, r.desc)
195-
cv.put(Db.COLUMN_ACTIONS, r.actions.serialize())
196-
cv.put(Db.COLUMN_ENABLED, if (r.enabled) 1 else 0)
197-
val rr = r as ReportApi
198-
cv.put(Db.COLUMN_AUTO_REPORT_TYPES, rr.autoReportTypes)
199-
return db.insert(tableName, null, cv)
200-
}
201-
202-
override fun addRecordWithId(ctx: Context, r: IApi) {
203-
val db = Db.getInstance(ctx).writableDatabase
204-
val cv = ContentValues()
205-
cv.put(Db.COLUMN_ID, r.id)
206-
cv.put(Db.COLUMN_DESC, r.desc)
207-
cv.put(Db.COLUMN_ACTIONS, r.actions.serialize())
208-
cv.put(Db.COLUMN_ENABLED, if (r.enabled) 1 else 0)
209-
val rr = r as ReportApi
210-
cv.put(Db.COLUMN_AUTO_REPORT_TYPES, rr.autoReportTypes)
211-
db.insert(tableName, null, cv)
212-
}
213-
214-
override fun updateById(ctx: Context, id: Long, r: IApi): Boolean {
215-
val db = Db.getInstance(ctx).writableDatabase
138+
override fun toContentValues(item: IApi, includeId: Boolean): ContentValues {
216139
val cv = ContentValues()
217-
cv.put(Db.COLUMN_DESC, r.desc)
218-
cv.put(Db.COLUMN_ACTIONS, r.actions.serialize())
219-
cv.put(Db.COLUMN_ENABLED, if (r.enabled) 1 else 0)
220-
val rr = r as ReportApi
140+
if (includeId) {
141+
cv.put(Db.COLUMN_ID, item.id)
142+
}
143+
cv.put(Db.COLUMN_DESC, item.desc)
144+
cv.put(Db.COLUMN_ACTIONS, item.actions.serialize())
145+
cv.put(Db.COLUMN_ENABLED, if (item.enabled) 1 else 0)
146+
val rr = item as ReportApi
221147
cv.put(Db.COLUMN_AUTO_REPORT_TYPES, rr.autoReportTypes)
222-
return db.update(tableName, cv, "${Db.COLUMN_ID} = $id", null) >= 0
148+
return cv
223149
}
224-
}
150+
}

0 commit comments

Comments
 (0)