'android room dao has higher repetition

i find every db operation need a method

if a class has n fields, I want each field to be queried individually, I hava to write n methods.

If any two fields are queried together, I hava to write n*(n-1) methods.

// example
@Entity(
    tableName = "any_table",
)
data class AnyTable(
    @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") var id: Long = 0,
    @ColumnInfo(name = "key1") var key1: Int,
    @ColumnInfo(name = "key2") var key2: Boolean,
    @ColumnInfo(name = "key3") var key3: String,
) {
    @Dao
    interface OperationDao {
        @Query("SELECT * FROM any_table")
        suspend fun query(): List<AnyTable>

        @Query("SELECT * FROM any_table where key1 = :key")
        suspend fun queryByKey1(key: Int): List<AnyTable>

        @Query("SELECT * FROM any_table where key2 = :key")
        suspend fun queryByKey2(key: Boolean): List<AnyTable>

        @Query("SELECT * FROM any_table where key3 = :key")
        suspend fun queryByKey3(key: String): List<AnyTable>

        @Query("SELECT * FROM any_table where key1 = :key1 and key2 = :key2")
        suspend fun queryByKey1AndKey2(key1: Int, key2:Boolean): List<AnyTable>

        @Query("SELECT * FROM any_table where key1 = :key1 and key3 = :key3")
        suspend fun queryByKey1AndKey3(key1: Int, key3:String): List<AnyTable>

    }
}

this way is so cockamamie fussy, Why is there no tool to automatically generate interfaces ?

or I just want a default dao that is automatic adaptation its class and every field.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source