Database: use a simpler method for identity usages
This commit is contained in:
parent
11bea0e585
commit
9a91c72d1e
|
@ -2,7 +2,7 @@
|
||||||
"formatVersion": 1,
|
"formatVersion": 1,
|
||||||
"database": {
|
"database": {
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"identityHash": "70da3095877de4a82021855471523b90",
|
"identityHash": "ffa6a7f7cce2d67541e0fbe28441e780",
|
||||||
"entities": [
|
"entities": [
|
||||||
{
|
{
|
||||||
"tableName": "HistoryEntry",
|
"tableName": "HistoryEntry",
|
||||||
|
@ -38,7 +38,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"tableName": "Identity",
|
"tableName": "Identity",
|
||||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `key` TEXT NOT NULL, `name` TEXT)",
|
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `key` TEXT NOT NULL, `name` TEXT, `urls` TEXT NOT NULL)",
|
||||||
"fields": [
|
"fields": [
|
||||||
{
|
{
|
||||||
"fieldPath": "id",
|
"fieldPath": "id",
|
||||||
|
@ -57,38 +57,12 @@
|
||||||
"columnName": "name",
|
"columnName": "name",
|
||||||
"affinity": "TEXT",
|
"affinity": "TEXT",
|
||||||
"notNull": false
|
"notNull": false
|
||||||
}
|
|
||||||
],
|
|
||||||
"primaryKey": {
|
|
||||||
"columnNames": [
|
|
||||||
"id"
|
|
||||||
],
|
|
||||||
"autoGenerate": true
|
|
||||||
},
|
|
||||||
"indices": [],
|
|
||||||
"foreignKeys": []
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"tableName": "IdentityUsage",
|
|
||||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `uri` TEXT NOT NULL, `identityId` INTEGER NOT NULL)",
|
|
||||||
"fields": [
|
|
||||||
{
|
|
||||||
"fieldPath": "id",
|
|
||||||
"columnName": "id",
|
|
||||||
"affinity": "INTEGER",
|
|
||||||
"notNull": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldPath": "uri",
|
"fieldPath": "urls",
|
||||||
"columnName": "uri",
|
"columnName": "urls",
|
||||||
"affinity": "TEXT",
|
"affinity": "TEXT",
|
||||||
"notNull": true
|
"notNull": true
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldPath": "identityId",
|
|
||||||
"columnName": "identityId",
|
|
||||||
"affinity": "INTEGER",
|
|
||||||
"notNull": true
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"primaryKey": {
|
"primaryKey": {
|
||||||
|
@ -104,7 +78,7 @@
|
||||||
"views": [],
|
"views": [],
|
||||||
"setupQueries": [
|
"setupQueries": [
|
||||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
"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, '70da3095877de4a82021855471523b90')"
|
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ffa6a7f7cce2d67541e0fbe28441e780')"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,18 +1,18 @@
|
||||||
package dev.lowrespalmtree.comet
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.Base64
|
||||||
|
import androidx.room.*
|
||||||
import androidx.room.Database
|
import androidx.room.Database
|
||||||
import androidx.room.Room
|
|
||||||
import androidx.room.RoomDatabase
|
|
||||||
|
|
||||||
@Database(
|
@Database(
|
||||||
entities = [
|
entities = [
|
||||||
History.HistoryEntry::class,
|
History.HistoryEntry::class,
|
||||||
Identities.Identity::class,
|
Identities.Identity::class,
|
||||||
Identities.IdentityUsage::class,
|
|
||||||
],
|
],
|
||||||
version = 1
|
version = 1
|
||||||
)
|
)
|
||||||
|
@TypeConverters(Converters::class)
|
||||||
abstract class AppDatabase : RoomDatabase() {
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
abstract fun historyEntryDao(): History.HistoryEntryDao
|
abstract fun historyEntryDao(): History.HistoryEntryDao
|
||||||
abstract fun identityDao(): Identities.IdentityDao
|
abstract fun identityDao(): Identities.IdentityDao
|
||||||
|
@ -26,4 +26,18 @@ object Database {
|
||||||
return
|
return
|
||||||
INSTANCE = Room.databaseBuilder(context, AppDatabase::class.java, "comet.db").build()
|
INSTANCE = Room.databaseBuilder(context, AppDatabase::class.java, "comet.db").build()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typealias UrlList = ArrayList<String>
|
||||||
|
|
||||||
|
class Converters {
|
||||||
|
@TypeConverter
|
||||||
|
fun fromUrlList(value: UrlList?): String? =
|
||||||
|
value?.joinToString("-") {
|
||||||
|
Base64.encodeToString(it.encodeToByteArray(), Base64.DEFAULT)
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun stringToUrlList(value: String?): UrlList? =
|
||||||
|
value?.split("-")?.map { Base64.decode(it, Base64.DEFAULT).decodeToString() } as UrlList?
|
||||||
}
|
}
|
|
@ -16,16 +16,8 @@ object Identities {
|
||||||
val key: String,
|
val key: String,
|
||||||
/** Label for this identity. */
|
/** Label for this identity. */
|
||||||
var name: String?,
|
var name: String?,
|
||||||
)
|
/** URL paths configured to use this identity. */
|
||||||
|
var urls: UrlList
|
||||||
@Entity
|
|
||||||
data class IdentityUsage(
|
|
||||||
/** ID. */
|
|
||||||
@PrimaryKey(autoGenerate = true) val id: Int,
|
|
||||||
/** URL path where an identity can be used. */
|
|
||||||
val uri: String,
|
|
||||||
/** ID of the Identity to use. */
|
|
||||||
val identityId: Int
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
|
@ -42,15 +34,12 @@ object Identities {
|
||||||
@Update
|
@Update
|
||||||
suspend fun update(vararg identities: Identity)
|
suspend fun update(vararg identities: Identity)
|
||||||
|
|
||||||
@Query("SELECT * FROM IdentityUsage WHERE :identityId = identityId")
|
|
||||||
suspend fun getUsagesFor(identityId: Int): List<IdentityUsage>
|
|
||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
suspend fun delete(vararg identities: Identity)
|
suspend fun delete(vararg identities: Identity)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun insert(key: String, name: String? = null): Long =
|
suspend fun insert(key: String, name: String? = null): Long =
|
||||||
Database.INSTANCE.identityDao().insert(Identity(0, key, name))
|
Database.INSTANCE.identityDao().insert(Identity(0, key, name, arrayListOf()))
|
||||||
|
|
||||||
suspend fun get(id: Long): Identity? =
|
suspend fun get(id: Long): Identity? =
|
||||||
Database.INSTANCE.identityDao().get(id)
|
Database.INSTANCE.identityDao().get(id)
|
||||||
|
|
Reference in a new issue