new mechanism of transactions

This commit is contained in:
2020-11-22 21:51:33 +06:00
parent 6bbe3a271f
commit 3de5558ed4
3 changed files with 16 additions and 7 deletions

View File

@@ -2,15 +2,22 @@ package dev.inmo.micro_utils.repos
import android.database.sqlite.SQLiteDatabase
import dev.inmo.micro_utils.coroutines.safely
import kotlinx.coroutines.newSingleThreadContext
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 {
return withContext(DatabaseCoroutineContext) {
when {
inTransaction() -> {
block()
}
else -> {
return when {
coroutineContext[InTransaction] == InTransaction -> {
block()
}
else -> {
withContext(InTransaction) {
beginTransaction()
safely(
{

View File

@@ -26,7 +26,7 @@ class AndroidSQLStandardVersionsRepoProxy(
createTable(
tableName,
tableNameColumnName to ColumnType.Text.NOT_NULLABLE,
tableVersionColumnName to ColumnType.Numeric.INTEGER()
tableVersionColumnName to ColumnType.Numeric.INTEGER()
)
}
}