Android Room Dao:按 CASE 排序不起作用

Posted

技术标签:

【中文标题】Android Room Dao:按 CASE 排序不起作用【英文标题】:Android Room Dao: Order By CASE not working 【发布时间】:2020-07-18 05:07:25 【问题描述】:

我有一个 Room 数据库,使用 Dao 来处理查询等。我正在使用静态(非实时数据)函数通过查询检索结果,当我手动硬编码 Order By 值和列时,一切正常,如下所示,但是将参数传递给 Dao 进行排序时,Order By 恢复为默认值(按 id 列排序)并且不会根据传递的排序参数检索结果

硬编码的 Dao 示例 作品,结果按 ASC 或 DESC 排序

@Query("SELECT * FROM cameras WHERE suburb LIKE '%' || :suburb || '%' AND postcode LIKE '%' || :postcode || '%' ORDER BY direction ASC LIMIT :limit OFFSET :offset ")
fun getCamerasViaStatic(suburb: String?, postcode: String?, limit: Int?, offset: Int?): List<CamerasModel>

//and results retrieved in fragment using

CamerasApplicationDatabase.getInstance(context!!).CamerasDao().getCamerasViaStatic("", "", limit, offset)

Sort Param 传递给 Dao 示例 不行,默认排序的结果排序

@Query("SELECT * FROM cameras WHERE suburb LIKE '%' || :suburb || '%' AND postcode LIKE '%' || :postcode || '%' ORDER BY :sort_by ASC LIMIT :limit OFFSET :offset ")
fun getCamerasViaStatic(suburb: String?, postcode: String?, limit: Int?, offset: Int?, sort_by: String): List<CamerasModel>

//and results retrieved in fragment using

var sort_by = "my_column_to_sort_by"
CamerasApplicationDatabase.getInstance(context!!).CamerasDao().getCamerasViaStatic("", "", limit, offset, sort_by)

考虑到传递的其他参数在两个示例中仍然有效,不确定为什么此添加不起作用,问题也是,稍后我通过 ASC/DESC 参数并在 CASE WHEN 中使用(下面的示例)

ORDER BY CASE WHEN :sort = 1 THEN :sort_by END ASC, CASE WHEN :sort = 0 THEN :sort_by END DESC

【问题讨论】:

【参考方案1】:

使用多个 CASE 表达式找到解决方案...解决方案来自以下链接

Room user configurable order by queries

Room database full dynamic query

@Query("SELECT * FROM cameras " +
        "WHERE suburb LIKE '%' || :suburb || '%' AND postcode LIKE '%' || :postcode || '%' " +
        "ORDER BY " +
        "CASE WHEN :sort_by = 'description'  AND :sort = 0 THEN description END DESC, " +
        "CASE WHEN :sort_by = 'description'  AND :sort = 1 THEN description END ASC, " +
        "CASE WHEN :sort_by = 'direction'    AND :sort = 0 THEN direction   END DESC, " +
        "CASE WHEN :sort_by = 'direction'    AND :sort = 1 THEN direction   END ASC, " +
        "CASE WHEN :sort_by = 'location'     AND :sort = 0 THEN locality    END DESC, " +
        "CASE WHEN :sort_by = 'location'     AND :sort = 1 THEN locality    END ASC, " +
        "CASE WHEN :sort_by = 'state'        AND :sort = 0 THEN state       END DESC, " +
        "CASE WHEN :sort_by = 'state'        AND :sort = 1 THEN state       END ASC " +
        "LIMIT :limit " +
        "OFFSET :offset "
)

fun getCamerasUsingPaginationStatic(suburb: String?, postcode: String?, limit: Int?, offset: Int?, sort: Int?, sort_by: String?): List<CamerasModel>

如果您需要基于 id/values/etc 的数组过滤结果,以下也是相同的查询类型,但传递了 id 数组(使用 IN(:filteredBookmarkedItems))...

@Query("SELECT * FROM cameras " +
        "WHERE camera_id IN(:filteredBookmarkedItems) AND suburb LIKE '%' || :suburb || '%' AND postcode LIKE '%' || :postcode || '%' " +
        "ORDER BY " +
        "CASE WHEN :sort_by = 'description'  AND :sort = 0 THEN description END DESC, " +
        "CASE WHEN :sort_by = 'description'  AND :sort = 1 THEN description END ASC, " +
        "CASE WHEN :sort_by = 'direction'    AND :sort = 0 THEN direction   END DESC, " +
        "CASE WHEN :sort_by = 'direction'    AND :sort = 1 THEN direction   END ASC, " +
        "CASE WHEN :sort_by = 'location'     AND :sort = 0 THEN locality    END DESC, " +
        "CASE WHEN :sort_by = 'location'     AND :sort = 1 THEN locality    END ASC, " +
        "CASE WHEN :sort_by = 'state'        AND :sort = 0 THEN state       END DESC, " +
        "CASE WHEN :sort_by = 'state'        AND :sort = 1 THEN state       END ASC " +
        "LIMIT :limit " +
        "OFFSET :offset "
)
fun getBookmarkedCamerasUsingPaginationStatic(filteredBookmarkedItems: List<Int>, suburb: String?, postcode: String?, limit: Int?, offset: Int?, sort: Int?, sort_by: String?): List<CamerasModel>

【讨论】:

以上是关于Android Room Dao:按 CASE 排序不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Android Room 数据库 DAO 调试日志

将 TypeConverters 应用到 Dao Android Room

Android Jetpack ROOM 的Dao返回LiveData<Bean>封装及Bean普通的区别

Android Kotlin 中的 ROOM 数据库出错

android----Room数据库的简单操作

Android Room 的可选查询参数