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