Skip to content

Commit

Permalink
BaseDao refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yamin8000 committed Feb 1, 2024
1 parent 026d7a1 commit 1ebf636
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* freeDictionaryApp/freeDictionaryApp.app.main
* BaseDao.kt Copyrighted by Yamin Siahmargooei at 2023/8/26
* BaseDao.kt Last modified at 2023/8/26
* This file is part of freeDictionaryApp/freeDictionaryApp.app.main.
* Copyright (C) 2023 Yamin Siahmargooei
*
* freeDictionaryApp/freeDictionaryApp.app.main 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.
*
* freeDictionaryApp/freeDictionaryApp.app.main 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 freeDictionaryApp. If not, see <https://www.gnu.org/licenses/>.
*/

package io.github.yamin8000.owl.data.db.dao

import androidx.room.RawQuery
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery

/**
* Advanced Dao
*/
abstract class AdvancedDao<T>(tableName: String) : BaseDao<T>(tableName) {

@RawQuery
protected abstract suspend fun getById(query: SupportSQLiteQuery): T?

suspend fun getById(id: Long) = getById(SimpleSQLiteQuery("$baseQuery where id = $id"))

@RawQuery
protected abstract suspend fun getAll(query: SupportSQLiteQuery): List<T>

suspend fun getAll() = getAll(SimpleSQLiteQuery(baseQuery))

@RawQuery
protected abstract suspend fun getAllByIds(query: SupportSQLiteQuery): List<T>

suspend fun getAllByIds(
ids: List<Long>
): List<T> {
val params = ids.joinToString("")
return getAllByIds(SimpleSQLiteQuery("$baseWhereQuery id in ($params)"))
}

@RawQuery
protected abstract suspend fun getByParam(query: SupportSQLiteQuery): List<T>

suspend fun <P> getByParam(
param: String, value: P
) = getByParam(SimpleSQLiteQuery("$baseWhereQuery $param='$value'"))

suspend fun getByParams(
paramPair: Pair<String, *>,
vararg paramPairs: Pair<String, *>
): List<T> {
val params = listOf(paramPair, *paramPairs)
val condition = buildString {
params.forEachIndexed { index, pair ->
append("${pair.first}='${pair.second}'")
if (index != params.lastIndex) append(" and ")
}
}
return getByParam(SimpleSQLiteQuery("$baseWhereQuery $condition"))
}
}
51 changes: 5 additions & 46 deletions app/src/main/java/io/github/yamin8000/owl/data/db/dao/BaseDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ package io.github.yamin8000.owl.data.db.dao

import androidx.room.Delete
import androidx.room.Insert
import androidx.room.RawQuery
import androidx.room.Update
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery

/**
* Basic CRUD Dao
*/
abstract class BaseDao<T>(tableName: String) {

private val baseQuery = "select * from `$tableName`"
protected val baseQuery = "select * from `$tableName`"

private val baseWhereQuery = "$baseQuery where"
protected val baseWhereQuery = "$baseQuery where"

@Insert
abstract suspend fun insert(entity: T): Long
Expand All @@ -48,45 +48,4 @@ abstract class BaseDao<T>(tableName: String) {

@Delete
abstract suspend fun deleteAll(entities: List<T>): Int

@RawQuery
protected abstract suspend fun getById(query: SupportSQLiteQuery): T?

suspend fun getById(id: Long) = getById(SimpleSQLiteQuery("$baseQuery where id = $id"))

@RawQuery
protected abstract suspend fun getAll(query: SupportSQLiteQuery): List<T>

suspend fun getAll() = getAll(SimpleSQLiteQuery(baseQuery))

@RawQuery
protected abstract suspend fun getAllByIds(query: SupportSQLiteQuery): List<T>

suspend fun getAllByIds(
ids: List<Long>
): List<T> {
val params = ids.joinToString("")
return getAllByIds(SimpleSQLiteQuery("$baseWhereQuery id in ($params)"))
}

@RawQuery
protected abstract suspend fun getByParam(query: SupportSQLiteQuery): List<T>

suspend fun <P> getByParam(
param: String, value: P
) = getByParam(SimpleSQLiteQuery("$baseWhereQuery $param='$value'"))

suspend fun getByParams(
paramPair: Pair<String, *>,
vararg paramPairs: Pair<String, *>
): List<T> {
val params = listOf(paramPair, *paramPairs)
val condition = buildString {
params.forEachIndexed { index, pair ->
append("${pair.first}='${pair.second}'")
if (index != params.lastIndex) append(" and ")
}
}
return getByParam(SimpleSQLiteQuery("$baseWhereQuery $condition"))
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/io/github/yamin8000/owl/data/db/dao/DAOs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ import io.github.yamin8000.owl.data.db.entity.SynonymEntity

object DAOs {
@Dao
abstract class AntonymDao : BaseDao<AntonymEntity>("AntonymEntity")
abstract class AntonymDao : AdvancedDao<AntonymEntity>("AntonymEntity")

@Dao
abstract class DefinitionDao : BaseDao<DefinitionEntity>("DefinitionEntity")
abstract class DefinitionDao : AdvancedDao<DefinitionEntity>("DefinitionEntity")

@Dao
abstract class EntryDao : BaseDao<EntryEntity>("EntryEntity")
abstract class EntryDao : AdvancedDao<EntryEntity>("EntryEntity")

@Dao
abstract class MeaningDao : BaseDao<MeaningEntity>("MeaningEntity")
abstract class MeaningDao : AdvancedDao<MeaningEntity>("MeaningEntity")

@Dao
abstract class PhoneticDao : BaseDao<PhoneticEntity>("PhoneticEntity")
abstract class PhoneticDao : AdvancedDao<PhoneticEntity>("PhoneticEntity")

@Dao
abstract class SynonymDao : BaseDao<SynonymEntity>("SynonymEntity")
abstract class SynonymDao : AdvancedDao<SynonymEntity>("SynonymEntity")
}

0 comments on commit 1ebf636

Please sign in to comment.