ソースを参照

Added key value table

Vadik Sirekanyan 2 年 前
コミット
7015e7537e

+ 44 - 0
app/src/main/java/org/sirekanyan/outline/db/KeyValueDao.kt

@@ -0,0 +1,44 @@
+package org.sirekanyan.outline.db
+
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.remember
+import androidx.compose.ui.platform.LocalContext
+import app.cash.sqldelight.coroutines.asFlow
+import app.cash.sqldelight.coroutines.mapToOneNotNull
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.withContext
+import org.sirekanyan.outline.app
+
+@Composable
+fun rememberKeyValueDao(): KeyValueDao {
+    val database = LocalContext.current.app().database
+    return remember { KeyValueDao(database) }
+}
+
+class KeyValueDao(database: OutlineDatabase) {
+
+    private val queries = database.keyValueQueries
+
+    fun observe(key: String): Flow<String?> =
+        queries.select(key).asFlow().mapToOneNotNull(Dispatchers.IO).map { it.content }
+
+    suspend fun find(key: String): String? =
+        withContext(Dispatchers.IO) {
+            queries.select(key).executeAsOneOrNull()?.content
+        }
+
+    suspend fun put(key: String, value: String) {
+        withContext(Dispatchers.IO) {
+            queries.insert(key, value)
+        }
+    }
+
+    suspend fun delete(key: String) {
+        withContext(Dispatchers.IO) {
+            queries.delete(key)
+        }
+    }
+
+}

BIN
app/src/main/sqldelight/databases/2.db


+ 5 - 0
app/src/main/sqldelight/migrations/1.sqm

@@ -1,2 +1,7 @@
 ALTER TABLE ApiUrl ADD COLUMN insecure INTEGER NOT NULL DEFAULT 0;
 UPDATE ApiUrl SET insecure = 1;
+
+CREATE TABLE IF NOT EXISTS KeyValue (
+  id TEXT NOT NULL PRIMARY KEY,
+  content TEXT
+);

+ 13 - 0
app/src/main/sqldelight/org/sirekanyan/outline/db/model/KeyValue.sq

@@ -0,0 +1,13 @@
+CREATE TABLE IF NOT EXISTS KeyValue (
+  id TEXT NOT NULL PRIMARY KEY,
+  content TEXT
+);
+
+select:
+SELECT content FROM KeyValue WHERE id = ?;
+
+insert:
+INSERT OR REPLACE INTO KeyValue (id, content) VALUES (?, ?);
+
+delete:
+DELETE FROM KeyValue WHERE id = ?;