From 67a1d9918e4bf17a197dad178588989f80b5a6e7 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Sat, 5 Feb 2022 02:41:49 +0100 Subject: [PATCH] Clean up & TODOs --- .../com/looker/droidify/database/DAOs.kt | 29 ++++++++++++------- .../looker/droidify/database/QueryBuilder.kt | 22 -------------- .../droidify/database/entity/Product.kt | 1 + 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/database/DAOs.kt b/src/main/kotlin/com/looker/droidify/database/DAOs.kt index 5d5811f1..0686e564 100644 --- a/src/main/kotlin/com/looker/droidify/database/DAOs.kt +++ b/src/main/kotlin/com/looker/droidify/database/DAOs.kt @@ -96,8 +96,6 @@ interface ProductDao : BaseDao { query: SupportSQLiteQuery ): DataSource.Factory - // TODO optimize and simplify - // TODO add an UpdateCategory argument fun queryList( installed: Boolean, updates: Boolean, searchQuery: String, section: Section, order: Order, numberOfItems: Int = 0 @@ -105,6 +103,7 @@ interface ProductDao : BaseDao { buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems) ) + // TODO add an UpdateCategory argument fun buildProductQuery( installed: Boolean, updates: Boolean, searchQuery: String, section: Section, order: Order, numberOfItems: Int = 0 @@ -115,6 +114,7 @@ interface ProductDao : BaseDao { product.${ROW_SIGNATURES} LIKE ('%.' || installed.${ROW_SIGNATURE} || '.%') AND 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}, @@ -124,6 +124,7 @@ interface ProductDao : BaseDao { AS ${ROW_CAN_UPDATE}, product.${ROW_COMPATIBLE}, product.${ROW_ICON}, product.${ROW_METADATA_ICON}, product.${ROW_RELEASES},""" + // Calculate the matching score with the search query if (searchQuery.isNotEmpty()) { builder += """(((product.${ROW_NAME} LIKE ? OR product.${ROW_SUMMARY} LIKE ?) * 7) | @@ -134,27 +135,34 @@ interface ProductDao : BaseDao { 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""" + + // Merge the matching repositories builder += """JOIN $ROW_REPOSITORY_NAME AS repository ON product.${ROW_REPOSITORY_ID} = repository.${ROW_ID}""" + + // Merge the matching locks builder += """LEFT JOIN $ROW_LOCK_NAME AS lock ON product.${ROW_PACKAGE_NAME} = lock.${ROW_PACKAGE_NAME}""" - if (!installed && !updates) { - builder += "LEFT" - } + // 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}""" + // 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}""" } + // Filter only active repositories builder += """WHERE repository.${ROW_ENABLED} != 0""" + // Filter only the selected repository/category if (section is Section.Category) { builder += "AND category.${ROW_NAME} = ?" builder %= section.name @@ -163,21 +171,22 @@ interface ProductDao : BaseDao { builder %= section.id.toString() } + // Filter only apps that have some matching score to the search query if (searchQuery.isNotEmpty()) { builder += """AND $ROW_MATCH_RANK > 0""" } + // Sum up all products with the same package name builder += "GROUP BY product.${ROW_PACKAGE_NAME} HAVING 1" + // Filter if only can update if (updates) { builder += "AND $ROW_CAN_UPDATE" } + + // Set sorting order builder += "ORDER BY" - - if (searchQuery.isNotEmpty()) { - builder += """$ROW_MATCH_RANK DESC,""" - } - + if (searchQuery.isNotEmpty()) builder += """$ROW_MATCH_RANK DESC,""" when (order) { Order.NAME -> Unit Order.DATE_ADDED -> builder += "product.${ROW_ADDED} DESC," diff --git a/src/main/kotlin/com/looker/droidify/database/QueryBuilder.kt b/src/main/kotlin/com/looker/droidify/database/QueryBuilder.kt index 86d3bcb1..9c7c940d 100644 --- a/src/main/kotlin/com/looker/droidify/database/QueryBuilder.kt +++ b/src/main/kotlin/com/looker/droidify/database/QueryBuilder.kt @@ -1,12 +1,5 @@ package com.looker.droidify.database -import android.database.Cursor -import android.database.sqlite.SQLiteDatabase -import android.os.CancellationSignal -import com.looker.droidify.BuildConfig -import com.looker.droidify.utility.extension.android.asSequence -import com.looker.droidify.utility.extension.text.debug - class QueryBuilder { companion object { fun trimQuery(query: String): String { @@ -34,19 +27,4 @@ class QueryBuilder { } fun build() = builder.toString() - - fun query(db: SQLiteDatabase, signal: CancellationSignal?): Cursor { - val query = builder.toString() - val arguments = arguments.toTypedArray() - if (BuildConfig.DEBUG) { - synchronized(QueryBuilder::class.java) { - debug(query) - db.rawQuery("EXPLAIN QUERY PLAN $query", arguments).use { - it.asSequence() - .forEach { debug(":: ${it.getString(it.getColumnIndex("detail"))}") } - } - } - } - return db.rawQuery(query, arguments, signal) - } } \ No newline at end of file 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 6912790d..70085d6c 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Product.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Product.kt @@ -21,6 +21,7 @@ open class Product { var metadataIcon = "" var releases: List = emptyList() + // TODO Remove in next iteration @ColumnInfo(typeAffinity = ColumnInfo.BLOB) var data: com.looker.droidify.entity.Product? = null