房间条件

我正在重构一个旧的应用程序,试图改变查询房间是简单的查询直截了当,但现在我需要一个方法来根据查询结果返回一个字符串,我不知道如何完成它?

@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类型中定义的类型。

有关更多信息,请参阅客房持久性库 。