Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Refactor database
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Sep 7, 2023
1 parent d64d885 commit 6a4785d
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 79 deletions.
146 changes: 146 additions & 0 deletions app/schemas/com.sanmer.geomag.database.AppDatabase/3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"formatVersion": 1,
"database": {
"version": 3,
"identityHash": "7678badb9e186af6b5ddc145d6735298",
"entities": [
{
"tableName": "records",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`model` TEXT NOT NULL, `time` TEXT NOT NULL, `altitude` REAL NOT NULL, `latitude` REAL NOT NULL, `longitude` REAL NOT NULL, `declination` REAL NOT NULL, `declinationSV` REAL NOT NULL, `inclination` REAL NOT NULL, `inclinationSV` REAL NOT NULL, `horizontalIntensity` REAL NOT NULL, `horizontalSV` REAL NOT NULL, `northComponent` REAL NOT NULL, `northSV` REAL NOT NULL, `eastComponent` REAL NOT NULL, `eastSV` REAL NOT NULL, `verticalComponent` REAL NOT NULL, `verticalSV` REAL NOT NULL, `totalIntensity` REAL NOT NULL, `totalSV` REAL NOT NULL, PRIMARY KEY(`model`, `time`, `altitude`, `latitude`, `longitude`))",
"fields": [
{
"fieldPath": "key.model",
"columnName": "model",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "key.time",
"columnName": "time",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "key.altitude",
"columnName": "altitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "key.latitude",
"columnName": "latitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "key.longitude",
"columnName": "longitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.declination",
"columnName": "declination",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.declinationSV",
"columnName": "declinationSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.inclination",
"columnName": "inclination",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.inclinationSV",
"columnName": "inclinationSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.horizontalIntensity",
"columnName": "horizontalIntensity",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.horizontalSV",
"columnName": "horizontalSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.northComponent",
"columnName": "northComponent",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.northSV",
"columnName": "northSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.eastComponent",
"columnName": "eastComponent",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.eastSV",
"columnName": "eastSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.verticalComponent",
"columnName": "verticalComponent",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.verticalSV",
"columnName": "verticalSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.totalIntensity",
"columnName": "totalIntensity",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.totalSV",
"columnName": "totalSV",
"affinity": "REAL",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"model",
"time",
"altitude",
"latitude",
"longitude"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7678badb9e186af6b5ddc145d6735298')"
]
}
}
46 changes: 44 additions & 2 deletions app/src/main/kotlin/com/sanmer/geomag/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.room.migration.Migration
import com.sanmer.geomag.database.dao.RecordDao
import com.sanmer.geomag.database.entity.RecordEntity

@Database(entities = [RecordEntity::class], version = 2)
@Database(entities = [RecordEntity::class], version = 3)
abstract class AppDatabase : RoomDatabase() {
abstract fun recordDao(): RecordDao

Expand All @@ -17,7 +17,8 @@ abstract class AppDatabase : RoomDatabase() {
return Room.databaseBuilder(context,
AppDatabase::class.java, "geomag")
.addMigrations(
MIGRATION_1_2
MIGRATION_1_2,
MIGRATION_2_3
)
.build()
}
Expand Down Expand Up @@ -63,5 +64,46 @@ abstract class AppDatabase : RoomDatabase() {
it.execSQL("DROP TABLE records")
it.execSQL("ALTER TABLE records_new RENAME TO records")
}

private val MIGRATION_2_3 = Migration(2, 3) {
it.execSQL("CREATE TABLE IF NOT EXISTS records_new (" +
"model TEXT NOT NULL, " +
"time TEXT NOT NULL, " +
"altitude REAL NOT NULL, " +
"latitude REAL NOT NULL, " +
"longitude REAL NOT NULL, " +
"declination REAL NOT NULL, " +
"declinationSV REAL NOT NULL, " +
"inclination REAL NOT NULL, " +
"inclinationSV REAL NOT NULL, " +
"horizontalIntensity REAL NOT NULL, " +
"horizontalSV REAL NOT NULL, " +
"northComponent REAL NOT NULL, " +
"northSV REAL NOT NULL, " +
"eastComponent REAL NOT NULL, " +
"eastSV REAL NOT NULL, " +
"verticalComponent REAL NOT NULL, " +
"verticalSV REAL NOT NULL, " +
"totalIntensity REAL NOT NULL, " +
"totalSV REAL NOT NULL, " +
"PRIMARY KEY(model, time, altitude, latitude, longitude))")

it.execSQL("INSERT INTO records_new (" +
"model, time, altitude, latitude, longitude, " +
"declination, declinationSV, inclination, inclinationSV, " +
"horizontalIntensity, horizontalSV, northComponent, northSV, " +
"eastComponent, eastSV, verticalComponent, verticalSV, " +
"totalIntensity, totalSV) " +
"SELECT " +
"model, time, altitude, latitude, longitude, " +
"declination, declinationSV, inclination, inclinationSV, " +
"horizontalIntensity, horizontalSV, northComponent, northSV, " +
"eastComponent, eastSV, verticalComponent, verticalSV, " +
"totalIntensity, totalSV " +
"FROM records")

it.execSQL("DROP TABLE records")
it.execSQL("ALTER TABLE records_new RENAME TO records")
}
}
}
12 changes: 10 additions & 2 deletions app/src/main/kotlin/com/sanmer/geomag/database/dao/RecordDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ interface RecordDao {
@Query("SELECT * FROM records")
fun getAllAsFlow(): Flow<List<RecordEntity>>

@Query("SELECT * FROM records WHERE id = :id LIMIT 1")
fun getById(id: Double): RecordEntity
@Query(
"SELECT * FROM records " +
"WHERE model = :model " +
"AND time = :time " +
"AND altitude = :altitude " +
"AND latitude = :latitude " +
"AND longitude = :longitude " +
"LIMIT 1"
)
fun getByKey(model: String, time: String, altitude: Double, latitude: Double, longitude: Double): RecordEntity

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(value: RecordEntity)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.sanmer.geomag.database.entity

import androidx.room.Entity
import com.sanmer.geomag.model.MagneticFieldExt

@Entity(tableName = "values")
data class MagneticFieldEntity(
val declination: Double,
val declinationSV: Double,
val inclination: Double,
val inclinationSV: Double,
val horizontalIntensity: Double,
val horizontalSV: Double,
val northComponent: Double,
val northSV: Double,
val eastComponent: Double,
val eastSV: Double,
val verticalComponent: Double,
val verticalSV: Double,
val totalIntensity: Double,
val totalSV: Double
)

fun MagneticFieldExt.toEntity() = MagneticFieldEntity(
declination, declinationSV,
inclination, inclinationSV,
horizontalIntensity, horizontalSV,
northComponent, northSV,
eastComponent, eastSV,
verticalComponent, verticalSV,
totalIntensity, totalSV
)

fun MagneticFieldEntity.toMF() = MagneticFieldExt(
declination, declinationSV,
inclination, inclinationSV,
horizontalIntensity, horizontalSV,
northComponent, northSV,
eastComponent, eastSV,
verticalComponent, verticalSV,
totalIntensity, totalSV
)
75 changes: 11 additions & 64 deletions app/src/main/kotlin/com/sanmer/geomag/database/entity/Record.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,32 @@ package com.sanmer.geomag.database.entity

import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.sanmer.geomag.GeomagExt
import com.sanmer.geomag.model.MagneticFieldExt
import com.sanmer.geomag.model.Position
import com.sanmer.geomag.model.Record
import kotlinx.datetime.toLocalDateTime

@Entity(tableName = "records")
@Entity(
tableName = "records",
primaryKeys = ["model", "time", "altitude", "latitude", "longitude"]
)
data class RecordEntity(
@PrimaryKey val id: Double,
val model: String,
val time: String,
val altitude: Double,
val latitude: Double,
val longitude: Double,
@Embedded val key: RecordKey,
@Embedded val values: MagneticFieldEntity
)

val Record.primaryKey: Double get() {
val decimal = GeomagExt.toDecimalYears(time)
val position = position.altitude - position.latitude - position.longitude
return decimal + position + model.ordinal
}

fun Record.toEntity() = RecordEntity(
id = primaryKey,
model = model.name,
time = time.toString(),
altitude = position.altitude,
latitude = position.latitude,
longitude = position.longitude,
key = RecordKey(this),
values = values.toEntity(),
)

fun RecordEntity.toRecord() = Record(
model = GeomagExt.Models.valueOf(model),
time = time.toLocalDateTime(),
model = GeomagExt.Models.valueOf(key.model),
time = key.time.toLocalDateTime(),
position = Position(
latitude = latitude,
longitude = longitude,
altitude = altitude
latitude = key.latitude,
longitude = key.longitude,
altitude = key.altitude
),
values = values.toMF()
)

@Entity(tableName = "values")
data class MagneticFieldEntity(
val declination: Double,
val declinationSV: Double,
val inclination: Double,
val inclinationSV: Double,
val horizontalIntensity: Double,
val horizontalSV: Double,
val northComponent: Double,
val northSV: Double,
val eastComponent: Double,
val eastSV: Double,
val verticalComponent: Double,
val verticalSV: Double,
val totalIntensity: Double,
val totalSV: Double
)

fun MagneticFieldExt.toEntity() = MagneticFieldEntity(
declination, declinationSV,
inclination, inclinationSV,
horizontalIntensity, horizontalSV,
northComponent, northSV,
eastComponent, eastSV,
verticalComponent, verticalSV,
totalIntensity, totalSV
)

fun MagneticFieldEntity.toMF() = MagneticFieldExt(
declination, declinationSV,
inclination, inclinationSV,
horizontalIntensity, horizontalSV,
northComponent, northSV,
eastComponent, eastSV,
verticalComponent, verticalSV,
totalIntensity, totalSV
)
Loading

0 comments on commit 6a4785d

Please sign in to comment.