Skip to content

Commit

Permalink
Optimize the logic related to table structure upgrade.(#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dora4 committed Nov 8, 2024
1 parent 21aeb86 commit 700d886
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dcache/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ afterEvaluate {
from(components["release"])
groupId = "com.github.dora4"
artifactId = "dcache-android"
version = "2.5.12"
version = "2.5.13"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dcache/src/main/java/dora/db/OrmSQLiteOpenHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class OrmSQLiteOpenHelper(private val context: Context, name: String, version: I
ormTable?.let {
val isRecreated = it.isUpgradeRecreated
if (isRecreated) {
Transaction.execute {
Transaction.execute(db) {
TableManager.dropTable(table)
TableManager.createTable(table)
}
Expand Down
27 changes: 25 additions & 2 deletions dcache/src/main/java/dora/db/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,31 @@ object Transaction {
* [execute] function.
* 简体中文:[android.database.sqlite.SQLiteDatabase]对象,可以在[execute]函数中直接使用。
*/
val db by lazy {
Orm.getDB()
val db: SQLiteDatabase
get() = Orm.getDB()

/**
* Execute a general transaction block.
* 简体中文:执行通用事务块。
*/
internal fun <T> execute(db: SQLiteDatabase, block: Transaction.() -> T) : Any = apply {
try {
// Begin the transaction.
// 简体中文:开始事务
db.beginTransaction()
// Execute the transaction operation.
// 简体中文:执行事务操作
block()
// Set the flag indicating that all operations were executed successfully.
// 简体中文:设置所有操作执行成功的标志位
db.setTransactionSuccessful()
} catch (e: SQLiteException) {
e.printStackTrace()
} finally {
// End the transaction.
// 简体中文:结束事务
db.endTransaction()
}
}

/**
Expand Down

0 comments on commit 700d886

Please sign in to comment.