Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room: could I check passing value to query for NULL?

I'm using Room libs, have DB Stations and want to execute query to get Stations with/without filter.

UPD. I have DAO and I want to get all the records when my array (groupIds) is null or empty and get filtered list if I have at list one element in array.

@Query("SELECT * FROM $STATIONS_TABLE_NAME WHERE ((:groupIds) IS NULL OR $GROUP_ID IN(:groupIds))
fun getStationsWithFilter(groupIds: IntArray?): Flowable<List<DbStation>>

At this moment I had an issue

08-29 16:09:00.139 9508-9508/->BaseActivity: showError: exception - near ",": syntax error (code 1): , while compiling: SELECT * FROM Stations WHERE ((?,?,?,?,?,?,?,?,?,?) IS NOT NULL

So,

1) could I check for null passing value to query and dynamically chabge SQL query?

2) if yes - what the syntax issue I have?

like image 350
Alex Zezekalo Avatar asked Aug 29 '18 13:08

Alex Zezekalo


1 Answers

The deal was in this part of the SQL query

WHERE ((:groupIds) IS NULL 

groupIds - is an Array and I had an syntax exception in that place.

My workaround: I began to pass 2 parameters to query instead of 1, first - String as a Nullable flag (I think it might be a direct Boolean) and second - an array of Int for second clause.

@Query("SELECT * FROM $STATIONS_TABLE_NAME WHERE :nullableFlag IS NULL OR $GROUP_ID IN (:groupIds))
fun getStationsWithFilter(nullableFlag: String?, groupIds: IntArray?): Flowable<List<DbStation>>
like image 161
Alex Zezekalo Avatar answered Sep 18 '22 23:09

Alex Zezekalo