mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-18 13:51:30 +00:00
VersionsRepo
This commit is contained in:
@@ -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.versions.*
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
@@ -36,6 +37,9 @@ class StandardSQLHelper(
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {}
|
||||
}
|
||||
val versionsRepo: VersionsRepo<SQLiteOpenHelper> by lazy {
|
||||
StandardVersionsRepo(AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper))
|
||||
}
|
||||
|
||||
suspend fun <T> writableTransaction(block: suspend SQLiteDatabase.() -> T): T = sqlOpenHelper.writableTransaction(block)
|
||||
|
||||
|
@@ -0,0 +1,63 @@
|
||||
package dev.inmo.micro_utils.repos.versions
|
||||
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import androidx.core.content.contentValuesOf
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
/**
|
||||
* Will create [VersionsRepo] based on [SQLiteOpenHelper] with table inside of [database]
|
||||
*/
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun versionsRepo(database: SQLiteOpenHelper): VersionsRepo<SQLiteOpenHelper> = StandardVersionsRepo(
|
||||
AndroidSQLStandardVersionsRepoProxy(database)
|
||||
)
|
||||
|
||||
class AndroidSQLStandardVersionsRepoProxy(
|
||||
override val database: SQLiteOpenHelper
|
||||
) : StandardVersionsRepoProxy<SQLiteOpenHelper> {
|
||||
private val tableName: String = "AndroidSQLStandardVersionsRepo"
|
||||
private val tableNameColumnName = "tableName"
|
||||
private val tableVersionColumnName = "version"
|
||||
|
||||
init {
|
||||
runBlocking(DatabaseCoroutineContext) {
|
||||
database.writableTransaction {
|
||||
createTable(
|
||||
tableName,
|
||||
tableNameColumnName to ColumnType.Text.NOT_NULLABLE,
|
||||
tableVersionColumnName to ColumnType.Numeric.INTEGER()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getTableVersion(tableName: String): Int? = database.writableTransaction {
|
||||
select(
|
||||
tableName,
|
||||
selection = "$tableNameColumnName=?",
|
||||
selectionArgs = arrayOf(tableName),
|
||||
limit = limitClause(1)
|
||||
).use {
|
||||
if (it.moveToFirst()) {
|
||||
it.getInt(tableVersionColumnName)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateTableVersion(tableName: String, version: Int) {
|
||||
database.writableTransaction {
|
||||
val updated = update(
|
||||
tableName,
|
||||
contentValuesOf(tableVersionColumnName to version),
|
||||
"$tableNameColumnName=?",
|
||||
arrayOf(version.toString())
|
||||
) > 0
|
||||
if (!updated) {
|
||||
insert(tableName, null, contentValuesOf(tableNameColumnName to tableName, tableVersionColumnName to version))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
@file:Suppress("NOTHING_TO_INLINE")
|
||||
|
||||
package dev.inmo.micro_utils.repos.versions
|
||||
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import androidx.core.content.contentValuesOf
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.keyvalue.keyValueStore
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
/**
|
||||
* Will create [VersionsRepo] based on [T], but versions will be stored in [StandardKeyValueRepo]
|
||||
*
|
||||
* @receiver Will be used to create [KeyValueBasedVersionsRepoProxy] via [keyValueStore] and pass it to [StandardVersionsRepo]
|
||||
*
|
||||
* @see [KeyValueBasedVersionsRepoProxy]
|
||||
* @see [keyValueStore]
|
||||
*/
|
||||
inline fun <T> Context.versionsKeyValueRepo(
|
||||
database: T
|
||||
): VersionsRepo<T> = StandardVersionsRepo(
|
||||
KeyValueBasedVersionsRepoProxy(
|
||||
keyValueStore("SPVersionsRepo"),
|
||||
database
|
||||
)
|
||||
)
|
||||
/**
|
||||
* Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [StandardKeyValueRepo]
|
||||
*
|
||||
* @receiver Will be used to create [StandardKeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo]
|
||||
*
|
||||
* @see [keyValueStore]
|
||||
*/
|
||||
inline fun Context.versionsKeyValueRepoForSQL(
|
||||
database: SQLiteOpenHelper
|
||||
) = versionsKeyValueRepo(database)
|
||||
|
||||
/**
|
||||
* Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [StandardKeyValueRepo]
|
||||
*
|
||||
* @param context Will be used to create [StandardKeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo]
|
||||
*
|
||||
* @see [keyValueStore]
|
||||
*/
|
||||
inline fun versionsRepo(
|
||||
context: Context,
|
||||
database: SQLiteOpenHelper
|
||||
) = context.versionsKeyValueRepoForSQL(database)
|
Reference in New Issue
Block a user