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