0

Why does this not work in Room?:

val dataSourceFactory =
    database.gameDao.getGames("Game.platforms LIKE '%XONE%'")

@Query("SELECT * FROM Game WHERE :likeClause")
fun getGames(likeClause: String): DataSource.Factory<Int, Game>

But this does?:

@Query("SELECT * FROM Game WHERE Game.platforms LIKE '%XONE%'")
fun getGames(): DataSource.Factory<Int, Game>

Is there any way to pass in a string that can stand in as part of the query?

EDIT: I know this isn't the correct way to form a single LIKE clause, but I'm actually trying to pass in multiple LIKE clauses. So I want a way to inject text directly into the query, but Room doesn't seem to want me to do that.

Gavin Wright
  • 3,124
  • 3
  • 14
  • 35

1 Answers1

0

You are talking about dynamic SQL and I dont think this is possible with room. what would work is

@Query("SELECT * FROM Game WHERE Game.platforms LIKE :likeClause1 AND Game.publisher LIKE :likeClause2")
fun getGames(likeClause1: String, likeClause2: String): DataSource.Factory<Int, Game>

and you can use either AND or OR as needed and if you want to ignore one of the like clauses just pass an empty string

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
  • Thanks. I ended up figuring this out by using a RawQuery (although your answer would have worked if I knew how many LIKE clauses I needed ahead of time): https://stackoverflow.com/questions/60559869/how-can-i-turn-a-list-of-strings-into-like-clauses-in-a-room-query – Gavin Wright Mar 22 '20 at 11:03