From 555ebac697eee52fbaa31d541fb1f0c2b807b04e Mon Sep 17 00:00:00 2001 From: Iamlooker Date: Wed, 29 Jun 2022 18:50:19 +0530 Subject: [PATCH] Cleanup Database --- .../com/looker/droidify/database/DatabaseX.kt | 8 ++ .../droidify/database/dao/ProductDao.kt | 131 +----------------- 2 files changed, 11 insertions(+), 128 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt b/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt index 9126181b..0bf84563 100644 --- a/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt +++ b/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt @@ -5,6 +5,14 @@ import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase import androidx.room.TypeConverters +import com.looker.droidify.database.dao.CategoryDao +import com.looker.droidify.database.dao.CategoryTempDao +import com.looker.droidify.database.dao.ExtrasDao +import com.looker.droidify.database.dao.InstalledDao +import com.looker.droidify.database.dao.ProductDao +import com.looker.droidify.database.dao.ProductTempDao +import com.looker.droidify.database.dao.ReleaseDao +import com.looker.droidify.database.dao.RepositoryDao import com.looker.droidify.database.entity.Category import com.looker.droidify.database.entity.CategoryTemp import com.looker.droidify.database.entity.Extras diff --git a/src/main/kotlin/com/looker/droidify/database/dao/ProductDao.kt b/src/main/kotlin/com/looker/droidify/database/dao/ProductDao.kt index c2e862ce..acc1c368 100644 --- a/src/main/kotlin/com/looker/droidify/database/dao/ProductDao.kt +++ b/src/main/kotlin/com/looker/droidify/database/dao/ProductDao.kt @@ -1,14 +1,11 @@ -package com.looker.droidify.database +package com.looker.droidify.database.dao import androidx.lifecycle.LiveData import androidx.room.Dao -import androidx.room.Delete import androidx.room.Insert -import androidx.room.OnConflictStrategy import androidx.room.Query import androidx.room.RawQuery import androidx.room.Transaction -import androidx.room.Update import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SupportSQLiteQuery import com.looker.droidify.ROW_ADDED @@ -46,72 +43,17 @@ import com.looker.droidify.TABLE_PRODUCT import com.looker.droidify.TABLE_PRODUCT_NAME import com.looker.droidify.TABLE_REPOSITORY import com.looker.droidify.TABLE_REPOSITORY_NAME -import com.looker.droidify.database.entity.Category +import com.looker.droidify.database.QueryBuilder import com.looker.droidify.database.entity.CategoryTemp import com.looker.droidify.database.entity.Extras -import com.looker.droidify.database.entity.Installed import com.looker.droidify.database.entity.Product import com.looker.droidify.database.entity.ProductTemp -import com.looker.droidify.database.entity.Release -import com.looker.droidify.database.entity.Repository import com.looker.droidify.database.entity.asProductTemp import com.looker.droidify.entity.Order import com.looker.droidify.entity.Section import com.looker.droidify.entity.UpdateCategory import com.looker.droidify.ui.fragments.Request -interface BaseDao { - @Insert - fun insert(vararg product: T) - - @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertReplace(vararg product: T) - - @Update(onConflict = OnConflictStrategy.REPLACE) - fun update(vararg obj: T): Int - - @Delete - fun delete(obj: T) -} - -@Dao -interface RepositoryDao : BaseDao { - @get:Query("SELECT COUNT(_id) FROM repository") - val count: Int - - fun put(repository: Repository): Repository { - repository.let { item -> - val newId = if (item.id > 0L) update(item).toLong() else returnInsert(item) - return if (newId != repository.id) repository.copy(id = newId) else repository - } - } - - @Insert - fun returnInsert(product: Repository): Long - - @Query("SELECT * FROM repository WHERE _id = :id") - fun get(id: Long): Repository? - - @Query("SELECT * FROM repository WHERE _id = :id") - fun getLive(id: Long): LiveData - - @get:Query("SELECT * FROM repository ORDER BY _id ASC") - val all: List - - @get:Query("SELECT * FROM repository ORDER BY _id ASC") - val allLive: LiveData> - - @get:Query("SELECT _id FROM repository WHERE enabled == 0 ORDER BY _id ASC") - val allDisabled: List - - // TODO clean up products and other tables afterwards - @Query("DELETE FROM repository WHERE _id = :id") - fun deleteById(id: Long): Int - - @Query("SELECT MAX(_id) FROM repository") - fun latestAddedId(): Long -} - @Dao interface ProductDao : BaseDao { @Query("SELECT COUNT(*) FROM product WHERE repositoryId = :id") @@ -284,64 +226,6 @@ interface ProductDao : BaseDao { } } -@Dao -interface ReleaseDao : BaseDao { - // This one for the mode combining releases of different sources - @Query("SELECT * FROM `release` WHERE packageName = :packageName") - fun get(packageName: String): List - - // This one for the separating releases of different sources - @Query("SELECT * FROM `release` WHERE packageName = :packageName AND signature = :signature") - fun get(packageName: String, signature: String): List -} - -@Dao -interface CategoryDao : BaseDao { - @get:Query( - """SELECT DISTINCT category.label - FROM category AS category - JOIN repository AS repository - ON category.repositoryId = repository._id - WHERE repository.enabled != 0""" - ) - val allNames: List - - @get:Query( - """SELECT DISTINCT category.label - FROM category AS category - JOIN repository AS repository - ON category.repositoryId = repository._id - WHERE repository.enabled != 0""" - ) - val allNamesLive: LiveData> - - @Query("DELETE FROM category WHERE repositoryId = :id") - fun deleteById(id: Long): Int -} - -// TODO make sure that apps that not uninstalled by Droid-ify still get removed -@Dao -interface InstalledDao : BaseDao { - fun put(vararg installed: Installed) { - installed.forEach { insertReplace(it) } - } - - @get:Query("SELECT * FROM memory_installed") - val allLive: LiveData> - - @Query("SELECT * FROM memory_installed WHERE packageName = :packageName") - fun get(packageName: String): Installed? - - @Query("SELECT * FROM memory_installed WHERE packageName = :packageName") - fun getLive(packageName: String): LiveData - - @Query("DELETE FROM memory_installed WHERE packageName = :packageName") - fun delete(packageName: String) - - @Query("DELETE FROM memory_installed") - fun emptyTable() -} - @Dao interface ProductTempDao : BaseDao { @get:Query("SELECT * FROM temporary_product") @@ -356,7 +240,7 @@ interface ProductTempDao : BaseDao { @Transaction fun putTemporary(products: List) { products.forEach { - insert(it.let { it.asProductTemp() }) + insert(it.asProductTemp()) it.categories.forEach { category -> insertCategory(CategoryTemp().apply { repositoryId = it.repositoryId @@ -368,15 +252,6 @@ interface ProductTempDao : BaseDao { } } -@Dao -interface CategoryTempDao : BaseDao { - @get:Query("SELECT * FROM temporary_category") - val all: Array - - @Query("DELETE FROM temporary_category") - fun emptyTable() -} - @Dao interface ExtrasDao : BaseDao { @Query("DELETE FROM extras WHERE packageName = :packageName")