问题
I have a Room database using Dao to process queries etc. I am using static (non live data) function to retrieve results via the query, all works well when I manual hard code the Order By values and column as below, however when passing params to the Dao to do the sorting, the Order By reverts back to default (order by id column) and does not retrieve results based on the passed sort param
Hard coded Dao example works, results sorted by ASC or 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 passed to Dao example not work, results sorted by default sort
@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)
Not sure why this addition does not work considering the other params passed still work in both examples, the issue is also, later on I pass the ASC/DESC param and use in CASE WHEN (example below)
ORDER BY CASE WHEN :sort = 1 THEN :sort_by END ASC, CASE WHEN :sort = 0 THEN :sort_by END DESC
回答1:
Found solution using multiple CASE Expressions ... solution helped from below links
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>
also below is same query type but with array of ids passed (using IN(:filteredBookmarkedItems)) if you need to filter results based on a array of id/values/etc...
@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>
来源:https://stackoverflow.com/questions/61055772/android-room-dao-order-by-case-not-working