mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
new mechanism of transactions
This commit is contained in:
parent
6bbe3a271f
commit
3de5558ed4
@ -10,6 +10,8 @@
|
|||||||
to get access to some end-store
|
to get access to some end-store
|
||||||
* Add default realization of `StandardVersionsRepoProxy` based on `KeyValue` repos
|
* Add default realization of `StandardVersionsRepoProxy` based on `KeyValue` repos
|
||||||
* Add realizations of `StandardVersionsRepoProxy` for exposed and android (`SQL` and `SharedPreferences`)
|
* Add realizations of `StandardVersionsRepoProxy` for exposed and android (`SQL` and `SharedPreferences`)
|
||||||
|
* `Commons`:
|
||||||
|
* In Android
|
||||||
|
|
||||||
## 0.4.3
|
## 0.4.3
|
||||||
|
|
||||||
|
@ -2,15 +2,22 @@ package dev.inmo.micro_utils.repos
|
|||||||
|
|
||||||
import android.database.sqlite.SQLiteDatabase
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import dev.inmo.micro_utils.coroutines.safely
|
import dev.inmo.micro_utils.coroutines.safely
|
||||||
|
import kotlinx.coroutines.newSingleThreadContext
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.coroutineContext
|
||||||
|
|
||||||
|
object InTransaction: CoroutineContext.Element, CoroutineContext.Key<InTransaction> {
|
||||||
|
override val key: CoroutineContext.Key<InTransaction> = InTransaction
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun <T> SQLiteDatabase.transaction(block: suspend SQLiteDatabase.() -> T): T {
|
suspend fun <T> SQLiteDatabase.transaction(block: suspend SQLiteDatabase.() -> T): T {
|
||||||
return withContext(DatabaseCoroutineContext) {
|
return when {
|
||||||
when {
|
coroutineContext[InTransaction] == InTransaction -> {
|
||||||
inTransaction() -> {
|
block()
|
||||||
block()
|
}
|
||||||
}
|
else -> {
|
||||||
else -> {
|
withContext(InTransaction) {
|
||||||
beginTransaction()
|
beginTransaction()
|
||||||
safely(
|
safely(
|
||||||
{
|
{
|
||||||
|
@ -26,7 +26,7 @@ class AndroidSQLStandardVersionsRepoProxy(
|
|||||||
createTable(
|
createTable(
|
||||||
tableName,
|
tableName,
|
||||||
tableNameColumnName to ColumnType.Text.NOT_NULLABLE,
|
tableNameColumnName to ColumnType.Text.NOT_NULLABLE,
|
||||||
tableVersionColumnName to ColumnType.Numeric.INTEGER()
|
tableVersionColumnName to ColumnType.Numeric.INTEGER()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user