房间DAO条件



我正在重构一个旧的应用程序,并试图将查询更改为房间很简单,对于简单的查询来说是直接的,但是现在我需要一种根据查询结果返回字符串的方法我不知道如何完成?

@Query("SELECT * FROM $SHIFT_TABLE  WHERE ($SHIFT_START_DATE BETWEEN :arg0.startDateTime  AND :arg0.endDateTime ) OR  $SHIFT_END_DATE  BETWEEN :arg0.startDateTime  AND :arg0.endDateTime )" +
        " OR (($SHIFT_START_DATE  <= :arg0.startDateTime) AND ($SHIFT_END_DATE  >= :arg0.endDateTime  ))")
fun create(shift: Shift) {
    //there is a shift at that time return shift_overlapping_with_other_shift
    //shift is shorter that 1 minute return shifts_shorter_than_one_minute_are_not_recorded
    //else enter the shift and return shift_was_enterd
}

编辑:这是我最终做的事情,如果有人知道一种更好的方式,我会很乐意知道

    @Query("SELECT * FROM $SHIFT_TABLE  WHERE ($SHIFT_START_DATE BETWEEN :arg0.startDateTime  AND :arg0.endDateTime ) OR  $SHIFT_END_DATE  BETWEEN :arg0.startDateTime  AND :arg0.endDateTime )" +
        " OR (($SHIFT_START_DATE  <= :arg0.startDateTime) AND ($SHIFT_END_DATE  >= :arg0.endDateTime  ))")
fun getShiftAtDate(shift: Shift):List<Shift>
@Insert
fun insertShift(shift: Shift)
fun create(shift: Shift):String {
    //shift is shorter that 1 minute return
    if (shift.totalTime == 0) {//shift is shorter that 1 minute
         return MyApp.instance.resources.getString(R.string.shifts_shorter_than_one_minute_are_not_recorded)
    }
    //there is a shift at that time
    if (!getShiftAtDate(shift).isEmpty()){
        return MyApp.instance.resources.getString(R.string.shift_overlapping_with_other_shift)
    }
    //else enter the shift
    insertShift(shift)
    return MyApp.instance.resources.getString(R.string.shift_was_enterd)
}

为了使其起作用,需要纠正一些东西。

首先,房间不希望该方法具有身体,而只是签名。房间将为您实施该方法。我猜Kotlin中的空体方法就像默认实现一样,因此不会使用它。

另外,我还没有看到像您这样的复杂参数的任何示例,所有示例与@query中的参数匹配,而不是参数中的字段。

所以,您要实现的是(请注意,我已经修复了不平衡的括号):

@Dao
interface MyDao {
    @Query("SELECT * FROM $SHIFT_TABLE  WHERE " +
           "($SHIFT_START_DATE BETWEEN :startDateTime AND :endDateTime) OR " +
           "($SHIFT_END_DATE BETWEEN :startDateTime AND :endDateTime) OR " +
           "(($SHIFT_START_DATE <= :startDateTime) AND ($SHIFT_END_DATE >= :endDateTime))")
    fun create(startDateTime: String, endDateTime: String): List<ShiftModel>
}

请注意,我使用的参数类型可能是错误的(将其调整为Shift类型中定义的类型。

有关其他信息,请参阅房间持久库。

最新更新