Update: Extract creating product query function

This commit is contained in:
machiav3lli 2022-02-04 00:04:17 +01:00
parent f18d7051f5
commit 67ec9649a8

View File

@ -87,84 +87,9 @@ interface ProductDao : BaseDao<Product> {
fun queryObject(
installed: Boolean, updates: Boolean, searchQuery: String,
section: Section, order: Order, numberOfItems: Int = 0
): List<Product> {
val builder = QueryBuilder()
val signatureMatches = """installed.${ROW_SIGNATURE} IS NOT NULL AND
product.${ROW_SIGNATURES} LIKE ('%.' || installed.${ROW_SIGNATURE} || '.%') AND
product.${ROW_SIGNATURES} != ''"""
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},"""
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 %= List(4) { "%$searchQuery%" }
} else {
builder += "0 AS ${ROW_MATCH_RANK},"
}
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 += """JOIN $ROW_REPOSITORY_NAME AS repository
ON product.${ROW_REPOSITORY_ID} = repository.${ROW_ID}"""
builder += """LEFT JOIN $ROW_LOCK_NAME AS lock
ON product.${ROW_PACKAGE_NAME} = lock.${ROW_PACKAGE_NAME}"""
if (!installed && !updates) {
builder += "LEFT"
}
builder += """JOIN $ROW_INSTALLED_NAME AS installed
ON product.${ROW_PACKAGE_NAME} = installed.${ROW_PACKAGE_NAME}"""
if (section is Section.Category) {
builder += """JOIN $ROW_CATEGORY_NAME AS category
ON product.${ROW_PACKAGE_NAME} = category.${ROW_PACKAGE_NAME}"""
}
builder += """WHERE repository.${ROW_ENABLED} != 0"""
if (section is Section.Category) {
builder += "AND category.${ROW_NAME} = ?"
builder %= section.name
} else if (section is Section.Repository) {
builder += "AND product.${ROW_REPOSITORY_ID} = ?"
builder %= section.id.toString()
}
if (searchQuery.isNotEmpty()) {
builder += """AND $ROW_MATCH_RANK > 0"""
}
builder += "GROUP BY product.${ROW_PACKAGE_NAME} HAVING 1"
if (updates) {
builder += "AND $ROW_CAN_UPDATE"
}
builder += "ORDER BY"
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,"
}::class
builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}"
return queryObject(SimpleSQLiteQuery(builder.build()))
}
): List<Product> = queryObject(
buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems)
)
@RawQuery(observedEntities = [Product::class])
fun queryList(
@ -176,7 +101,14 @@ interface ProductDao : BaseDao<Product> {
fun queryList(
installed: Boolean, updates: Boolean, searchQuery: String,
section: Section, order: Order, numberOfItems: Int = 0
): DataSource.Factory<Int, Product> {
): DataSource.Factory<Int, Product> = queryList(
buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems)
)
fun buildProductQuery(
installed: Boolean, updates: Boolean, searchQuery: String,
section: Section, order: Order, numberOfItems: Int = 0
): SupportSQLiteQuery {
val builder = QueryBuilder()
val signatureMatches = """installed.${ROW_SIGNATURE} IS NOT NULL AND
@ -252,7 +184,7 @@ interface ProductDao : BaseDao<Product> {
}::class
builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}"
return queryList(SimpleSQLiteQuery(builder.build()))
return SimpleSQLiteQuery(builder.build())
}
}