Skip to content

Commit

Permalink
Upgrade to SQLDelight 2
Browse files Browse the repository at this point in the history
  • Loading branch information
arkon committed Jul 29, 2023
1 parent f5936e9 commit 6a558ad
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 54 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ dependencies {
implementation(androidx.paging.compose)

implementation(libs.bundles.sqlite)
implementation(libs.sqldelight.primitive.adapters)

implementation(kotlinx.reflect)

Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/eu/kanade/tachiyomi/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import android.os.Build
import androidx.core.content.ContextCompat
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver
import app.cash.sqldelight.adapter.primitive.FloatColumnAdapter
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.domain.track.service.TrackPreferences
Expand Down Expand Up @@ -37,9 +38,11 @@ import tachiyomi.core.preference.PreferenceStore
import tachiyomi.core.provider.AndroidBackupFolderProvider
import tachiyomi.core.provider.AndroidDownloadFolderProvider
import tachiyomi.data.AndroidDatabaseHandler
import tachiyomi.data.Chapters
import tachiyomi.data.Database
import tachiyomi.data.DatabaseHandler
import tachiyomi.data.History
import tachiyomi.data.Manga_sync
import tachiyomi.data.Mangas
import tachiyomi.data.dateAdapter
import tachiyomi.data.listOfStringsAdapter
Expand Down Expand Up @@ -90,9 +93,15 @@ class AppModule(val app: Application) : InjektModule {
addSingletonFactory {
Database(
driver = get(),
chaptersAdapter = Chapters.Adapter(
chapter_numberAdapter = FloatColumnAdapter,
),
historyAdapter = History.Adapter(
last_readAdapter = dateAdapter,
),
manga_syncAdapter = Manga_sync.Adapter(
scoreAdapter = FloatColumnAdapter,
),
mangasAdapter = Mangas.Adapter(
genreAdapter = listOfStringsAdapter,
update_strategyAdapter = updateStrategyAdapter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class BackupManager(
}
if (!found) {
// Let the db assign the id
val id = handler.awaitOne {
val id = handler.awaitOneExecutable {
categoriesQueries.insert(category.name, category.order, category.flags)
categoriesQueries.selectLastInsertedRowId()
}
Expand Down Expand Up @@ -488,7 +488,7 @@ class BackupManager(
* @return id of [Manga], null if not found
*/
private suspend fun insertManga(manga: Manga): Long {
return handler.awaitOne(true) {
return handler.awaitOneExecutable(true) {
mangasQueries.insert(
source = manga.source,
url = manga.url,
Expand Down Expand Up @@ -526,11 +526,11 @@ class BackupManager(
title = manga.title,
status = manga.status,
thumbnailUrl = manga.thumbnailUrl,
favorite = manga.favorite.toLong(),
favorite = manga.favorite,
lastUpdate = manga.lastUpdate,
nextUpdate = null,
calculateInterval = null,
initialized = manga.initialized.toLong(),
initialized = manga.initialized,
viewer = manga.viewerFlags,
chapterFlags = manga.chapterFlags,
coverLastModified = manga.coverLastModified,
Expand Down Expand Up @@ -576,8 +576,8 @@ class BackupManager(
url = null,
name = null,
scanlator = null,
read = chapter.read.toLong(),
bookmark = chapter.bookmark.toLong(),
read = chapter.read,
bookmark = chapter.bookmark,
lastPageRead = chapter.lastPageRead,
chapterNumber = null,
sourceOrder = null,
Expand Down
16 changes: 8 additions & 8 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id("com.android.library")
kotlin("android")
kotlin("plugin.serialization")
id("com.squareup.sqldelight")
id("app.cash.sqldelight")
}

android {
Expand All @@ -13,10 +13,12 @@ android {
}

sqldelight {
database("Database") {
packageName = "tachiyomi.data"
dialect = "sqlite:3.24"
schemaOutputDirectory = project.file("./src/main/sqldelight")
databases {
create("Database") {
packageName.set("tachiyomi.data")
dialect(libs.sqldelight.dialects.sql)
schemaOutputDirectory.set(project.file("./src/main/sqldelight"))
}
}
}
}
Expand All @@ -26,9 +28,7 @@ dependencies {
implementation(project(":domain"))
implementation(project(":core"))

api(libs.sqldelight.android.driver)
api(libs.sqldelight.coroutines)
api(libs.sqldelight.android.paging)
api(libs.bundles.sqldelight)
}

tasks {
Expand Down
27 changes: 21 additions & 6 deletions data/src/main/java/tachiyomi/data/AndroidDatabaseHandler.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tachiyomi.data

import androidx.paging.PagingSource
import com.squareup.sqldelight.Query
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import com.squareup.sqldelight.runtime.coroutines.mapToOne
import com.squareup.sqldelight.runtime.coroutines.mapToOneOrNull
import app.cash.sqldelight.ExecutableQuery
import app.cash.sqldelight.Query
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOne
import app.cash.sqldelight.coroutines.mapToOneOrNull
import app.cash.sqldelight.db.SqlDriver
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -39,13 +40,27 @@ class AndroidDatabaseHandler(
return dispatch(inTransaction) { block(db).executeAsOne() }
}

override suspend fun <T : Any> awaitOneExecutable(
inTransaction: Boolean,
block: suspend Database.() -> ExecutableQuery<T>,
): T {
return dispatch(inTransaction) { block(db).executeAsOne() }
}

override suspend fun <T : Any> awaitOneOrNull(
inTransaction: Boolean,
block: suspend Database.() -> Query<T>,
): T? {
return dispatch(inTransaction) { block(db).executeAsOneOrNull() }
}

override suspend fun <T : Any> awaitOneOrNullExecutable(
inTransaction: Boolean,
block: suspend Database.() -> ExecutableQuery<T>,
): T? {
return dispatch(inTransaction) { block(db).executeAsOneOrNull() }
}

override fun <T : Any> subscribeToList(block: Database.() -> Query<T>): Flow<List<T>> {
return block(db).asFlow().mapToList(queryDispatcher)
}
Expand Down
2 changes: 1 addition & 1 deletion data/src/main/java/tachiyomi/data/DatabaseAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tachiyomi.data

import com.squareup.sqldelight.ColumnAdapter
import app.cash.sqldelight.ColumnAdapter
import eu.kanade.tachiyomi.source.model.UpdateStrategy
import java.util.Date

Expand Down
13 changes: 12 additions & 1 deletion data/src/main/java/tachiyomi/data/DatabaseHandler.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package tachiyomi.data

import androidx.paging.PagingSource
import com.squareup.sqldelight.Query
import app.cash.sqldelight.ExecutableQuery
import app.cash.sqldelight.Query
import kotlinx.coroutines.flow.Flow

interface DatabaseHandler {
Expand All @@ -18,11 +19,21 @@ interface DatabaseHandler {
block: suspend Database.() -> Query<T>,
): T

suspend fun <T : Any> awaitOneExecutable(
inTransaction: Boolean = false,
block: suspend Database.() -> ExecutableQuery<T>,
): T

suspend fun <T : Any> awaitOneOrNull(
inTransaction: Boolean = false,
block: suspend Database.() -> Query<T>,
): T?

suspend fun <T : Any> awaitOneOrNullExecutable(
inTransaction: Boolean = false,
block: suspend Database.() -> ExecutableQuery<T>,
): T?

fun <T : Any> subscribeToList(block: Database.() -> Query<T>): Flow<List<T>>

fun <T : Any> subscribeToOne(block: Database.() -> Query<T>): Flow<T>
Expand Down
2 changes: 1 addition & 1 deletion data/src/main/java/tachiyomi/data/QueryPagingSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tachiyomi.data

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.squareup.sqldelight.Query
import app.cash.sqldelight.Query
import kotlin.properties.Delegates

class QueryPagingSource<RowType : Any>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tachiyomi.data.chapter

import kotlinx.coroutines.flow.Flow
import logcat.LogPriority
import tachiyomi.core.util.lang.toLong
import tachiyomi.core.util.system.logcat
import tachiyomi.data.DatabaseHandler
import tachiyomi.domain.chapter.model.Chapter
Expand Down Expand Up @@ -56,8 +55,8 @@ class ChapterRepositoryImpl(
url = chapterUpdate.url,
name = chapterUpdate.name,
scanlator = chapterUpdate.scanlator,
read = chapterUpdate.read?.toLong(),
bookmark = chapterUpdate.bookmark?.toLong(),
read = chapterUpdate.read,
bookmark = chapterUpdate.bookmark,
lastPageRead = chapterUpdate.lastPageRead,
chapterNumber = chapterUpdate.chapterNumber?.toDouble(),
sourceOrder = chapterUpdate.sourceOrder,
Expand Down
6 changes: 3 additions & 3 deletions data/src/main/java/tachiyomi/data/manga/MangaMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ val mangaMapper: (Long, Long, String, String?, String?, String?, List<String>?,
)
}

val libraryManga: (Long, Long, String, String?, String?, String?, List<String>?, String, Long, String?, Boolean, Long?, Long?, Boolean, Long, Long, Long, Long, UpdateStrategy, Long, Long, Long?, Long, Long, Long, Long, Long, Long, Long) -> LibraryManga =
val libraryManga: (Long, Long, String, String?, String?, String?, List<String>?, String, Long, String?, Boolean, Long?, Long?, Boolean, Long, Long, Long, Long, UpdateStrategy, Long, Long, Long?, Long, Double, Long, Long, Long, Double, Long) -> LibraryManga =
{ id, source, url, artist, author, description, genre, title, status, thumbnailUrl, favorite, lastUpdate, nextUpdate, initialized, viewerFlags, chapterFlags, coverLastModified, dateAdded, updateStrategy, calculateInterval, lastModifiedAt, favoriteModifiedAt, totalCount, readCount, latestUpload, chapterFetchedAt, lastRead, bookmarkCount, category ->
LibraryManga(
manga = mangaMapper(
Expand Down Expand Up @@ -61,8 +61,8 @@ val libraryManga: (Long, Long, String, String?, String?, String?, List<String>?,
),
category = category,
totalChapters = totalCount,
readCount = readCount,
bookmarkCount = bookmarkCount,
readCount = readCount.toLong(),
bookmarkCount = bookmarkCount.toLong(),
latestUpload = latestUpload,
chapterFetchedAt = chapterFetchedAt,
lastRead = lastRead,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MangaRepositoryImpl(
}

override suspend fun insert(manga: Manga): Long? {
return handler.awaitOneOrNull(inTransaction = true) {
return handler.awaitOneOrNullExecutable(inTransaction = true) {
mangasQueries.insert(
source = manga.source,
url = manga.url,
Expand Down Expand Up @@ -133,11 +133,11 @@ class MangaRepositoryImpl(
title = value.title,
status = value.status,
thumbnailUrl = value.thumbnailUrl,
favorite = value.favorite?.toLong(),
favorite = value.favorite,
lastUpdate = value.lastUpdate,
nextUpdate = value.nextUpdate,
calculateInterval = value.fetchInterval?.toLong(),
initialized = value.initialized?.toLong(),
initialized = value.initialized,
viewer = value.viewerFlags,
chapterFlags = value.chapterFlags,
coverLastModified = value.coverLastModified,
Expand Down
9 changes: 6 additions & 3 deletions data/src/main/sqldelight/tachiyomi/data/chapters.sq
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import kotlin.Boolean;
import kotlin.Float;

CREATE TABLE chapters(
_id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL,
Expand All @@ -9,9 +12,9 @@ CREATE TABLE chapters(
last_page_read INTEGER NOT NULL,
chapter_number REAL AS Float NOT NULL,
source_order INTEGER NOT NULL,
date_fetch INTEGER AS Long NOT NULL,
date_upload INTEGER AS Long NOT NULL,
last_modified_at INTEGER AS Long NOT NULL DEFAULT 0,
date_fetch INTEGER NOT NULL,
date_upload INTEGER NOT NULL,
last_modified_at INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
ON DELETE CASCADE
);
Expand Down
6 changes: 4 additions & 2 deletions data/src/main/sqldelight/tachiyomi/data/manga_sync.sq
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import kotlin.Float;

CREATE TABLE manga_sync(
_id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL,
Expand All @@ -10,8 +12,8 @@ CREATE TABLE manga_sync(
status INTEGER NOT NULL,
score REAL AS Float NOT NULL,
remote_url TEXT NOT NULL,
start_date INTEGER AS Long NOT NULL,
finish_date INTEGER AS Long NOT NULL,
start_date INTEGER NOT NULL,
finish_date INTEGER NOT NULL,
UNIQUE (manga_id, sync_id) ON CONFLICT REPLACE,
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
ON DELETE CASCADE
Expand Down
15 changes: 8 additions & 7 deletions data/src/main/sqldelight/tachiyomi/data/mangas.sq
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import eu.kanade.tachiyomi.source.model.UpdateStrategy;
import java.lang.String;
import kotlin.collections.List;
import kotlin.Boolean;
import kotlin.String;

CREATE TABLE mangas(
_id INTEGER NOT NULL PRIMARY KEY,
Expand All @@ -14,17 +15,17 @@ CREATE TABLE mangas(
status INTEGER NOT NULL,
thumbnail_url TEXT,
favorite INTEGER AS Boolean NOT NULL,
last_update INTEGER AS Long,
next_update INTEGER AS Long,
last_update INTEGER,
next_update INTEGER,
initialized INTEGER AS Boolean NOT NULL,
viewer INTEGER NOT NULL,
chapter_flags INTEGER NOT NULL,
cover_last_modified INTEGER AS Long NOT NULL,
date_added INTEGER AS Long NOT NULL,
cover_last_modified INTEGER NOT NULL,
date_added INTEGER NOT NULL,
update_strategy INTEGER AS UpdateStrategy NOT NULL DEFAULT 0,
calculate_interval INTEGER DEFAULT 0 NOT NULL,
last_modified_at INTEGER AS Long NOT NULL DEFAULT 0,
favorite_modified_at INTEGER AS Long
last_modified_at INTEGER NOT NULL DEFAULT 0,
favorite_modified_at INTEGER
);

CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CREATE TABLE mangas_categories(
_id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL,
category_id INTEGER NOT NULL,
last_modified_at INTEGER AS Long NOT NULL DEFAULT 0,
last_modified_at INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY(category_id) REFERENCES categories (_id)
ON DELETE CASCADE,
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
Expand Down
Loading

0 comments on commit 6a558ad

Please sign in to comment.