From d2c0a997d2e0ec2356bb0e4d44297f6a93d58c72 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Fri, 8 Apr 2022 02:24:45 +0200 Subject: [PATCH] Update: Replace entities hard strings with constants --- src/main/kotlin/com/looker/droidify/Common.kt | 22 ++++-- .../com/looker/droidify/database/DAOs.kt | 74 +++++++++---------- .../droidify/database/entity/Category.kt | 8 +- .../droidify/database/entity/Installed.kt | 3 +- .../looker/droidify/database/entity/Lock.kt | 3 +- .../droidify/database/entity/Product.kt | 8 +- .../droidify/database/entity/Release.kt | 9 ++- .../droidify/database/entity/Repository.kt | 6 +- 8 files changed, 80 insertions(+), 53 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/Common.kt b/src/main/kotlin/com/looker/droidify/Common.kt index 879082c2..4a9d217c 100644 --- a/src/main/kotlin/com/looker/droidify/Common.kt +++ b/src/main/kotlin/com/looker/droidify/Common.kt @@ -10,9 +10,23 @@ const val NOTIFICATION_ID_UPDATES = 2 const val NOTIFICATION_ID_DOWNLOADING = 3 const val NOTIFICATION_ID_INSTALLER = 4 +const val TABLE_CATEGORY = "category" +const val TABLE_CATEGORY_NAME = "category" +const val TABLE_CATEGORY_TEMP_NAME = "temporary_category" +const val TABLE_INSTALLED = "installed" +const val TABLE_INSTALLED_NAME = "memory_installed" +const val TABLE_LOCK = "lock" +const val TABLE_LOCK_NAME = "memory_lock" +const val TABLE_PRODUCT = "product" +const val TABLE_PRODUCT_NAME = "product" +const val TABLE_PRODUCT_TEMP_NAME = "temporary_product" +const val TABLE_RELEASE = "release" +const val TABLE_RELEASE_NAME = "release" +const val TABLE_REPOSITORY = "repository" +const val TABLE_REPOSITORY_NAME = "repository" + const val ROW_REPOSITORY_ID = "repositoryId" const val ROW_PACKAGE_NAME = "packageName" -const val ROW_NAME = "name" const val ROW_LABEL = "label" const val ROW_SUMMARY = "summary" const val ROW_DESCRIPTION = "description" @@ -33,14 +47,8 @@ const val ROW_VERSION = "version" const val ROW_SIGNATURE = "signature" const val ROW_ID = "_id" const val ROW_ENABLED = "enabled" -const val ROW_DELETED = "deleted" const val ROW_CAN_UPDATE = "can_update" const val ROW_MATCH_RANK = "match_rank" -const val ROW_REPOSITORY_NAME = "repository" -const val ROW_PRODUCT_NAME = "product" -const val ROW_CATEGORY_NAME = "category" -const val ROW_INSTALLED_NAME = "memory_installed" -const val ROW_LOCK_NAME = "memory_lock" const val JOB_ID_SYNC = 1 diff --git a/src/main/kotlin/com/looker/droidify/database/DAOs.kt b/src/main/kotlin/com/looker/droidify/database/DAOs.kt index 897fd3cf..8f313183 100644 --- a/src/main/kotlin/com/looker/droidify/database/DAOs.kt +++ b/src/main/kotlin/com/looker/droidify/database/DAOs.kt @@ -135,65 +135,65 @@ interface ProductDao : BaseDao { ): SupportSQLiteQuery { val builder = QueryBuilder() - val signatureMatches = """installed.${ROW_SIGNATURE} IS NOT NULL AND - product.${ROW_SIGNATURES} LIKE ('%.' || installed.${ROW_SIGNATURE} || '.%') AND - product.${ROW_SIGNATURES} != ''""" + val signatureMatches = """$TABLE_INSTALLED.$ROW_SIGNATURE IS NOT NULL AND + $TABLE_PRODUCT.$ROW_SIGNATURES LIKE ('%.' || $TABLE_INSTALLED.$ROW_SIGNATURE || '.%') AND + $TABLE_PRODUCT.$ROW_SIGNATURES != ''""" // Select the return fields - builder += """SELECT product.rowid AS _id, product.$ROW_REPOSITORY_ID, - product.$ROW_PACKAGE_NAME, product.$ROW_NAME, - product.$ROW_SUMMARY, installed.$ROW_VERSION, - (COALESCE(lock.$ROW_VERSION_CODE, -1) NOT IN (0, product.$ROW_VERSION_CODE) AND - product.$ROW_COMPATIBLE != 0 AND product.$ROW_VERSION_CODE > - COALESCE(installed.$ROW_VERSION_CODE, 0xffffffff) AND $signatureMatches) - AS $ROW_CAN_UPDATE, product.$ROW_COMPATIBLE, product.$ROW_ICON, - product.$ROW_METADATA_ICON, product.$ROW_RELEASES, product.$ROW_CATEGORIES, - product.$ROW_ANTIFEATURES, product.$ROW_LICENSES, product.$ROW_DONATES, product.$ROW_SCREENSHOTS,""" + builder += """SELECT $TABLE_PRODUCT.rowid AS $ROW_ID, $TABLE_PRODUCT.$ROW_REPOSITORY_ID, + $TABLE_PRODUCT.$ROW_PACKAGE_NAME, $TABLE_PRODUCT.$ROW_LABEL, + $TABLE_PRODUCT.$ROW_SUMMARY, $TABLE_INSTALLED.$ROW_VERSION, + (COALESCE($TABLE_LOCK.$ROW_VERSION_CODE, -1) NOT IN (0, $TABLE_PRODUCT.$ROW_VERSION_CODE) AND + $TABLE_PRODUCT.$ROW_COMPATIBLE != 0 AND $TABLE_PRODUCT.$ROW_VERSION_CODE > + COALESCE($TABLE_INSTALLED.$ROW_VERSION_CODE, 0xffffffff) AND $signatureMatches) + AS $ROW_CAN_UPDATE, $TABLE_PRODUCT.$ROW_COMPATIBLE, $TABLE_PRODUCT.$ROW_ICON, + $TABLE_PRODUCT.$ROW_METADATA_ICON, $TABLE_PRODUCT.$ROW_RELEASES, $TABLE_PRODUCT.$ROW_CATEGORIES, + $TABLE_PRODUCT.$ROW_ANTIFEATURES, $TABLE_PRODUCT.$ROW_LICENSES, $TABLE_PRODUCT.$ROW_DONATES, $TABLE_PRODUCT.$ROW_SCREENSHOTS,""" // Calculate the matching score with the search query if (searchQuery.isNotEmpty()) { - builder += """(((product.${ROW_NAME} LIKE ? OR - product.${ROW_SUMMARY} LIKE ?) * 7) | - ((product.${ROW_PACKAGE_NAME} LIKE ?) * 3) | - (product.${ROW_DESCRIPTION} LIKE ?)) AS ${ROW_MATCH_RANK},""" + builder += """((($TABLE_PRODUCT.$ROW_LABEL LIKE ? OR + $TABLE_PRODUCT.$ROW_SUMMARY LIKE ?) * 7) | + (($TABLE_PRODUCT.$ROW_PACKAGE_NAME LIKE ?) * 3) | + ($TABLE_PRODUCT.$ROW_DESCRIPTION LIKE ?)) AS $ROW_MATCH_RANK,""" builder %= List(4) { "%$searchQuery%" } } else { - builder += "0 AS ${ROW_MATCH_RANK}," + builder += "0 AS $ROW_MATCH_RANK," } // Take product as main table - builder += """MAX((product.${ROW_COMPATIBLE} AND - (installed.${ROW_SIGNATURE} IS NULL OR $signatureMatches)) || - PRINTF('%016X', product.${ROW_VERSION_CODE})) FROM $ROW_PRODUCT_NAME AS product""" + builder += """MAX(($TABLE_PRODUCT.$ROW_COMPATIBLE AND + ($TABLE_INSTALLED.$ROW_SIGNATURE IS NULL OR $signatureMatches)) || + PRINTF('%016X', $TABLE_PRODUCT.$ROW_VERSION_CODE)) FROM $TABLE_PRODUCT_NAME AS $TABLE_PRODUCT""" // Merge the matching repositories - builder += """JOIN $ROW_REPOSITORY_NAME AS repository - ON product.${ROW_REPOSITORY_ID} = repository.${ROW_ID}""" + builder += """JOIN $TABLE_REPOSITORY_NAME AS $TABLE_REPOSITORY + ON $TABLE_PRODUCT.$ROW_REPOSITORY_ID = $TABLE_REPOSITORY.$ROW_ID""" // Merge the matching locks - builder += """LEFT JOIN $ROW_LOCK_NAME AS lock - ON product.${ROW_PACKAGE_NAME} = lock.${ROW_PACKAGE_NAME}""" + builder += """LEFT JOIN $TABLE_LOCK_NAME AS $TABLE_LOCK + ON $TABLE_PRODUCT.$ROW_PACKAGE_NAME = $TABLE_LOCK.$ROW_PACKAGE_NAME""" // Merge the matching installed if (!installed && !updates) builder += "LEFT" - builder += """JOIN $ROW_INSTALLED_NAME AS installed - ON product.${ROW_PACKAGE_NAME} = installed.${ROW_PACKAGE_NAME}""" + builder += """JOIN $TABLE_INSTALLED_NAME AS $TABLE_INSTALLED + ON $TABLE_PRODUCT.$ROW_PACKAGE_NAME = $TABLE_INSTALLED.$ROW_PACKAGE_NAME""" // Merge the matching category if (section is Section.Category) { - builder += """JOIN $ROW_CATEGORY_NAME AS category - ON product.${ROW_PACKAGE_NAME} = category.${ROW_PACKAGE_NAME}""" + builder += """JOIN $TABLE_CATEGORY_NAME AS $TABLE_CATEGORY + ON $TABLE_PRODUCT.$ROW_PACKAGE_NAME = $TABLE_CATEGORY.$ROW_PACKAGE_NAME""" } // Filter only active repositories - builder += """WHERE repository.${ROW_ENABLED} != 0""" + builder += """WHERE $TABLE_REPOSITORY.$ROW_ENABLED != 0""" // Filter only the selected repository/category if (section is Section.Category) { - builder += "AND category.${ROW_LABEL} = ?" + builder += "AND $TABLE_CATEGORY.$ROW_LABEL = ?" builder %= section.name } else if (section is Section.Repository) { - builder += "AND product.${ROW_REPOSITORY_ID} = ?" + builder += "AND $TABLE_PRODUCT.$ROW_REPOSITORY_ID = ?" builder %= section.id.toString() } @@ -204,12 +204,12 @@ interface ProductDao : BaseDao { when (updateCategory) { UpdateCategory.ALL -> Unit - UpdateCategory.NEW -> builder += """AND product.${ROW_ADDED} = product.${ROW_UPDATED}""" - UpdateCategory.UPDATED -> builder += """AND product.${ROW_ADDED} < product.${ROW_UPDATED}""" + UpdateCategory.NEW -> builder += """AND $TABLE_PRODUCT.$ROW_ADDED = $TABLE_PRODUCT.$ROW_UPDATED""" + UpdateCategory.UPDATED -> builder += """AND $TABLE_PRODUCT.$ROW_ADDED < $TABLE_PRODUCT.$ROW_UPDATED""" } // Sum up all products with the same package name - builder += "GROUP BY product.${ROW_PACKAGE_NAME} HAVING 1" + builder += "GROUP BY $TABLE_PRODUCT.$ROW_PACKAGE_NAME HAVING 1" // Filter if only can update if (updates) { @@ -221,10 +221,10 @@ interface ProductDao : BaseDao { if (searchQuery.isNotEmpty()) builder += """$ROW_MATCH_RANK DESC,""" when (order) { Order.NAME -> Unit - Order.DATE_ADDED -> builder += "product.${ROW_ADDED} DESC," - Order.LAST_UPDATE -> builder += "product.${ROW_UPDATED} DESC," + Order.DATE_ADDED -> builder += "$TABLE_PRODUCT.$ROW_ADDED DESC," + Order.LAST_UPDATE -> builder += "$TABLE_PRODUCT.$ROW_UPDATED DESC," }::class - builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}" + builder += "$TABLE_PRODUCT.$ROW_LABEL COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}" return SimpleSQLiteQuery(builder.build(), builder.arguments.toTypedArray()) } diff --git a/src/main/kotlin/com/looker/droidify/database/entity/Category.kt b/src/main/kotlin/com/looker/droidify/database/entity/Category.kt index eb45acca..0a7aa668 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Category.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Category.kt @@ -1,13 +1,17 @@ package com.looker.droidify.database.entity import androidx.room.Entity +import com.looker.droidify.* -@Entity(tableName = "category", primaryKeys = ["repositoryId", "packageName", "label"]) +@Entity( + tableName = TABLE_CATEGORY_NAME, + primaryKeys = [ROW_REPOSITORY_ID, ROW_PACKAGE_NAME, ROW_LABEL] +) open class Category { var repositoryId: Long = 0 var packageName = "" var label = "" } -@Entity(tableName = "temporary_category") +@Entity(tableName = TABLE_CATEGORY_TEMP_NAME) class CategoryTemp : Category() \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/database/entity/Installed.kt b/src/main/kotlin/com/looker/droidify/database/entity/Installed.kt index 1dcdb428..07e10a55 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Installed.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Installed.kt @@ -2,8 +2,9 @@ package com.looker.droidify.database.entity import androidx.room.Entity import androidx.room.PrimaryKey +import com.looker.droidify.TABLE_INSTALLED_NAME -@Entity(tableName = "memory_installed") +@Entity(tableName = TABLE_INSTALLED_NAME) data class Installed( @PrimaryKey var packageName: String = "", diff --git a/src/main/kotlin/com/looker/droidify/database/entity/Lock.kt b/src/main/kotlin/com/looker/droidify/database/entity/Lock.kt index 17832a3f..5f27889a 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Lock.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Lock.kt @@ -2,8 +2,9 @@ package com.looker.droidify.database.entity import androidx.room.Entity import androidx.room.PrimaryKey +import com.looker.droidify.TABLE_LOCK_NAME -@Entity(tableName = "memory_lock") +@Entity(tableName = TABLE_LOCK_NAME) data class Lock( @PrimaryKey var packageName: String = "", diff --git a/src/main/kotlin/com/looker/droidify/database/entity/Product.kt b/src/main/kotlin/com/looker/droidify/database/entity/Product.kt index 9e353670..17b867f6 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Product.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Product.kt @@ -1,6 +1,10 @@ package com.looker.droidify.database.entity import androidx.room.Entity +import com.looker.droidify.ROW_PACKAGE_NAME +import com.looker.droidify.ROW_REPOSITORY_ID +import com.looker.droidify.TABLE_PRODUCT_NAME +import com.looker.droidify.TABLE_PRODUCT_TEMP_NAME import com.looker.droidify.entity.Author import com.looker.droidify.entity.Donate import com.looker.droidify.entity.ProductItem @@ -12,7 +16,7 @@ import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json // TODO Add Product Extras to handle favorite lists etc.. -@Entity(tableName = "product", primaryKeys = ["repositoryId", "packageName"]) +@Entity(tableName = TABLE_PRODUCT_NAME, primaryKeys = [ROW_REPOSITORY_ID, ROW_PACKAGE_NAME]) @Serializable open class Product( var repositoryId: Long, @@ -131,7 +135,7 @@ open class Product( } } -@Entity(tableName = "temporary_product") +@Entity(tableName = TABLE_PRODUCT_TEMP_NAME) class ProductTemp( repositoryId: Long, packageName: String, diff --git a/src/main/kotlin/com/looker/droidify/database/entity/Release.kt b/src/main/kotlin/com/looker/droidify/database/entity/Release.kt index 7e24496d..497667cc 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Release.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Release.kt @@ -2,13 +2,20 @@ package com.looker.droidify.database.entity import android.net.Uri import androidx.room.Entity +import com.looker.droidify.ROW_PACKAGE_NAME +import com.looker.droidify.ROW_SIGNATURE +import com.looker.droidify.ROW_VERSION_CODE +import com.looker.droidify.TABLE_RELEASE_NAME import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json // TODO make a Room entity -@Entity(primaryKeys = ["packageName", "versionCode", "signature"]) +@Entity( + tableName = TABLE_RELEASE_NAME, + primaryKeys = [ROW_PACKAGE_NAME, ROW_VERSION_CODE, ROW_SIGNATURE] +) @Serializable data class Release( val packageName: String, diff --git a/src/main/kotlin/com/looker/droidify/database/entity/Repository.kt b/src/main/kotlin/com/looker/droidify/database/entity/Repository.kt index ad8bb426..d074e6ce 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Repository.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Repository.kt @@ -3,17 +3,19 @@ package com.looker.droidify.database.entity import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey +import com.looker.droidify.ROW_ID +import com.looker.droidify.TABLE_REPOSITORY_NAME import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import java.net.URL -@Entity +@Entity(tableName = TABLE_REPOSITORY_NAME) @Serializable data class Repository( @PrimaryKey(autoGenerate = true) - @ColumnInfo(name = "_id") + @ColumnInfo(name = ROW_ID) var id: Long = 0, var address: String = "", var mirrors: List = emptyList(),