functionobjectparametersandroid-roomdao

Is it not possible to call a Dao-Room function passing the whole object as parameter rather than each member?


I have a table of words that contains words with their definition.

The class is Mot, the word is Mot.mot (primary key) and its definition is Mot.defMot.

I want to find all words that have the same definition as my word (but different spellings). My first plan was to write a Dao function passing mot as an object to find all words with same definition (same mot.defMot but different mot.mot).

But the use of object members in SQLite query is rejected by the IDE. The only way seems to pass two parameters (mot, defMot) and use them for the query.

Can someone confirm that ?

Rejected form (the first dot is wavy red underlined):

@Query("SELECT * FROM mots WHERE defMot = :mot.defMot AND mot <> :mot.mot")
fun allOrtho(mot: Mot): LiveData<List<Mot>>

Solution ?:

@Query("SELECT * FROM mots WHERE defMot = :def AND mot <> :mot")
fun allOrtho(mot: String, def: String): LiveData<List<Mot>>

Solution

  • You could have/use something like:-

    @Query("SELECT * FROM mots WHERE defMot = :def AND mot <> :mot")
    fun allOrtho(mot: String, def: String): LiveData<List<Mot>>
    

    in conjunction with:-

    @Query("") /* not really needed */
    fun allOrthoMain(mot: Mot): LiveData<List<Mot>> {
       return allOrtho(mot.mot,mot.defMot)
    }
    

    Obviously you can then use allOrthoMain (name of the function can be whatever suits) with the Mot object as the parameter.

    Otherwise no you cannot pass custom objects directly and use fields of that in the SQL (bar Lists for use in and IN clause where Room will do the necessary conversion of the list into a suitable CSV of the values).