From 1ebf636df9c5805ce1d9b0738c94478e5882075d Mon Sep 17 00:00:00 2001 From: YaMiN Date: Thu, 1 Feb 2024 23:06:00 +0330 Subject: [PATCH] BaseDao refactor --- .../yamin8000/owl/data/db/dao/AdvancedDao.kt | 73 +++++++++++++++++++ .../yamin8000/owl/data/db/dao/BaseDao.kt | 51 ++----------- .../github/yamin8000/owl/data/db/dao/DAOs.kt | 12 +-- 3 files changed, 84 insertions(+), 52 deletions(-) create mode 100644 app/src/main/java/io/github/yamin8000/owl/data/db/dao/AdvancedDao.kt diff --git a/app/src/main/java/io/github/yamin8000/owl/data/db/dao/AdvancedDao.kt b/app/src/main/java/io/github/yamin8000/owl/data/db/dao/AdvancedDao.kt new file mode 100644 index 00000000..414940f9 --- /dev/null +++ b/app/src/main/java/io/github/yamin8000/owl/data/db/dao/AdvancedDao.kt @@ -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 . + */ + +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(tableName: String) : BaseDao(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 + + suspend fun getAll() = getAll(SimpleSQLiteQuery(baseQuery)) + + @RawQuery + protected abstract suspend fun getAllByIds(query: SupportSQLiteQuery): List + + suspend fun getAllByIds( + ids: List + ): List { + val params = ids.joinToString("") + return getAllByIds(SimpleSQLiteQuery("$baseWhereQuery id in ($params)")) + } + + @RawQuery + protected abstract suspend fun getByParam(query: SupportSQLiteQuery): List + + suspend fun

getByParam( + param: String, value: P + ) = getByParam(SimpleSQLiteQuery("$baseWhereQuery $param='$value'")) + + suspend fun getByParams( + paramPair: Pair, + vararg paramPairs: Pair + ): List { + 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")) + } +} \ No newline at end of file diff --git a/app/src/main/java/io/github/yamin8000/owl/data/db/dao/BaseDao.kt b/app/src/main/java/io/github/yamin8000/owl/data/db/dao/BaseDao.kt index 52e0e53a..1083ec2c 100644 --- a/app/src/main/java/io/github/yamin8000/owl/data/db/dao/BaseDao.kt +++ b/app/src/main/java/io/github/yamin8000/owl/data/db/dao/BaseDao.kt @@ -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(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 @@ -48,45 +48,4 @@ abstract class BaseDao(tableName: String) { @Delete abstract suspend fun deleteAll(entities: List): 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 - - suspend fun getAll() = getAll(SimpleSQLiteQuery(baseQuery)) - - @RawQuery - protected abstract suspend fun getAllByIds(query: SupportSQLiteQuery): List - - suspend fun getAllByIds( - ids: List - ): List { - val params = ids.joinToString("") - return getAllByIds(SimpleSQLiteQuery("$baseWhereQuery id in ($params)")) - } - - @RawQuery - protected abstract suspend fun getByParam(query: SupportSQLiteQuery): List - - suspend fun

getByParam( - param: String, value: P - ) = getByParam(SimpleSQLiteQuery("$baseWhereQuery $param='$value'")) - - suspend fun getByParams( - paramPair: Pair, - vararg paramPairs: Pair - ): List { - 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")) - } } \ No newline at end of file diff --git a/app/src/main/java/io/github/yamin8000/owl/data/db/dao/DAOs.kt b/app/src/main/java/io/github/yamin8000/owl/data/db/dao/DAOs.kt index 686f6f9c..da7bd1cc 100644 --- a/app/src/main/java/io/github/yamin8000/owl/data/db/dao/DAOs.kt +++ b/app/src/main/java/io/github/yamin8000/owl/data/db/dao/DAOs.kt @@ -31,20 +31,20 @@ import io.github.yamin8000.owl.data.db.entity.SynonymEntity object DAOs { @Dao - abstract class AntonymDao : BaseDao("AntonymEntity") + abstract class AntonymDao : AdvancedDao("AntonymEntity") @Dao - abstract class DefinitionDao : BaseDao("DefinitionEntity") + abstract class DefinitionDao : AdvancedDao("DefinitionEntity") @Dao - abstract class EntryDao : BaseDao("EntryEntity") + abstract class EntryDao : AdvancedDao("EntryEntity") @Dao - abstract class MeaningDao : BaseDao("MeaningEntity") + abstract class MeaningDao : AdvancedDao("MeaningEntity") @Dao - abstract class PhoneticDao : BaseDao("PhoneticEntity") + abstract class PhoneticDao : AdvancedDao("PhoneticEntity") @Dao - abstract class SynonymDao : BaseDao("SynonymEntity") + abstract class SynonymDao : AdvancedDao("SynonymEntity") } \ No newline at end of file