From d40cb77a9b2540993f337ff7f44aa1aead80ea16 Mon Sep 17 00:00:00 2001 From: Iamlooker Date: Wed, 15 Jun 2022 20:07:05 +0530 Subject: [PATCH] Refactor Database --- .../looker/droidify/database/dao/BaseDao.kt | 24 ++++++++++ .../droidify/database/dao/CategoryDao.kt | 40 +++++++++++++++++ .../droidify/database/dao/InstalledDao.kt | 29 ++++++++++++ .../database/{DAOs.kt => dao/ProductDao.kt} | 0 .../droidify/database/dao/ReleaseDao.kt | 16 +++++++ .../droidify/database/dao/RepositoryDao.kt | 45 +++++++++++++++++++ 6 files changed, 154 insertions(+) create mode 100644 src/main/kotlin/com/looker/droidify/database/dao/BaseDao.kt create mode 100644 src/main/kotlin/com/looker/droidify/database/dao/CategoryDao.kt create mode 100644 src/main/kotlin/com/looker/droidify/database/dao/InstalledDao.kt rename src/main/kotlin/com/looker/droidify/database/{DAOs.kt => dao/ProductDao.kt} (100%) create mode 100644 src/main/kotlin/com/looker/droidify/database/dao/ReleaseDao.kt create mode 100644 src/main/kotlin/com/looker/droidify/database/dao/RepositoryDao.kt diff --git a/src/main/kotlin/com/looker/droidify/database/dao/BaseDao.kt b/src/main/kotlin/com/looker/droidify/database/dao/BaseDao.kt new file mode 100644 index 00000000..b8f4123f --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/database/dao/BaseDao.kt @@ -0,0 +1,24 @@ +package com.looker.droidify.database.dao + +import androidx.room.* +import com.looker.droidify.database.entity.Ignored + +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 LockDao : BaseDao { + @Query("DELETE FROM memory_lock WHERE packageName = :packageName") + fun delete(packageName: String) +} \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/database/dao/CategoryDao.kt b/src/main/kotlin/com/looker/droidify/database/dao/CategoryDao.kt new file mode 100644 index 00000000..120bd149 --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/database/dao/CategoryDao.kt @@ -0,0 +1,40 @@ +package com.looker.droidify.database.dao + +import androidx.lifecycle.LiveData +import androidx.room.Dao +import androidx.room.Query +import com.looker.droidify.database.entity.Category +import com.looker.droidify.database.entity.CategoryTemp + +@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 +} + +@Dao +interface CategoryTempDao : BaseDao { + @get:Query("SELECT * FROM temporary_category") + val all: Array + + @Query("DELETE FROM temporary_category") + fun emptyTable() +} \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/database/dao/InstalledDao.kt b/src/main/kotlin/com/looker/droidify/database/dao/InstalledDao.kt new file mode 100644 index 00000000..1f9df9cb --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/database/dao/InstalledDao.kt @@ -0,0 +1,29 @@ +package com.looker.droidify.database.dao + +import androidx.lifecycle.LiveData +import androidx.room.Dao +import androidx.room.Query +import com.looker.droidify.database.entity.Installed + +// 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() +} \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/database/DAOs.kt b/src/main/kotlin/com/looker/droidify/database/dao/ProductDao.kt similarity index 100% rename from src/main/kotlin/com/looker/droidify/database/DAOs.kt rename to src/main/kotlin/com/looker/droidify/database/dao/ProductDao.kt diff --git a/src/main/kotlin/com/looker/droidify/database/dao/ReleaseDao.kt b/src/main/kotlin/com/looker/droidify/database/dao/ReleaseDao.kt new file mode 100644 index 00000000..5ac7ccf5 --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/database/dao/ReleaseDao.kt @@ -0,0 +1,16 @@ +package com.looker.droidify.database.dao + +import androidx.room.Dao +import androidx.room.Query +import com.looker.droidify.database.entity.Release + +@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 +} diff --git a/src/main/kotlin/com/looker/droidify/database/dao/RepositoryDao.kt b/src/main/kotlin/com/looker/droidify/database/dao/RepositoryDao.kt new file mode 100644 index 00000000..5c32459d --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/database/dao/RepositoryDao.kt @@ -0,0 +1,45 @@ +package com.looker.droidify.database.dao + +import androidx.lifecycle.LiveData +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.Query +import com.looker.droidify.database.entity.Repository + +@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 +} \ No newline at end of file