Skip to content

Commit f604e4e

Browse files
committed
refactor: Replace Requery's SQLite with AndroidX's new KMP SQLite
1 parent a04ea9f commit f604e4e

File tree

5 files changed

+38
-60
lines changed

5 files changed

+38
-60
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The format is simplified version of [Keep a Changelog](https://keepachangelog.co
4949
- Refactor Library to store LibraryMap instead of flatten list of LibraryItem
5050
- LibraryItem abstraction to make it easier to manage
5151
- LibraryManga no longer extend MangaImpl
52+
- Replace Requery's SQLite with AndroidX's new KMP SQLite
5253
- Update dependency gradle to v8.12
5354
- Update user agent (@Hiirbaf)
5455
- Update serialization to v1.8.1
@@ -65,8 +66,7 @@ The format is simplified version of [Keep a Changelog](https://keepachangelog.co
6566
- Update dependency com.getkeepsafe.taptargetview:taptargetview to v1.15.0
6667
- Update dependency androidx.window:window to v1.4.0
6768
- Update dependency androidx.webkit:webkit to v1.13.0
68-
- Update dependency androidx.sqlite:sqlite-ktx to v2.5.1
69-
- Update dependency androidx.sqlite:sqlite to v2.5.1
69+
- Update androidxSqlite to v2.5.1
7070
- Update dependency androidx.recyclerview:recyclerview to v1.4.0
7171
- Update dependency androidx.core:core-ktx to v1.16.0
7272
- Update dependency androidx.compose:compose-bom to v2025.05.01

app/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ dependencies {
200200
implementation(libs.play.services.gcm)
201201

202202
// Database
203-
implementation(libs.sqlite.android)
204203
implementation(libs.bundles.sqlite)
205204

206205
// Model View Presenter

app/src/main/java/yokai/core/di/AppModule.kt

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ package yokai.core.di
22

33
import android.app.Application
44
import androidx.core.content.ContextCompat
5-
import androidx.sqlite.db.SupportSQLiteDatabase
5+
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
6+
import androidx.sqlite.driver.bundled.SQLITE_OPEN_CREATE
7+
import androidx.sqlite.driver.bundled.SQLITE_OPEN_READWRITE
68
import app.cash.sqldelight.db.SqlDriver
7-
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
89
import co.touchlab.kermit.Logger
910
import com.chuckerteam.chucker.api.ChuckerCollector
1011
import com.chuckerteam.chucker.api.ChuckerInterceptor
12+
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteConfiguration
13+
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteDatabaseType
14+
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteDriver
15+
import com.eygraber.sqldelight.androidx.driver.File
16+
import com.eygraber.sqldelight.androidx.driver.SqliteJournalMode
17+
import com.eygraber.sqldelight.androidx.driver.SqliteSync
1118
import eu.kanade.tachiyomi.BuildConfig
1219
import eu.kanade.tachiyomi.core.storage.AndroidStorageFolderProvider
1320
import eu.kanade.tachiyomi.data.cache.ChapterCache
@@ -23,7 +30,6 @@ import eu.kanade.tachiyomi.network.NetworkHelper
2330
import eu.kanade.tachiyomi.source.SourceManager
2431
import eu.kanade.tachiyomi.util.chapter.ChapterFilter
2532
import eu.kanade.tachiyomi.util.manga.MangaShortcutManager
26-
import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory
2733
import kotlinx.serialization.json.Json
2834
import kotlinx.serialization.protobuf.ProtoBuf
2935
import nl.adaptivity.xmlutil.XmlDeclMode
@@ -42,46 +48,23 @@ fun appModule(app: Application) = module {
4248
single { app }
4349

4450
single<SqlDriver> {
45-
AndroidSqliteDriver(
51+
AndroidxSqliteDriver(
52+
createConnection = { name ->
53+
BundledSQLiteDriver().open(name, SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE)
54+
},
55+
databaseType = AndroidxSqliteDatabaseType.File(app, "tachiyomi.db"),
56+
configuration = AndroidxSqliteConfiguration().apply {
57+
isForeignKeyConstraintsEnabled = true
58+
journalMode = SqliteJournalMode.WAL
59+
sync = SqliteSync.Normal
60+
},
4661
schema = Database.Schema,
47-
context = app,
48-
name = "tachiyomi.db",
49-
// factory = if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
50-
// // Support database inspector in Android Studio
51-
// FrameworkSQLiteOpenHelperFactory()
52-
// } else {
53-
// RequerySQLiteOpenHelperFactory()
54-
// },
55-
factory = RequerySQLiteOpenHelperFactory(),
56-
callback = object : AndroidSqliteDriver.Callback(Database.Schema) {
57-
override fun onOpen(db: SupportSQLiteDatabase) {
58-
super.onOpen(db)
59-
setPragma(db, "foreign_keys = ON")
60-
setPragma(db, "journal_mode = WAL")
61-
setPragma(db, "synchronous = NORMAL")
62-
}
63-
64-
private fun setPragma(db: SupportSQLiteDatabase, pragma: String) {
65-
val cursor = db.query("PRAGMA $pragma")
66-
cursor.moveToFirst()
67-
cursor.close()
68-
}
69-
70-
// Not sure if this is still needed, but just in case
71-
override fun onConfigure(db: SupportSQLiteDatabase) {
72-
db.setForeignKeyConstraintsEnabled(true)
73-
}
74-
75-
override fun onCreate(db: SupportSQLiteDatabase) {
76-
Logger.d { "Creating new database..." }
77-
super.onCreate(db)
78-
}
79-
80-
override fun onUpgrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) {
81-
if (oldVersion < newVersion) {
82-
Logger.d { "Upgrading database from $oldVersion to $newVersion" }
83-
super.onUpgrade(db, oldVersion, newVersion)
84-
}
62+
onCreate = {
63+
Logger.d { "Creating new database..." }
64+
},
65+
onUpdate = { oldVersion, newVersion ->
66+
if (oldVersion < newVersion) {
67+
Logger.d { "Upgrading database from $oldVersion to $newVersion" }
8568
}
8669
},
8770
)

gradle/androidx.versions.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ multidex = { module = "androidx.multidex:multidex", version = "2.0.1" }
2828
palette = { module = "androidx.palette:palette", version = "1.0.0" }
2929
preference = { module = "androidx.preference:preference-ktx", version = "1.2.1" }
3030
recyclerview = { module = "androidx.recyclerview:recyclerview", version = "1.4.0" }
31-
sqlite = { module = "androidx.sqlite:sqlite", version = "2.5.1" }
3231
webkit = { module = "androidx.webkit:webkit", version = "1.13.0" }
3332
work = { module = "androidx.work:work-runtime-ktx", version = "2.10.1" }
3433
window = { module = "androidx.window:window", version = "1.4.0" }
@@ -38,7 +37,7 @@ androidx = [
3837
"activity", "activity-compose", "annotation", "appcompat", "browser", "biometric", "cardview", "core",
3938
"core-splashscreen", "layout-constraint", "glance-appwidget", "lifecycle-common", "lifecycle-livedata",
4039
"lifecycle-process", "lifecycle-runtime", "lifecycle-viewmodel", "lifecycle-viewmodel-compose", "multidex",
41-
"palette", "preference", "recyclerview", "sqlite", "layout-swiperefresh", "webkit", "work", "window"
40+
"palette", "preference", "recyclerview", "layout-swiperefresh", "webkit", "work", "window"
4241
]
4342

4443
[plugins]

gradle/libs.versions.toml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ fast_adapter = "5.7.0"
66
moko = "0.24.5"
77
okhttp = "5.0.0-alpha.16"
88
shizuku = "13.1.5"
9-
# FIXME: Uncomment once SQLDelight support KMP AndroidX SQLiteDriver
10-
#sqlite = "2.5.0-alpha04"
11-
sqlite = "2.5.1"
9+
androidxSqlite = "2.5.1"
10+
# FIXME: Stay at 2.0.2 until 2.1.1 released because dialect is borked
11+
# REF: https://github.com/sqldelight/sqldelight/issues/5758
1212
sqldelight = "2.0.2"
1313
junit = "5.11.3"
1414
kermit = "2.0.5"
@@ -82,13 +82,12 @@ rxjava = { module = "io.reactivex:rxjava", version = "1.3.8" }
8282
rxandroid = { module = "io.reactivex:rxandroid", version = "1.2.1" }
8383
slice = { module = "com.github.mthli:Slice", version = "v1.2" }
8484

85-
# FIXME: Uncomment once SQLDelight support KMP AndroidX SQLiteDriver
86-
#sqlite = { module = "androidx.sqlite:sqlite-bundled", version.ref = "sqlite" }
87-
sqlite-ktx = { module = "androidx.sqlite:sqlite-ktx", version.ref = "sqlite" }
88-
sqlite-android = { module = "com.github.requery:sqlite-android", version = "3.49.0" }
85+
# SQLite interface
86+
sqlite = { module = "androidx.sqlite:sqlite", version.ref = "androidxSqlite" }
87+
sqlite-bundled = { module = "androidx.sqlite:sqlite-bundled", version.ref = "androidxSqlite" }
8988

9089
sqldelight-coroutines = { module = "app.cash.sqldelight:coroutines-extensions", version.ref = "sqldelight" }
91-
sqldelight-android-driver = { module = "app.cash.sqldelight:android-driver", version.ref = "sqldelight" }
90+
sqldelight-androidx-driver = { module = "com.eygraber:sqldelight-androidx-driver", version = "0.0.12" }
9291
sqldelight-android-paging = { module = "app.cash.sqldelight:androidx-paging3-extensions", version.ref = "sqldelight" }
9392
sqldelight-dialects-sql = { module = "app.cash.sqldelight:sqlite-3-38-dialect", version.ref = "sqldelight" }
9493

@@ -114,13 +113,11 @@ moko = { id = "dev.icerock.mobile.multiplatform-resources", version.ref = "moko"
114113
sqldelight = { id = "app.cash.sqldelight", version.ref = "sqldelight" }
115114

116115
[bundles]
117-
db = [ "sqldelight-coroutines" ]
118-
db-android = [ "sqldelight-android-driver", "sqldelight-android-paging" ]
116+
db = [ "sqldelight-coroutines", "sqldelight-androidx-driver" ]
117+
db-android = [ "sqldelight-android-paging" ]
119118
coil = [ "coil3", "coil3-svg", "coil3-gif", "coil3-okhttp" ]
120119
logging = [ "kermit" ]
121-
# FIXME: Uncomment once SQLDelight support KMP AndroidX SQLiteDriver
122-
#sqlite = [ "sqlite", "sqlite-ktx" ]
123-
sqlite = [ "sqlite-ktx" ]
120+
sqlite = [ "sqlite", "sqlite-bundled" ]
124121
test = [ "junit-api", "kotest-assertions", "mockk" ]
125122
test-android = [ "junit-android" ]
126123
test-runtime = [ "junit-engine" ]

0 commit comments

Comments
 (0)