Skip to content

Commit

Permalink
database fixes for foreign key constraints for normal measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
xcadrik authored and dzlabing committed Apr 25, 2023
1 parent 4424b80 commit 55f96ff
Show file tree
Hide file tree
Showing 28 changed files with 168 additions and 172 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/at/specure/data/CoreDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import at.specure.data.entity.VoipTestResultRecord
ConnectivityStateRecord::class,
HistoryReference::class],
// Needs to upgraded when schema changes - else: "Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number."
version = 119
version = 130
)
@TypeConverters(TypeConverter::class)
abstract class CoreDatabase : RoomDatabase() {
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/at/specure/data/dao/CapabilitiesDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import at.specure.data.entity.CapabilitiesRecord
@Dao
interface CapabilitiesDao {

@Query("SELECT * from ${Tables.CAPABILITIES} WHERE testUUID == :testUUID LIMIT 1")
fun get(testUUID: String): CapabilitiesRecord
@Query("SELECT * from ${Tables.CAPABILITIES} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId)) LIMIT 1")
fun get(testUUID: String?, signalChunkId: String?): CapabilitiesRecord

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(capabilities: CapabilitiesRecord)

@Query("DELETE FROM ${Tables.CAPABILITIES} WHERE testUUID=:testUUID")
fun remove(testUUID: String)
@Query("DELETE FROM ${Tables.CAPABILITIES} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun remove(testUUID: String?, signalChunkId: String?)
}
21 changes: 10 additions & 11 deletions core/src/main/java/at/specure/data/dao/CellInfoDao.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
package at.specure.data.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Upsert
import at.specure.data.Tables
import at.specure.data.entity.CellInfoRecord
import at.specure.info.TransportType

@Dao
abstract class CellInfoDao {

@Query("SELECT * FROM ${Tables.CELL_INFO} WHERE testUUID=:testUUID")
abstract fun get(testUUID: String): List<CellInfoRecord>
@Query("SELECT * FROM ${Tables.CELL_INFO} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
abstract fun get(testUUID: String?, signalChunkId: String?): List<CellInfoRecord>

@Insert(onConflict = OnConflictStrategy.IGNORE)
@Upsert
abstract fun insert(cellInfo: List<CellInfoRecord>)

@Query("DELETE FROM ${Tables.CELL_INFO} WHERE testUUID=:testUUID")
abstract fun removeAllCellInfo(testUUID: String)
@Query("DELETE FROM ${Tables.CELL_INFO} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
abstract fun removeAllCellInfo(testUUID: String?, signalChunkId: String?)

@Query("DELETE FROM ${Tables.CELL_INFO} WHERE testUUID=:testUUID AND uuid==:cellInfoUUID")
abstract fun removeSingleCellInfo(testUUID: String?, cellInfoUUID: String)
@Query("DELETE FROM ${Tables.CELL_INFO} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId)) AND uuid==:cellInfoUUID")
abstract fun removeSingleCellInfo(testUUID: String?, signalChunkId: String?, cellInfoUUID: String)

@Transaction
open fun clearInsert(testUUID: String?, cellInfo: List<CellInfoRecord>) {
open fun clearInsert(testUUID: String?, signalChunkId: String?, cellInfo: List<CellInfoRecord>) {
val filteredCellInfo = cellInfo.filter { it.cellTechnology != null || it.transportType == TransportType.WIFI }
filteredCellInfo.forEach {
if (it.cellTechnology != null || it.transportType == TransportType.WIFI) removeSingleCellInfo(testUUID, it.uuid)
if (it.cellTechnology != null || it.transportType == TransportType.WIFI) removeSingleCellInfo(testUUID, signalChunkId, it.uuid)
}

insert(filteredCellInfo)
Expand Down
15 changes: 7 additions & 8 deletions core/src/main/java/at/specure/data/dao/CellLocationDao.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package at.specure.data.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Upsert
import at.specure.data.Tables
import at.specure.data.entity.CellLocationRecord
import timber.log.Timber

@Dao
interface CellLocationDao {

@Query("SELECT * from ${Tables.CELL_LOCATION} WHERE testUUID == :testUUID")
fun get(testUUID: String): List<CellLocationRecord>
@Query("SELECT * from ${Tables.CELL_LOCATION} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun get(testUUID: String?, signalChunkId: String?): List<CellLocationRecord>

@Insert(onConflict = OnConflictStrategy.IGNORE)
@Upsert
fun insert(cellLocation: CellLocationRecord): Long

@Query("DELETE FROM ${Tables.CELL_LOCATION} WHERE testUUID=:testUUID")
fun remove(testUUID: String)
@Query("DELETE FROM ${Tables.CELL_LOCATION} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun remove(testUUID: String?, signalChunkId: String?)

@Query("SELECT * FROM ${Tables.CELL_LOCATION} WHERE ((:testUUID!=null AND testUUID=:testUUID) OR (:signalChunkId!=null AND signalChunkId=:signalChunkId)) AND scramblingCode==:scramblingCode")
@Query("SELECT * FROM ${Tables.CELL_LOCATION} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId)) AND scramblingCode==:scramblingCode")
fun getSingleCellLocation(testUUID: String?, signalChunkId: String?, scramblingCode: Int): List<CellLocationRecord>

@Transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ interface ConnectivityStateDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun saveState(record: ConnectivityStateRecord)

@Query("SELECT * FROM ${Tables.CONNECTIVITY_STATE} WHERE uuid=:uuid")
@Query("SELECT * FROM ${Tables.CONNECTIVITY_STATE} WHERE uuid==:uuid")
fun getStates(uuid: String): List<ConnectivityStateRecord>

@Query("DELETE FROM ${Tables.CONNECTIVITY_STATE} WHERE uuid=:testUUID")
@Query("DELETE FROM ${Tables.CONNECTIVITY_STATE} WHERE uuid==:testUUID")
fun remove(testUUID: String)
}
8 changes: 4 additions & 4 deletions core/src/main/java/at/specure/data/dao/GeoLocationDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import at.specure.data.entity.GeoLocationRecord
@Dao
interface GeoLocationDao {

@Query("SELECT * from ${Tables.GEO_LOCATION} WHERE testUUID == :testUUID")
fun get(testUUID: String): List<GeoLocationRecord>
@Query("SELECT * from ${Tables.GEO_LOCATION} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun get(testUUID: String?, signalChunkId: String?): List<GeoLocationRecord>

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(geoLocations: GeoLocationRecord): Long

@Query("DELETE FROM ${Tables.GEO_LOCATION} WHERE testUUID=:testUUID")
fun remove(testUUID: String)
@Query("DELETE FROM ${Tables.GEO_LOCATION} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun remove(testUUID: String?, signalChunkId: String?)
}
6 changes: 3 additions & 3 deletions core/src/main/java/at/specure/data/dao/HistoryDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ abstract class HistoryDao {
clearHistory()
}

@Query("SELECT * FROM ${Tables.HISTORY} WHERE testUUID =:testUUID ORDER BY time DESC")
@Query("SELECT * FROM ${Tables.HISTORY} WHERE testUUID ==:testUUID ORDER BY time DESC")
abstract fun getItemByUUID(testUUID: String): History?

@Query("SELECT * FROM ${Tables.HISTORY} WHERE loopUUID =:loopUuid ORDER BY time DESC")
@Query("SELECT * FROM ${Tables.HISTORY} WHERE loopUUID ==:loopUuid ORDER BY time DESC")
abstract fun getItemByLoopUUID(loopUuid: String): List<History>

@Query("SELECT * FROM ${Tables.HISTORY} WHERE loopUUID =:loopUuid ORDER BY time DESC")
@Query("SELECT * FROM ${Tables.HISTORY} WHERE loopUUID ==:loopUuid ORDER BY time DESC")
abstract fun getItemByLoopUUIDLiveData(loopUuid: String): LiveData<List<History>?>
}
2 changes: 1 addition & 1 deletion core/src/main/java/at/specure/data/dao/HistoryMedianDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ abstract class HistoryMedianDao {
clearHistory()
}

@Query("SELECT * FROM ${Tables.HISTORY_MEDIAN} WHERE loopUUID =:loopUuid")
@Query("SELECT * FROM ${Tables.HISTORY_MEDIAN} WHERE loopUUID ==:loopUuid")
abstract fun getItemByLoopUUID(loopUuid: String): LiveData<HistoryLoopMedian?>
}
2 changes: 1 addition & 1 deletion core/src/main/java/at/specure/data/dao/JplResultsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ interface JplResultsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(voipTestResultRecord: VoipTestResultRecord)

@Query("DELETE FROM ${Tables.JPL} WHERE testUUID=:testUUID")
@Query("DELETE FROM ${Tables.JPL} WHERE testUUID==:testUUID")
fun remove(testUUID: String)
}
8 changes: 4 additions & 4 deletions core/src/main/java/at/specure/data/dao/PermissionStatusDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import at.specure.data.entity.PermissionStatusRecord
@Dao
interface PermissionStatusDao {

@Query("SELECT * from ${Tables.PERMISSIONS_STATUS} WHERE testUUID == :testUUID")
fun get(testUUID: String): List<PermissionStatusRecord>
@Query("SELECT * from ${Tables.PERMISSIONS_STATUS} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun get(testUUID: String?, signalChunkId: String?): List<PermissionStatusRecord>

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(permissionStatus: PermissionStatusRecord)

@Query("DELETE FROM ${Tables.PERMISSIONS_STATUS} WHERE testUUID=:testUUID")
fun remove(testUUID: String)
@Query("DELETE FROM ${Tables.PERMISSIONS_STATUS} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun remove(testUUID: String?, signalChunkId: String?)
}
12 changes: 6 additions & 6 deletions core/src/main/java/at/specure/data/dao/SignalDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import at.specure.data.entity.SignalRecord
@Dao
interface SignalDao {

@Query("SELECT * from ${Tables.SIGNAL} WHERE testUUID == :testUUID ORDER BY timeNanos")
fun get(testUUID: String): List<SignalRecord>
@Query("SELECT * from ${Tables.SIGNAL} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId)) ORDER BY timeNanos")
fun get(testUUID: String?, signalChunkId: String?): List<SignalRecord>

@Query("SELECT * from ${Tables.SIGNAL} WHERE testUUID == :testUUID AND cellUuid == :cellUUID ORDER BY timeNanos LIMIT 1")
fun getLatestForCell(testUUID: String, cellUUID: String): SignalRecord
@Query("SELECT * from ${Tables.SIGNAL} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId)) AND cellUuid == :cellUUID ORDER BY timeNanos LIMIT 1")
fun getLatestForCell(testUUID: String?, signalChunkId: String?, cellUUID: String): SignalRecord?

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(signal: SignalRecord)

@Query("DELETE FROM ${Tables.SIGNAL} WHERE testUUID=:testUUID")
fun remove(testUUID: String)
@Query("DELETE FROM ${Tables.SIGNAL} WHERE ((testUUID IS :testUUID) OR (signalChunkId IS :signalChunkId))")
fun remove(testUUID: String?, signalChunkId: String?)
}
23 changes: 21 additions & 2 deletions core/src/main/java/at/specure/data/dao/TestDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import androidx.room.Upsert
import at.specure.data.Tables
Expand Down Expand Up @@ -33,11 +34,29 @@ interface TestDao {
@Update
fun update(test: TestRecord)

@Transaction
fun deleteAll() {
deleteAllTest()
deleteAllWLAN()
deleteAllTelephony()
}
@Query("DELETE FROM ${Tables.TEST}")
fun deleteAll(): Int
fun deleteAllTest(): Int

@Query("DELETE FROM ${Tables.TEST_WLAN_RECORD}")
fun deleteAllWLAN(): Int

@Query("DELETE FROM ${Tables.TEST_TELEPHONY_RECORD}")
fun deleteAllTelephony(): Int

@Transaction
fun deleteTest(test: TestRecord) {
deleteTestRecord(test)
removeWlanRecord(test.uuid)
removeTelephonyInfo(test.uuid)
}
@Delete
fun deleteTest(test: TestRecord): Int
fun deleteTestRecord(test: TestRecord): Int

@Query("SELECT * FROM ${Tables.TEST} WHERE uuid == :uuid")
fun get(uuid: String): TestRecord?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import at.specure.data.Tables
data class CapabilitiesRecord(
@PrimaryKey(autoGenerate = true)
val id: Long = 0,
val testUUID: String,
val testUUID: String?,
val signalChunkId: String?,
val classificationCount: Int,
val qosSupportInfo: Boolean,
val rmbtHttpStatus: Boolean
Expand Down
6 changes: 0 additions & 6 deletions core/src/main/java/at/specure/data/entity/CellInfoRecord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import at.specure.info.cell.CellTechnology
parentColumns = [Columns.TEST_UUID_PARENT_COLUMN],
childColumns = ["testUUID"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = SignalMeasurementChunk::class,
parentColumns = [Columns.SIGNAL_MEASUREMENT_ID_PARENT_COLUMN],
childColumns = ["signalChunkId"],
onDelete = ForeignKey.CASCADE
)
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import at.specure.data.Tables
parentColumns = [Columns.TEST_UUID_PARENT_COLUMN],
childColumns = ["testUUID"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = SignalMeasurementChunk::class,
parentColumns = [Columns.SIGNAL_MEASUREMENT_ID_PARENT_COLUMN],
childColumns = ["signalChunkId"],
onDelete = ForeignKey.CASCADE
)
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import at.specure.data.Tables
data class GeoLocationRecord(
@PrimaryKey(autoGenerate = true)
val id: Long = 0,
val testUUID: String,
val testUUID: String?,
val signalChunkId: String?,
val latitude: Double,
val longitude: Double,
val provider: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import at.specure.data.Tables
data class PermissionStatusRecord(
@PrimaryKey(autoGenerate = true)
val id: Long = 0,
val testUUID: String,
val testUUID: String?,
val signalChunkId: String?,
val permissionName: String,
val status: Boolean
)
6 changes: 0 additions & 6 deletions core/src/main/java/at/specure/data/entity/SignalRecord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ import at.specure.info.strength.SignalSource
parentColumns = [Columns.TEST_UUID_PARENT_COLUMN],
childColumns = ["testUUID"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = SignalMeasurementChunk::class,
parentColumns = [Columns.SIGNAL_MEASUREMENT_ID_PARENT_COLUMN],
childColumns = ["signalChunkId"],
onDelete = ForeignKey.CASCADE
)
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import at.specure.data.Tables

@Entity(
tableName = Tables.TEST_TELEPHONY_RECORD,
foreignKeys = [
ForeignKey(
entity = TestRecord::class,
parentColumns = [Columns.TEST_UUID_PARENT_COLUMN],
childColumns = ["testUUID"],
onDelete = ForeignKey.CASCADE
)
]
)
data class TestTelephonyRecord(
@PrimaryKey
Expand Down
10 changes: 1 addition & 9 deletions core/src/main/java/at/specure/data/entity/TestWlanRecord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import at.specure.data.Columns
import at.specure.data.Tables

@Entity(
tableName = Tables.TEST_WLAN_RECORD,
foreignKeys = [
ForeignKey(
entity = TestRecord::class,
parentColumns = [Columns.TEST_UUID_PARENT_COLUMN],
childColumns = ["testUUID"],
onDelete = ForeignKey.CASCADE
)
]
tableName = Tables.TEST_WLAN_RECORD
)
class TestWlanRecord(

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface MeasurementRepository {

fun saveWlanInfo(uuid: String)

fun saveCapabilities(uuid: String)
fun saveCapabilities(uuid: String?, signalChunkId: String?)

fun savePermissionsStatus(uuid: String)
fun savePermissionsStatus(uuid: String?, signalChunkId: String?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ class MeasurementRepositoryImpl @Inject constructor(
private val permissionsWatcher: PermissionsWatcher
) : MeasurementRepository {

override fun savePermissionsStatus(uuid: String) {
override fun savePermissionsStatus(uuid: String?, signalChunkId: String?) {
val permissions = permissionsWatcher.allPermissions
permissions.forEach { permission ->
val permissionGranted = context.hasPermission(permission)
repository.savePermissionStatus(uuid, permission, permissionGranted)
repository.savePermissionStatus(uuid, signalChunkId, permission, permissionGranted)
}
}

override fun saveCapabilities(uuid: String) {
override fun saveCapabilities(uuid: String?, signalChunkId: String?) {
repository.saveCapabilities(
uuid,
signalChunkId,
config.capabilitiesRmbtHttp,
config.capabilitiesQosSupportsInfo,
config.capabilitiesClassificationCount
Expand Down

0 comments on commit 55f96ff

Please sign in to comment.