mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-15 21:03:50 +00:00
one more update to versions
This commit is contained in:
parent
b5072486b4
commit
7f813a519b
@ -18,19 +18,16 @@ class StandardVersionsRepo<T>(
|
||||
onCreate: suspend T.() -> Unit,
|
||||
onUpdate: suspend T.(from: Int, to: Int) -> Unit
|
||||
) {
|
||||
var savedVersion = proxy.getTableVersion(tableName)
|
||||
if (savedVersion == null) {
|
||||
var currentVersion = proxy.getTableVersion(tableName)
|
||||
if (currentVersion == null) {
|
||||
proxy.database.onCreate()
|
||||
proxy.updateTableVersion(tableName, version)
|
||||
} else {
|
||||
while (savedVersion != null && savedVersion < version) {
|
||||
val newVersion = savedVersion + 1
|
||||
|
||||
proxy.database.onUpdate(savedVersion, newVersion)
|
||||
|
||||
proxy.updateTableVersion(tableName, newVersion)
|
||||
savedVersion = newVersion
|
||||
}
|
||||
while (currentVersion == null || currentVersion < version) {
|
||||
val oldVersion = currentVersion ?: 0
|
||||
currentVersion = oldVersion + 1
|
||||
proxy.database.onUpdate(oldVersion, currentVersion)
|
||||
|
||||
proxy.updateTableVersion(tableName, currentVersion)
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import android.database.DatabaseErrorHandler
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import dev.inmo.micro_utils.coroutines.safely
|
||||
import dev.inmo.micro_utils.repos.keyvalue.keyValueStore
|
||||
import dev.inmo.micro_utils.repos.versions.*
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.resume
|
||||
@ -15,7 +16,8 @@ class StandardSQLHelper(
|
||||
name: String,
|
||||
factory: SQLiteDatabase.CursorFactory? = null,
|
||||
version: Int = 1,
|
||||
errorHandler: DatabaseErrorHandler? = null
|
||||
errorHandler: DatabaseErrorHandler? = null,
|
||||
useSharedPreferencesForVersions: Boolean = false
|
||||
) {
|
||||
val sqlOpenHelper = object : SQLiteOpenHelper(context, name, factory, version, errorHandler) {
|
||||
override fun onCreate(db: SQLiteDatabase?) {}
|
||||
@ -23,7 +25,16 @@ class StandardSQLHelper(
|
||||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {}
|
||||
}
|
||||
val versionsRepo: VersionsRepo<SQLiteOpenHelper> by lazy {
|
||||
StandardVersionsRepo(AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper))
|
||||
StandardVersionsRepo(
|
||||
if (useSharedPreferencesForVersions) {
|
||||
KeyValueBasedVersionsRepoProxy(
|
||||
context.keyValueStore("AndroidSPStandardVersionsRepo"),
|
||||
sqlOpenHelper
|
||||
)
|
||||
} else {
|
||||
AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun <T> writableTransaction(block: suspend SQLiteDatabase.() -> T): T = sqlOpenHelper.writableTransaction(block)
|
||||
|
Loading…
Reference in New Issue
Block a user