Kaynağa Gözat

added table for storing key-value pairs

sirekanian 3 yıl önce
ebeveyn
işleme
d984acd4e1

+ 1 - 0
app/schemas/MetaEntity.csv

@@ -0,0 +1 @@
+"date","2022-05-20"

+ 0 - 0
app/schemas/init.csv → app/schemas/WarmongerEntity.csv


+ 28 - 2
app/schemas/com.sirekanian.acf.data.local.Database/1.json

@@ -2,7 +2,7 @@
   "formatVersion": 1,
   "database": {
     "version": 1,
-    "identityHash": "67617e1b82bf6e5f548c1a717680e21b",
+    "identityHash": "16167d0ed0bd86c6fc41266aa0abe921",
     "entities": [
       {
         "ftsVersion": "FTS4",
@@ -45,12 +45,38 @@
         },
         "indices": [],
         "foreignKeys": []
+      },
+      {
+        "tableName": "MetaEntity",
+        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` TEXT NOT NULL, `value` TEXT NOT NULL, PRIMARY KEY(`key`))",
+        "fields": [
+          {
+            "fieldPath": "key",
+            "columnName": "key",
+            "affinity": "TEXT",
+            "notNull": true
+          },
+          {
+            "fieldPath": "value",
+            "columnName": "value",
+            "affinity": "TEXT",
+            "notNull": true
+          }
+        ],
+        "primaryKey": {
+          "columnNames": [
+            "key"
+          ],
+          "autoGenerate": false
+        },
+        "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, '67617e1b82bf6e5f548c1a717680e21b')"
+      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '16167d0ed0bd86c6fc41266aa0abe921')"
     ]
   }
 }

+ 4 - 2
app/schemas/init.sql

@@ -1,4 +1,6 @@
 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, '67617e1b82bf6e5f548c1a717680e21b');
+INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '16167d0ed0bd86c6fc41266aa0abe921');
+CREATE TABLE IF NOT EXISTS `MetaEntity` (`key` TEXT NOT NULL, `value` TEXT NOT NULL, PRIMARY KEY(`key`));
+.import --csv app/schemas/MetaEntity.csv MetaEntity
 CREATE VIRTUAL TABLE IF NOT EXISTS `WarmongerEntity` USING FTS4(`cyrillicName` TEXT NOT NULL, `name` TEXT NOT NULL, `notes` TEXT NOT NULL, tokenize=unicode61);
-.import --csv app/schemas/init.csv WarmongerEntity
+.import --csv app/schemas/WarmongerEntity.csv WarmongerEntity

BIN
app/src/main/assets/warmongers.db


+ 2 - 1
app/src/main/java/com/sirekanian/acf/App.kt

@@ -9,8 +9,9 @@ import com.sirekanian.acf.data.local.Database
 class App : Application() {
     private val db by lazy {
         Room.databaseBuilder(this, Database::class.java, "database")
+            .fallbackToDestructiveMigration()
             .createFromAsset("warmongers.db")
             .build()
     }
-    val repository: Repository by lazy { RepositoryImpl(db.warmongerDao()) }
+    val repository: Repository by lazy { RepositoryImpl(db.getWarmongerDao()) }
 }

+ 3 - 2
app/src/main/java/com/sirekanian/acf/data/local/Database.kt

@@ -3,9 +3,10 @@ package com.sirekanian.acf.data.local
 import androidx.room.Database
 import androidx.room.RoomDatabase
 
-@Database(entities = [WarmongerEntity::class], version = 1)
+@Database(entities = [WarmongerEntity::class, MetaEntity::class], version = 1)
 abstract class Database : RoomDatabase() {
 
-    abstract fun warmongerDao(): WarmongerDao
+    abstract fun getWarmongerDao(): WarmongerDao
+    abstract fun getMetaDao(): MetaDao
 
 }

+ 17 - 0
app/src/main/java/com/sirekanian/acf/data/local/MetaDao.kt

@@ -0,0 +1,17 @@
+package com.sirekanian.acf.data.local
+
+import androidx.room.Dao
+import androidx.room.Insert
+import androidx.room.OnConflictStrategy
+import androidx.room.Query
+
+@Dao
+interface MetaDao {
+
+    @Query("SELECT value FROM MetaEntity WHERE `key` = :key LIMIT 1")
+    suspend fun find(key: String): String?
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    suspend fun put(meta: MetaEntity)
+
+}

+ 11 - 0
app/src/main/java/com/sirekanian/acf/data/local/MetaEntity.kt

@@ -0,0 +1,11 @@
+package com.sirekanian.acf.data.local
+
+import androidx.room.Entity
+import androidx.room.PrimaryKey
+
+@Entity
+class MetaEntity(
+    @PrimaryKey
+    val key: String,
+    val value: String,
+)

+ 8 - 0
app/src/main/java/com/sirekanian/acf/data/remote/MetaDto.kt

@@ -0,0 +1,8 @@
+package com.sirekanian.acf.data.remote
+
+import kotlinx.serialization.Serializable
+
+@Serializable
+class MetaDto(
+    val date: String,
+)

+ 8 - 2
app/src/main/java/com/sirekanian/acf/data/remote/Network.kt

@@ -9,7 +9,10 @@ import io.ktor.client.plugins.contentnegotiation.*
 import io.ktor.client.request.*
 import io.ktor.serialization.kotlinx.json.*
 
-private const val ACF_URL = "https://sirekanian.github.io/warmongers.json"
+private const val ENDPOINT = "https://sirekanian.github.io/warmongr"
+private const val DATA_URL = "$ENDPOINT/data.json"
+private const val META_URL = "$ENDPOINT/meta.json"
+
 private val httpClient = HttpClient {
     install(ContentNegotiation) {
         json()
@@ -20,4 +23,7 @@ private val httpClient = HttpClient {
 }
 
 suspend fun getWarmongers(listener: ProgressListener): List<WarmongerDto> =
-    httpClient.get(ACF_URL) { onDownload(listener) }.body()
+    httpClient.get(DATA_URL) { onDownload(listener) }.body()
+
+suspend fun getMeta(): MetaDto =
+    httpClient.get(META_URL).body()

+ 25 - 10
update.sh

@@ -2,24 +2,39 @@
 
 set -e
 
-JSON=$(find app/schemas/com.sirekanian.acf.data.local.Database/*.json | sort -V | tail -1)
-TABLE="WarmongerEntity"
+ENDPOINT="https://sirekanian.github.io/warmongr"
+SCHEMAS="app/schemas/com.sirekanian.acf.data.local.Database"
+SCHEMA=$(find "$SCHEMAS" -name "*.json" | sort -V | tail -1)
 
-wget --header="Accept-Encoding: gzip" -qO- https://sirekanian.github.io/warmongers.json | gunzip |
+# transform meta.json to csv
+wget -qO- "$ENDPOINT/meta.json" |
+  jq -r 'to_entries[] | [.key, .value] | @csv' \
+    >"app/schemas/MetaEntity.csv"
+
+# transform data.json to csv
+wget --header="Accept-Encoding: gzip" -qO- "$ENDPOINT/data.json" | gunzip |
   jq -r 'map([.["0"],.["1"],.["4"]])[] | @csv' \
-    >"app/schemas/init.csv"
+    >"app/schemas/WarmongerEntity.csv"
 
-jq -r ".database.setupQueries[]" "$JSON" |
+# copy setupQueries from schema
+jq -r ".database.setupQueries[]" "$SCHEMA" |
   sed 's/$/;/' \
     >app/schemas/init.sql
 
-jq -r ".database.entities[] | select(.tableName==\"$TABLE\") | .createSql" "$JSON" |
-  sed "s/\${TABLE_NAME}/$TABLE/" |
-  sed 's/$/;/' \
+for TABLE in MetaEntity WarmongerEntity; do
+
+  # copy createSql from schema
+  jq -r ".database.entities[] | select(.tableName==\"$TABLE\") | .createSql" "$SCHEMA" |
+    sed "s/\${TABLE_NAME}/$TABLE/" |
+    sed 's/$/;/' \
+      >>app/schemas/init.sql
+
+  # generate import csv command
+  echo ".import --csv app/schemas/$TABLE.csv $TABLE" \
     >>app/schemas/init.sql
 
-echo ".import --csv app/schemas/init.csv $TABLE" \
-  >>app/schemas/init.sql
+done
 
+# recreate pre-packaged database
 rm -f app/src/main/assets/warmongers.db
 sqlite3 app/src/main/assets/warmongers.db <app/schemas/init.sql