From 4a0b761867002224aac4e77346e3567b16ca5a3d Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 13 Jul 2018 17:10:13 +0200 Subject: [PATCH 1/4] Create folder_extra_values table --- .../com/fsck/k9/mailstore/LocalStore.java | 2 +- .../k9/mailstore/StoreSchemaDefinition.java | 16 +++++++++++++ .../k9/mailstore/migrations/MigrationTo64.kt | 24 +++++++++++++++++++ .../k9/mailstore/migrations/Migrations.java | 2 ++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java b/app/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java index 7082574cd..ab51f42c5 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java +++ b/app/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java @@ -179,7 +179,7 @@ public class LocalStore { */ private static final int THREAD_FLAG_UPDATE_BATCH_SIZE = 500; - public static final int DB_VERSION = 63; + public static final int DB_VERSION = 64; private final Context context; private final ContentResolver contentResolver; diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java b/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java index c043f0872..0091a860d 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java +++ b/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java @@ -97,6 +97,15 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition { db.execSQL("DROP INDEX IF EXISTS folder_server_id"); db.execSQL("CREATE INDEX folder_server_id ON folders (server_id)"); + db.execSQL("DROP TABLE IF EXISTS folder_extra_values"); + db.execSQL("CREATE TABLE folder_extra_values (" + + "folder_id INTEGER NOT NULL, " + + "name TEXT NOT NULL, " + + "value_text TEXT, " + + "value_integer INTEGER, " + + "PRIMARY KEY (folder_id, name)" + + ")"); + db.execSQL("DROP TABLE IF EXISTS messages"); db.execSQL("CREATE TABLE messages (" + "id INTEGER PRIMARY KEY, " + @@ -203,6 +212,13 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition { db.execSQL("DROP TRIGGER IF EXISTS delete_folder"); db.execSQL("CREATE TRIGGER delete_folder BEFORE DELETE ON folders BEGIN DELETE FROM messages WHERE old.id = folder_id; END;"); + db.execSQL("DROP TRIGGER IF EXISTS delete_folder_extra_values"); + db.execSQL("CREATE TRIGGER delete_folder_extra_values " + + "BEFORE DELETE ON folders " + + "BEGIN " + + "DELETE FROM folder_extra_values WHERE old.id = folder_id; " + + "END;"); + db.execSQL("DROP TRIGGER IF EXISTS delete_message"); db.execSQL("CREATE TRIGGER delete_message " + "BEFORE DELETE ON messages " + diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt new file mode 100644 index 000000000..bd90199ce --- /dev/null +++ b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt @@ -0,0 +1,24 @@ +package com.fsck.k9.mailstore.migrations + + +import android.database.sqlite.SQLiteDatabase + + +internal object MigrationTo64 { + @JvmStatic + fun addFolderExtraValuesTable(db: SQLiteDatabase) { + db.execSQL("CREATE TABLE folder_extra_values (" + + "folder_id INTEGER NOT NULL, " + + "name TEXT NOT NULL, " + + "value_text TEXT, " + + "value_integer INTEGER, " + + "PRIMARY KEY (folder_id, name)" + + ")") + + db.execSQL("CREATE TRIGGER delete_folder_extra_values " + + "BEFORE DELETE ON folders " + + "BEGIN " + + "DELETE FROM folder_extra_values WHERE old.id = folder_id; " + + "END;") + } +} diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java index a98ce708e..d3dcbc255 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java +++ b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java @@ -83,6 +83,8 @@ public class Migrations { MigrationTo61.removeErrorsFolder(db); case 61: MigrationTo62.addServerIdColumnToFoldersTable(db); + case 63: + MigrationTo64.addFolderExtraValuesTable(db); } if (shouldBuildFtsTable) { From 6bb8805aa996e1499bc3eeed6ff3a780c970151d Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 13 Jul 2018 17:19:08 +0200 Subject: [PATCH 2/4] Update BackendFolder to allow getting/setting extra folder values --- .../com/fsck/k9/mailstore/K9BackendFolder.kt | 66 +++++++++++++++++++ .../com/fsck/k9/backend/api/BackendFolder.kt | 4 ++ 2 files changed, 70 insertions(+) diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendFolder.kt b/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendFolder.kt index dbce30a6e..f48e2471c 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendFolder.kt +++ b/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendFolder.kt @@ -234,6 +234,36 @@ class K9BackendFolder( } } + override fun getFolderExtraString(name: String): String? { + return database.getStringOrNull( + table = "folder_extra_values", + column = "value_string" + ) + } + + override fun setFolderExtraString(name: String, value: String) { + database.setString( + table = "folder_extra_values", + column = "value_string", + value = value + ) + } + + override fun getFolderExtraNumber(name: String): Long? { + return database.getLongOrNull( + table = "folder_extra_values", + column = "value_integer" + ) + } + + override fun setFolderExtraNumber(name: String, value: Long) { + database.setLong( + table = "folder_extra_values", + column = "value_integer", + value = value + ) + } + private fun LockableDatabase.getString( table: String = "folders", @@ -253,6 +283,24 @@ class K9BackendFolder( } } + private fun LockableDatabase.getStringOrNull( + table: String = "folders", + column: String, + selection: String = "id = ?", + vararg selectionArgs: String = arrayOf(databaseId) + ): String? { + return execute(false) { db -> + val cursor = db.query(table, arrayOf(column), selection, selectionArgs, null, null, null) + cursor.use { + if (it.moveToFirst()) { + it.getStringOrNull(0) + } else { + null + } + } + } + } + private fun LockableDatabase.setString( table: String = "folders", column: String, @@ -281,6 +329,24 @@ class K9BackendFolder( } } + private fun LockableDatabase.getLongOrNull( + table: String = "folders", + column: String, + selection: String = "id = ?", + vararg selectionArgs: String = arrayOf(databaseId) + ): Long? { + return execute(false) { db -> + val cursor = db.query(table, arrayOf(column), selection, selectionArgs, null, null, null) + cursor.use { + if (it.moveToFirst()) { + it.getLongOrNull(0) + } else { + null + } + } + } + } + private fun LockableDatabase.setLong( table: String = "folders", column: String, diff --git a/backend/api/src/main/java/com/fsck/k9/backend/api/BackendFolder.kt b/backend/api/src/main/java/com/fsck/k9/backend/api/BackendFolder.kt index ec95142e6..cb1e05b36 100644 --- a/backend/api/src/main/java/com/fsck/k9/backend/api/BackendFolder.kt +++ b/backend/api/src/main/java/com/fsck/k9/backend/api/BackendFolder.kt @@ -28,6 +28,10 @@ interface BackendFolder { fun getLatestOldMessageSeenTime(): Date fun setLatestOldMessageSeenTime(date: Date) fun getOldestMessageDate(): Date? + fun getFolderExtraString(name: String): String? + fun setFolderExtraString(name: String, value: String) + fun getFolderExtraNumber(name: String): Long? + fun setFolderExtraNumber(name: String, value: Long) enum class MoreMessages { UNKNOWN, From e56b03d7bc49ae722d23870c50f9a1973a78a97f Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 15 Jul 2018 15:04:15 +0200 Subject: [PATCH 3/4] Create account_extra_values table --- .../java/com/fsck/k9/mailstore/StoreSchemaDefinition.java | 7 +++++++ .../com/fsck/k9/mailstore/migrations/MigrationTo64.kt | 8 +++++++- .../java/com/fsck/k9/mailstore/migrations/Migrations.java | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java b/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java index 0091a860d..75cc5927e 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java +++ b/app/core/src/main/java/com/fsck/k9/mailstore/StoreSchemaDefinition.java @@ -73,6 +73,13 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition { } private static void dbCreateDatabaseFromScratch(SQLiteDatabase db) { + db.execSQL("DROP TABLE IF EXISTS account_extra_values"); + db.execSQL("CREATE TABLE account_extra_values (" + + "name TEXT NOT NULL PRIMARY KEY, " + + "value_text TEXT, " + + "value_integer INTEGER " + + ")"); + db.execSQL("DROP TABLE IF EXISTS folders"); db.execSQL("CREATE TABLE folders (" + "id INTEGER PRIMARY KEY," + diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt index bd90199ce..eeadfa3c6 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt +++ b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo64.kt @@ -6,7 +6,13 @@ import android.database.sqlite.SQLiteDatabase internal object MigrationTo64 { @JvmStatic - fun addFolderExtraValuesTable(db: SQLiteDatabase) { + fun addExtraValuesTables(db: SQLiteDatabase) { + db.execSQL("CREATE TABLE account_extra_values (" + + "name TEXT NOT NULL PRIMARY KEY, " + + "value_text TEXT, " + + "value_integer INTEGER " + + ")") + db.execSQL("CREATE TABLE folder_extra_values (" + "folder_id INTEGER NOT NULL, " + "name TEXT NOT NULL, " + diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java index d3dcbc255..f68d79f07 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java +++ b/app/core/src/main/java/com/fsck/k9/mailstore/migrations/Migrations.java @@ -84,7 +84,7 @@ public class Migrations { case 61: MigrationTo62.addServerIdColumnToFoldersTable(db); case 63: - MigrationTo64.addFolderExtraValuesTable(db); + MigrationTo64.addExtraValuesTables(db); } if (shouldBuildFtsTable) { From 510478046854891793a2353674bcfc2b93beb458 Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 15 Jul 2018 15:48:39 +0200 Subject: [PATCH 4/4] Update BackendStorage to allow getting/setting extra values --- .../com/fsck/k9/mailstore/K9BackendStorage.kt | 64 ++++++++++++++++++- .../com/fsck/k9/backend/api/BackendStorage.kt | 5 ++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendStorage.kt b/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendStorage.kt index 41c7954fd..ddb5047ca 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendStorage.kt +++ b/app/core/src/main/java/com/fsck/k9/mailstore/K9BackendStorage.kt @@ -1,5 +1,8 @@ package com.fsck.k9.mailstore +import android.content.ContentValues +import android.database.Cursor +import androidx.core.database.getStringOrNull import com.fsck.k9.Account import com.fsck.k9.Preferences import com.fsck.k9.backend.api.BackendFolder @@ -8,9 +11,68 @@ import com.fsck.k9.backend.api.BackendStorage class K9BackendStorage( private val preferences: Preferences, private val account: Account, - private val localStore: LocalStore) : BackendStorage { + private val localStore: LocalStore +) : BackendStorage { + private val database = localStore.database + override fun getFolder(folderServerId: String): BackendFolder { return K9BackendFolder(preferences, account, localStore, folderServerId) } + + override fun getExtraString(name: String): String? { + return database.execute(false) { db -> + val cursor = db.query( + "account_extra_values", + arrayOf("value_string"), + "name = ?", + arrayOf(name), + null, null, null) + cursor.use { + if (it.moveToFirst()) { + it.getStringOrNull(0) + } else { + null + } + } + } + } + + override fun setExtraString(name: String, value: String) { + database.execute(false) { db -> + val contentValues = ContentValues().apply { + put("value_string", value) + } + db.update("account_extra_values", contentValues, "name = ?", arrayOf(name)) + } + } + + override fun getExtraNumber(name: String): Long? { + return database.execute(false) { db -> + val cursor = db.query( + "account_extra_values", + arrayOf("value_integer"), + "name = ?", + arrayOf(name), + null, null, null) + cursor.use { + if (it.moveToFirst()) { + it.getLongOrNull(0) + } else { + null + } + } + } + } + + override fun setExtraNumber(name: String, value: Long) { + database.execute(false) { db -> + val contentValues = ContentValues().apply { + put("value_integer", value) + } + db.update("account_extra_values", contentValues, "name = ?", arrayOf(name)) + } + } + + private fun Cursor.getLongOrNull(columnIndex: Int): Long? = if (isNull(columnIndex)) null else getLong(columnIndex) } diff --git a/backend/api/src/main/java/com/fsck/k9/backend/api/BackendStorage.kt b/backend/api/src/main/java/com/fsck/k9/backend/api/BackendStorage.kt index e9f57accd..be531bbe5 100644 --- a/backend/api/src/main/java/com/fsck/k9/backend/api/BackendStorage.kt +++ b/backend/api/src/main/java/com/fsck/k9/backend/api/BackendStorage.kt @@ -2,4 +2,9 @@ package com.fsck.k9.backend.api interface BackendStorage { fun getFolder(folderServerId: String): BackendFolder + + fun getExtraString(name: String): String? + fun setExtraString(name: String, value: String) + fun getExtraNumber(name: String): Long? + fun setExtraNumber(name: String, value: Long) }