The issue is that you can only bind (replace ? with a value) literal values, not KEYWORDS or component names. In short if bound then the values are suitably enclosed so binding the string ASC
would result in 'ASC'
You basically have the following options.
a) Multiple @Query
s with selection of the appropriate Query (ascending or descending) e.g.
@Query("SELECT * FROM t_article ORDER BY id ASC")
fun pagingSourceAsc(): PagingSource<Int, Article>
@Query("SELECT * FROM t_article ORDER BY id DESC")
fun pagingSourceDesc(): PagingSource<Int, Article>
fun pagingSource(order: String): PagingSource<Int, Article> {
if(order.equals
}
b) Manipulation according to a value passed to the query where the keywords are selected according to the selection criteria e.g.
@Query("SELECT * FROM t_articale ORDER BY WHEN :order = 'ASC' THEN id ASC ELSE id DESC END")
c) By using a @RawQuery
, which is very flexible BUT not validated at compile time e.g.
@RawQuery
fun pagingSourceRawQuery(rawQuery: SupportSQLiteQuery): PagingSource<Int, Article>
fun pagingSource(order: String): PagingSource<Int,Article> {
return pagingSourceRawQuery(SimpleSQLiteQuery("SELECT * FROM t_artical ORDER BY $order"))
}
- Noting that Room will not validate the SQL at compile time and also that as the
order
is not bound then there is the potential for SQL injection.
- this option can eliminate complex SQL but for the sake of the complexity being moved to the programming code (and thus often more understandable).