Search code examples
androidkotlinandroid-room

CASE WHEN query in room database


**CASE WHEN query in room database **

I have three type case where I want to fire three different condition

  1. case 1 : check date and return result
  2. case 2: check date and end_date & neverends=1 return result
  3. case 3 : checK DATE and return result

below is the query

  @Query(
    "select * FROM `Task` WHERE task_type =(CASE  WHEN ${Constant.REPEAT_TYPE.ONETIME} THEN task_start_date=:todayDate END DESC," +
            "CASE type WHEN ${Constant.REPEAT_TYPE.REPEAT} THEN task_start_date=:todayDate OR task_never_end=1 OR task_end_date >=:todayDate END DESC," +
            "CASE type WHEN ${Constant.REPEAT_TYPE.LOCATION} THEN task_start_date=:todayDate END DESC)"
)

Error Log

    error: mismatched input 'DESC' expecting {')', ',', '=', '*', '+', '-', '||', '/', '%', '<<', '>>', '&', '|', '<', '<=', '>', '>=', '==', '!=', '<>', K_AND, K_BETWEEN, K_COLLATE, K_GLOB, K_IN, K_IS, K_ISNULL, K_LIKE, K_MATCH, K_NOT, K_NOTNULL, K_OR, K_REGEXP}
    public abstract java.util.List<com.example.todolist.database.entity.Task> getTodassyTask(long todayDate);
                                                                              ^
  extraneous input 'DESC' expecting {<EOF>, ';', K_ALTER, K_ANALYZE, K_ATTACH, K_BEGIN, K_COMMIT, K_CREATE, K_DELETE, K_DETACH, K_DROP, K_END, K_EXPLAIN, K_INSERT, K_PRAGMA, K_REINDEX, K_RELEASE, K_REPLACE, K_ROLLBACK, K_SAVEPOINT, K_SELECT, K_UPDATE, K_VACUUM, K_VALUES, K_WITH, UNEXPECTED_CHAR}
  extraneous input 'DESC' expecting {<EOF>, ';', K_ALTER, K_ANALYZE, K_ATTACH, K_BEGIN, K_COMMIT, K_CREATE, K_DELETE, K_DETACH, K_DROP, K_END, K_EXPLAIN, K_INSERT, K_PRAGMA, K_REINDEX, K_RELEASE, K_REPLACE, K_ROLLBACK, K_SAVEPOINT, K_SELECT, K_UPDATE, K_VACUUM, K_VALUES, K_WITH, UNEXPECTED_CHAR}

Solution

  • DESC is short for DESCENDING, it's use is in an ORDER BY clause. You do not have an ORDER BY clause. Hence, it saying it is expecting something before DESC.

    If the returned column is to be called DESC then enclose it in `` e.g.

    `DESC`
    
    • not the best name for a column though