diff --git a/database/impl/src/main/java/app/aaps/database/impl/AppRepository.kt b/database/impl/src/main/java/app/aaps/database/impl/AppRepository.kt index 7cccd799e3..657aa71706 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/AppRepository.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/AppRepository.kt @@ -214,11 +214,6 @@ import kotlin.math.roundToInt .map { if (!ascending) it.reversed() else it } .subscribeOn(Schedulers.io()) - @Suppress("unused") - fun getModifiedTemporaryTargetsDataFromId(lastId: Long): Single> = - database.temporaryTargetDao.getModifiedFrom(lastId) - .subscribeOn(Schedulers.io()) - fun getTemporaryTargetActiveAt(timestamp: Long): Single> = database.temporaryTargetDao.getTemporaryTargetActiveAt(timestamp) .subscribeOn(Schedulers.io()) @@ -485,11 +480,6 @@ import kotlin.math.roundToInt } } - @Suppress("unused") - fun getModifiedBolusesDataFromId(lastId: Long): Single> = - database.bolusDao.getModifiedFrom(lastId) - .subscribeOn(Schedulers.io()) - fun getLastBolusRecord(): Bolus? = database.bolusDao.getLastBolusRecord() @@ -710,11 +700,6 @@ import kotlin.math.roundToInt } } - @Suppress("unused") - fun getModifiedTemporaryBasalDataFromId(lastId: Long): Single> = - database.temporaryBasalDao.getModifiedFrom(lastId) - .subscribeOn(Schedulers.io()) - fun getTemporaryBasalActiveAt(timestamp: Long): Single> = database.temporaryBasalDao.getTemporaryBasalActiveAt(timestamp) .subscribeOn(Schedulers.io()) diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/BolusCalculatorResultDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/BolusCalculatorResultDao.kt index 2887bbdcd1..49b6012d13 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/BolusCalculatorResultDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/BolusCalculatorResultDao.kt @@ -26,20 +26,20 @@ internal interface BolusCalculatorResultDao : TraceableDao= :timestamp AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE likely(isValid = 1) AND unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getBolusCalculatorResultsFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getBolusCalculatorResultsIncludingInvalidFromTime(timestamp: Long): Single> // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE id > :id) ORDER BY id ASC") + @Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE unlikely(id > :id) AND likely(referenceId IS NULL) OR id IN (SELECT DISTINCT referenceId FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single> // for WS we need 1 record only diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/BolusDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/BolusDao.kt index f3b57198dd..3d3bd67d61 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/BolusDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/BolusDao.kt @@ -26,45 +26,41 @@ internal interface BolusDao : TraceableDao { @Query("SELECT id FROM $TABLE_BOLUSES ORDER BY id DESC limit 1") fun getLastId(): Long? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE timestamp = :timestamp AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE unlikely(timestamp = :timestamp) AND likely(referenceId IS NULL)") fun findByTimestamp(timestamp: Long): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE pumpId = :pumpId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE unlikely(pumpId = :pumpId) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpIds(pumpId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE temporaryId = :temporaryId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE unlikely(temporaryId = :temporaryId) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpTempIds(temporaryId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE likely(isValid = 1) AND type <> :exclude AND likely(referenceId IS NULL) ORDER BY timestamp DESC LIMIT 1") fun getLastBolusRecord(exclude: Bolus.Type = Bolus.Type.PRIMING): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE likely(isValid = 1) AND type <> :exclude AND likely(referenceId IS NULL) ORDER BY timestamp DESC LIMIT 1") fun getLastBolusRecordMaybe(exclude: Bolus.Type = Bolus.Type.PRIMING): Maybe - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type == :only AND referenceId IS NULL ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE likely(isValid = 1) AND type == :only AND likely(referenceId IS NULL) ORDER BY timestamp DESC LIMIT 1") fun getLastBolusRecordOfType(only: Bolus.Type): Maybe - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY timestamp ASC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE likely(isValid = 1) AND unlikely(type <> :exclude) AND unlikely(referenceId IS NULL) ORDER BY timestamp ASC LIMIT 1") fun getOldestBolusRecord(exclude: Bolus.Type = Bolus.Type.PRIMING): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE likely(isValid = 1) AND unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getBolusesFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND timestamp >= :start AND timestamp <= :end AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE likely(isValid = 1) AND unlikely(timestamp BETWEEN :start AND :end) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getBolusesFromTime(start: Long, end: Long): Single> - @Query("SELECT * FROM $TABLE_BOLUSES WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getBolusesIncludingInvalidFromTime(timestamp: Long): Single> - // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_BOLUSES WHERE id > :id AND pumpId IS NOT NULL AND type <> :exclude AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_BOLUSES WHERE id > :id) ORDER BY id ASC") - fun getModifiedFrom(id: Long, exclude: Bolus.Type = Bolus.Type.PRIMING): Single> - // for WS we need 1 record only - @Query("SELECT * FROM $TABLE_BOLUSES WHERE id > :id AND pumpId IS NOT NULL AND type <> :exclude ORDER BY id ASC limit 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE unlikely(id > :id) AND likely(pumpId IS NOT NULL) AND likely(type <> :exclude) ORDER BY id ASC limit 1") fun getNextModifiedOrNewAfterExclude(id: Long, exclude: Bolus.Type = Bolus.Type.PRIMING): Maybe @Query("SELECT * FROM $TABLE_BOLUSES WHERE id = :referenceId") diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/CarbsDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/CarbsDao.kt index e5b1b49f6a..53e06804e9 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/CarbsDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/CarbsDao.kt @@ -25,10 +25,10 @@ internal interface CarbsDao : TraceableDao { @Query("SELECT id FROM $TABLE_CARBS ORDER BY id DESC limit 1") fun getLastId(): Long? - @Query("SELECT * FROM $TABLE_CARBS WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_CARBS WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): Carbs? - @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp = :timestamp AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_CARBS WHERE unlikely(timestamp = :timestamp) AND likely(referenceId IS NULL)") fun findByTimestamp(timestamp: Long): Carbs? @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND referenceId IS NULL ORDER BY id DESC LIMIT 1") @@ -40,23 +40,23 @@ internal interface CarbsDao : TraceableDao { @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND referenceId IS NULL ORDER BY id ASC LIMIT 1") fun getOldestCarbsRecord(): Carbs? - @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_CARBS WHERE likely(isValid = 1) AND unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getCarbsFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp + duration >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_CARBS WHERE likely(isValid = 1) AND unlikely((timestamp + duration) >= :timestamp) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getCarbsFromTimeExpandable(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp + duration > :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_CARBS WHERE likely(isValid = 1) AND unlikely((timestamp + duration) > :from) AND unlikely(timestamp <= :to) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getCarbsFromTimeToTimeExpandable(from: Long, to: Long): Single> - @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_CARBS WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getCarbsIncludingInvalidFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_CARBS WHERE unlikely(timestamp BETWEEN :from AND :to) AND likely(referenceId IS NULL) ORDER BY id DESC") fun getCarbsIncludingInvalidFromTimeToTime(from: Long, to: Long): Single> // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_CARBS WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_CARBS WHERE id > :id) ORDER BY id ASC") + @Query("SELECT * FROM $TABLE_CARBS WHERE unlikely(id > :id) AND likely(referenceId IS NULL) OR id IN (SELECT DISTINCT referenceId FROM $TABLE_CARBS WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single> // for WS we need 1 record only diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/EffectiveProfileSwitchDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/EffectiveProfileSwitchDao.kt index 29b2dacae0..873fce99b1 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/EffectiveProfileSwitchDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/EffectiveProfileSwitchDao.kt @@ -25,32 +25,32 @@ internal interface EffectiveProfileSwitchDao : TraceableDao= timestamp AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE unlikely(:timestamp >= timestamp) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getEffectiveProfileSwitchActiveAt(timestamp: Long): Maybe - @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getEffectiveProfileSwitchDataFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE timestamp BETWEEN :start AND :end AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE unlikely(timestamp BETWEEN :start AND :end) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getEffectiveProfileSwitchDataFromTimeToTime(start: Long, end: Long): Single> - @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getEffectiveProfileSwitchDataIncludingInvalidFromTime(timestamp: Long): Single> @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") fun getEffectiveProfileSwitchData(): Single> // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE id > :id) ORDER BY id ASC") + @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE unlikely(id > :id) AND likely(referenceId IS NULL) OR id IN (SELECT DISTINCT referenceId FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single> // for WS we need 1 record only diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/ExtendedBolusDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/ExtendedBolusDao.kt index 80e2ce8884..b6b2c080b6 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/ExtendedBolusDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/ExtendedBolusDao.kt @@ -26,31 +26,31 @@ internal interface ExtendedBolusDao : TraceableDao { @Query("SELECT id FROM $TABLE_EXTENDED_BOLUSES ORDER BY id DESC limit 1") fun getLastId(): Long? - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE timestamp = :timestamp AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(timestamp = :timestamp) AND likely(referenceId IS NULL)") fun findByTimestamp(timestamp: Long): ExtendedBolus? - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): ExtendedBolus? - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE pumpId = :pumpId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(pumpId = :pumpId) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpIds(pumpId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): ExtendedBolus? - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE endId = :endPumpId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(endId = :endPumpId) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpEndIds(endPumpId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): ExtendedBolus? - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(timestamp <= :timestamp) AND unlikely((timestamp + duration) > :timestamp) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL) AND unlikely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getExtendedBolusActiveAt(timestamp: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): Maybe - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(timestamp <= :timestamp) AND unlikely((timestamp + duration) > :timestamp) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getExtendedBolusActiveAt(timestamp: Long): Maybe - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getExtendedBolusDataFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE timestamp >= :from AND timestamp <= :to AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(timestamp BETWEEN :from AND :to) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getExtendedBolusDataFromTimeToTime(from: Long, to: Long): Single> - @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getExtendedBolusDataIncludingInvalidFromTime(timestamp: Long): Single> // This query will be used with v3 to get all changed records diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/GlucoseValueDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/GlucoseValueDao.kt index 3eb9cf8de4..e1ced4f1e5 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/GlucoseValueDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/GlucoseValueDao.kt @@ -28,23 +28,23 @@ internal interface GlucoseValueDao : TraceableDao { @Query("SELECT id FROM $TABLE_GLUCOSE_VALUES ORDER BY id DESC limit 1") fun getLastId(): Long? - @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): GlucoseValue? - @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE timestamp = :timestamp AND sourceSensor = :sourceSensor AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE unlikely(timestamp = :timestamp) AND likely(sourceSensor = :sourceSensor) AND likely(referenceId IS NULL)") fun findByTimestampAndSensor(timestamp: Long, sourceSensor: GlucoseValue.SourceSensor): GlucoseValue? - @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL AND value >= 39 ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) AND likely(value >= 39) ORDER BY timestamp ASC") fun compatGetBgReadingsDataFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE timestamp BETWEEN :start AND :end AND isValid = 1 AND referenceId IS NULL AND value >= 39 ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE unlikely(timestamp BETWEEN :start AND :end) AND likely(isValid = 1) AND likely(referenceId IS NULL) AND likely(value >= 39) ORDER BY timestamp ASC") fun compatGetBgReadingsDataFromTime(start: Long, end: Long): Single> - @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE id > :lastId AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE unlikely(id > :lastId) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getDataFromId(lastId: Long): Single> // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_GLUCOSE_VALUES WHERE id > :id) ORDER BY id ASC") + @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE unlikely(id > :id) AND likely(referenceId IS NULL) OR id IN (SELECT DISTINCT referenceId FROM $TABLE_GLUCOSE_VALUES WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single> // for WS we need 1 record only diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/OfflineEventDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/OfflineEventDao.kt index ebaa6cef05..061ca3c494 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/OfflineEventDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/OfflineEventDao.kt @@ -25,17 +25,18 @@ internal interface OfflineEventDao : TraceableDao { @Query("SELECT id FROM $TABLE_OFFLINE_EVENTS ORDER BY id DESC limit 1") fun getLastId(): Long? - @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): OfflineEvent? - @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE unlikely(timestamp <= :timestamp) AND unlikely((timestamp + duration) > :timestamp) AND likely(referenceId IS NULL) AND likely(isValid = 1) " + + "ORDER BY timestamp DESC LIMIT 1") fun getOfflineEventActiveAt(timestamp: Long): Maybe - @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE timestamp BETWEEN :start AND :end AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE unlikely(timestamp BETWEEN :start AND :end) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getOfflineEventDataFromTimeToTime(start: Long, end: Long): Single> // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_OFFLINE_EVENTS WHERE id > :id) ORDER BY id ASC") + @Query("SELECT * FROM $TABLE_OFFLINE_EVENTS WHERE unlikely(id > :id) AND likely(referenceId IS NULL) OR id IN (SELECT DISTINCT referenceId FROM $TABLE_OFFLINE_EVENTS WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single> // for WS we need 1 record only diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/ProfileSwitchDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/ProfileSwitchDao.kt index cdba01a7a5..d29d41de78 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/ProfileSwitchDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/ProfileSwitchDao.kt @@ -30,26 +30,26 @@ internal interface ProfileSwitchDao : ProfileSwitchDaoWorkaround { @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE timestamp = :timestamp AND referenceId IS NULL") fun findByTimestamp(timestamp: Long): ProfileSwitch? - @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): ProfileSwitch? - @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE unlikely(timestamp <= :timestamp) AND unlikely((timestamp + duration) > :timestamp) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getTemporaryProfileSwitchActiveAt(timestamp: Long): Maybe - @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE timestamp <= :timestamp AND duration = 0 AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE unlikely(timestamp <= :timestamp) AND unlikely(duration = 0) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getPermanentProfileSwitchActiveAt(timestamp: Long): Maybe @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") fun getAllProfileSwitches(): Single> - @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getProfileSwitchDataIncludingInvalidFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getProfileSwitchDataFromTime(timestamp: Long): Single> // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_PROFILE_SWITCHES WHERE id > :id) ORDER BY id ASC") + @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE unlikely(id > :id) AND likely(referenceId IS NULL) OR id IN (SELECT DISTINCT referenceId FROM $TABLE_PROFILE_SWITCHES WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single> // for WS we need 1 record only diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryBasalDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryBasalDao.kt index 83158d5dda..3aef578941 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryBasalDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryBasalDao.kt @@ -29,48 +29,44 @@ internal interface TemporaryBasalDao : TraceableDao { @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE temporaryId = :temporaryId") fun findByTempId(temporaryId: Long): TemporaryBasal? - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp = :timestamp AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(timestamp = :timestamp) AND likely(referenceId IS NULL)") fun findByTimestamp(timestamp: Long): TemporaryBasal? - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE pumpId = :pumpId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(pumpId = :pumpId) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpIds(pumpId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): TemporaryBasal? - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE endId = :endPumpId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE endId = :endPumpId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND likely(referenceId IS NULL)") fun findByPumpEndIds(endPumpId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): TemporaryBasal? - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): TemporaryBasal? - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE temporaryId = :temporaryId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(temporaryId = :temporaryId) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpTempIds(temporaryId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): TemporaryBasal? - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(timestamp <= :timestamp) AND unlikely((timestamp + duration) > :timestamp) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getTemporaryBasalActiveAt(timestamp: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): Maybe - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(timestamp <= :timestamp) AND unlikely((timestamp + duration) > :timestamp) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getTemporaryBasalActiveAt(timestamp: Long): Maybe - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp <= :to AND (timestamp + duration) > :from AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(timestamp <= :to) AND unlikely((timestamp + duration) > :from) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC") fun getTemporaryBasalActiveBetweenTimeAndTime(from: Long, to: Long): Single> - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTemporaryBasalDataFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp >= :from AND timestamp <= :to AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(timestamp BETWEEN :from and :to) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTemporaryBasalDataFromTimeToTime(from: Long, to: Long): Single> - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTemporaryBasalDataIncludingInvalidFromTime(timestamp: Long): Single> @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE referenceId = :id ORDER BY id DESC LIMIT 1") fun getLastHistoryRecord(id: Long): TemporaryBasal? - // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE id > :id AND pumpId IS NOT NULL AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_TEMPORARY_BASALS WHERE id > :id) ORDER BY id ASC") - fun getModifiedFrom(id: Long): Single> - // for WS we need 1 record only - @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE id > :id AND pumpId IS NOT NULL ORDER BY id ASC limit 1") + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE unlikely(id > :id) AND likely(pumpId IS NOT NULL) ORDER BY id ASC limit 1") fun getNextModifiedOrNewAfter(id: Long): Maybe @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE id = :referenceId") diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryTargetDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryTargetDao.kt index e4317d3ee5..0d18a9616c 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryTargetDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/TemporaryTargetDao.kt @@ -25,22 +25,18 @@ internal interface TemporaryTargetDao : TraceableDao { @Query("SELECT id FROM $TABLE_TEMPORARY_TARGETS ORDER BY id DESC limit 1") fun getLastId(): Long? - @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): TemporaryTarget? - @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE unlikely(timestamp <= :timestamp) AND unlikely((timestamp + duration) > :timestamp) AND likely(referenceId IS NULL) AND likely(isValid = 1) ORDER BY timestamp DESC LIMIT 1") fun getTemporaryTargetActiveAt(timestamp: Long): Maybe - @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTemporaryTargetDataFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTemporaryTargetDataIncludingInvalidFromTime(timestamp: Long): Single> - // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_TEMPORARY_TARGETS WHERE id > :id) ORDER BY id ASC") - fun getModifiedFrom(id: Long): Single> - // for WS we need 1 record only @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE id > :id ORDER BY id ASC limit 1") fun getNextModifiedOrNewAfter(id: Long): Maybe diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/TherapyEventDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/TherapyEventDao.kt index 7afcb17c78..d5f6283a17 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/TherapyEventDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/TherapyEventDao.kt @@ -25,32 +25,32 @@ internal interface TherapyEventDao : TraceableDao { @Query("SELECT id FROM $TABLE_THERAPY_EVENTS ORDER BY id DESC limit 1") fun getLastId(): Long? - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND timestamp = :timestamp AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE likely(type = :type) AND unlikely(timestamp = :timestamp) AND likely(referenceId IS NULL)") fun findByTimestamp(type: TherapyEvent.Type, timestamp: Long): TherapyEvent? - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE unlikely(type = :type) AND likely(referenceId IS NULL)") fun getValidByType(type: TherapyEvent.Type): List - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE nightscoutId = :nsId AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE unlikely(nightscoutId = :nsId) AND likely(referenceId IS NULL)") fun findByNSId(nsId: String): TherapyEvent? - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTherapyEventDataFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND unlikely(timestamp >= :timestamp) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTherapyEventDataFromTime(timestamp: Long, type: TherapyEvent.Type): Single> - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE unlikely(timestamp >= :timestamp) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun getTherapyEventDataIncludingInvalidFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND isValid = 1 AND timestamp <= :now ORDER BY timestamp DESC LIMIT 1") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND likely(isValid = 1) AND unlikely(timestamp <= :now) ORDER BY timestamp DESC LIMIT 1") fun getLastTherapyRecord(type: TherapyEvent.Type, now: Long): Maybe - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE timestamp >= :from AND timestamp <= :to AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE unlikely(timestamp BETWEEN :from AND :to) AND likely(isValid = 1) AND likely(referenceId IS NULL) ORDER BY timestamp ASC") fun compatGetTherapyEventDataFromToTime(from: Long, to: Long): Single> // This query will be used with v3 to get all changed records - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_THERAPY_EVENTS WHERE id > :id) ORDER BY id ASC") + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE unlikely(id > :id) AND likely(referenceId IS NULL) OR id IN (SELECT DISTINCT referenceId FROM $TABLE_THERAPY_EVENTS WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single> // for WS we need 1 record only diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/TotalDailyDoseDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/TotalDailyDoseDao.kt index 686981ccb9..3774d5e75b 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/TotalDailyDoseDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/TotalDailyDoseDao.kt @@ -23,13 +23,13 @@ internal interface TotalDailyDoseDao : TraceableDao { @Query("DELETE FROM $TABLE_TOTAL_DAILY_DOSES WHERE referenceId IS NOT NULL") override fun deleteTrackedChanges(): Int - @Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE pumpId = :pumpId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE unlikely(pumpId = :pumpId) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpIds(pumpId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): TotalDailyDose? - @Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE timestamp = :timestamp AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE unlikely(timestamp = :timestamp) AND likely(pumpType = :pumpType) AND likely(pumpSerial = :pumpSerial) AND likely(referenceId IS NULL)") fun findByPumpTimestamp(timestamp: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): TotalDailyDose? - @Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE timestamp = :timestamp AND pumpType = :pumpType AND referenceId IS NULL") + @Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE unlikely(timestamp = :timestamp) AND likely(pumpType = :pumpType) AND likely(referenceId IS NULL)") fun findByTimestamp(timestamp: Long, pumpType: InterfaceIDs.PumpType): Maybe @Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE isValid = 1 AND referenceId IS NULL AND pumpType <> :exclude ORDER BY timestamp DESC LIMIT :count") diff --git a/database/impl/src/main/java/app/aaps/database/impl/daos/UserEntryDao.kt b/database/impl/src/main/java/app/aaps/database/impl/daos/UserEntryDao.kt index 93de4fe7d9..dc08dba01b 100644 --- a/database/impl/src/main/java/app/aaps/database/impl/daos/UserEntryDao.kt +++ b/database/impl/src/main/java/app/aaps/database/impl/daos/UserEntryDao.kt @@ -20,7 +20,7 @@ interface UserEntryDao { @Query("SELECT * FROM $TABLE_USER_ENTRY WHERE timestamp >= :timestamp ORDER BY id DESC") fun getUserEntryDataFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_USER_ENTRY WHERE timestamp >= :timestamp AND source != :excludeSource ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_USER_ENTRY WHERE unlikely(timestamp >= :timestamp) AND likely(source != :excludeSource) ORDER BY id DESC") fun getUserEntryFilteredDataFromTime(excludeSource: Sources, timestamp: Long): Single> } \ No newline at end of file