Merge Mihon 0.21.2 updates and use BookOasis updater
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
This commit is contained in:
@@ -32,6 +32,7 @@ dependencies {
|
||||
implementation(projects.sourceApi)
|
||||
implementation(projects.domain)
|
||||
implementation(projects.core.common)
|
||||
implementation(projects.core.metro)
|
||||
|
||||
implementation(libs.metro.runtime)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
@@ -42,5 +43,6 @@ dependencies {
|
||||
|
||||
implementation(libs.kotlinx.datetime)
|
||||
|
||||
api(libs.bundles.sqldelight)
|
||||
implementation(libs.androidx.sqlite.bundled)
|
||||
implementation(libs.bundles.sqldelight)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package mihon.data.database
|
||||
|
||||
import app.cash.sqldelight.db.QueryResult
|
||||
import app.cash.sqldelight.db.SqlDriver
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.withContext
|
||||
import mihon.domain.database.repository.DatabaseRepository
|
||||
|
||||
@Inject
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DatabaseRepositoryImpl(
|
||||
// Lazy since the driver reports its migration state here
|
||||
private val driver: Lazy<SqlDriver>,
|
||||
) : DatabaseRepository {
|
||||
|
||||
override val isMigrating = MutableStateFlow(false)
|
||||
|
||||
override suspend fun awaitOpen() {
|
||||
withContext(Dispatchers.IO) {
|
||||
driver.value.executeQuery(null, "SELECT 1", { QueryResult.Value(Unit) }, 0).await()
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
-13
@@ -1,16 +1,24 @@
|
||||
package mihon.data.extension.repository
|
||||
|
||||
import app.cash.sqldelight.async.coroutines.awaitAsList
|
||||
import app.cash.sqldelight.async.coroutines.awaitAsOne
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import eu.kanade.tachiyomi.extension.model.Extension
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.WhileSubscribed
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.shareIn
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import logcat.LogPriority
|
||||
import mihon.core.metro.AppCoroutineScope
|
||||
import mihon.data.extension.service.ExtensionStoreService
|
||||
import mihon.domain.extension.model.ExtensionStore
|
||||
import mihon.domain.extension.repository.ExtensionStoreRepository
|
||||
@@ -18,14 +26,33 @@ import tachiyomi.core.common.util.system.logcat
|
||||
import tachiyomi.data.Database
|
||||
import tachiyomi.data.subscribeToList
|
||||
import tachiyomi.data.subscribeToOne
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@Inject
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class ExtensionStoreRepositoryImpl(
|
||||
@AppCoroutineScope scope: CoroutineScope,
|
||||
private val service: ExtensionStoreService,
|
||||
private val database: Database,
|
||||
) : ExtensionStoreRepository {
|
||||
|
||||
private val stores = database.extension_storeQueries.getAll(::extensionStoreMapper)
|
||||
.subscribeToList()
|
||||
.catch {
|
||||
logcat(LogPriority.ERROR, it) { "Failed to query extension stores" }
|
||||
emit(emptyList())
|
||||
}
|
||||
.shareIn(
|
||||
scope = scope,
|
||||
started = SharingStarted.WhileSubscribed(
|
||||
stopTimeout = 5.seconds,
|
||||
replayExpiration = Duration.ZERO,
|
||||
),
|
||||
replay = 1,
|
||||
)
|
||||
|
||||
override suspend fun insert(indexUrl: String): Result<Unit> {
|
||||
return service.fetch(indexUrl).mapCatching { upsert(it) }
|
||||
}
|
||||
@@ -45,28 +72,33 @@ class ExtensionStoreRepositoryImpl(
|
||||
|
||||
override suspend fun refreshAll() {
|
||||
try {
|
||||
database.extension_storeQueries.getAll().awaitAsList().forEach { store ->
|
||||
service.fetch(store.index_url)
|
||||
.mapCatching {
|
||||
database.transaction {
|
||||
upsert(it)
|
||||
if (store.index_url != it.indexUrl) {
|
||||
database.extension_storeQueries.delete(store.index_url)
|
||||
}
|
||||
}
|
||||
}
|
||||
val refreshed = stores.first().mapNotNull { store ->
|
||||
service.fetch(store.indexUrl)
|
||||
.onFailure {
|
||||
logcat(LogPriority.ERROR, it) {
|
||||
"Failed to refresh extension store '${store.name} (${store.index_url})'"
|
||||
"Failed to refresh extension store '${store.name} (${store.indexUrl})'"
|
||||
}
|
||||
}
|
||||
.getOrNull()
|
||||
?.let { store.indexUrl to it }
|
||||
}
|
||||
database.transaction {
|
||||
refreshed.forEach { (indexUrl, store) ->
|
||||
if (!database.extension_storeQueries.contains(indexUrl).awaitAsOne()) {
|
||||
return@forEach
|
||||
}
|
||||
upsert(store)
|
||||
if (indexUrl != store.indexUrl) {
|
||||
database.extension_storeQueries.delete(indexUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun upsert(store: ExtensionStore) {
|
||||
override suspend fun upsert(store: ExtensionStore) {
|
||||
database.extension_storeQueries.upsert(
|
||||
indexUrl = store.indexUrl,
|
||||
name = store.name,
|
||||
@@ -105,7 +137,7 @@ class ExtensionStoreRepositoryImpl(
|
||||
}
|
||||
|
||||
override fun getAllAsFlow(): Flow<List<ExtensionStore>> {
|
||||
return database.extension_storeQueries.getAll(::extensionStoreMapper).subscribeToList()
|
||||
return stores
|
||||
}
|
||||
|
||||
override fun getCountAsFlow(): Flow<Long> {
|
||||
|
||||
@@ -7,6 +7,8 @@ import eu.kanade.tachiyomi.extension.model.Extension
|
||||
import eu.kanade.tachiyomi.network.GET
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import eu.kanade.tachiyomi.network.awaitSuccess
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.decodeFromByteArray
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.okio.decodeFromBufferedSource
|
||||
@@ -30,7 +32,9 @@ class ExtensionStoreService(
|
||||
private val json: Json,
|
||||
private val protoBuf: ProtoBuf,
|
||||
) {
|
||||
suspend fun fetch(indexUrl: String): Result<ExtensionStore> {
|
||||
suspend fun fetch(indexUrl: String): Result<ExtensionStore> = withContext(Dispatchers.IO) { fetchStore(indexUrl) }
|
||||
|
||||
private suspend fun fetchStore(indexUrl: String): Result<ExtensionStore> {
|
||||
var updatedIndexUrl: String = indexUrl
|
||||
return try {
|
||||
val response = network.client.newCall(GET(updatedIndexUrl)).awaitSuccess()
|
||||
@@ -56,7 +60,7 @@ class ExtensionStoreService(
|
||||
}
|
||||
|
||||
if (networkStore is NetworkLegacyExtensionRepo && networkStore.indexV2 != null) {
|
||||
return fetch(networkStore.indexV2)
|
||||
return fetchStore(networkStore.indexV2)
|
||||
}
|
||||
|
||||
networkStore.toExtensionStore(updatedIndexUrl)
|
||||
@@ -72,8 +76,8 @@ class ExtensionStoreService(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getExtensions(store: ExtensionStore): Result<List<Extension.Available>> {
|
||||
return try {
|
||||
suspend fun getExtensions(store: ExtensionStore): Result<List<Extension.Available>> = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val extensions = if (store.extensionListUrl != null) {
|
||||
val response = network.client.newCall(GET(store.extensionListUrl!!)).awaitSuccess()
|
||||
response.body.source().decompressIfGzipped().use { source ->
|
||||
|
||||
@@ -2,13 +2,14 @@ package tachiyomi.data
|
||||
|
||||
import app.cash.sqldelight.ColumnAdapter
|
||||
import eu.kanade.tachiyomi.source.model.UpdateStrategy
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import java.util.Date
|
||||
import mihon.core.common.extensions.toByteArray
|
||||
import mihon.core.common.extensions.toJsonObject
|
||||
import kotlin.time.Instant
|
||||
|
||||
object DateColumnAdapter : ColumnAdapter<Date, Long> {
|
||||
override fun decode(databaseValue: Long): Date = Date(databaseValue)
|
||||
override fun encode(value: Date): Long = value.time
|
||||
object InstantColumnAdapter : ColumnAdapter<Instant, Long> {
|
||||
override fun decode(databaseValue: Long): Instant = Instant.fromEpochMilliseconds(databaseValue)
|
||||
override fun encode(value: Instant): Long = value.toEpochMilliseconds()
|
||||
}
|
||||
|
||||
private const val LIST_OF_STRINGS_SEPARATOR = ", "
|
||||
@@ -31,11 +32,13 @@ object UpdateStrategyColumnAdapter : ColumnAdapter<UpdateStrategy, Long> {
|
||||
}
|
||||
|
||||
object MemoColumnAdapter : ColumnAdapter<JsonObject, ByteArray> {
|
||||
override fun decode(databaseValue: ByteArray): JsonObject {
|
||||
return Json.decodeFromString<JsonObject>(databaseValue.decodeToString())
|
||||
}
|
||||
|
||||
override fun encode(value: JsonObject): ByteArray {
|
||||
return value.toString().encodeToByteArray()
|
||||
}
|
||||
override fun decode(databaseValue: ByteArray): JsonObject = databaseValue.toJsonObject()
|
||||
override fun encode(value: JsonObject): ByteArray = value.toByteArray()
|
||||
}
|
||||
|
||||
// SQLite has one integer type, so a column holding a value that cannot exceed Int range narrows
|
||||
// here rather than at every call site that reads or writes it.
|
||||
object IntColumnAdapter : ColumnAdapter<Int, Long> {
|
||||
override fun decode(databaseValue: Long): Int = databaseValue.toInt()
|
||||
override fun encode(value: Int): Long = value.toLong()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package tachiyomi.data
|
||||
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import androidx.core.content.getSystemService
|
||||
import androidx.sqlite.SQLiteConnection
|
||||
import androidx.sqlite.SQLiteDriver
|
||||
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
|
||||
import androidx.sqlite.execSQL
|
||||
import app.cash.sqldelight.db.QueryResult
|
||||
import app.cash.sqldelight.db.SqlDriver
|
||||
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteConcurrencyModel.MultipleReadersSingleWriter
|
||||
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteConfiguration
|
||||
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteConnectionFactory
|
||||
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteDatabaseType
|
||||
import com.eygraber.sqldelight.androidx.driver.AndroidxSqliteDriver
|
||||
import com.eygraber.sqldelight.androidx.driver.FileProvider
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.BindingContainer
|
||||
import dev.zacsweers.metro.ContributesTo
|
||||
import dev.zacsweers.metro.Provides
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import mihon.data.database.DatabaseRepositoryImpl
|
||||
|
||||
@ContributesTo(AppScope::class)
|
||||
@BindingContainer
|
||||
object DatabaseBindings {
|
||||
|
||||
/**
|
||||
* WAL on every device, so a write never blocks reads. Low-RAM devices get one reader instead of four, since each
|
||||
* connection keeps its own page cache.
|
||||
*/
|
||||
@Provides
|
||||
@SingleIn(AppScope::class)
|
||||
fun providesSqlDriver(context: Context, databaseRepository: DatabaseRepositoryImpl): SqlDriver {
|
||||
val isLowRam = context.getSystemService<ActivityManager>()?.isLowRamDevice == true
|
||||
return AndroidxSqliteDriver(
|
||||
connectionFactory = object : AndroidxSqliteConnectionFactory {
|
||||
override val driver: SQLiteDriver = BundledSQLiteDriver()
|
||||
|
||||
override fun createConnection(name: String): SQLiteConnection {
|
||||
return driver.open(name).apply {
|
||||
execSQL("PRAGMA busy_timeout = 3000")
|
||||
}
|
||||
}
|
||||
},
|
||||
databaseType = AndroidxSqliteDatabaseType.FileProvider(context, "tachiyomi.db"),
|
||||
schema = Database.Schema,
|
||||
configuration = AndroidxSqliteConfiguration(
|
||||
isForeignKeyConstraintsEnabled = true,
|
||||
concurrencyModel = MultipleReadersSingleWriter(isWal = true, walCount = if (isLowRam) 1 else 4),
|
||||
),
|
||||
// Runs before the driver compares the schema version, and onOpen once the schema is ready
|
||||
onConfigure = {
|
||||
val version = executePragmaQuery("user_version", { QueryResult.Value(it.apply { next() }.getLong(0)) })
|
||||
databaseRepository.isMigrating.value = version in 1..<Database.Schema.version
|
||||
},
|
||||
onOpen = {
|
||||
databaseRepository.isMigrating.value = false
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@SingleIn(AppScope::class)
|
||||
fun providesDatabase(driver: SqlDriver): Database {
|
||||
return Database(
|
||||
driver = driver,
|
||||
historyAdapter = History.Adapter(
|
||||
read_atAdapter = InstantColumnAdapter,
|
||||
),
|
||||
mangaAdapter = Manga.Adapter(
|
||||
remote_genreAdapter = StringListColumnAdapter,
|
||||
remote_update_strategyAdapter = UpdateStrategyColumnAdapter,
|
||||
remote_memoAdapter = MemoColumnAdapter,
|
||||
user_favorite_atAdapter = InstantColumnAdapter,
|
||||
state_chapter_last_updateAdapter = InstantColumnAdapter,
|
||||
state_chapter_next_updateAdapter = InstantColumnAdapter,
|
||||
state_cover_last_modifiedAdapter = InstantColumnAdapter,
|
||||
),
|
||||
chapterAdapter = Chapter.Adapter(
|
||||
remote_date_uploadAdapter = InstantColumnAdapter,
|
||||
remote_memoAdapter = MemoColumnAdapter,
|
||||
user_last_page_readAdapter = IntColumnAdapter,
|
||||
state_date_fetchAdapter = InstantColumnAdapter,
|
||||
),
|
||||
manga_trackAdapter = Manga_track.Adapter(
|
||||
start_dateAdapter = InstantColumnAdapter,
|
||||
finish_dateAdapter = InstantColumnAdapter,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -6,17 +6,18 @@ import app.cash.sqldelight.coroutines.mapToList
|
||||
import app.cash.sqldelight.coroutines.mapToOne
|
||||
import app.cash.sqldelight.coroutines.mapToOneOrNull
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import tachiyomi.core.common.util.lang.pausedWhileUnsubscribed
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
fun <T : Any> Query<T>.subscribeToList(
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
): Flow<List<T>> = asFlow().mapToList(context)
|
||||
): Flow<List<T>> = asFlow().pausedWhileUnsubscribed().mapToList(context)
|
||||
|
||||
fun <T : Any> Query<T>.subscribeToOne(
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
): Flow<T> = asFlow().mapToOne(context)
|
||||
): Flow<T> = asFlow().pausedWhileUnsubscribed().mapToOne(context)
|
||||
|
||||
fun <T : Any> Query<T>.subscribeToOneOrNull(
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
): Flow<T?> = asFlow().mapToOneOrNull(context)
|
||||
): Flow<T?> = asFlow().pausedWhileUnsubscribed().mapToOneOrNull(context)
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
package tachiyomi.data.backup
|
||||
|
||||
import app.cash.sqldelight.async.coroutines.awaitAsList
|
||||
import app.cash.sqldelight.async.coroutines.awaitAsOne
|
||||
import app.cash.sqldelight.async.coroutines.awaitAsOneOrNull
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import tachiyomi.data.Database
|
||||
import tachiyomi.domain.backup.model.RestoredHistory
|
||||
import tachiyomi.domain.backup.model.RestoredManga
|
||||
import tachiyomi.domain.backup.repository.RestoreRepository
|
||||
import tachiyomi.domain.chapter.model.Chapter
|
||||
import tachiyomi.domain.chapter.repository.ChapterRepository
|
||||
import tachiyomi.domain.manga.model.Manga
|
||||
import tachiyomi.domain.manga.model.MangaUpdate
|
||||
import tachiyomi.domain.manga.repository.MangaRepository
|
||||
import tachiyomi.domain.track.model.Track
|
||||
import tachiyomi.domain.track.repository.TrackRepository
|
||||
import kotlin.math.max
|
||||
import kotlin.time.Instant
|
||||
|
||||
@Inject
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class RestoreRepositoryImpl(
|
||||
private val database: Database,
|
||||
private val mangaRepository: MangaRepository,
|
||||
private val chapterRepository: ChapterRepository,
|
||||
private val trackRepository: TrackRepository,
|
||||
) : RestoreRepository {
|
||||
|
||||
override suspend fun getMangaUrlsBySourceId(): Map<Long, Set<String>> {
|
||||
return database.mangaQueries
|
||||
.getAllMangaSourceAndUrl()
|
||||
.awaitAsList()
|
||||
.groupBy({ it.source_id }, { it.remote_url })
|
||||
.mapValues { it.value.toSet() }
|
||||
}
|
||||
|
||||
override suspend fun restoreManga(entries: List<RestoredManga>, update: suspend (Manga) -> MangaUpdate) {
|
||||
database.transaction {
|
||||
entries.forEach { entry ->
|
||||
// A cancelled restore stops between entries and rolls the whole batch back
|
||||
currentCoroutineContext().ensureActive()
|
||||
mangaRepository.update(update(restore(entry)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun restore(entry: RestoredManga): Manga {
|
||||
val dbManga = mangaRepository.getMangaByUrlAndSourceId(entry.manga.url, entry.manga.source)
|
||||
val manga = if (dbManga == null) {
|
||||
entry.manga.copy(id = insertManga(entry.manga))
|
||||
} else {
|
||||
updateManga(mergeManga(entry.manga, dbManga))
|
||||
}
|
||||
|
||||
if (entry.categoryIds.isNotEmpty()) {
|
||||
mangaRepository.setMangaCategories(manga.id, entry.categoryIds)
|
||||
}
|
||||
restoreChapters(manga, entry.chapters)
|
||||
restoreTracking(manga, entry.tracks)
|
||||
restoreHistory(manga, entry.history)
|
||||
restoreExcludedScanlators(manga, entry.excludedScanlators)
|
||||
return manga
|
||||
}
|
||||
|
||||
private fun mergeManga(manga: Manga, dbManga: Manga): Manga {
|
||||
val details = if (dbManga.initialized || !manga.initialized) dbManga else manga
|
||||
return dbManga.copy(
|
||||
favoriteAt = if (dbManga.favorite || manga.favorite) {
|
||||
listOfNotNull(dbManga.favoriteAt, manga.favoriteAt)
|
||||
.filter { it > Manga.UNKNOWN_FAVORITE_AT }
|
||||
.minOrNull()
|
||||
?: Manga.UNKNOWN_FAVORITE_AT
|
||||
} else {
|
||||
null
|
||||
},
|
||||
title = details.title,
|
||||
artist = details.artist,
|
||||
author = details.author,
|
||||
description = details.description,
|
||||
genre = details.genre,
|
||||
status = details.status,
|
||||
thumbnailUrl = details.thumbnailUrl,
|
||||
updateStrategy = details.updateStrategy,
|
||||
initialized = dbManga.initialized || manga.initialized,
|
||||
// Merge both backup and local data with local winning
|
||||
memo = JsonObject(manga.memo + dbManga.memo),
|
||||
nextUpdate = dbManga.nextUpdate ?: manga.nextUpdate,
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun updateManga(manga: Manga): Manga {
|
||||
database.mangaQueries.updateFromBackup(
|
||||
userFavoriteAt = manga.favoriteAt,
|
||||
remoteArtist = manga.artist,
|
||||
remoteAuthor = manga.author,
|
||||
remoteDescription = manga.description,
|
||||
remoteGenre = manga.genre,
|
||||
remoteTitle = manga.title,
|
||||
remoteStatus = manga.status,
|
||||
remoteCover = manga.thumbnailUrl,
|
||||
stateChapterLastUpdate = manga.lastUpdate,
|
||||
stateInitialized = manga.initialized,
|
||||
userReaderFlags = manga.viewerFlags,
|
||||
userChapterFlags = manga.chapterFlags,
|
||||
stateCoverLastModified = manga.coverLastModified,
|
||||
id = manga.id,
|
||||
remoteUpdateStrategy = manga.updateStrategy,
|
||||
userNotes = manga.notes,
|
||||
remoteMemo = manga.memo,
|
||||
)
|
||||
return manga
|
||||
}
|
||||
|
||||
private suspend fun insertManga(manga: Manga): Long {
|
||||
return database.mangaQueries.insertReturningId(
|
||||
userFavoriteAt = manga.favoriteAt,
|
||||
sourceId = manga.source,
|
||||
remoteUrl = manga.url,
|
||||
remoteArtist = manga.artist,
|
||||
remoteAuthor = manga.author,
|
||||
remoteDescription = manga.description,
|
||||
remoteGenre = manga.genre,
|
||||
remoteTitle = manga.title,
|
||||
remoteStatus = manga.status,
|
||||
remoteCover = manga.thumbnailUrl,
|
||||
stateChapterLastUpdate = manga.lastUpdate,
|
||||
stateChapterNextUpdate = null,
|
||||
stateChapterFetchInterval = 0L,
|
||||
stateInitialized = manga.initialized,
|
||||
userReaderFlags = manga.viewerFlags,
|
||||
userChapterFlags = manga.chapterFlags,
|
||||
stateCoverLastModified = manga.coverLastModified,
|
||||
remoteUpdateStrategy = manga.updateStrategy,
|
||||
userNotes = manga.notes,
|
||||
remoteMemo = manga.memo,
|
||||
)
|
||||
.awaitAsOne()
|
||||
}
|
||||
|
||||
private suspend fun restoreChapters(manga: Manga, restoredChapters: List<Chapter>) {
|
||||
val dbChaptersByUrl = chapterRepository.getChapterByMangaId(manga.id)
|
||||
.associateBy { it.url }
|
||||
|
||||
val (existingChapters, newChapters) = restoredChapters
|
||||
.groupBy { it.url }
|
||||
.map { (_, copies) ->
|
||||
copies.reduce { kept, other ->
|
||||
kept.copy(
|
||||
read = kept.read || other.read,
|
||||
bookmark = kept.bookmark || other.bookmark,
|
||||
lastPageRead = max(kept.lastPageRead, other.lastPageRead),
|
||||
)
|
||||
}
|
||||
}
|
||||
.mapNotNull {
|
||||
val chapter = it.copy(mangaId = manga.id)
|
||||
|
||||
val dbChapter = dbChaptersByUrl[chapter.url]
|
||||
?: // New chapter
|
||||
return@mapNotNull chapter
|
||||
|
||||
if (chapter.forComparison() == dbChapter.forComparison()) {
|
||||
// Same state; skip
|
||||
return@mapNotNull null
|
||||
}
|
||||
|
||||
chapter
|
||||
.copyFrom(dbChapter)
|
||||
.copy(
|
||||
id = dbChapter.id,
|
||||
read = chapter.read || dbChapter.read,
|
||||
bookmark = chapter.bookmark || dbChapter.bookmark,
|
||||
lastPageRead = max(chapter.lastPageRead, dbChapter.lastPageRead),
|
||||
dateFetch = dbChapter.dateFetch,
|
||||
sourceOrder = dbChapter.sourceOrder,
|
||||
// Merge both backup and local data with local winning
|
||||
memo = JsonObject(chapter.memo + dbChapter.memo),
|
||||
)
|
||||
}
|
||||
.partition { it.id > 0 }
|
||||
|
||||
newChapters.forEach { chapter ->
|
||||
database.chapterQueries.insertReturningId(
|
||||
mangaId = chapter.mangaId,
|
||||
remoteUrl = chapter.url,
|
||||
remoteName = chapter.name,
|
||||
remoteScanlator = chapter.scanlator,
|
||||
userRead = chapter.read,
|
||||
userBookmark = chapter.bookmark,
|
||||
userLastPageRead = chapter.lastPageRead,
|
||||
remoteChapterNumber = chapter.chapterNumber,
|
||||
remoteOrder = chapter.sourceOrder,
|
||||
stateDateFetch = chapter.dateFetch,
|
||||
remoteDateUpload = chapter.dateUpload,
|
||||
remoteMemo = chapter.memo,
|
||||
)
|
||||
.awaitAsOneOrNull()
|
||||
}
|
||||
existingChapters.forEach { chapter ->
|
||||
database.chapterQueries.updateFromBackup(
|
||||
userRead = chapter.read,
|
||||
userBookmark = chapter.bookmark,
|
||||
userLastPageRead = chapter.lastPageRead,
|
||||
id = chapter.id,
|
||||
remoteMemo = chapter.memo,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Chapter.forComparison() =
|
||||
this.copy(id = 0L, mangaId = 0L, dateFetch = Instant.DISTANT_PAST, dateUpload = null)
|
||||
|
||||
private suspend fun restoreHistory(manga: Manga, restoredHistory: List<RestoredHistory>) {
|
||||
if (restoredHistory.isEmpty()) return
|
||||
val chapterIdsByUrl = chapterRepository.getChapterByMangaId(manga.id).associate { it.url to it.id }
|
||||
val dbHistoryByChapterId = database.historyQueries
|
||||
.getHistoryByMangaId(manga.id)
|
||||
.awaitAsList()
|
||||
.associateBy { it.chapter_id }
|
||||
|
||||
val toUpdate = restoredHistory
|
||||
// Chapter doesn't exist; skip
|
||||
.filter { it.chapterUrl in chapterIdsByUrl }
|
||||
// A backup of a library that still had duplicate chapters carries a history entry for
|
||||
// each copy; they are one chapter now, read as late as any copy and for as long as all
|
||||
.groupBy { chapterIdsByUrl.getValue(it.chapterUrl) }
|
||||
.map { (chapterId, copies) ->
|
||||
val readAt = copies.mapNotNull { it.readAt }.maxOrNull()
|
||||
val readDuration = copies.sumOf { it.readDuration }
|
||||
val dbHistory = dbHistoryByChapterId[chapterId]
|
||||
// New history entry
|
||||
?: return@map Triple(chapterId, readAt, readDuration)
|
||||
|
||||
// Update history entry
|
||||
Triple(
|
||||
chapterId,
|
||||
listOfNotNull(readAt, dbHistory.read_at).maxOrNull(),
|
||||
max(readDuration, dbHistory.read_duration) - dbHistory.read_duration,
|
||||
)
|
||||
}
|
||||
|
||||
toUpdate.forEach { (chapterId, readAt, readDuration) ->
|
||||
database.historyQueries.upsert(chapterId = chapterId, readAt = readAt, readDuration = readDuration)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun restoreTracking(manga: Manga, restoredTracks: List<Track>) {
|
||||
val dbTrackByTrackerId = trackRepository.getTracksByMangaId(manga.id).associateBy { it.trackerId }
|
||||
|
||||
val (existingTracks, newTracks) = restoredTracks
|
||||
.mapNotNull { track ->
|
||||
val dbTrack = dbTrackByTrackerId[track.trackerId]
|
||||
?: // New track
|
||||
return@mapNotNull track.copy(
|
||||
id = 0, // Let DB assign new ID
|
||||
mangaId = manga.id,
|
||||
)
|
||||
|
||||
if (track.lastChapterRead <= dbTrack.lastChapterRead) return@mapNotNull null
|
||||
|
||||
// Update to an existing track
|
||||
dbTrack.copy(lastChapterRead = track.lastChapterRead)
|
||||
}
|
||||
.partition { it.id > 0 }
|
||||
|
||||
if (newTracks.isNotEmpty()) {
|
||||
trackRepository.upsertAll(newTracks)
|
||||
}
|
||||
|
||||
existingTracks.forEach { track ->
|
||||
database.manga_trackQueries.update(
|
||||
mangaId = track.mangaId,
|
||||
trackerId = track.trackerId,
|
||||
remoteId = track.remoteId,
|
||||
libraryId = track.libraryId,
|
||||
title = track.title,
|
||||
lastChapterRead = track.lastChapterRead,
|
||||
totalChapters = track.totalChapters,
|
||||
status = track.status,
|
||||
score = track.score,
|
||||
remoteUrl = track.remoteUrl,
|
||||
startDate = track.startDate,
|
||||
finishDate = track.finishDate,
|
||||
`private` = track.private,
|
||||
id = track.id,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun restoreExcludedScanlators(manga: Manga, excludedScanlators: List<String>) {
|
||||
excludedScanlators.forEach { database.excluded_scanlatorQueries.insert(manga.id, it) }
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
import tachiyomi.data.Database
|
||||
import tachiyomi.data.subscribeToList
|
||||
import tachiyomi.domain.category.model.Category
|
||||
import tachiyomi.domain.category.model.NewCategory
|
||||
import tachiyomi.domain.category.repository.CategoryRepository
|
||||
|
||||
@Inject
|
||||
@@ -20,65 +21,81 @@ class CategoryRepositoryImpl(
|
||||
) : CategoryRepository {
|
||||
|
||||
override suspend fun get(id: Long): Category? {
|
||||
return database.categoriesQueries
|
||||
return database.categoryQueries
|
||||
.getCategory(id, ::mapCategory)
|
||||
.awaitAsOneOrNull()
|
||||
}
|
||||
|
||||
override suspend fun getAll(): List<Category> {
|
||||
return database.categoriesQueries
|
||||
return database.categoryQueries
|
||||
.getCategories(::mapCategory)
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override fun getAllAsFlow(): Flow<List<Category>> {
|
||||
return database.categoriesQueries
|
||||
return database.categoryQueries
|
||||
.getCategories(::mapCategory)
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override suspend fun getCategoriesByMangaId(mangaId: Long): List<Category> {
|
||||
return database.categoriesQueries
|
||||
return database.categoryQueries
|
||||
.getCategoriesByMangaId(mangaId, ::mapCategory)
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override fun getCategoriesByMangaIdAsFlow(mangaId: Long): Flow<List<Category>> {
|
||||
return database.categoriesQueries
|
||||
return database.categoryQueries
|
||||
.getCategoriesByMangaId(mangaId, ::mapCategory)
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override suspend fun insert(category: Category) {
|
||||
database.categoriesQueries.insert(
|
||||
name = category.name,
|
||||
order = category.order,
|
||||
flags = category.flags,
|
||||
)
|
||||
override suspend fun getCategoriesByMangaIds(mangaIds: List<Long>): Map<Long, List<Category>> {
|
||||
return database.categoryQueries
|
||||
.getCategoriesByMangaIds(mangaIds) { mangaId, id, name, order, flags ->
|
||||
mangaId to mapCategory(id, name, order, flags)
|
||||
}
|
||||
.awaitAsList()
|
||||
.groupBy({ it.first }, { it.second })
|
||||
}
|
||||
|
||||
override suspend fun insert(category: NewCategory) {
|
||||
database.categoryQueries.insert(name = category.name, flags = category.flags)
|
||||
}
|
||||
|
||||
override suspend fun insertAll(categories: List<NewCategory>) {
|
||||
database.transaction {
|
||||
categories.forEach { insert(it) }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateName(categoryId: Long, name: String) {
|
||||
database.categoriesQueries.updateName(name = name, categoryId = categoryId)
|
||||
database.categoryQueries.updateName(name = name, id = categoryId)
|
||||
}
|
||||
|
||||
override suspend fun updateFlags(categoryId: Long, flags: Long) {
|
||||
database.categoriesQueries.updateFlags(flags = flags, categoryId = categoryId)
|
||||
database.categoryQueries.updateFlags(flags = flags, id = categoryId)
|
||||
}
|
||||
|
||||
override suspend fun updateAllFlags(flags: Long?) {
|
||||
database.categoriesQueries.updateAllFlags(flags = flags)
|
||||
override suspend fun updateAllFlags(flags: Long) {
|
||||
database.categoryQueries.updateAllFlags(flags = flags)
|
||||
}
|
||||
|
||||
override suspend fun updateAllOrders(orderedIds: List<Long>) {
|
||||
database.transaction {
|
||||
orderedIds.forEachIndexed { index, categoryId ->
|
||||
database.categoriesQueries.updateOrder(order = index.toLong(), categoryId = categoryId)
|
||||
val current = database.categoryQueries.getUserCategoryIds().awaitAsList()
|
||||
val ids = orderedIds.filter { it in current } + current.filterNot { it in orderedIds }
|
||||
ids.forEachIndexed { index, categoryId ->
|
||||
database.categoryQueries.updateOrder(order = -index - 2L, id = categoryId)
|
||||
}
|
||||
ids.forEachIndexed { index, categoryId ->
|
||||
database.categoryQueries.updateOrder(order = index.toLong(), id = categoryId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun delete(categoryId: Long) {
|
||||
database.categoriesQueries.delete(categoryId = categoryId)
|
||||
database.categoryQueries.delete(id = categoryId)
|
||||
}
|
||||
|
||||
private fun mapCategory(
|
||||
|
||||
@@ -9,15 +9,15 @@ import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import logcat.LogPriority
|
||||
import tachiyomi.core.common.util.lang.toLong
|
||||
import tachiyomi.core.common.util.system.logcat
|
||||
import tachiyomi.data.Database
|
||||
import tachiyomi.data.MemoColumnAdapter
|
||||
import tachiyomi.data.subscribeToList
|
||||
import tachiyomi.domain.chapter.model.Chapter
|
||||
import tachiyomi.domain.chapter.model.ChapterRemoteUpdate
|
||||
import tachiyomi.domain.chapter.model.ChapterSummary
|
||||
import tachiyomi.domain.chapter.model.ChapterUpdate
|
||||
import tachiyomi.domain.chapter.repository.ChapterRepository
|
||||
import kotlin.time.Instant
|
||||
|
||||
@Inject
|
||||
@SingleIn(AppScope::class)
|
||||
@@ -26,35 +26,6 @@ class ChapterRepositoryImpl(
|
||||
private val database: Database,
|
||||
) : ChapterRepository {
|
||||
|
||||
override suspend fun addAll(chapters: List<Chapter>): List<Chapter> {
|
||||
return try {
|
||||
database.transactionWithResult {
|
||||
chapters.map { chapter ->
|
||||
val chapterId = database.chaptersQueries.insertReturningId(
|
||||
chapter.mangaId,
|
||||
chapter.url,
|
||||
chapter.name,
|
||||
chapter.scanlator,
|
||||
chapter.read,
|
||||
chapter.bookmark,
|
||||
chapter.lastPageRead,
|
||||
chapter.chapterNumber,
|
||||
chapter.sourceOrder,
|
||||
chapter.dateFetch,
|
||||
chapter.dateUpload,
|
||||
chapter.version,
|
||||
chapter.memo,
|
||||
)
|
||||
.awaitAsOne()
|
||||
chapter.copy(id = chapterId)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e)
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun update(chapterUpdate: ChapterUpdate) {
|
||||
partialUpdate(chapterUpdate)
|
||||
}
|
||||
@@ -63,113 +34,167 @@ class ChapterRepositoryImpl(
|
||||
partialUpdate(*chapterUpdates.toTypedArray())
|
||||
}
|
||||
|
||||
override suspend fun updateFromRemote(
|
||||
removedIds: List<Long>,
|
||||
added: List<Chapter>,
|
||||
updated: List<ChapterRemoteUpdate>,
|
||||
): List<Chapter> {
|
||||
return database.transactionWithResult {
|
||||
if (removedIds.isNotEmpty()) {
|
||||
database.chapterQueries.removeChaptersWithIds(removedIds)
|
||||
}
|
||||
val existing = added.map { it.mangaId }
|
||||
.distinct()
|
||||
.flatMap { mangaId ->
|
||||
database.chapterQueries
|
||||
.getChaptersByMangaId(
|
||||
mangaId = mangaId,
|
||||
applyScanlatorFilter = false.toLong(),
|
||||
mapper = ::mapChapter,
|
||||
)
|
||||
.awaitAsList()
|
||||
.map { mangaId to it.url }
|
||||
}
|
||||
.toMutableSet()
|
||||
val stored = added.filter { existing.add(it.mangaId to it.url) }.map { chapter ->
|
||||
val chapterId = database.chapterQueries.insertReturningId(
|
||||
mangaId = chapter.mangaId,
|
||||
remoteUrl = chapter.url,
|
||||
remoteName = chapter.name,
|
||||
remoteScanlator = chapter.scanlator,
|
||||
userRead = chapter.read,
|
||||
userBookmark = chapter.bookmark,
|
||||
userLastPageRead = chapter.lastPageRead,
|
||||
remoteChapterNumber = chapter.chapterNumber,
|
||||
remoteOrder = chapter.sourceOrder,
|
||||
stateDateFetch = chapter.dateFetch,
|
||||
remoteDateUpload = chapter.dateUpload,
|
||||
remoteMemo = chapter.memo,
|
||||
)
|
||||
.awaitAsOne()
|
||||
chapter.copy(id = chapterId)
|
||||
}
|
||||
updated.forEach { chapterUpdate ->
|
||||
database.chapterQueries.updateRemote(
|
||||
remoteName = chapterUpdate.name,
|
||||
remoteScanlator = chapterUpdate.scanlator,
|
||||
remoteChapterNumber = chapterUpdate.chapterNumber,
|
||||
remoteOrder = chapterUpdate.sourceOrder,
|
||||
remoteDateUpload = chapterUpdate.dateUpload,
|
||||
id = chapterUpdate.id,
|
||||
remoteMemo = chapterUpdate.memo,
|
||||
)
|
||||
}
|
||||
stored
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun partialUpdate(vararg chapterUpdates: ChapterUpdate) {
|
||||
database.transaction {
|
||||
chapterUpdates.forEach { chapterUpdate ->
|
||||
database.chaptersQueries.update(
|
||||
mangaId = chapterUpdate.mangaId,
|
||||
url = chapterUpdate.url,
|
||||
name = chapterUpdate.name,
|
||||
scanlator = chapterUpdate.scanlator,
|
||||
read = chapterUpdate.read,
|
||||
bookmark = chapterUpdate.bookmark,
|
||||
lastPageRead = chapterUpdate.lastPageRead,
|
||||
chapterNumber = chapterUpdate.chapterNumber,
|
||||
sourceOrder = chapterUpdate.sourceOrder,
|
||||
dateFetch = chapterUpdate.dateFetch,
|
||||
dateUpload = chapterUpdate.dateUpload,
|
||||
chapterId = chapterUpdate.id,
|
||||
version = chapterUpdate.version,
|
||||
isSyncing = 0,
|
||||
memo = chapterUpdate.memo?.let(MemoColumnAdapter::encode),
|
||||
database.chapterQueries.update(
|
||||
userRead = chapterUpdate.read,
|
||||
userBookmark = chapterUpdate.bookmark,
|
||||
userLastPageRead = chapterUpdate.lastPageRead,
|
||||
stateDateFetch = chapterUpdate.dateFetch,
|
||||
id = chapterUpdate.id,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun removeChaptersWithIds(chapterIds: List<Long>) {
|
||||
try {
|
||||
database.chaptersQueries.removeChaptersWithIds(chapterIds)
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getChapterByMangaId(mangaId: Long, applyScanlatorFilter: Boolean): List<Chapter> {
|
||||
return database.chaptersQueries
|
||||
.getChaptersByMangaId(mangaId, applyScanlatorFilter.toLong(), ::mapChapter)
|
||||
return database.chapterQueries
|
||||
.getChaptersByMangaId(
|
||||
mangaId = mangaId,
|
||||
applyScanlatorFilter = applyScanlatorFilter.toLong(),
|
||||
mapper = ::mapChapter,
|
||||
)
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override suspend fun getChaptersByMangaIds(mangaIds: List<Long>): Map<Long, List<Chapter>> {
|
||||
return database.chapterQueries
|
||||
.getChaptersByMangaIds(mangaIds, ::mapChapter)
|
||||
.awaitAsList()
|
||||
.groupBy { it.mangaId }
|
||||
}
|
||||
|
||||
override suspend fun getChapterSummaryByMangaId(mangaId: Long): ChapterSummary {
|
||||
return database.chapterQueries
|
||||
.getChapterSummaryByMangaId(mangaId) { total, latestChapterNumber ->
|
||||
ChapterSummary(chapterCount = total, latestChapterNumber = latestChapterNumber)
|
||||
}
|
||||
.awaitAsOne()
|
||||
}
|
||||
|
||||
override suspend fun getScanlatorsByMangaId(mangaId: Long): List<String> {
|
||||
return database.chaptersQueries
|
||||
return database.chapterQueries
|
||||
.getScanlatorsByMangaId(mangaId) { it.orEmpty() }
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override fun getScanlatorsByMangaIdAsFlow(mangaId: Long): Flow<List<String>> {
|
||||
return database.chaptersQueries
|
||||
return database.chapterQueries
|
||||
.getScanlatorsByMangaId(mangaId) { it.orEmpty() }
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override suspend fun getBookmarkedChaptersByMangaId(mangaId: Long): List<Chapter> {
|
||||
return database.chaptersQueries
|
||||
return database.chapterQueries
|
||||
.getBookmarkedChaptersByMangaId(mangaId, ::mapChapter)
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override suspend fun getChapterById(id: Long): Chapter? {
|
||||
return database.chaptersQueries
|
||||
return database.chapterQueries
|
||||
.getChapterById(id, ::mapChapter)
|
||||
.awaitAsOneOrNull()
|
||||
}
|
||||
|
||||
override suspend fun getChapterByMangaIdAsFlow(mangaId: Long, applyScanlatorFilter: Boolean): Flow<List<Chapter>> {
|
||||
return database.chaptersQueries
|
||||
.getChaptersByMangaId(mangaId, applyScanlatorFilter.toLong(), ::mapChapter)
|
||||
return database.chapterQueries
|
||||
.getChaptersByMangaId(
|
||||
mangaId = mangaId,
|
||||
applyScanlatorFilter = applyScanlatorFilter.toLong(),
|
||||
mapper = ::mapChapter,
|
||||
)
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override suspend fun getChapterByUrlAndMangaId(url: String, mangaId: Long): Chapter? {
|
||||
return database.chaptersQueries
|
||||
.getChapterByUrlAndMangaId(url, mangaId, ::mapChapter)
|
||||
return database.chapterQueries
|
||||
.getChapterByUrlAndMangaId(mangaId = mangaId, remoteUrl = url, mapper = ::mapChapter)
|
||||
.awaitAsOneOrNull()
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
private fun mapChapter(
|
||||
id: Long,
|
||||
mangaId: Long,
|
||||
url: String,
|
||||
name: String,
|
||||
scanlator: String?,
|
||||
read: Boolean,
|
||||
bookmark: Boolean,
|
||||
lastPageRead: Long,
|
||||
chapterNumber: Double,
|
||||
sourceOrder: Long,
|
||||
dateFetch: Long,
|
||||
dateUpload: Long,
|
||||
lastModifiedAt: Long,
|
||||
version: Long,
|
||||
isSyncing: Long,
|
||||
memo: JsonObject,
|
||||
remoteUrl: String,
|
||||
remoteName: String,
|
||||
remoteScanlator: String?,
|
||||
remoteChapterNumber: Double,
|
||||
remoteDateUpload: Instant?,
|
||||
remoteOrder: Long,
|
||||
remoteMemo: JsonObject,
|
||||
userRead: Boolean,
|
||||
userBookmark: Boolean,
|
||||
userLastPageRead: Int,
|
||||
stateDateFetch: Instant,
|
||||
): Chapter = Chapter(
|
||||
id = id,
|
||||
mangaId = mangaId,
|
||||
read = read,
|
||||
bookmark = bookmark,
|
||||
lastPageRead = lastPageRead,
|
||||
dateFetch = dateFetch,
|
||||
sourceOrder = sourceOrder,
|
||||
url = url,
|
||||
name = name,
|
||||
dateUpload = dateUpload,
|
||||
chapterNumber = chapterNumber,
|
||||
scanlator = scanlator,
|
||||
lastModifiedAt = lastModifiedAt,
|
||||
version = version,
|
||||
memo = memo,
|
||||
read = userRead,
|
||||
bookmark = userBookmark,
|
||||
lastPageRead = userLastPageRead,
|
||||
dateFetch = stateDateFetch,
|
||||
sourceOrder = remoteOrder,
|
||||
url = remoteUrl,
|
||||
name = remoteName,
|
||||
dateUpload = remoteDateUpload,
|
||||
chapterNumber = remoteChapterNumber,
|
||||
scanlator = remoteScanlator,
|
||||
memo = remoteMemo,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ package tachiyomi.data.history
|
||||
import tachiyomi.domain.history.model.History
|
||||
import tachiyomi.domain.history.model.HistoryWithRelations
|
||||
import tachiyomi.domain.manga.model.MangaCover
|
||||
import java.util.Date
|
||||
import kotlin.time.Instant
|
||||
|
||||
object HistoryMapper {
|
||||
fun mapHistory(
|
||||
id: Long,
|
||||
chapterId: Long,
|
||||
readAt: Date?,
|
||||
readAt: Instant?,
|
||||
readDuration: Long,
|
||||
): History = History(
|
||||
id = id,
|
||||
@@ -26,9 +26,9 @@ object HistoryMapper {
|
||||
thumbnailUrl: String?,
|
||||
sourceId: Long,
|
||||
isFavorite: Boolean,
|
||||
coverLastModified: Long,
|
||||
coverLastModified: Instant?,
|
||||
chapterNumber: Double,
|
||||
readAt: Date?,
|
||||
readAt: Instant?,
|
||||
readDuration: Long,
|
||||
): HistoryWithRelations = HistoryWithRelations(
|
||||
id = historyId,
|
||||
|
||||
@@ -48,6 +48,15 @@ class HistoryRepositoryImpl(
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override suspend fun getHistoryByMangaIds(mangaIds: List<Long>): Map<Long, List<History>> {
|
||||
return database.historyQueries
|
||||
.getHistoryByMangaIds(mangaIds) { mangaId, id, chapterId, readAt, readDuration ->
|
||||
mangaId to HistoryMapper.mapHistory(id, chapterId, readAt, readDuration)
|
||||
}
|
||||
.awaitAsList()
|
||||
.groupBy({ it.first }, { it.second })
|
||||
}
|
||||
|
||||
override suspend fun resetHistory(historyId: Long) {
|
||||
try {
|
||||
database.historyQueries.resetHistoryById(historyId)
|
||||
@@ -74,12 +83,16 @@ class HistoryRepositoryImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun deleteResetHistory() {
|
||||
database.historyQueries.removeResettedHistory()
|
||||
}
|
||||
|
||||
override suspend fun upsertHistory(historyUpdate: HistoryUpdate) {
|
||||
try {
|
||||
database.historyQueries.upsert(
|
||||
historyUpdate.chapterId,
|
||||
historyUpdate.readAt,
|
||||
historyUpdate.sessionReadDuration,
|
||||
chapterId = historyUpdate.chapterId,
|
||||
readAt = historyUpdate.readAt,
|
||||
readDuration = historyUpdate.sessionReadDuration,
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, throwable = e)
|
||||
|
||||
@@ -5,126 +5,107 @@ import kotlinx.serialization.json.JsonObject
|
||||
import tachiyomi.domain.library.model.LibraryManga
|
||||
import tachiyomi.domain.manga.model.Manga
|
||||
import tachiyomi.domain.manga.model.MangaWithChapterCount
|
||||
import kotlin.time.Instant
|
||||
|
||||
object MangaMapper {
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
fun mapManga(
|
||||
id: Long,
|
||||
source: Long,
|
||||
url: String,
|
||||
artist: String?,
|
||||
author: String?,
|
||||
description: String?,
|
||||
genre: List<String>?,
|
||||
title: String,
|
||||
status: Long,
|
||||
thumbnailUrl: String?,
|
||||
favorite: Boolean,
|
||||
lastUpdate: Long?,
|
||||
nextUpdate: Long?,
|
||||
initialized: Boolean,
|
||||
viewerFlags: Long,
|
||||
chapterFlags: Long,
|
||||
coverLastModified: Long,
|
||||
dateAdded: Long,
|
||||
updateStrategy: UpdateStrategy,
|
||||
calculateInterval: Long,
|
||||
lastModifiedAt: Long,
|
||||
favoriteModifiedAt: Long?,
|
||||
version: Long,
|
||||
isSyncing: Long,
|
||||
notes: String,
|
||||
memo: JsonObject,
|
||||
sourceId: Long,
|
||||
remoteUrl: String,
|
||||
remoteTitle: String,
|
||||
remoteAuthor: String?,
|
||||
remoteArtist: String?,
|
||||
remoteDescription: String?,
|
||||
remoteGenre: List<String>?,
|
||||
remoteStatus: Long,
|
||||
remoteCover: String?,
|
||||
remoteUpdateStrategy: UpdateStrategy,
|
||||
remoteMemo: JsonObject,
|
||||
userFavoriteAt: Instant?,
|
||||
userNotes: String,
|
||||
userReaderFlags: Long,
|
||||
userChapterFlags: Long,
|
||||
stateChapterLastUpdate: Instant?,
|
||||
stateChapterNextUpdate: Instant?,
|
||||
stateChapterFetchInterval: Long,
|
||||
stateCoverLastModified: Instant?,
|
||||
stateInitialized: Boolean,
|
||||
): Manga = Manga(
|
||||
id = id,
|
||||
source = source,
|
||||
favorite = favorite,
|
||||
lastUpdate = lastUpdate ?: 0,
|
||||
nextUpdate = nextUpdate ?: 0,
|
||||
fetchInterval = calculateInterval.toInt(),
|
||||
dateAdded = dateAdded,
|
||||
viewerFlags = viewerFlags,
|
||||
chapterFlags = chapterFlags,
|
||||
coverLastModified = coverLastModified,
|
||||
url = url,
|
||||
title = title,
|
||||
artist = artist,
|
||||
author = author,
|
||||
description = description,
|
||||
genre = genre,
|
||||
status = status,
|
||||
thumbnailUrl = thumbnailUrl,
|
||||
updateStrategy = updateStrategy,
|
||||
initialized = initialized,
|
||||
lastModifiedAt = lastModifiedAt,
|
||||
favoriteModifiedAt = favoriteModifiedAt,
|
||||
version = version,
|
||||
notes = notes,
|
||||
memo = memo,
|
||||
source = sourceId,
|
||||
favoriteAt = userFavoriteAt,
|
||||
lastUpdate = stateChapterLastUpdate,
|
||||
nextUpdate = stateChapterNextUpdate,
|
||||
fetchInterval = stateChapterFetchInterval.toInt(),
|
||||
viewerFlags = userReaderFlags,
|
||||
chapterFlags = userChapterFlags,
|
||||
coverLastModified = stateCoverLastModified,
|
||||
url = remoteUrl,
|
||||
title = remoteTitle,
|
||||
artist = remoteArtist,
|
||||
author = remoteAuthor,
|
||||
description = remoteDescription,
|
||||
genre = remoteGenre,
|
||||
status = remoteStatus,
|
||||
thumbnailUrl = remoteCover,
|
||||
updateStrategy = remoteUpdateStrategy,
|
||||
initialized = stateInitialized,
|
||||
notes = userNotes,
|
||||
memo = remoteMemo,
|
||||
)
|
||||
|
||||
fun mapLibraryManga(
|
||||
id: Long,
|
||||
source: Long,
|
||||
url: String,
|
||||
artist: String?,
|
||||
author: String?,
|
||||
description: String?,
|
||||
genre: List<String>?,
|
||||
title: String,
|
||||
status: Long,
|
||||
thumbnailUrl: String?,
|
||||
favorite: Boolean,
|
||||
lastUpdate: Long?,
|
||||
nextUpdate: Long?,
|
||||
initialized: Boolean,
|
||||
viewerFlags: Long,
|
||||
chapterFlags: Long,
|
||||
coverLastModified: Long,
|
||||
dateAdded: Long,
|
||||
updateStrategy: UpdateStrategy,
|
||||
calculateInterval: Long,
|
||||
lastModifiedAt: Long,
|
||||
favoriteModifiedAt: Long?,
|
||||
version: Long,
|
||||
isSyncing: Long,
|
||||
notes: String,
|
||||
memo: JsonObject,
|
||||
sourceId: Long,
|
||||
remoteUrl: String,
|
||||
remoteTitle: String,
|
||||
remoteAuthor: String?,
|
||||
remoteArtist: String?,
|
||||
remoteDescription: String?,
|
||||
remoteGenre: List<String>?,
|
||||
remoteStatus: Long,
|
||||
remoteCover: String?,
|
||||
remoteUpdateStrategy: UpdateStrategy,
|
||||
remoteMemo: JsonObject,
|
||||
userFavoriteAt: Instant,
|
||||
userNotes: String,
|
||||
userReaderFlags: Long,
|
||||
userChapterFlags: Long,
|
||||
stateChapterLastUpdate: Instant?,
|
||||
stateChapterNextUpdate: Instant?,
|
||||
stateChapterFetchInterval: Long,
|
||||
stateCoverLastModified: Instant?,
|
||||
stateInitialized: Boolean,
|
||||
totalCount: Long,
|
||||
readCount: Double,
|
||||
latestUpload: Long,
|
||||
chapterFetchedAt: Long,
|
||||
lastRead: Long,
|
||||
latestUpload: Instant?,
|
||||
chapterFetchedAt: Instant?,
|
||||
lastRead: Instant?,
|
||||
bookmarkCount: Double,
|
||||
categories: String,
|
||||
): LibraryManga = LibraryManga(
|
||||
manga = mapManga(
|
||||
id,
|
||||
source,
|
||||
url,
|
||||
artist,
|
||||
author,
|
||||
description,
|
||||
genre,
|
||||
title,
|
||||
status,
|
||||
thumbnailUrl,
|
||||
favorite,
|
||||
lastUpdate,
|
||||
nextUpdate,
|
||||
initialized,
|
||||
viewerFlags,
|
||||
chapterFlags,
|
||||
coverLastModified,
|
||||
dateAdded,
|
||||
updateStrategy,
|
||||
calculateInterval,
|
||||
lastModifiedAt,
|
||||
favoriteModifiedAt,
|
||||
version,
|
||||
isSyncing,
|
||||
notes,
|
||||
memo,
|
||||
id = id,
|
||||
sourceId = sourceId,
|
||||
remoteUrl = remoteUrl,
|
||||
remoteTitle = remoteTitle,
|
||||
remoteAuthor = remoteAuthor,
|
||||
remoteArtist = remoteArtist,
|
||||
remoteDescription = remoteDescription,
|
||||
remoteGenre = remoteGenre,
|
||||
remoteStatus = remoteStatus,
|
||||
remoteCover = remoteCover,
|
||||
remoteUpdateStrategy = remoteUpdateStrategy,
|
||||
remoteMemo = remoteMemo,
|
||||
userFavoriteAt = userFavoriteAt,
|
||||
userNotes = userNotes,
|
||||
userReaderFlags = userReaderFlags,
|
||||
userChapterFlags = userChapterFlags,
|
||||
stateChapterLastUpdate = stateChapterLastUpdate,
|
||||
stateChapterNextUpdate = stateChapterNextUpdate,
|
||||
stateChapterFetchInterval = stateChapterFetchInterval,
|
||||
stateCoverLastModified = stateCoverLastModified,
|
||||
stateInitialized = stateInitialized,
|
||||
),
|
||||
categories = categories.split(",").map { it.toLong() },
|
||||
totalChapters = totalCount,
|
||||
@@ -137,61 +118,51 @@ object MangaMapper {
|
||||
|
||||
fun mapMangaWithChapterCount(
|
||||
id: Long,
|
||||
source: Long,
|
||||
url: String,
|
||||
artist: String?,
|
||||
author: String?,
|
||||
description: String?,
|
||||
genre: List<String>?,
|
||||
title: String,
|
||||
status: Long,
|
||||
thumbnailUrl: String?,
|
||||
favorite: Boolean,
|
||||
lastUpdate: Long?,
|
||||
nextUpdate: Long?,
|
||||
initialized: Boolean,
|
||||
viewerFlags: Long,
|
||||
chapterFlags: Long,
|
||||
coverLastModified: Long,
|
||||
dateAdded: Long,
|
||||
updateStrategy: UpdateStrategy,
|
||||
calculateInterval: Long,
|
||||
lastModifiedAt: Long,
|
||||
favoriteModifiedAt: Long?,
|
||||
version: Long,
|
||||
isSyncing: Long,
|
||||
notes: String,
|
||||
memo: JsonObject,
|
||||
totalCount: Long,
|
||||
sourceId: Long,
|
||||
remoteUrl: String,
|
||||
remoteTitle: String,
|
||||
remoteAuthor: String?,
|
||||
remoteArtist: String?,
|
||||
remoteDescription: String?,
|
||||
remoteGenre: List<String>?,
|
||||
remoteStatus: Long,
|
||||
remoteCover: String?,
|
||||
remoteUpdateStrategy: UpdateStrategy,
|
||||
remoteMemo: JsonObject,
|
||||
userFavoriteAt: Instant?,
|
||||
userNotes: String,
|
||||
userReaderFlags: Long,
|
||||
userChapterFlags: Long,
|
||||
stateChapterLastUpdate: Instant?,
|
||||
stateChapterNextUpdate: Instant?,
|
||||
stateChapterFetchInterval: Long,
|
||||
stateCoverLastModified: Instant?,
|
||||
stateInitialized: Boolean,
|
||||
chapterCount: Long,
|
||||
): MangaWithChapterCount = MangaWithChapterCount(
|
||||
manga = mapManga(
|
||||
id,
|
||||
source,
|
||||
url,
|
||||
artist,
|
||||
author,
|
||||
description,
|
||||
genre,
|
||||
title,
|
||||
status,
|
||||
thumbnailUrl,
|
||||
favorite,
|
||||
lastUpdate,
|
||||
nextUpdate,
|
||||
initialized,
|
||||
viewerFlags,
|
||||
chapterFlags,
|
||||
coverLastModified,
|
||||
dateAdded,
|
||||
updateStrategy,
|
||||
calculateInterval,
|
||||
lastModifiedAt,
|
||||
favoriteModifiedAt,
|
||||
version,
|
||||
isSyncing,
|
||||
notes,
|
||||
memo,
|
||||
id = id,
|
||||
sourceId = sourceId,
|
||||
remoteUrl = remoteUrl,
|
||||
remoteTitle = remoteTitle,
|
||||
remoteAuthor = remoteAuthor,
|
||||
remoteArtist = remoteArtist,
|
||||
remoteDescription = remoteDescription,
|
||||
remoteGenre = remoteGenre,
|
||||
remoteStatus = remoteStatus,
|
||||
remoteCover = remoteCover,
|
||||
remoteUpdateStrategy = remoteUpdateStrategy,
|
||||
remoteMemo = remoteMemo,
|
||||
userFavoriteAt = userFavoriteAt,
|
||||
userNotes = userNotes,
|
||||
userReaderFlags = userReaderFlags,
|
||||
userChapterFlags = userChapterFlags,
|
||||
stateChapterLastUpdate = stateChapterLastUpdate,
|
||||
stateChapterNextUpdate = stateChapterNextUpdate,
|
||||
stateChapterFetchInterval = stateChapterFetchInterval,
|
||||
stateCoverLastModified = stateCoverLastModified,
|
||||
stateInitialized = stateInitialized,
|
||||
),
|
||||
chapterCount = totalCount,
|
||||
chapterCount = chapterCount,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -8,20 +8,20 @@ import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.atStartOfDayIn
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import logcat.LogPriority
|
||||
import tachiyomi.core.common.util.lang.toLong
|
||||
import tachiyomi.core.common.util.system.logcat
|
||||
import tachiyomi.data.Database
|
||||
import tachiyomi.data.MemoColumnAdapter
|
||||
import tachiyomi.data.StringListColumnAdapter
|
||||
import tachiyomi.data.UpdateStrategyColumnAdapter
|
||||
import tachiyomi.data.subscribeToList
|
||||
import tachiyomi.data.subscribeToOne
|
||||
import tachiyomi.data.subscribeToOneOrNull
|
||||
import tachiyomi.domain.library.model.LibraryManga
|
||||
import tachiyomi.domain.manga.model.Manga
|
||||
import tachiyomi.domain.manga.model.MangaRemoteUpdate
|
||||
import tachiyomi.domain.manga.model.MangaUpdate
|
||||
import tachiyomi.domain.manga.model.MangaWithChapterCount
|
||||
import tachiyomi.domain.manga.repository.MangaRepository
|
||||
@@ -35,37 +35,37 @@ class MangaRepositoryImpl(
|
||||
) : MangaRepository {
|
||||
|
||||
override suspend fun getMangaById(id: Long): Manga {
|
||||
return database.mangasQueries
|
||||
return database.mangaQueries
|
||||
.getMangaById(id, MangaMapper::mapManga)
|
||||
.awaitAsOne()
|
||||
}
|
||||
|
||||
override fun getMangaByIdAsFlow(id: Long): Flow<Manga> {
|
||||
return database.mangasQueries
|
||||
return database.mangaQueries
|
||||
.getMangaById(id, MangaMapper::mapManga)
|
||||
.subscribeToOne()
|
||||
}
|
||||
|
||||
override suspend fun getMangaByUrlAndSourceId(url: String, sourceId: Long): Manga? {
|
||||
return database.mangasQueries
|
||||
.getMangaByUrlAndSource(url, sourceId, MangaMapper::mapManga)
|
||||
return database.mangaQueries
|
||||
.getMangaByUrlAndSource(sourceId = sourceId, remoteUrl = url, mapper = MangaMapper::mapManga)
|
||||
.awaitAsOneOrNull()
|
||||
}
|
||||
|
||||
override fun getMangaByUrlAndSourceIdAsFlow(url: String, sourceId: Long): Flow<Manga?> {
|
||||
return database.mangasQueries
|
||||
.getMangaByUrlAndSource(url, sourceId, MangaMapper::mapManga)
|
||||
return database.mangaQueries
|
||||
.getMangaByUrlAndSource(sourceId = sourceId, remoteUrl = url, mapper = MangaMapper::mapManga)
|
||||
.subscribeToOneOrNull()
|
||||
}
|
||||
|
||||
override suspend fun getFavorites(): List<Manga> {
|
||||
return database.mangasQueries
|
||||
return database.mangaQueries
|
||||
.getFavorites(MangaMapper::mapManga)
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override suspend fun getReadMangaNotInLibrary(): List<Manga> {
|
||||
return database.mangasQueries
|
||||
return database.mangaQueries
|
||||
.getReadMangaNotInLibrary(MangaMapper::mapManga)
|
||||
.awaitAsList()
|
||||
}
|
||||
@@ -83,13 +83,13 @@ class MangaRepositoryImpl(
|
||||
}
|
||||
|
||||
override fun getFavoritesBySourceId(sourceId: Long): Flow<List<Manga>> {
|
||||
return database.mangasQueries
|
||||
return database.mangaQueries
|
||||
.getFavoriteBySourceId(sourceId, MangaMapper::mapManga)
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override suspend fun getDuplicateLibraryManga(id: Long, title: String): List<MangaWithChapterCount> {
|
||||
return database.mangasQueries
|
||||
return database.mangaQueries
|
||||
.getDuplicateLibraryManga(id, title, MangaMapper::mapMangaWithChapterCount)
|
||||
.awaitAsList()
|
||||
}
|
||||
@@ -100,11 +100,10 @@ class MangaRepositoryImpl(
|
||||
includedCategories: List<Long>,
|
||||
): Flow<List<Manga>> {
|
||||
val timeZone = TimeZone.currentSystemDefault()
|
||||
val epochMillis =
|
||||
Clock.System.now().toLocalDateTime(timeZone).date.atStartOfDayIn(timeZone).toEpochMilliseconds()
|
||||
return database.mangasQueries
|
||||
val startOfDay = Clock.System.now().toLocalDateTime(timeZone).date.atStartOfDayIn(timeZone)
|
||||
return database.mangaQueries
|
||||
.getUpcomingManga(
|
||||
startOfDay = epochMillis,
|
||||
startOfDay = startOfDay,
|
||||
statuses = statuses,
|
||||
includedEmpty = includedCategories.isEmpty(),
|
||||
includedCategories = includedCategories,
|
||||
@@ -117,7 +116,7 @@ class MangaRepositoryImpl(
|
||||
|
||||
override suspend fun resetViewerFlags(): Boolean {
|
||||
return try {
|
||||
database.mangasQueries.resetViewerFlags()
|
||||
database.mangaQueries.resetViewerFlags()
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e)
|
||||
@@ -125,11 +124,53 @@ class MangaRepositoryImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun deleteNonLibraryManga(sourceIds: List<Long>, keepReadManga: Boolean) {
|
||||
database.mangaQueries.deleteNonLibraryManga(sourceIds, keepReadManga.toLong())
|
||||
}
|
||||
|
||||
override suspend fun setMangaCategories(mangaId: Long, categoryIds: List<Long>) {
|
||||
database.transaction {
|
||||
database.mangas_categoriesQueries.deleteMangaCategoryByMangaId(mangaId)
|
||||
database.manga_categoryQueries.deleteMangaCategoryByMangaId(mangaId)
|
||||
categoryIds.forEach { categoryId ->
|
||||
database.mangas_categoriesQueries.insert(mangaId, categoryId)
|
||||
database.manga_categoryQueries.insert(mangaId = mangaId, categoryId = categoryId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getExcludedScanlators(mangaId: Long): Set<String> {
|
||||
return database.excluded_scanlatorQueries
|
||||
.getExcludedScanlatorsByMangaId(mangaId)
|
||||
.awaitAsList()
|
||||
.toSet()
|
||||
}
|
||||
|
||||
override suspend fun getExcludedScanlatorsByMangaIds(mangaIds: List<Long>): Map<Long, List<String>> {
|
||||
return database.excluded_scanlatorQueries
|
||||
.getExcludedScanlatorsByMangaIds(mangaIds)
|
||||
.awaitAsList()
|
||||
.groupBy({ it.manga_id }, { it.scanlator })
|
||||
}
|
||||
|
||||
override fun getExcludedScanlatorsAsFlow(mangaId: Long): Flow<Set<String>> {
|
||||
return database.excluded_scanlatorQueries
|
||||
.getExcludedScanlatorsByMangaId(mangaId)
|
||||
.subscribeToList()
|
||||
.map { it.toSet() }
|
||||
}
|
||||
|
||||
override suspend fun setExcludedScanlators(mangaId: Long, excludedScanlators: Set<String>) {
|
||||
database.transaction {
|
||||
val current = database.excluded_scanlatorQueries
|
||||
.getExcludedScanlatorsByMangaId(mangaId)
|
||||
.awaitAsList()
|
||||
.toSet()
|
||||
excludedScanlators.minus(current).forEach { scanlator ->
|
||||
database.excluded_scanlatorQueries.insert(mangaId, scanlator)
|
||||
}
|
||||
val toRemove = current.minus(excludedScanlators)
|
||||
// An empty delete still tells every chapter query to run again
|
||||
if (toRemove.isNotEmpty()) {
|
||||
database.excluded_scanlatorQueries.remove(mangaId, toRemove)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -157,28 +198,26 @@ class MangaRepositoryImpl(
|
||||
override suspend fun insertNetworkManga(manga: List<Manga>): List<Manga> {
|
||||
return database.transactionWithResult {
|
||||
manga.map {
|
||||
database.mangasQueries.insertNetworkManga(
|
||||
source = it.source,
|
||||
url = it.url,
|
||||
artist = it.artist,
|
||||
author = it.author,
|
||||
description = it.description,
|
||||
genre = it.genre,
|
||||
title = it.title,
|
||||
status = it.status,
|
||||
thumbnailUrl = it.thumbnailUrl,
|
||||
favorite = it.favorite,
|
||||
lastUpdate = it.lastUpdate,
|
||||
nextUpdate = it.nextUpdate,
|
||||
calculateInterval = it.fetchInterval.toLong(),
|
||||
initialized = it.initialized,
|
||||
viewerFlags = it.viewerFlags,
|
||||
chapterFlags = it.chapterFlags,
|
||||
coverLastModified = it.coverLastModified,
|
||||
dateAdded = it.dateAdded,
|
||||
updateStrategy = it.updateStrategy,
|
||||
version = it.version,
|
||||
memo = it.memo,
|
||||
database.mangaQueries.insertNetworkManga(
|
||||
sourceId = it.source,
|
||||
remoteUrl = it.url,
|
||||
remoteArtist = it.artist,
|
||||
remoteAuthor = it.author,
|
||||
remoteDescription = it.description,
|
||||
remoteGenre = it.genre,
|
||||
remoteTitle = it.title,
|
||||
remoteStatus = it.status,
|
||||
remoteCover = it.thumbnailUrl,
|
||||
userFavoriteAt = it.favoriteAt,
|
||||
stateChapterLastUpdate = it.lastUpdate,
|
||||
stateChapterNextUpdate = it.nextUpdate,
|
||||
stateChapterFetchInterval = it.fetchInterval.toLong(),
|
||||
stateInitialized = it.initialized,
|
||||
userReaderFlags = it.viewerFlags,
|
||||
userChapterFlags = it.chapterFlags,
|
||||
stateCoverLastModified = it.coverLastModified,
|
||||
remoteUpdateStrategy = it.updateStrategy,
|
||||
remoteMemo = it.memo,
|
||||
updateTitle = it.title.isNotBlank(),
|
||||
updateCover = !it.thumbnailUrl.isNullOrBlank(),
|
||||
updateDetails = it.initialized,
|
||||
@@ -189,35 +228,46 @@ class MangaRepositoryImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateRemote(update: MangaRemoteUpdate): Boolean {
|
||||
return try {
|
||||
database.mangaQueries.updateRemote(
|
||||
remoteArtist = update.artist,
|
||||
remoteAuthor = update.author,
|
||||
remoteDescription = update.description,
|
||||
remoteGenre = update.genre,
|
||||
remoteTitle = update.title,
|
||||
remoteStatus = update.status,
|
||||
remoteCover = update.thumbnailUrl,
|
||||
stateInitialized = update.initialized,
|
||||
stateCoverLastModified = update.coverLastModified,
|
||||
remoteUpdateStrategy = update.updateStrategy,
|
||||
remoteMemo = update.memo,
|
||||
id = update.id,
|
||||
)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun partialUpdate(vararg mangaUpdates: MangaUpdate) {
|
||||
database.transaction {
|
||||
mangaUpdates.forEach { value ->
|
||||
database.mangasQueries.update(
|
||||
source = value.source,
|
||||
url = value.url,
|
||||
artist = value.artist,
|
||||
author = value.author,
|
||||
description = value.description,
|
||||
genre = value.genre?.let(StringListColumnAdapter::encode),
|
||||
title = value.title,
|
||||
status = value.status,
|
||||
thumbnailUrl = value.thumbnailUrl,
|
||||
favorite = value.favorite,
|
||||
lastUpdate = value.lastUpdate,
|
||||
nextUpdate = value.nextUpdate,
|
||||
calculateInterval = value.fetchInterval?.toLong(),
|
||||
initialized = value.initialized,
|
||||
viewer = value.viewerFlags,
|
||||
chapterFlags = value.chapterFlags,
|
||||
coverLastModified = value.coverLastModified,
|
||||
dateAdded = value.dateAdded,
|
||||
mangaId = value.id,
|
||||
updateStrategy = value.updateStrategy?.let(UpdateStrategyColumnAdapter::encode),
|
||||
version = value.version,
|
||||
isSyncing = 0,
|
||||
notes = value.notes,
|
||||
memo = value.memo?.let(MemoColumnAdapter::encode),
|
||||
)
|
||||
with(value) {
|
||||
database.mangaQueries.update(
|
||||
userFavoriteAtSet = isSet(::favoriteAt),
|
||||
userFavoriteAt = favoriteAt,
|
||||
stateChapterLastUpdate = lastUpdate,
|
||||
stateChapterNextUpdate = nextUpdate,
|
||||
stateChapterFetchInterval = fetchInterval?.toLong(),
|
||||
userReaderFlags = viewerFlags,
|
||||
userChapterFlags = chapterFlags,
|
||||
stateCoverLastModified = coverLastModified,
|
||||
id = id,
|
||||
userNotes = notes,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import eu.kanade.tachiyomi.network.GET
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import eu.kanade.tachiyomi.network.awaitSuccess
|
||||
import eu.kanade.tachiyomi.network.parseAs
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import tachiyomi.domain.release.interactor.GetApplicationRelease
|
||||
import tachiyomi.domain.release.model.Release
|
||||
@@ -23,11 +25,13 @@ class ReleaseServiceImpl(
|
||||
) : ReleaseService {
|
||||
|
||||
override suspend fun latest(arguments: GetApplicationRelease.Arguments): Release? {
|
||||
val release = with(json) {
|
||||
networkService.client
|
||||
.newCall(GET("https://api.github.com/repos/${arguments.repository}/releases/latest"))
|
||||
.awaitSuccess()
|
||||
.parseAs<GithubRelease>()
|
||||
val release = withContext(Dispatchers.IO) {
|
||||
with(json) {
|
||||
networkService.client
|
||||
.newCall(GET("https://gitea.derekkoo.win/api/v1/repos/${arguments.repository}/releases/latest"))
|
||||
.awaitSuccess()
|
||||
.parseAs<GithubRelease>()
|
||||
}
|
||||
}
|
||||
|
||||
val downloadLink = getDownloadLink(release = release, isFoss = arguments.isFoss) ?: return null
|
||||
|
||||
@@ -5,8 +5,9 @@ import eu.kanade.tachiyomi.source.Source
|
||||
import eu.kanade.tachiyomi.source.model.FilterList
|
||||
import eu.kanade.tachiyomi.source.model.MangasPage
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import mihon.domain.manga.model.toDomainManga
|
||||
import tachiyomi.core.common.util.lang.withIOContext
|
||||
import tachiyomi.domain.manga.interactor.NetworkToLocalManga
|
||||
import tachiyomi.domain.manga.model.Manga
|
||||
import tachiyomi.domain.source.repository.SourcePagingSource
|
||||
@@ -54,7 +55,7 @@ abstract class BaseSourcePagingSource(
|
||||
|
||||
return try {
|
||||
val source = source()
|
||||
val mangasPage = withIOContext {
|
||||
val mangasPage = withContext(Dispatchers.IO) {
|
||||
requestNextPage(source, page.toInt())
|
||||
.takeIf { it.mangas.isNotEmpty() }
|
||||
?: throw NoResultsException()
|
||||
|
||||
@@ -48,7 +48,7 @@ class SourceRepositoryImpl(
|
||||
}
|
||||
|
||||
override fun getSourcesWithFavoriteCount(): Flow<List<Pair<DomainSource, Long>>> {
|
||||
val sourceIdWithFavoriteCountFlow = database.mangasQueries
|
||||
val sourceIdWithFavoriteCountFlow = database.mangaQueries
|
||||
.getSourceIdWithFavoriteCount()
|
||||
.subscribeToList()
|
||||
return combine(sourceIdWithFavoriteCountFlow, sourceManager.sources) { sourceIdWithFavoriteCount, _ ->
|
||||
@@ -66,7 +66,7 @@ class SourceRepositoryImpl(
|
||||
}
|
||||
|
||||
override fun getSourcesWithNonLibraryManga(): Flow<List<SourceWithCount>> {
|
||||
return database.mangasQueries
|
||||
return database.mangaQueries
|
||||
.getSourceIdsWithNonLibraryManga()
|
||||
.subscribeToList()
|
||||
.map { sourceId ->
|
||||
|
||||
@@ -19,24 +19,24 @@ class StubSourceRepositoryImpl(
|
||||
) : StubSourceRepository {
|
||||
|
||||
override fun subscribeAll(): Flow<List<StubSource>> {
|
||||
return database.sourcesQueries
|
||||
return database.sourceQueries
|
||||
.findAll(::mapStubSource)
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override suspend fun getStubSource(id: Long): StubSource? {
|
||||
return database.sourcesQueries
|
||||
return database.sourceQueries
|
||||
.findOne(id, ::mapStubSource)
|
||||
.awaitAsOneOrNull()
|
||||
}
|
||||
|
||||
override suspend fun upsertStubSource(id: Long, lang: String, name: String) {
|
||||
database.sourcesQueries.upsert(id, lang, name)
|
||||
database.sourceQueries.upsert(id = id, name = name, language = lang)
|
||||
}
|
||||
|
||||
private fun mapStubSource(
|
||||
id: Long,
|
||||
lang: String,
|
||||
name: String,
|
||||
lang: String,
|
||||
): StubSource = StubSource(id = id, lang = lang, name = name)
|
||||
}
|
||||
|
||||
@@ -1,27 +1,28 @@
|
||||
package tachiyomi.data.track
|
||||
|
||||
import tachiyomi.domain.track.model.Track
|
||||
import kotlin.time.Instant
|
||||
|
||||
object TrackMapper {
|
||||
fun mapTrack(
|
||||
id: Long,
|
||||
mangaId: Long,
|
||||
syncId: Long,
|
||||
trackerId: Long,
|
||||
remoteId: Long,
|
||||
remoteUrl: String,
|
||||
libraryId: Long?,
|
||||
title: String,
|
||||
lastChapterRead: Double,
|
||||
totalChapters: Long,
|
||||
status: Long,
|
||||
score: Double,
|
||||
remoteUrl: String,
|
||||
startDate: Long,
|
||||
finishDate: Long,
|
||||
startDate: Instant?,
|
||||
finishDate: Instant?,
|
||||
private: Boolean,
|
||||
): Track = Track(
|
||||
id = id,
|
||||
mangaId = mangaId,
|
||||
trackerId = syncId,
|
||||
trackerId = trackerId,
|
||||
remoteId = remoteId,
|
||||
libraryId = libraryId,
|
||||
title = title,
|
||||
|
||||
@@ -20,50 +20,63 @@ class TrackRepositoryImpl(
|
||||
) : TrackRepository {
|
||||
|
||||
override suspend fun getTrackById(id: Long): Track? {
|
||||
return database.manga_syncQueries
|
||||
return database.manga_trackQueries
|
||||
.getTrackById(id, TrackMapper::mapTrack)
|
||||
.awaitAsOneOrNull()
|
||||
}
|
||||
|
||||
override suspend fun getTracksByMangaId(mangaId: Long): List<Track> {
|
||||
return database.manga_syncQueries
|
||||
return database.manga_trackQueries
|
||||
.getTracksByMangaId(mangaId, TrackMapper::mapTrack)
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override suspend fun getTracksByMangaIds(mangaIds: List<Long>): Map<Long, List<Track>> {
|
||||
return database.manga_trackQueries
|
||||
.getTracksByMangaIds(mangaIds, TrackMapper::mapTrack)
|
||||
.awaitAsList()
|
||||
.groupBy { it.mangaId }
|
||||
}
|
||||
|
||||
override suspend fun getTracks(): List<Track> {
|
||||
return database.manga_trackQueries
|
||||
.getTracks(TrackMapper::mapTrack)
|
||||
.awaitAsList()
|
||||
}
|
||||
|
||||
override fun getTracksAsFlow(): Flow<List<Track>> {
|
||||
return database.manga_syncQueries
|
||||
return database.manga_trackQueries
|
||||
.getTracks(TrackMapper::mapTrack)
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override fun getTracksByMangaIdAsFlow(mangaId: Long): Flow<List<Track>> {
|
||||
return database.manga_syncQueries
|
||||
return database.manga_trackQueries
|
||||
.getTracksByMangaId(mangaId, TrackMapper::mapTrack)
|
||||
.subscribeToList()
|
||||
}
|
||||
|
||||
override suspend fun delete(mangaId: Long, trackerId: Long) {
|
||||
database.manga_syncQueries.delete(
|
||||
database.manga_trackQueries.delete(
|
||||
mangaId = mangaId,
|
||||
syncId = trackerId,
|
||||
trackerId = trackerId,
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun insert(track: Track) {
|
||||
insertValues(track)
|
||||
override suspend fun upsert(track: Track) {
|
||||
upsertValues(track)
|
||||
}
|
||||
|
||||
override suspend fun insertAll(tracks: List<Track>) {
|
||||
insertValues(*tracks.toTypedArray())
|
||||
override suspend fun upsertAll(tracks: List<Track>) {
|
||||
upsertValues(*tracks.toTypedArray())
|
||||
}
|
||||
|
||||
private suspend fun insertValues(vararg tracks: Track) {
|
||||
private suspend fun upsertValues(vararg tracks: Track) {
|
||||
database.transaction {
|
||||
tracks.forEach { mangaTrack ->
|
||||
database.manga_syncQueries.insert(
|
||||
database.manga_trackQueries.upsert(
|
||||
mangaId = mangaTrack.mangaId,
|
||||
syncId = mangaTrack.trackerId,
|
||||
trackerId = mangaTrack.trackerId,
|
||||
remoteId = mangaTrack.remoteId,
|
||||
libraryId = mangaTrack.libraryId,
|
||||
title = mangaTrack.title,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package tachiyomi.data.updates
|
||||
|
||||
import app.cash.sqldelight.Query
|
||||
import app.cash.sqldelight.async.coroutines.awaitAsList
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
@@ -12,6 +13,7 @@ import tachiyomi.data.subscribeToList
|
||||
import tachiyomi.domain.manga.model.MangaCover
|
||||
import tachiyomi.domain.updates.model.UpdatesWithRelations
|
||||
import tachiyomi.domain.updates.repository.UpdatesRepository
|
||||
import kotlin.time.Instant
|
||||
|
||||
@Inject
|
||||
@SingleIn(AppScope::class)
|
||||
@@ -22,21 +24,14 @@ class UpdatesRepositoryImpl(
|
||||
|
||||
override suspend fun awaitWithRead(
|
||||
read: Boolean,
|
||||
after: Long,
|
||||
after: Instant,
|
||||
limit: Long,
|
||||
): List<UpdatesWithRelations> {
|
||||
return database.updatesViewQueries
|
||||
.getUpdatesByReadStatus(
|
||||
read = read,
|
||||
after = after,
|
||||
limit = limit,
|
||||
mapper = ::mapUpdatesWithRelations,
|
||||
)
|
||||
.awaitAsList()
|
||||
return getUpdatesByReadStatus(read = read, after = after, limit = limit).awaitAsList()
|
||||
}
|
||||
|
||||
override fun subscribeAll(
|
||||
after: Long,
|
||||
after: Instant,
|
||||
limit: Long,
|
||||
unread: Boolean?,
|
||||
started: Boolean?,
|
||||
@@ -64,17 +59,26 @@ class UpdatesRepositoryImpl(
|
||||
|
||||
override fun subscribeWithRead(
|
||||
read: Boolean,
|
||||
after: Long,
|
||||
after: Instant,
|
||||
limit: Long,
|
||||
): Flow<List<UpdatesWithRelations>> {
|
||||
return database.updatesViewQueries
|
||||
.getUpdatesByReadStatus(
|
||||
read = read,
|
||||
return getUpdatesByReadStatus(read = read, after = after, limit = limit).subscribeToList()
|
||||
}
|
||||
|
||||
private fun getUpdatesByReadStatus(read: Boolean, after: Instant, limit: Long): Query<UpdatesWithRelations> {
|
||||
return if (read) {
|
||||
database.updatesViewQueries.getReadUpdates(
|
||||
after = after,
|
||||
limit = limit,
|
||||
mapper = ::mapUpdatesWithRelations,
|
||||
)
|
||||
.subscribeToList()
|
||||
} else {
|
||||
database.updatesViewQueries.getUnreadUpdates(
|
||||
after = after,
|
||||
limit = limit,
|
||||
mapper = ::mapUpdatesWithRelations,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
@@ -87,13 +91,13 @@ class UpdatesRepositoryImpl(
|
||||
chapterUrl: String,
|
||||
read: Boolean,
|
||||
bookmark: Boolean,
|
||||
lastPageRead: Long,
|
||||
lastPageRead: Int,
|
||||
sourceId: Long,
|
||||
favorite: Boolean,
|
||||
thumbnailUrl: String?,
|
||||
coverLastModified: Long,
|
||||
dateUpload: Long,
|
||||
dateFetch: Long,
|
||||
coverLastModified: Instant?,
|
||||
dateUpload: Instant?,
|
||||
dateFetch: Instant,
|
||||
excludedScanlator: String?,
|
||||
): UpdatesWithRelations = UpdatesWithRelations(
|
||||
mangaId = mangaId,
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
CREATE TABLE categories(
|
||||
_id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
sort INTEGER NOT NULL,
|
||||
flags INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- Insert system category
|
||||
INSERT OR IGNORE INTO categories(_id, name, sort, flags) VALUES (0, "", -1, 0);
|
||||
-- Disallow deletion of default category
|
||||
CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE
|
||||
ON categories
|
||||
BEGIN SELECT CASE
|
||||
WHEN old._id <= 0 THEN
|
||||
RAISE(ABORT, "System category can't be deleted")
|
||||
END;
|
||||
END;
|
||||
|
||||
getCategory:
|
||||
SELECT *
|
||||
FROM categories
|
||||
WHERE _id = :id
|
||||
LIMIT 1;
|
||||
|
||||
getCategories:
|
||||
SELECT
|
||||
_id AS id,
|
||||
name,
|
||||
sort AS `order`,
|
||||
flags
|
||||
FROM categories
|
||||
ORDER BY sort;
|
||||
|
||||
getCategoriesByMangaId:
|
||||
SELECT
|
||||
C._id AS id,
|
||||
C.name,
|
||||
C.sort AS `order`,
|
||||
C.flags
|
||||
FROM categories C
|
||||
JOIN mangas_categories MC
|
||||
ON C._id = MC.category_id
|
||||
WHERE MC.manga_id = :mangaId;
|
||||
|
||||
insert:
|
||||
INSERT INTO categories(name, sort, flags)
|
||||
VALUES (:name, :order, :flags);
|
||||
|
||||
delete:
|
||||
DELETE FROM categories
|
||||
WHERE _id = :categoryId;
|
||||
|
||||
updateName:
|
||||
UPDATE categories
|
||||
SET name = :name
|
||||
WHERE _id = :categoryId;
|
||||
|
||||
updateOrder:
|
||||
UPDATE categories
|
||||
SET sort = :order
|
||||
WHERE _id = :categoryId;
|
||||
|
||||
updateFlags:
|
||||
UPDATE categories
|
||||
SET flags = :flags
|
||||
WHERE _id = :categoryId;
|
||||
|
||||
updateAllFlags:
|
||||
UPDATE categories SET
|
||||
flags = coalesce(:flags, flags);
|
||||
@@ -0,0 +1,78 @@
|
||||
CREATE TABLE category(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
`order` INTEGER NOT NULL UNIQUE,
|
||||
flags INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- Insert system category
|
||||
INSERT OR IGNORE INTO category(id, name, `order`, flags) VALUES (0, "", -1, 0);
|
||||
-- Disallow deletion of default category
|
||||
CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE
|
||||
ON category
|
||||
BEGIN SELECT CASE
|
||||
WHEN old.id <= 0 THEN
|
||||
RAISE(ABORT, "System category can't be deleted")
|
||||
END;
|
||||
END;
|
||||
|
||||
getCategory:
|
||||
SELECT id, name, `order`, flags
|
||||
FROM category
|
||||
WHERE id = :id;
|
||||
|
||||
getCategories:
|
||||
SELECT id, name, `order`, flags
|
||||
FROM category
|
||||
ORDER BY `order`;
|
||||
|
||||
getCategoriesByMangaId:
|
||||
SELECT C.id, C.name, C.`order`, C.flags
|
||||
FROM category C
|
||||
JOIN manga_category MC
|
||||
ON C.id = MC.category_id
|
||||
WHERE MC.manga_id = :mangaId
|
||||
ORDER BY C.`order`;
|
||||
|
||||
getCategoriesByMangaIds:
|
||||
SELECT MC.manga_id, C.id, C.name, C.`order`, C.flags
|
||||
FROM category C
|
||||
JOIN manga_category MC
|
||||
ON C.id = MC.category_id
|
||||
WHERE MC.manga_id IN :mangaIds
|
||||
ORDER BY MC.manga_id, C.`order`;
|
||||
|
||||
insert:
|
||||
-- The position is taken inside the insert, so two categories created at once cannot claim the same one
|
||||
INSERT INTO category(name, `order`, flags)
|
||||
SELECT :name, coalesce(max(`order`), -1) + 1, :flags
|
||||
FROM category;
|
||||
|
||||
delete:
|
||||
DELETE FROM category
|
||||
WHERE id = :id;
|
||||
|
||||
updateName:
|
||||
UPDATE category
|
||||
SET name = :name
|
||||
WHERE id = :id;
|
||||
|
||||
getUserCategoryIds:
|
||||
SELECT id
|
||||
FROM category
|
||||
WHERE id > 0
|
||||
ORDER BY `order`;
|
||||
|
||||
updateOrder:
|
||||
UPDATE category
|
||||
SET `order` = :order
|
||||
WHERE id = :id;
|
||||
|
||||
updateFlags:
|
||||
UPDATE category
|
||||
SET flags = :flags
|
||||
WHERE id = :id;
|
||||
|
||||
updateAllFlags:
|
||||
UPDATE category
|
||||
SET flags = :flags;
|
||||
@@ -0,0 +1,133 @@
|
||||
import kotlin.Boolean;
|
||||
import kotlin.Int;
|
||||
import kotlin.time.Instant;
|
||||
import kotlinx.serialization.json.JsonObject;
|
||||
|
||||
CREATE TABLE chapter(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
remote_name TEXT NOT NULL,
|
||||
remote_scanlator TEXT,
|
||||
remote_chapter_number REAL NOT NULL,
|
||||
remote_date_upload INTEGER AS Instant,
|
||||
remote_order INTEGER NOT NULL,
|
||||
remote_memo BLOB AS JsonObject NOT NULL,
|
||||
user_read INTEGER AS Boolean NOT NULL,
|
||||
user_bookmark INTEGER AS Boolean NOT NULL,
|
||||
user_last_page_read INTEGER AS Int NOT NULL,
|
||||
state_date_fetch INTEGER AS Instant NOT NULL,
|
||||
UNIQUE(manga_id, remote_url),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX chapter_state_date_fetch_index ON chapter(state_date_fetch);
|
||||
CREATE INDEX chapter_unread_state_date_fetch_index ON chapter(state_date_fetch) WHERE user_read = 0;
|
||||
|
||||
getChapterById:
|
||||
SELECT
|
||||
id, manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number,
|
||||
remote_date_upload, remote_order, remote_memo, user_read, user_bookmark,
|
||||
user_last_page_read, state_date_fetch
|
||||
FROM chapter
|
||||
WHERE id = :id;
|
||||
|
||||
getChaptersByMangaId:
|
||||
SELECT
|
||||
C.id, C.manga_id, C.remote_url, C.remote_name, C.remote_scanlator, C.remote_chapter_number,
|
||||
C.remote_date_upload, C.remote_order, C.remote_memo, C.user_read, C.user_bookmark,
|
||||
C.user_last_page_read, C.state_date_fetch
|
||||
FROM chapter C
|
||||
LEFT JOIN excluded_scanlator ES
|
||||
ON C.manga_id = ES.manga_id
|
||||
AND C.remote_scanlator = ES.scanlator
|
||||
WHERE C.manga_id = :mangaId
|
||||
AND (
|
||||
:applyScanlatorFilter = 0
|
||||
OR ES.scanlator IS NULL
|
||||
);
|
||||
|
||||
getChaptersByMangaIds:
|
||||
-- The unique index's order, which getChaptersByMangaId returns too, so backups keep their chapter order
|
||||
SELECT
|
||||
id, manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number,
|
||||
remote_date_upload, remote_order, remote_memo, user_read, user_bookmark,
|
||||
user_last_page_read, state_date_fetch
|
||||
FROM chapter
|
||||
WHERE manga_id IN :mangaIds
|
||||
ORDER BY manga_id, remote_url;
|
||||
|
||||
getChapterSummaryByMangaId:
|
||||
SELECT count(*) AS total, max(remote_chapter_number) AS latestChapterNumber
|
||||
FROM chapter
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
getScanlatorsByMangaId:
|
||||
SELECT DISTINCT remote_scanlator
|
||||
FROM chapter
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
getBookmarkedChaptersByMangaId:
|
||||
SELECT
|
||||
id, manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number,
|
||||
remote_date_upload, remote_order, remote_memo, user_read, user_bookmark,
|
||||
user_last_page_read, state_date_fetch
|
||||
FROM chapter
|
||||
WHERE manga_id = :mangaId
|
||||
AND user_bookmark;
|
||||
|
||||
getChapterByUrlAndMangaId:
|
||||
SELECT
|
||||
id, manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number,
|
||||
remote_date_upload, remote_order, remote_memo, user_read, user_bookmark,
|
||||
user_last_page_read, state_date_fetch
|
||||
FROM chapter
|
||||
WHERE manga_id = :mangaId
|
||||
AND remote_url = :remoteUrl;
|
||||
|
||||
removeChaptersWithIds:
|
||||
DELETE FROM chapter
|
||||
WHERE id IN :chapterIds;
|
||||
|
||||
insertReturningId:
|
||||
INSERT INTO chapter(
|
||||
manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number,
|
||||
remote_date_upload, remote_order, remote_memo, user_read, user_bookmark,
|
||||
user_last_page_read, state_date_fetch
|
||||
)
|
||||
VALUES (
|
||||
:mangaId, :remoteUrl, :remoteName, :remoteScanlator, :remoteChapterNumber,
|
||||
:remoteDateUpload, :remoteOrder, :remoteMemo, :userRead, :userBookmark,
|
||||
:userLastPageRead, :stateDateFetch
|
||||
)
|
||||
-- Two syncs of the same entry can race; the chapter the other one already stored is skipped rather
|
||||
-- than failing the whole batch
|
||||
ON CONFLICT(manga_id, remote_url) DO NOTHING
|
||||
RETURNING id;
|
||||
|
||||
update:
|
||||
UPDATE chapter
|
||||
SET user_read = coalesce(:userRead, user_read),
|
||||
user_bookmark = coalesce(:userBookmark, user_bookmark),
|
||||
user_last_page_read = coalesce(:userLastPageRead, user_last_page_read),
|
||||
state_date_fetch = coalesce(:stateDateFetch, state_date_fetch)
|
||||
WHERE id = :id;
|
||||
|
||||
updateFromBackup:
|
||||
UPDATE chapter
|
||||
SET user_read = coalesce(:userRead, user_read),
|
||||
user_bookmark = coalesce(:userBookmark, user_bookmark),
|
||||
user_last_page_read = coalesce(:userLastPageRead, user_last_page_read),
|
||||
remote_memo = coalesce(:remoteMemo, remote_memo)
|
||||
WHERE id = :id;
|
||||
|
||||
updateRemote:
|
||||
UPDATE chapter
|
||||
SET remote_name = :remoteName,
|
||||
remote_scanlator = coalesce(:remoteScanlator, remote_scanlator),
|
||||
remote_chapter_number = :remoteChapterNumber,
|
||||
remote_order = :remoteOrder,
|
||||
remote_date_upload = coalesce(:remoteDateUpload, remote_date_upload),
|
||||
remote_memo = :remoteMemo
|
||||
WHERE id = :id;
|
||||
@@ -1,128 +0,0 @@
|
||||
import kotlin.Boolean;
|
||||
import kotlinx.serialization.json.JsonObject;
|
||||
|
||||
CREATE TABLE chapters(
|
||||
_id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
scanlator TEXT,
|
||||
read INTEGER AS Boolean NOT NULL,
|
||||
bookmark INTEGER AS Boolean NOT NULL,
|
||||
last_page_read INTEGER NOT NULL,
|
||||
chapter_number REAL NOT NULL,
|
||||
source_order INTEGER NOT NULL,
|
||||
date_fetch INTEGER NOT NULL,
|
||||
date_upload INTEGER NOT NULL,
|
||||
last_modified_at INTEGER NOT NULL DEFAULT 0,
|
||||
version INTEGER NOT NULL DEFAULT 0,
|
||||
is_syncing INTEGER NOT NULL DEFAULT 0,
|
||||
memo BLOB AS JsonObject NOT NULL DEFAULT '{}',
|
||||
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX chapters_manga_id_index ON chapters(manga_id);
|
||||
CREATE INDEX chapters_unread_by_manga_index ON chapters(manga_id, read) WHERE read = 0;
|
||||
CREATE INDEX idx_chapters_url ON chapters(url);
|
||||
|
||||
CREATE TRIGGER update_last_modified_at_chapters
|
||||
AFTER UPDATE ON chapters
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE chapters
|
||||
SET last_modified_at = strftime('%s', 'now')
|
||||
WHERE _id = new._id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER update_chapter_and_manga_version AFTER UPDATE ON chapters
|
||||
WHEN new.is_syncing = 0 AND (
|
||||
new.read != old.read OR
|
||||
new.bookmark != old.bookmark OR
|
||||
new.last_page_read != old.last_page_read
|
||||
)
|
||||
BEGIN
|
||||
-- Update the chapter version
|
||||
UPDATE chapters SET version = version + 1
|
||||
WHERE _id = new._id;
|
||||
|
||||
-- Update the manga version
|
||||
UPDATE mangas SET version = version + 1
|
||||
WHERE _id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE _id = new.manga_id) = 0;
|
||||
END;
|
||||
|
||||
getChapterById:
|
||||
SELECT *
|
||||
FROM chapters
|
||||
WHERE _id = :id;
|
||||
|
||||
getChaptersByMangaId:
|
||||
SELECT C.*
|
||||
FROM chapters C
|
||||
LEFT JOIN excluded_scanlators ES
|
||||
ON C.manga_id = ES.manga_id
|
||||
AND C.scanlator = ES.scanlator
|
||||
WHERE C.manga_id = :mangaId
|
||||
AND (
|
||||
:applyScanlatorFilter = 0
|
||||
OR ES.scanlator IS NULL
|
||||
);
|
||||
|
||||
getScanlatorsByMangaId:
|
||||
SELECT scanlator
|
||||
FROM chapters
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
getBookmarkedChaptersByMangaId:
|
||||
SELECT *
|
||||
FROM chapters
|
||||
WHERE bookmark
|
||||
AND manga_id = :mangaId;
|
||||
|
||||
getChapterByUrl:
|
||||
SELECT *
|
||||
FROM chapters
|
||||
WHERE url = :chapterUrl;
|
||||
|
||||
getChapterByUrlAndMangaId:
|
||||
SELECT *
|
||||
FROM chapters
|
||||
WHERE url = :chapterUrl
|
||||
AND manga_id = :mangaId
|
||||
LIMIT 1;
|
||||
|
||||
removeChaptersWithIds:
|
||||
DELETE FROM chapters
|
||||
WHERE _id IN :chapterIds;
|
||||
|
||||
resetIsSyncing:
|
||||
UPDATE chapters
|
||||
SET is_syncing = 0
|
||||
WHERE is_syncing = 1;
|
||||
|
||||
insert:
|
||||
INSERT INTO chapters(manga_id, url, name, scanlator, read, bookmark, last_page_read, chapter_number, source_order, date_fetch, date_upload, last_modified_at, version, is_syncing, memo)
|
||||
VALUES (:mangaId, :url, :name, :scanlator, :read, :bookmark, :lastPageRead, :chapterNumber, :sourceOrder, :dateFetch, :dateUpload, 0, :version, 0, :memo);
|
||||
|
||||
insertReturningId:
|
||||
INSERT INTO chapters(manga_id, url, name, scanlator, read, bookmark, last_page_read, chapter_number, source_order, date_fetch, date_upload, last_modified_at, version, is_syncing, memo)
|
||||
VALUES (:mangaId, :url, :name, :scanlator, :read, :bookmark, :lastPageRead, :chapterNumber, :sourceOrder, :dateFetch, :dateUpload, 0, :version, 0, :memo)
|
||||
RETURNING _id;
|
||||
|
||||
update:
|
||||
UPDATE chapters
|
||||
SET manga_id = coalesce(:mangaId, manga_id),
|
||||
url = coalesce(:url, url),
|
||||
name = coalesce(:name, name),
|
||||
scanlator = coalesce(:scanlator, scanlator),
|
||||
read = coalesce(:read, read),
|
||||
bookmark = coalesce(:bookmark, bookmark),
|
||||
last_page_read = coalesce(:lastPageRead, last_page_read),
|
||||
chapter_number = coalesce(:chapterNumber, chapter_number),
|
||||
source_order = coalesce(:sourceOrder, source_order),
|
||||
date_fetch = coalesce(:dateFetch, date_fetch),
|
||||
date_upload = coalesce(:dateUpload, date_upload),
|
||||
version = coalesce(:version, version),
|
||||
is_syncing = coalesce(:isSyncing, is_syncing),
|
||||
memo = coalesce(:memo, memo)
|
||||
WHERE _id = :chapterId;
|
||||
@@ -0,0 +1,29 @@
|
||||
CREATE TABLE excluded_scanlator(
|
||||
manga_id INTEGER NOT NULL,
|
||||
scanlator TEXT NOT NULL,
|
||||
UNIQUE(manga_id, scanlator),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
|
||||
insert:
|
||||
INSERT INTO excluded_scanlator(manga_id, scanlator)
|
||||
VALUES (:mangaId, :scanlator)
|
||||
ON CONFLICT(manga_id, scanlator) DO NOTHING;
|
||||
|
||||
remove:
|
||||
DELETE FROM excluded_scanlator
|
||||
WHERE manga_id = :mangaId
|
||||
AND scanlator IN :scanlators;
|
||||
|
||||
getExcludedScanlatorsByMangaId:
|
||||
SELECT scanlator
|
||||
FROM excluded_scanlator
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
getExcludedScanlatorsByMangaIds:
|
||||
SELECT manga_id, scanlator
|
||||
FROM excluded_scanlator
|
||||
WHERE manga_id IN :mangaIds
|
||||
ORDER BY manga_id, scanlator;
|
||||
@@ -1,23 +0,0 @@
|
||||
CREATE TABLE excluded_scanlators(
|
||||
manga_id INTEGER NOT NULL,
|
||||
scanlator TEXT NOT NULL,
|
||||
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX excluded_scanlators_manga_id_index ON excluded_scanlators(manga_id);
|
||||
CREATE INDEX idx_excluded_scanlators_scanlator ON excluded_scanlators(scanlator);
|
||||
|
||||
insert:
|
||||
INSERT INTO excluded_scanlators(manga_id, scanlator)
|
||||
VALUES (:mangaId, :scanlator);
|
||||
|
||||
remove:
|
||||
DELETE FROM excluded_scanlators
|
||||
WHERE manga_id = :mangaId
|
||||
AND scanlator IN :scanlators;
|
||||
|
||||
getExcludedScanlatorsByMangaId:
|
||||
SELECT scanlator
|
||||
FROM excluded_scanlators
|
||||
WHERE manga_id = :mangaId;
|
||||
@@ -11,33 +11,36 @@ CREATE TABLE extension_store(
|
||||
extension_list_url TEXT
|
||||
);
|
||||
|
||||
get:
|
||||
SELECT *
|
||||
FROM extension_store
|
||||
WHERE index_url = :indexUrl;
|
||||
contains:
|
||||
SELECT EXISTS(SELECT 0 FROM extension_store WHERE index_url = :indexUrl);
|
||||
|
||||
getAll:
|
||||
SELECT *
|
||||
SELECT
|
||||
index_url, name, badge_label, signing_key,
|
||||
contact_website, contact_discord, is_legacy, extension_list_url
|
||||
FROM extension_store;
|
||||
|
||||
getCount:
|
||||
SELECT COUNT(*)
|
||||
SELECT count(*)
|
||||
FROM extension_store;
|
||||
|
||||
upsert:
|
||||
INSERT INTO extension_store(index_url, name, badge_label, signing_key, contact_website, contact_discord, is_legacy, extension_list_url)
|
||||
VALUES (:indexUrl, :name, :badgeLabel, :signingKey, :contactWebsite, :contactDiscord, :isLegacy, :extensionListUrl)
|
||||
ON CONFLICT(index_url)
|
||||
DO UPDATE
|
||||
SET
|
||||
name = :name,
|
||||
badge_label = :badgeLabel,
|
||||
signing_key =: signingKey,
|
||||
contact_website = :contactWebsite,
|
||||
contact_discord = :contactDiscord,
|
||||
is_legacy = :isLegacy,
|
||||
extension_list_url = :extensionListUrl
|
||||
WHERE index_url = :indexUrl;
|
||||
INSERT INTO extension_store(
|
||||
index_url, name, badge_label, signing_key, contact_website, contact_discord, is_legacy,
|
||||
extension_list_url
|
||||
)
|
||||
VALUES (
|
||||
:indexUrl, :name, :badgeLabel, :signingKey, :contactWebsite, :contactDiscord, :isLegacy,
|
||||
:extensionListUrl
|
||||
)
|
||||
ON CONFLICT(index_url) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
badge_label = excluded.badge_label,
|
||||
signing_key = excluded.signing_key,
|
||||
contact_website = excluded.contact_website,
|
||||
contact_discord = excluded.contact_discord,
|
||||
is_legacy = excluded.is_legacy,
|
||||
extension_list_url = excluded.extension_list_url;
|
||||
|
||||
delete:
|
||||
DELETE FROM extension_store
|
||||
|
||||
@@ -1,75 +1,57 @@
|
||||
import java.util.Date;
|
||||
import kotlin.time.Instant;
|
||||
|
||||
CREATE TABLE history(
|
||||
_id INTEGER NOT NULL PRIMARY KEY,
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
chapter_id INTEGER NOT NULL UNIQUE,
|
||||
last_read INTEGER AS Date,
|
||||
time_read INTEGER NOT NULL,
|
||||
FOREIGN KEY(chapter_id) REFERENCES chapters (_id)
|
||||
read_at INTEGER AS Instant,
|
||||
read_duration INTEGER NOT NULL,
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE,
|
||||
FOREIGN KEY(chapter_id) REFERENCES chapter (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX history_history_chapter_id_index ON history(chapter_id);
|
||||
CREATE INDEX idx_history_last_read ON history(last_read);
|
||||
CREATE INDEX history_manga_id_index ON history(manga_id);
|
||||
CREATE INDEX history_read_at_index ON history(read_at);
|
||||
|
||||
getHistoryByMangaId:
|
||||
SELECT
|
||||
H._id,
|
||||
H.chapter_id,
|
||||
H.last_read,
|
||||
H.time_read
|
||||
FROM history H
|
||||
JOIN chapters C
|
||||
ON H.chapter_id = C._id
|
||||
WHERE C.manga_id = :mangaId AND C._id = H.chapter_id;
|
||||
SELECT id, chapter_id, read_at, read_duration
|
||||
FROM history
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
getHistoryByChapterUrlAndMangaId:
|
||||
SELECT
|
||||
H._id,
|
||||
H.chapter_id,
|
||||
H.last_read,
|
||||
H.time_read
|
||||
FROM history H
|
||||
JOIN chapters C
|
||||
ON H.chapter_id = C._id
|
||||
WHERE C.url = :chapterUrl AND C.manga_id = :mangaId AND C._id = H.chapter_id
|
||||
LIMIT 1;
|
||||
getHistoryByMangaIds:
|
||||
SELECT manga_id, id, chapter_id, read_at, read_duration
|
||||
FROM history
|
||||
WHERE manga_id IN :mangaIds
|
||||
ORDER BY manga_id, id;
|
||||
|
||||
resetHistoryById:
|
||||
UPDATE history
|
||||
SET last_read = 0
|
||||
WHERE _id = :historyId;
|
||||
SET read_at = NULL
|
||||
WHERE id = :id;
|
||||
|
||||
resetHistoryByMangaId:
|
||||
UPDATE history
|
||||
SET last_read = 0
|
||||
WHERE _id IN (
|
||||
SELECT H._id
|
||||
FROM mangas M
|
||||
INNER JOIN chapters C
|
||||
ON M._id = C.manga_id
|
||||
INNER JOIN history H
|
||||
ON C._id = H.chapter_id
|
||||
WHERE M._id = :mangaId
|
||||
);
|
||||
SET read_at = NULL
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
removeAllHistory:
|
||||
DELETE FROM history;
|
||||
|
||||
removeResettedHistory:
|
||||
DELETE FROM history
|
||||
WHERE last_read = 0;
|
||||
WHERE read_at IS NULL;
|
||||
|
||||
upsert:
|
||||
INSERT INTO history(chapter_id, last_read, time_read)
|
||||
VALUES (:chapterId, :readAt, :time_read)
|
||||
ON CONFLICT(chapter_id)
|
||||
DO UPDATE
|
||||
SET
|
||||
last_read = :readAt,
|
||||
time_read = time_read + :time_read
|
||||
WHERE chapter_id = :chapterId;
|
||||
INSERT INTO history(manga_id, chapter_id, read_at, read_duration)
|
||||
SELECT manga_id, id, :readAt, :readDuration
|
||||
FROM chapter
|
||||
WHERE id = :chapterId
|
||||
ON CONFLICT(chapter_id) DO UPDATE SET
|
||||
read_at = excluded.read_at,
|
||||
read_duration = read_duration + excluded.read_duration;
|
||||
|
||||
getReadDuration:
|
||||
SELECT coalesce(sum(time_read), 0)
|
||||
SELECT coalesce(sum(read_duration), 0)
|
||||
FROM history;
|
||||
|
||||
@@ -0,0 +1,312 @@
|
||||
import eu.kanade.tachiyomi.source.model.UpdateStrategy;
|
||||
import kotlin.Boolean;
|
||||
import kotlin.String;
|
||||
import kotlin.collections.List;
|
||||
import kotlin.time.Instant;
|
||||
import kotlinx.serialization.json.JsonObject;
|
||||
|
||||
CREATE TABLE manga(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
source_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
remote_title TEXT NOT NULL,
|
||||
remote_author TEXT,
|
||||
remote_artist TEXT,
|
||||
remote_description TEXT,
|
||||
remote_genre TEXT AS List<String>,
|
||||
remote_status INTEGER NOT NULL,
|
||||
remote_cover TEXT,
|
||||
remote_update_strategy INTEGER AS UpdateStrategy NOT NULL,
|
||||
remote_memo BLOB AS JsonObject NOT NULL,
|
||||
user_favorite_at INTEGER AS Instant,
|
||||
user_notes TEXT NOT NULL,
|
||||
user_reader_flags INTEGER NOT NULL,
|
||||
user_chapter_flags INTEGER NOT NULL,
|
||||
state_chapter_last_update INTEGER AS Instant,
|
||||
state_chapter_next_update INTEGER AS Instant,
|
||||
state_chapter_fetch_interval INTEGER NOT NULL,
|
||||
state_cover_last_modified INTEGER AS Instant,
|
||||
state_initialized INTEGER AS Boolean NOT NULL,
|
||||
UNIQUE(source_id, remote_url)
|
||||
);
|
||||
|
||||
CREATE INDEX manga_user_favorite_at_index ON manga(user_favorite_at) WHERE user_favorite_at IS NOT NULL;
|
||||
|
||||
getMangaById:
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
FROM manga
|
||||
WHERE id = :id;
|
||||
|
||||
getMangaByUrlAndSource:
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
FROM manga
|
||||
WHERE source_id = :sourceId
|
||||
AND remote_url = :remoteUrl;
|
||||
|
||||
getFavorites:
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
FROM manga
|
||||
WHERE user_favorite_at IS NOT NULL;
|
||||
|
||||
getReadMangaNotInLibrary:
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
FROM manga
|
||||
WHERE user_favorite_at IS NULL
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM chapter
|
||||
WHERE chapter.manga_id = manga.id
|
||||
AND (user_read = 1 OR user_last_page_read != 0)
|
||||
);
|
||||
|
||||
getAllMangaSourceAndUrl:
|
||||
SELECT source_id, remote_url
|
||||
FROM manga;
|
||||
|
||||
getSourceIdWithFavoriteCount:
|
||||
SELECT source_id, count(*)
|
||||
FROM manga
|
||||
WHERE user_favorite_at IS NOT NULL
|
||||
GROUP BY source_id;
|
||||
|
||||
getFavoriteBySourceId:
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
FROM manga
|
||||
WHERE user_favorite_at IS NOT NULL
|
||||
AND source_id = :sourceId;
|
||||
|
||||
getDuplicateLibraryManga:
|
||||
WITH
|
||||
track_dupes AS (
|
||||
SELECT DISTINCT T2.manga_id
|
||||
FROM manga_track T1
|
||||
JOIN manga_track T2
|
||||
ON T1.tracker_id = T2.tracker_id
|
||||
AND T1.remote_id = T2.remote_id
|
||||
AND T1.remote_id != 0
|
||||
AND T1.manga_id != T2.manga_id
|
||||
WHERE T1.manga_id = :id
|
||||
)
|
||||
SELECT
|
||||
M.id, M.source_id, M.remote_url, M.remote_title, M.remote_author, M.remote_artist,
|
||||
M.remote_description, M.remote_genre, M.remote_status, M.remote_cover,
|
||||
M.remote_update_strategy, M.remote_memo, M.user_favorite_at, M.user_notes,
|
||||
M.user_reader_flags, M.user_chapter_flags, M.state_chapter_last_update,
|
||||
M.state_chapter_next_update, M.state_chapter_fetch_interval,
|
||||
M.state_cover_last_modified, M.state_initialized,
|
||||
(
|
||||
SELECT count(*)
|
||||
FROM chapter C
|
||||
LEFT JOIN excluded_scanlator ES
|
||||
ON C.manga_id = ES.manga_id
|
||||
AND C.remote_scanlator = ES.scanlator
|
||||
WHERE C.manga_id = M.id
|
||||
AND ES.scanlator IS NULL
|
||||
) AS chapter_count
|
||||
FROM manga M
|
||||
WHERE M.user_favorite_at IS NOT NULL
|
||||
AND M.id != :id
|
||||
-- A blank title would match every title
|
||||
AND (
|
||||
(trim(:remoteTitle) != '' AND lower(M.remote_title) LIKE '%' || lower(:remoteTitle) || '%')
|
||||
OR M.id IN (SELECT manga_id FROM track_dupes)
|
||||
);
|
||||
|
||||
getUpcomingManga:
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
FROM manga
|
||||
WHERE state_chapter_next_update >= :startOfDay
|
||||
AND user_favorite_at IS NOT NULL
|
||||
AND remote_status IN :statuses
|
||||
AND (
|
||||
-- includedEmpty being true expresses "don't care" state and bypasses the membership filter
|
||||
:includedEmpty
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM manga_category
|
||||
WHERE manga_category.manga_id = manga.id
|
||||
AND manga_category.category_id IN :includedCategories
|
||||
)
|
||||
OR (0 IN :includedCategories AND NOT EXISTS (
|
||||
SELECT 1 FROM manga_category WHERE manga_category.manga_id = manga.id)
|
||||
)
|
||||
)
|
||||
AND (
|
||||
-- excludedEmpty being true expresses "don't care" state and bypasses the membership filter
|
||||
:excludedEmpty
|
||||
OR (
|
||||
NOT EXISTS (
|
||||
SELECT 1 FROM manga_category
|
||||
WHERE manga_category.manga_id = manga.id
|
||||
AND manga_category.category_id IN :excludedCategories
|
||||
)
|
||||
AND (
|
||||
0 NOT IN :excludedCategories
|
||||
OR EXISTS (SELECT 1 FROM manga_category WHERE manga_category.manga_id = manga.id)
|
||||
)
|
||||
)
|
||||
)
|
||||
ORDER BY state_chapter_next_update ASC;
|
||||
|
||||
resetViewerFlags:
|
||||
UPDATE manga
|
||||
SET user_reader_flags = 0
|
||||
WHERE user_reader_flags != 0;
|
||||
|
||||
getSourceIdsWithNonLibraryManga:
|
||||
SELECT source_id, count(*) AS manga_count
|
||||
FROM manga
|
||||
WHERE user_favorite_at IS NULL
|
||||
GROUP BY source_id;
|
||||
|
||||
deleteNonLibraryManga:
|
||||
DELETE FROM manga
|
||||
WHERE user_favorite_at IS NULL
|
||||
AND source_id IN :sourceIds
|
||||
AND (
|
||||
:keepReadManga = 0
|
||||
OR NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM chapter
|
||||
WHERE chapter.manga_id = manga.id
|
||||
AND (user_read = 1 OR user_last_page_read != 0)
|
||||
)
|
||||
);
|
||||
|
||||
insertReturningId:
|
||||
INSERT INTO manga(
|
||||
source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
)
|
||||
VALUES (
|
||||
:sourceId, :remoteUrl, :remoteTitle, :remoteAuthor, :remoteArtist, :remoteDescription,
|
||||
:remoteGenre, :remoteStatus, :remoteCover, :remoteUpdateStrategy, :remoteMemo,
|
||||
:userFavoriteAt, :userNotes, :userReaderFlags, :userChapterFlags,
|
||||
:stateChapterLastUpdate, :stateChapterNextUpdate, :stateChapterFetchInterval,
|
||||
:stateCoverLastModified, :stateInitialized
|
||||
)
|
||||
RETURNING id;
|
||||
|
||||
update:
|
||||
UPDATE manga SET
|
||||
-- NULL is a real value here (not in the library), which coalesce cannot write
|
||||
user_favorite_at = CASE WHEN :userFavoriteAtSet THEN :userFavoriteAt ELSE user_favorite_at END,
|
||||
user_notes = coalesce(:userNotes, user_notes),
|
||||
user_reader_flags = coalesce(:userReaderFlags, user_reader_flags),
|
||||
user_chapter_flags = coalesce(:userChapterFlags, user_chapter_flags),
|
||||
state_chapter_last_update = coalesce(:stateChapterLastUpdate, state_chapter_last_update),
|
||||
state_chapter_next_update = coalesce(:stateChapterNextUpdate, state_chapter_next_update),
|
||||
state_chapter_fetch_interval = coalesce(:stateChapterFetchInterval, state_chapter_fetch_interval),
|
||||
state_cover_last_modified = coalesce(:stateCoverLastModified, state_cover_last_modified)
|
||||
WHERE id = :id;
|
||||
|
||||
updateFromBackup:
|
||||
UPDATE manga SET
|
||||
remote_title = coalesce(:remoteTitle, remote_title),
|
||||
remote_author = coalesce(:remoteAuthor, remote_author),
|
||||
remote_artist = coalesce(:remoteArtist, remote_artist),
|
||||
remote_description = coalesce(:remoteDescription, remote_description),
|
||||
remote_genre = coalesce(:remoteGenre, remote_genre),
|
||||
remote_status = coalesce(:remoteStatus, remote_status),
|
||||
remote_cover = coalesce(:remoteCover, remote_cover),
|
||||
remote_update_strategy = coalesce(:remoteUpdateStrategy, remote_update_strategy),
|
||||
remote_memo = coalesce(:remoteMemo, remote_memo),
|
||||
user_favorite_at = :userFavoriteAt,
|
||||
user_notes = coalesce(:userNotes, user_notes),
|
||||
user_reader_flags = coalesce(:userReaderFlags, user_reader_flags),
|
||||
user_chapter_flags = coalesce(:userChapterFlags, user_chapter_flags),
|
||||
state_chapter_last_update = coalesce(:stateChapterLastUpdate, state_chapter_last_update),
|
||||
state_cover_last_modified = coalesce(:stateCoverLastModified, state_cover_last_modified),
|
||||
state_initialized = coalesce(:stateInitialized, state_initialized)
|
||||
WHERE id = :id;
|
||||
|
||||
updateRemote:
|
||||
UPDATE manga SET
|
||||
remote_title = coalesce(:remoteTitle, remote_title),
|
||||
remote_author = coalesce(:remoteAuthor, remote_author),
|
||||
remote_artist = coalesce(:remoteArtist, remote_artist),
|
||||
remote_description = coalesce(:remoteDescription, remote_description),
|
||||
remote_genre = coalesce(:remoteGenre, remote_genre),
|
||||
remote_status = :remoteStatus,
|
||||
remote_cover = coalesce(:remoteCover, remote_cover),
|
||||
remote_update_strategy = :remoteUpdateStrategy,
|
||||
remote_memo = :remoteMemo,
|
||||
state_cover_last_modified = coalesce(:stateCoverLastModified, state_cover_last_modified),
|
||||
state_initialized = :stateInitialized
|
||||
WHERE id = :id;
|
||||
|
||||
insertNetworkManga {
|
||||
-- A new entry is inserted as the source reported it. An existing one takes the fresh details
|
||||
-- only while it is outside the library, so a library entry keeps what the user already has.
|
||||
INSERT INTO manga(
|
||||
source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
)
|
||||
VALUES (
|
||||
:sourceId, :remoteUrl, :remoteTitle, :remoteAuthor, :remoteArtist, :remoteDescription,
|
||||
:remoteGenre, :remoteStatus, :remoteCover, :remoteUpdateStrategy, :remoteMemo,
|
||||
:userFavoriteAt, '', :userReaderFlags, :userChapterFlags,
|
||||
:stateChapterLastUpdate, :stateChapterNextUpdate, :stateChapterFetchInterval,
|
||||
:stateCoverLastModified, :stateInitialized
|
||||
)
|
||||
ON CONFLICT(source_id, remote_url) DO UPDATE SET
|
||||
remote_title = CASE WHEN :updateTitle THEN excluded.remote_title ELSE remote_title END,
|
||||
remote_cover = CASE WHEN :updateCover THEN excluded.remote_cover ELSE remote_cover END,
|
||||
remote_author = CASE WHEN :updateDetails THEN excluded.remote_author ELSE remote_author END,
|
||||
remote_artist = CASE WHEN :updateDetails THEN excluded.remote_artist ELSE remote_artist END,
|
||||
remote_description = CASE WHEN :updateDetails THEN excluded.remote_description ELSE remote_description END,
|
||||
remote_genre = CASE WHEN :updateDetails THEN excluded.remote_genre ELSE remote_genre END,
|
||||
remote_status = CASE WHEN :updateDetails THEN excluded.remote_status ELSE remote_status END,
|
||||
remote_update_strategy = CASE
|
||||
WHEN :updateDetails THEN excluded.remote_update_strategy
|
||||
ELSE remote_update_strategy
|
||||
END,
|
||||
remote_memo = excluded.remote_memo,
|
||||
state_initialized = :updateDetails
|
||||
WHERE user_favorite_at IS NULL;
|
||||
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
FROM manga
|
||||
WHERE source_id = :sourceId
|
||||
AND remote_url = :remoteUrl;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE manga_category(
|
||||
manga_id INTEGER NOT NULL,
|
||||
category_id INTEGER NOT NULL,
|
||||
UNIQUE(manga_id, category_id),
|
||||
FOREIGN KEY(category_id) REFERENCES category (id)
|
||||
ON DELETE CASCADE,
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX manga_category_category_id_index ON manga_category(category_id);
|
||||
|
||||
insert:
|
||||
INSERT INTO manga_category(manga_id, category_id)
|
||||
VALUES (:mangaId, :categoryId)
|
||||
ON CONFLICT(manga_id, category_id) DO NOTHING;
|
||||
|
||||
deleteMangaCategoryByMangaId:
|
||||
DELETE FROM manga_category
|
||||
WHERE manga_id = :mangaId;
|
||||
@@ -1,63 +0,0 @@
|
||||
import kotlin.Boolean;
|
||||
|
||||
CREATE TABLE manga_sync(
|
||||
_id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
sync_id INTEGER NOT NULL,
|
||||
remote_id INTEGER NOT NULL,
|
||||
library_id INTEGER,
|
||||
title TEXT NOT NULL,
|
||||
last_chapter_read REAL NOT NULL,
|
||||
total_chapters INTEGER NOT NULL,
|
||||
status INTEGER NOT NULL,
|
||||
score REAL NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
start_date INTEGER NOT NULL,
|
||||
finish_date INTEGER NOT NULL,
|
||||
private INTEGER AS Boolean DEFAULT 0 NOT NULL,
|
||||
UNIQUE (manga_id, sync_id) ON CONFLICT REPLACE,
|
||||
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX idx_manga_sync_manga_id ON manga_sync(manga_id);
|
||||
|
||||
delete:
|
||||
DELETE FROM manga_sync
|
||||
WHERE manga_id = :mangaId AND sync_id = :syncId;
|
||||
|
||||
getTracks:
|
||||
SELECT *
|
||||
FROM manga_sync;
|
||||
|
||||
getTrackById:
|
||||
SELECT *
|
||||
FROM manga_sync
|
||||
WHERE _id = :id;
|
||||
|
||||
getTracksByMangaId:
|
||||
SELECT *
|
||||
FROM manga_sync
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
insert:
|
||||
INSERT INTO manga_sync(manga_id,sync_id,remote_id,library_id,title,last_chapter_read,total_chapters,status,score,remote_url,start_date,finish_date,private)
|
||||
VALUES (:mangaId,:syncId,:remoteId,:libraryId,:title,:lastChapterRead,:totalChapters,:status,:score,:remoteUrl,:startDate,:finishDate,:private);
|
||||
|
||||
update:
|
||||
UPDATE manga_sync
|
||||
SET
|
||||
manga_id = coalesce(:mangaId, manga_id),
|
||||
sync_id = coalesce(:syncId, sync_id),
|
||||
remote_id = coalesce(:mediaId, remote_id),
|
||||
library_id = coalesce(:libraryId, library_id),
|
||||
title = coalesce(:title, title),
|
||||
last_chapter_read = coalesce(:lastChapterRead, last_chapter_read),
|
||||
total_chapters = coalesce(:totalChapter, total_chapters),
|
||||
status = coalesce(:status, status),
|
||||
score = coalesce(:score, score),
|
||||
remote_url = coalesce(:trackingUrl, remote_url),
|
||||
start_date = coalesce(:startDate, start_date),
|
||||
finish_date = coalesce(:finishDate, finish_date),
|
||||
private = coalesce(:private, private)
|
||||
WHERE _id = :id;
|
||||
@@ -0,0 +1,97 @@
|
||||
import kotlin.Boolean;
|
||||
import kotlin.time.Instant;
|
||||
|
||||
CREATE TABLE manga_track(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
tracker_id INTEGER NOT NULL,
|
||||
remote_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
library_id INTEGER,
|
||||
title TEXT NOT NULL,
|
||||
last_chapter_read REAL NOT NULL,
|
||||
total_chapters INTEGER NOT NULL,
|
||||
status INTEGER NOT NULL,
|
||||
score REAL NOT NULL,
|
||||
start_date INTEGER AS Instant,
|
||||
finish_date INTEGER AS Instant,
|
||||
private INTEGER AS Boolean NOT NULL DEFAULT 0,
|
||||
UNIQUE (manga_id, tracker_id),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX manga_track_tracker_id_remote_id_index ON manga_track(tracker_id, remote_id);
|
||||
|
||||
delete:
|
||||
DELETE FROM manga_track
|
||||
WHERE manga_id = :mangaId
|
||||
AND tracker_id = :trackerId;
|
||||
|
||||
getTracks:
|
||||
SELECT
|
||||
id, manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read,
|
||||
total_chapters, status, score, start_date, finish_date, private
|
||||
FROM manga_track;
|
||||
|
||||
getTrackById:
|
||||
SELECT
|
||||
id, manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read,
|
||||
total_chapters, status, score, start_date, finish_date, private
|
||||
FROM manga_track
|
||||
WHERE id = :id;
|
||||
|
||||
getTracksByMangaId:
|
||||
SELECT
|
||||
id, manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read,
|
||||
total_chapters, status, score, start_date, finish_date, private
|
||||
FROM manga_track
|
||||
WHERE manga_id = :mangaId;
|
||||
|
||||
getTracksByMangaIds:
|
||||
SELECT
|
||||
id, manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read,
|
||||
total_chapters, status, score, start_date, finish_date, private
|
||||
FROM manga_track
|
||||
WHERE manga_id IN :mangaIds
|
||||
ORDER BY manga_id, tracker_id;
|
||||
|
||||
upsert:
|
||||
INSERT INTO manga_track(
|
||||
manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read,
|
||||
total_chapters, status, score, start_date, finish_date, private
|
||||
)
|
||||
VALUES (
|
||||
:mangaId, :trackerId, :remoteId, :remoteUrl, :libraryId, :title, :lastChapterRead,
|
||||
:totalChapters, :status, :score, :startDate, :finishDate, :private
|
||||
)
|
||||
ON CONFLICT(manga_id, tracker_id) DO UPDATE SET
|
||||
remote_id = excluded.remote_id,
|
||||
remote_url = excluded.remote_url,
|
||||
library_id = excluded.library_id,
|
||||
title = excluded.title,
|
||||
last_chapter_read = excluded.last_chapter_read,
|
||||
total_chapters = excluded.total_chapters,
|
||||
status = excluded.status,
|
||||
score = excluded.score,
|
||||
start_date = excluded.start_date,
|
||||
finish_date = excluded.finish_date,
|
||||
private = excluded.private;
|
||||
|
||||
update:
|
||||
UPDATE manga_track
|
||||
SET
|
||||
manga_id = coalesce(:mangaId, manga_id),
|
||||
tracker_id = coalesce(:trackerId, tracker_id),
|
||||
remote_id = coalesce(:remoteId, remote_id),
|
||||
remote_url = coalesce(:remoteUrl, remote_url),
|
||||
library_id = coalesce(:libraryId, library_id),
|
||||
title = coalesce(:title, title),
|
||||
last_chapter_read = coalesce(:lastChapterRead, last_chapter_read),
|
||||
total_chapters = coalesce(:totalChapters, total_chapters),
|
||||
status = coalesce(:status, status),
|
||||
score = coalesce(:score, score),
|
||||
start_date = coalesce(:startDate, start_date),
|
||||
finish_date = coalesce(:finishDate, finish_date),
|
||||
private = coalesce(:private, private)
|
||||
WHERE id = :id;
|
||||
@@ -1,293 +0,0 @@
|
||||
import eu.kanade.tachiyomi.source.model.UpdateStrategy;
|
||||
import kotlin.Boolean;
|
||||
import kotlin.String;
|
||||
import kotlin.collections.List;
|
||||
import kotlinx.serialization.json.JsonObject;
|
||||
|
||||
CREATE TABLE mangas(
|
||||
_id INTEGER NOT NULL PRIMARY KEY,
|
||||
source INTEGER NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
artist TEXT,
|
||||
author TEXT,
|
||||
description TEXT,
|
||||
genre TEXT AS List<String>,
|
||||
title TEXT NOT NULL,
|
||||
status INTEGER NOT NULL,
|
||||
thumbnail_url TEXT,
|
||||
favorite INTEGER AS Boolean NOT NULL,
|
||||
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 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 NOT NULL DEFAULT 0,
|
||||
favorite_modified_at INTEGER,
|
||||
version INTEGER NOT NULL DEFAULT 0,
|
||||
is_syncing INTEGER NOT NULL DEFAULT 0,
|
||||
notes TEXT NOT NULL DEFAULT "",
|
||||
memo BLOB AS JsonObject NOT NULL DEFAULT '{}'
|
||||
);
|
||||
|
||||
CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1;
|
||||
CREATE INDEX mangas_url_index ON mangas(url);
|
||||
CREATE INDEX idx_mangas_source ON mangas(source);
|
||||
|
||||
CREATE TRIGGER update_last_favorited_at_mangas
|
||||
AFTER UPDATE OF favorite ON mangas
|
||||
BEGIN
|
||||
UPDATE mangas
|
||||
SET favorite_modified_at = strftime('%s', 'now')
|
||||
WHERE _id = new._id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER update_last_modified_at_mangas
|
||||
AFTER UPDATE ON mangas
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE mangas
|
||||
SET last_modified_at = strftime('%s', 'now')
|
||||
WHERE _id = new._id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER update_manga_version AFTER UPDATE ON mangas
|
||||
BEGIN
|
||||
UPDATE mangas SET version = version + 1
|
||||
WHERE _id = new._id AND new.is_syncing = 0 AND (
|
||||
new.url != old.url OR
|
||||
new.description != old.description OR
|
||||
new.favorite != old.favorite
|
||||
);
|
||||
END;
|
||||
|
||||
getMangaById:
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE _id = :id;
|
||||
|
||||
-- TODO: this should ideally never really have more than 1 result
|
||||
getMangaByUrlAndSource:
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE url = :url
|
||||
AND source = :source
|
||||
LIMIT 1;
|
||||
|
||||
getFavorites:
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE favorite = 1;
|
||||
|
||||
getReadMangaNotInLibrary:
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE favorite = 0 AND _id IN (
|
||||
SELECT DISTINCT chapters.manga_id
|
||||
FROM chapters
|
||||
WHERE read = 1 OR last_page_read != 0
|
||||
);
|
||||
|
||||
getAllManga:
|
||||
SELECT *
|
||||
FROM mangas;
|
||||
|
||||
getAllMangaSourceAndUrl:
|
||||
SELECT source, url
|
||||
FROM mangas;
|
||||
|
||||
getMangasWithFavoriteTimestamp:
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE favorite_modified_at IS NOT NULL;
|
||||
|
||||
getSourceIdWithFavoriteCount:
|
||||
SELECT
|
||||
source,
|
||||
count(*)
|
||||
FROM mangas
|
||||
WHERE favorite = 1
|
||||
GROUP BY source;
|
||||
|
||||
getFavoriteBySourceId:
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE favorite = 1
|
||||
AND source = :sourceId;
|
||||
|
||||
getDuplicateLibraryManga:
|
||||
WITH
|
||||
track_dupes AS (
|
||||
SELECT DISTINCT S2.manga_id
|
||||
FROM manga_sync S1
|
||||
INNER JOIN manga_sync S2
|
||||
ON S1.sync_id = S2.sync_id
|
||||
AND S1.remote_id = S2.remote_id
|
||||
AND S1.manga_id != S2.manga_id
|
||||
WHERE S1.manga_id = :id
|
||||
),
|
||||
duplicates AS (
|
||||
SELECT M.*
|
||||
FROM mangas M
|
||||
LEFT JOIN track_dupes D
|
||||
ON D.manga_id = _id
|
||||
WHERE favorite = 1
|
||||
AND _id != :id
|
||||
AND (lower(title) LIKE '%' || lower(:title) || '%' OR D.manga_id IS NOT NULL)
|
||||
),
|
||||
chapter_counts AS (
|
||||
SELECT
|
||||
M._id AS manga_id,
|
||||
count(*) AS chapter_count
|
||||
FROM duplicates M
|
||||
JOIN chapters C
|
||||
ON M._id = C.manga_id
|
||||
LEFT JOIN excluded_scanlators ES
|
||||
ON C.manga_id = ES.manga_id
|
||||
AND C.scanlator = ES.scanlator
|
||||
WHERE ES.scanlator IS NULL
|
||||
GROUP BY M._id
|
||||
)
|
||||
SELECT
|
||||
M.*,
|
||||
coalesce(CC.chapter_count, 0) AS chapter_count
|
||||
FROM duplicates M
|
||||
LEFT JOIN chapter_counts CC
|
||||
ON M._id = CC.manga_id;
|
||||
|
||||
getUpcomingManga:
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE next_update >= :startOfDay
|
||||
AND favorite = 1
|
||||
AND status IN :statuses
|
||||
AND (
|
||||
-- includedEmpty being true expresses "don't care" state and bypasses the membership filter
|
||||
:includedEmpty
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM mangas_categories
|
||||
WHERE mangas_categories.manga_id = mangas._id
|
||||
AND COALESCE(mangas_categories.category_id, 0) IN :includedCategories
|
||||
)
|
||||
OR (0 IN :includedCategories AND NOT EXISTS (
|
||||
SELECT 1 FROM mangas_categories WHERE mangas_categories.manga_id = mangas._id)
|
||||
)
|
||||
)
|
||||
AND (
|
||||
-- excludedEmpty being true expresses "don't care" state and bypasses the membership filter
|
||||
:excludedEmpty
|
||||
OR (
|
||||
NOT EXISTS (
|
||||
SELECT 1 FROM mangas_categories
|
||||
WHERE mangas_categories.manga_id = mangas._id
|
||||
AND COALESCE(mangas_categories.category_id, 0) IN :excludedCategories
|
||||
)
|
||||
AND (
|
||||
0 NOT IN :excludedCategories
|
||||
OR EXISTS (SELECT 1 FROM mangas_categories WHERE mangas_categories.manga_id = mangas._id)
|
||||
)
|
||||
)
|
||||
)
|
||||
ORDER BY next_update ASC;
|
||||
|
||||
resetViewerFlags:
|
||||
UPDATE mangas
|
||||
SET viewer = 0;
|
||||
|
||||
resetIsSyncing:
|
||||
UPDATE mangas
|
||||
SET is_syncing = 0
|
||||
WHERE is_syncing = 1;
|
||||
|
||||
getSourceIdsWithNonLibraryManga:
|
||||
SELECT source, COUNT(*) AS manga_count
|
||||
FROM mangas
|
||||
WHERE favorite = 0
|
||||
GROUP BY source;
|
||||
|
||||
deleteNonLibraryManga:
|
||||
DELETE FROM mangas
|
||||
WHERE favorite = 0
|
||||
AND source IN :sourceIds
|
||||
AND (
|
||||
:keepReadManga = 0
|
||||
OR _id NOT IN (
|
||||
SELECT DISTINCT manga_id
|
||||
FROM chapters
|
||||
WHERE read = 1
|
||||
OR last_page_read != 0
|
||||
)
|
||||
);
|
||||
|
||||
insertReturningId:
|
||||
INSERT INTO mangas(source, url, artist, author, description, genre, title, status, thumbnail_url, favorite, last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified, date_added, update_strategy, calculate_interval, last_modified_at, version, notes, memo)
|
||||
VALUES (:source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favorite, :lastUpdate, :nextUpdate, :initialized, :viewerFlags, :chapterFlags, :coverLastModified, :dateAdded, :updateStrategy, :calculateInterval, 0, :version, :notes, :memo)
|
||||
RETURNING _id;
|
||||
|
||||
update:
|
||||
UPDATE mangas SET
|
||||
source = coalesce(:source, source),
|
||||
url = coalesce(:url, url),
|
||||
artist = coalesce(:artist, artist),
|
||||
author = coalesce(:author, author),
|
||||
description = coalesce(:description, description),
|
||||
genre = coalesce(:genre, genre),
|
||||
title = coalesce(:title, title),
|
||||
status = coalesce(:status, status),
|
||||
thumbnail_url = coalesce(:thumbnailUrl, thumbnail_url),
|
||||
favorite = coalesce(:favorite, favorite),
|
||||
last_update = coalesce(:lastUpdate, last_update),
|
||||
next_update = coalesce(:nextUpdate, next_update),
|
||||
initialized = coalesce(:initialized, initialized),
|
||||
viewer = coalesce(:viewer, viewer),
|
||||
chapter_flags = coalesce(:chapterFlags, chapter_flags),
|
||||
cover_last_modified = coalesce(:coverLastModified, cover_last_modified),
|
||||
date_added = coalesce(:dateAdded, date_added),
|
||||
update_strategy = coalesce(:updateStrategy, update_strategy),
|
||||
calculate_interval = coalesce(:calculateInterval, calculate_interval),
|
||||
version = coalesce(:version, version),
|
||||
is_syncing = coalesce(:isSyncing, is_syncing),
|
||||
notes = coalesce(:notes, notes),
|
||||
memo = coalesce(:memo, memo)
|
||||
WHERE _id = :mangaId;
|
||||
|
||||
insertNetworkManga {
|
||||
-- Insert the manga if it doesn't exist already
|
||||
INSERT INTO mangas(
|
||||
source, url, artist, author, description, genre, title, status, thumbnail_url, favorite,
|
||||
last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified, date_added,
|
||||
update_strategy, calculate_interval, last_modified_at, version, memo
|
||||
)
|
||||
SELECT
|
||||
:source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favorite,
|
||||
:lastUpdate, :nextUpdate, :initialized, :viewerFlags, :chapterFlags, :coverLastModified, :dateAdded,
|
||||
:updateStrategy, :calculateInterval, 0, :version, :memo
|
||||
WHERE NOT EXISTS(SELECT 0 FROM mangas WHERE source = :source AND url = :url);
|
||||
|
||||
-- Update the relevant details if applicable and not favorite
|
||||
UPDATE mangas
|
||||
SET
|
||||
title = CASE WHEN :updateTitle THEN :title ELSE title END,
|
||||
thumbnail_url = CASE WHEN :updateCover THEN :thumbnailUrl ELSE thumbnail_url END,
|
||||
author = CASE WHEN :updateDetails THEN :author ELSE author END,
|
||||
artist = CASE WHEN :updateDetails THEN :artist ELSE artist END,
|
||||
description = CASE WHEN :updateDetails THEN :description ELSE description END,
|
||||
genre = CASE WHEN :updateDetails THEN :genre ELSE genre END,
|
||||
status = CASE WHEN :updateDetails THEN :status ELSE status END,
|
||||
update_strategy = CASE WHEN :updateDetails THEN :updateStrategy ELSE update_strategy END,
|
||||
initialized = :updateDetails,
|
||||
memo = :memo
|
||||
WHERE source = :source
|
||||
AND url = :url
|
||||
AND favorite = 0;
|
||||
|
||||
-- Finally return the manga
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE source = :source
|
||||
AND url = :url
|
||||
LIMIT 1;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
CREATE TABLE mangas_categories(
|
||||
_id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
category_id INTEGER NOT NULL,
|
||||
FOREIGN KEY(category_id) REFERENCES categories (_id)
|
||||
ON DELETE CASCADE,
|
||||
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX idx_mangas_categories_manga_id ON mangas_categories(manga_id);
|
||||
CREATE INDEX idx_mangas_categories_category_id ON mangas_categories(category_id);
|
||||
|
||||
CREATE TRIGGER insert_manga_category_update_version AFTER INSERT ON mangas_categories
|
||||
BEGIN
|
||||
UPDATE mangas
|
||||
SET version = version + 1
|
||||
WHERE _id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE _id = new.manga_id) = 0;
|
||||
END;
|
||||
|
||||
insert:
|
||||
INSERT INTO mangas_categories(manga_id, category_id)
|
||||
VALUES (:mangaId, :categoryId);
|
||||
|
||||
deleteMangaCategoryByMangaId:
|
||||
DELETE FROM mangas_categories
|
||||
WHERE manga_id = :mangaId;
|
||||
@@ -0,0 +1,21 @@
|
||||
CREATE TABLE source(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
language TEXT NOT NULL
|
||||
);
|
||||
|
||||
findAll:
|
||||
SELECT id, name, language
|
||||
FROM source;
|
||||
|
||||
findOne:
|
||||
SELECT id, name, language
|
||||
FROM source
|
||||
WHERE id = :id;
|
||||
|
||||
upsert:
|
||||
INSERT INTO source(id, name, language)
|
||||
VALUES (:id, :name, :language)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
language = excluded.language;
|
||||
@@ -1,24 +0,0 @@
|
||||
CREATE TABLE sources(
|
||||
_id INTEGER NOT NULL PRIMARY KEY,
|
||||
lang TEXT NOT NULL,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
||||
findAll:
|
||||
SELECT *
|
||||
FROM sources;
|
||||
|
||||
findOne:
|
||||
SELECT *
|
||||
FROM sources
|
||||
WHERE _id = :id;
|
||||
|
||||
upsert:
|
||||
INSERT INTO sources(_id, lang, name)
|
||||
VALUES (:id, :lang, :name)
|
||||
ON CONFLICT(_id)
|
||||
DO UPDATE
|
||||
SET
|
||||
lang = :lang,
|
||||
name = :name
|
||||
WHERE _id = :id;
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Remove the sync scaffolding: version, is_syncing and last_modified_at.
|
||||
|
||||
DROP TRIGGER update_manga_version;
|
||||
DROP TRIGGER update_last_modified_at_mangas;
|
||||
DROP TRIGGER update_chapter_and_manga_version;
|
||||
DROP TRIGGER update_last_modified_at_chapters;
|
||||
DROP TRIGGER insert_manga_category_update_version;
|
||||
|
||||
ALTER TABLE mangas DROP COLUMN version;
|
||||
ALTER TABLE mangas DROP COLUMN is_syncing;
|
||||
ALTER TABLE mangas DROP COLUMN last_modified_at;
|
||||
|
||||
ALTER TABLE chapters DROP COLUMN version;
|
||||
ALTER TABLE chapters DROP COLUMN is_syncing;
|
||||
ALTER TABLE chapters DROP COLUMN last_modified_at;
|
||||
@@ -0,0 +1,114 @@
|
||||
-- Merge favorite, date_added and favorite_modified_at into favorite_at
|
||||
|
||||
DROP VIEW historyView;
|
||||
DROP VIEW libraryView;
|
||||
DROP VIEW updatesView;
|
||||
|
||||
DROP TRIGGER update_last_favorited_at_mangas;
|
||||
DROP INDEX library_favorite_index;
|
||||
|
||||
ALTER TABLE mangas ADD COLUMN favorite_at INTEGER;
|
||||
|
||||
-- favorite_modified_at is no fallback for a missing date_added: every manga update rewrote it
|
||||
UPDATE mangas
|
||||
SET favorite_at = coalesce(date_added, 0)
|
||||
WHERE favorite = 1;
|
||||
|
||||
ALTER TABLE mangas DROP COLUMN favorite;
|
||||
ALTER TABLE mangas DROP COLUMN date_added;
|
||||
ALTER TABLE mangas DROP COLUMN favorite_modified_at;
|
||||
|
||||
CREATE INDEX library_favorite_at_index ON mangas(favorite_at) WHERE favorite_at IS NOT NULL;
|
||||
|
||||
CREATE VIEW libraryView AS
|
||||
SELECT
|
||||
M.*,
|
||||
coalesce(C.total, 0) AS totalCount,
|
||||
coalesce(C.readCount, 0) AS readCount,
|
||||
coalesce(C.latestUpload, 0) AS latestUpload,
|
||||
coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
|
||||
coalesce(C.lastRead, 0) AS lastRead,
|
||||
coalesce(C.bookmarkCount, 0) AS bookmarkCount,
|
||||
coalesce(MC.categories, '0') AS categories
|
||||
FROM mangas M
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
chapters.manga_id,
|
||||
count(*) AS total,
|
||||
sum(read) AS readCount,
|
||||
coalesce(max(chapters.date_upload), 0) AS latestUpload,
|
||||
coalesce(max(history.last_read), 0) AS lastRead,
|
||||
coalesce(max(chapters.date_fetch), 0) AS fetchedAt,
|
||||
sum(chapters.bookmark) AS bookmarkCount
|
||||
FROM chapters
|
||||
LEFT JOIN excluded_scanlators
|
||||
ON chapters.manga_id = excluded_scanlators.manga_id
|
||||
AND chapters.scanlator = excluded_scanlators.scanlator
|
||||
LEFT JOIN history
|
||||
ON chapters._id = history.chapter_id
|
||||
WHERE excluded_scanlators.scanlator IS NULL
|
||||
GROUP BY chapters.manga_id
|
||||
) AS C
|
||||
ON M._id = C.manga_id
|
||||
LEFT JOIN (
|
||||
SELECT manga_id, group_concat(category_id) AS categories
|
||||
FROM mangas_categories
|
||||
GROUP BY manga_id
|
||||
) AS MC
|
||||
ON MC.manga_id = M._id
|
||||
WHERE M.favorite_at IS NOT NULL;
|
||||
|
||||
CREATE VIEW updatesView AS
|
||||
SELECT
|
||||
mangas._id AS mangaId,
|
||||
mangas.title AS mangaTitle,
|
||||
chapters._id AS chapterId,
|
||||
chapters.name AS chapterName,
|
||||
chapters.scanlator,
|
||||
chapters.url AS chapterUrl,
|
||||
chapters.read,
|
||||
chapters.bookmark,
|
||||
chapters.last_page_read,
|
||||
mangas.source,
|
||||
mangas.favorite_at IS NOT NULL AS favorite,
|
||||
mangas.thumbnail_url AS thumbnailUrl,
|
||||
mangas.cover_last_modified AS coverLastModified,
|
||||
chapters.date_upload AS dateUpload,
|
||||
chapters.date_fetch AS datefetch,
|
||||
excluded_scanlators.scanlator AS excludedScanlator
|
||||
FROM mangas JOIN chapters
|
||||
ON mangas._id = chapters.manga_id
|
||||
LEFT JOIN excluded_scanlators
|
||||
ON mangas._id = excluded_scanlators.manga_id
|
||||
AND chapters.scanlator = excluded_scanlators.scanlator
|
||||
WHERE favorite_at IS NOT NULL
|
||||
AND date_fetch > favorite_at
|
||||
ORDER BY date_fetch DESC;
|
||||
|
||||
CREATE VIEW historyView AS
|
||||
SELECT
|
||||
history._id AS id,
|
||||
mangas._id AS mangaId,
|
||||
chapters._id AS chapterId,
|
||||
mangas.title,
|
||||
mangas.thumbnail_url AS thumbnailUrl,
|
||||
mangas.source,
|
||||
mangas.favorite_at IS NOT NULL AS favorite,
|
||||
mangas.cover_last_modified,
|
||||
chapters.chapter_number AS chapterNumber,
|
||||
history.last_read AS readAt,
|
||||
history.time_read AS readDuration,
|
||||
max_last_read.last_read AS maxReadAt,
|
||||
max_last_read.chapter_id AS maxReadAtChapterId
|
||||
FROM mangas
|
||||
JOIN chapters
|
||||
ON mangas._id = chapters.manga_id
|
||||
JOIN history
|
||||
ON chapters._id = history.chapter_id
|
||||
JOIN (
|
||||
SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read
|
||||
FROM chapters JOIN history
|
||||
ON chapters._id = history.chapter_id
|
||||
GROUP BY chapters.manga_id
|
||||
) AS max_last_read
|
||||
ON chapters.manga_id = max_last_read.manga_id;
|
||||
@@ -0,0 +1,617 @@
|
||||
import eu.kanade.tachiyomi.source.model.UpdateStrategy;
|
||||
import kotlin.Boolean;
|
||||
import kotlin.String;
|
||||
import kotlin.collections.List;
|
||||
import kotlinx.serialization.json.JsonObject;
|
||||
|
||||
-- Every view reads `mangas`, so they are dropped here and recreated against the new tables at the end
|
||||
DROP VIEW historyView;
|
||||
DROP VIEW libraryView;
|
||||
DROP VIEW updatesView;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- damaged indexes
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- Some databases hold rows their unique constraints should have rejected, which a damaged index explains. A
|
||||
-- write to a table also writes its indexes and fails on a damaged one, and a read through one can miss rows,
|
||||
-- so each table changed in place below is first copied from its rows alone, leaving its indexes behind.
|
||||
CREATE TABLE mangas_copy AS
|
||||
SELECT
|
||||
_id, source, url, artist, author, description, genre, title, status, thumbnail_url, last_update, next_update,
|
||||
initialized, viewer, chapter_flags, cover_last_modified, update_strategy, calculate_interval, notes, memo,
|
||||
favorite_at
|
||||
FROM mangas NOT INDEXED;
|
||||
DROP TABLE mangas;
|
||||
ALTER TABLE mangas_copy RENAME TO mangas;
|
||||
|
||||
CREATE TABLE chapters_copy AS
|
||||
SELECT
|
||||
_id, manga_id, url, name, scanlator, read, bookmark, last_page_read, chapter_number, source_order, date_fetch,
|
||||
date_upload, memo
|
||||
FROM chapters NOT INDEXED;
|
||||
DROP TABLE chapters;
|
||||
ALTER TABLE chapters_copy RENAME TO chapters;
|
||||
|
||||
CREATE TABLE history_copy AS
|
||||
SELECT _id, chapter_id, last_read, time_read
|
||||
FROM history NOT INDEXED;
|
||||
DROP TABLE history;
|
||||
ALTER TABLE history_copy RENAME TO history;
|
||||
|
||||
CREATE TABLE mangas_categories_copy AS
|
||||
SELECT _id, manga_id, category_id
|
||||
FROM mangas_categories NOT INDEXED;
|
||||
DROP TABLE mangas_categories;
|
||||
ALTER TABLE mangas_categories_copy RENAME TO mangas_categories;
|
||||
|
||||
CREATE TABLE manga_sync_copy AS
|
||||
SELECT
|
||||
_id, manga_id, sync_id, remote_id, library_id, title, last_chapter_read, total_chapters, status, score,
|
||||
remote_url, start_date, finish_date, private
|
||||
FROM manga_sync NOT INDEXED;
|
||||
DROP TABLE manga_sync;
|
||||
ALTER TABLE manga_sync_copy RENAME TO manga_sync;
|
||||
|
||||
CREATE TABLE excluded_scanlators_copy AS
|
||||
SELECT manga_id, scanlator
|
||||
FROM excluded_scanlators NOT INDEXED;
|
||||
DROP TABLE excluded_scanlators;
|
||||
ALTER TABLE excluded_scanlators_copy RENAME TO excluded_scanlators;
|
||||
|
||||
-- Merging duplicate chapters looks their history up by chapter
|
||||
CREATE INDEX history_chapter_id_index ON history(chapter_id);
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- orphans
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- Rows pointing at an entry, chapter or category that no longer exists were written while foreign keys weren't
|
||||
-- enforced. Nothing can reach them, and copied over they'd fail the foreign key check that ends the migration.
|
||||
DELETE FROM chapters WHERE manga_id NOT IN (SELECT _id FROM mangas);
|
||||
DELETE FROM history WHERE chapter_id NOT IN (SELECT _id FROM chapters);
|
||||
DELETE FROM mangas_categories
|
||||
WHERE manga_id NOT IN (SELECT _id FROM mangas) OR category_id NOT IN (SELECT _id FROM categories);
|
||||
DELETE FROM manga_sync WHERE manga_id NOT IN (SELECT _id FROM mangas);
|
||||
DELETE FROM excluded_scanlators WHERE manga_id NOT IN (SELECT _id FROM mangas);
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - manga
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- Entries sharing a source and url are merged into one, since the new table won't hold both. The copies are
|
||||
-- ranked with the ones in the library first, since that is the entry the user has, then by age. The first
|
||||
-- one stays, takes each detail it is missing from the first copy that has it, and takes over everything
|
||||
-- attached to the others.
|
||||
CREATE TEMP TABLE duplicate_mangas AS
|
||||
SELECT
|
||||
_id,
|
||||
first_value(_id) OVER (PARTITION BY source, url ORDER BY favorite_at IS NOT NULL DESC, _id ASC) AS keep_id,
|
||||
row_number() OVER (PARTITION BY source, url ORDER BY favorite_at IS NOT NULL DESC, _id ASC) AS precedence,
|
||||
favorite_at IS NOT NULL AS favorite
|
||||
FROM mangas
|
||||
WHERE (source, url) IN (
|
||||
SELECT source, url
|
||||
FROM mangas
|
||||
GROUP BY source, url
|
||||
HAVING count(*) > 1
|
||||
);
|
||||
|
||||
-- Moving the chapters looks each one's entry up here, which without an index scans the whole table per chapter
|
||||
CREATE INDEX duplicate_mangas_id_index ON duplicate_mangas(_id);
|
||||
|
||||
UPDATE mangas
|
||||
SET favorite_at = merged.favorite_at,
|
||||
initialized = merged.initialized,
|
||||
artist = merged.artist,
|
||||
author = merged.author,
|
||||
description = merged.description,
|
||||
genre = merged.genre,
|
||||
thumbnail_url = merged.thumbnail_url,
|
||||
status = merged.status,
|
||||
viewer = merged.viewer,
|
||||
chapter_flags = merged.chapter_flags,
|
||||
notes = merged.notes,
|
||||
memo = merged.memo,
|
||||
last_update = merged.last_update,
|
||||
cover_last_modified = merged.cover_last_modified
|
||||
FROM (
|
||||
SELECT DISTINCT
|
||||
D.keep_id,
|
||||
-- The earliest real date if any copy has one, else 0 if any copy is in the library, else NULL
|
||||
coalesce(
|
||||
min(nullif(M.favorite_at, 0)) OVER (PARTITION BY D.keep_id),
|
||||
min(M.favorite_at) OVER (PARTITION BY D.keep_id)
|
||||
) AS favorite_at,
|
||||
max(M.initialized) OVER (PARTITION BY D.keep_id) AS initialized,
|
||||
first_value(M.artist) OVER (PARTITION BY D.keep_id ORDER BY M.artist IS NULL, D.precedence) AS artist,
|
||||
first_value(M.author) OVER (PARTITION BY D.keep_id ORDER BY M.author IS NULL, D.precedence) AS author,
|
||||
first_value(M.description) OVER (PARTITION BY D.keep_id ORDER BY M.description IS NULL, D.precedence) AS description,
|
||||
first_value(M.genre) OVER (PARTITION BY D.keep_id ORDER BY M.genre IS NULL, D.precedence) AS genre,
|
||||
first_value(M.thumbnail_url) OVER (PARTITION BY D.keep_id ORDER BY M.thumbnail_url IS NULL, D.precedence) AS thumbnail_url,
|
||||
first_value(M.status) OVER (PARTITION BY D.keep_id ORDER BY M.status = 0, D.precedence) AS status,
|
||||
first_value(M.viewer) OVER (PARTITION BY D.keep_id ORDER BY M.viewer = 0, D.precedence) AS viewer,
|
||||
first_value(M.chapter_flags) OVER (PARTITION BY D.keep_id ORDER BY M.chapter_flags = 0, D.precedence) AS chapter_flags,
|
||||
first_value(M.notes) OVER (PARTITION BY D.keep_id ORDER BY M.notes = '', D.precedence) AS notes,
|
||||
first_value(M.memo) OVER (PARTITION BY D.keep_id ORDER BY M.memo = '{}', D.precedence) AS memo,
|
||||
max(M.last_update) OVER (PARTITION BY D.keep_id) AS last_update,
|
||||
max(M.cover_last_modified) OVER (PARTITION BY D.keep_id) AS cover_last_modified
|
||||
FROM duplicate_mangas D
|
||||
JOIN mangas M
|
||||
ON M._id = D._id
|
||||
) AS merged
|
||||
WHERE mangas._id = merged.keep_id;
|
||||
|
||||
-- Every child table is rebuilt further down and resolves the surviving entry there, so nothing
|
||||
-- else has to be rewritten here first.
|
||||
DELETE FROM mangas
|
||||
WHERE _id IN (SELECT _id FROM duplicate_mangas WHERE _id != keep_id);
|
||||
|
||||
CREATE TABLE manga(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
source_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
remote_title TEXT NOT NULL,
|
||||
remote_author TEXT,
|
||||
remote_artist TEXT,
|
||||
remote_description TEXT,
|
||||
remote_genre TEXT AS List<String>,
|
||||
remote_status INTEGER NOT NULL,
|
||||
remote_cover TEXT,
|
||||
remote_update_strategy INTEGER AS UpdateStrategy NOT NULL,
|
||||
remote_memo BLOB AS JsonObject NOT NULL,
|
||||
user_favorite_at INTEGER,
|
||||
user_notes TEXT NOT NULL,
|
||||
user_reader_flags INTEGER NOT NULL,
|
||||
user_chapter_flags INTEGER NOT NULL,
|
||||
state_chapter_last_update INTEGER,
|
||||
state_chapter_next_update INTEGER,
|
||||
state_chapter_fetch_interval INTEGER NOT NULL,
|
||||
state_cover_last_modified INTEGER NOT NULL,
|
||||
state_initialized INTEGER AS Boolean NOT NULL,
|
||||
UNIQUE(source_id, remote_url)
|
||||
);
|
||||
|
||||
INSERT INTO manga(
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description, remote_genre,
|
||||
remote_status, remote_cover, remote_update_strategy, remote_memo, user_favorite_at, user_notes,
|
||||
user_reader_flags, user_chapter_flags, state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized
|
||||
)
|
||||
SELECT
|
||||
_id, source, url, title, author, artist, description, genre,
|
||||
status, thumbnail_url, update_strategy, memo,
|
||||
favorite_at, notes,
|
||||
viewer, chapter_flags, last_update, next_update, calculate_interval,
|
||||
cover_last_modified, initialized
|
||||
FROM mangas;
|
||||
|
||||
DROP TABLE mangas;
|
||||
|
||||
CREATE INDEX manga_user_favorite_at_index ON manga(user_favorite_at) WHERE user_favorite_at IS NOT NULL;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - chapter
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- The entries that merged in the previous group brought their chapters with them
|
||||
UPDATE chapters
|
||||
SET manga_id = (SELECT keep_id FROM duplicate_mangas WHERE _id = chapters.manga_id)
|
||||
WHERE manga_id IN (SELECT _id FROM duplicate_mangas WHERE _id != keep_id);
|
||||
|
||||
-- Moving the chapters over brings rows for the same chapter together, and duplicates like those are
|
||||
-- what issue #647 is about in the first place. The one with reading history is kept, the oldest
|
||||
-- otherwise, and the read state and history of the others are merged into it.
|
||||
CREATE TEMP TABLE duplicate_chapters AS
|
||||
SELECT
|
||||
_id,
|
||||
first_value(_id) OVER (PARTITION BY manga_id, url ORDER BY has_history DESC, _id ASC) AS keep_id
|
||||
FROM (
|
||||
SELECT
|
||||
_id,
|
||||
manga_id,
|
||||
url,
|
||||
EXISTS(SELECT 1 FROM history WHERE history.chapter_id = chapters._id) AS has_history
|
||||
FROM chapters
|
||||
WHERE (manga_id, url) IN (
|
||||
SELECT manga_id, url
|
||||
FROM chapters
|
||||
GROUP BY manga_id, url
|
||||
HAVING count(*) > 1
|
||||
)
|
||||
);
|
||||
|
||||
UPDATE chapters
|
||||
SET read = merged.read,
|
||||
bookmark = merged.bookmark,
|
||||
last_page_read = merged.last_page_read,
|
||||
date_fetch = merged.date_fetch,
|
||||
date_upload = merged.date_upload
|
||||
FROM (
|
||||
SELECT
|
||||
D.keep_id,
|
||||
max(C.read) AS read,
|
||||
max(C.bookmark) AS bookmark,
|
||||
max(C.last_page_read) AS last_page_read,
|
||||
-- 0 means the fetch date was never recorded rather than "the beginning of time", so it only
|
||||
-- wins when no copy recorded one. Updates only lists chapters fetched after the entry joined
|
||||
-- the library, so letting a 0 through would hide the merged chapter from it.
|
||||
coalesce(min(nullif(C.date_fetch, 0)), 0) AS date_fetch,
|
||||
max(C.date_upload) AS date_upload
|
||||
FROM duplicate_chapters D
|
||||
JOIN chapters C
|
||||
ON C._id = D._id
|
||||
GROUP BY D.keep_id
|
||||
) AS merged
|
||||
WHERE chapters._id = merged.keep_id;
|
||||
|
||||
-- The kept chapter has a history entry whenever any of its duplicates has one
|
||||
UPDATE history
|
||||
SET last_read = merged.last_read,
|
||||
time_read = merged.time_read
|
||||
FROM (
|
||||
SELECT
|
||||
D.keep_id,
|
||||
max(H.last_read) AS last_read,
|
||||
-- Each duplicate accumulated its own reading time, so the durations add up; only the
|
||||
-- last-read timestamp is a point in time and takes the latest.
|
||||
sum(H.time_read) AS time_read
|
||||
FROM duplicate_chapters D
|
||||
JOIN history H
|
||||
ON H.chapter_id = D._id
|
||||
GROUP BY D.keep_id
|
||||
) AS merged
|
||||
WHERE history.chapter_id = merged.keep_id;
|
||||
|
||||
-- Foreign keys are off during migrations, so the history of a discarded chapter goes explicitly
|
||||
DELETE FROM history
|
||||
WHERE chapter_id IN (SELECT _id FROM duplicate_chapters WHERE _id != keep_id);
|
||||
|
||||
DELETE FROM chapters
|
||||
WHERE _id IN (SELECT _id FROM duplicate_chapters WHERE _id != keep_id);
|
||||
|
||||
DROP TABLE duplicate_chapters;
|
||||
|
||||
CREATE TABLE chapter(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
remote_name TEXT NOT NULL,
|
||||
remote_scanlator TEXT,
|
||||
remote_chapter_number REAL NOT NULL,
|
||||
remote_date_upload INTEGER NOT NULL,
|
||||
remote_order INTEGER NOT NULL,
|
||||
remote_memo BLOB NOT NULL,
|
||||
user_read INTEGER NOT NULL,
|
||||
user_bookmark INTEGER NOT NULL,
|
||||
user_last_page_read INTEGER NOT NULL,
|
||||
state_date_fetch INTEGER NOT NULL,
|
||||
UNIQUE(manga_id, remote_url),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO chapter(
|
||||
id, manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number, remote_date_upload,
|
||||
remote_order, remote_memo, user_read, user_bookmark, user_last_page_read, state_date_fetch
|
||||
)
|
||||
SELECT
|
||||
_id, manga_id, url, name, scanlator, chapter_number, date_upload,
|
||||
source_order, memo, read, bookmark, last_page_read, date_fetch
|
||||
FROM chapters;
|
||||
|
||||
DROP TABLE chapters;
|
||||
|
||||
CREATE INDEX chapter_state_date_fetch_index ON chapter(state_date_fetch);
|
||||
CREATE INDEX chapter_unread_state_date_fetch_index ON chapter(state_date_fetch) WHERE user_read = 0;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - category
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE category(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
`order` INTEGER NOT NULL UNIQUE,
|
||||
flags INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- `sort` was never unique, and reordering wrote colliding values often enough that it cannot simply be
|
||||
-- copied under the new constraint. The rows keep the order they were displayed in and are renumbered from
|
||||
-- 0, with ties broken by id. The system category is pinned to -1 rather than renumbered, because it is
|
||||
-- identified by id 0 everywhere else and sorts ahead of the categories the user made.
|
||||
INSERT INTO category(id, name, `order`, flags)
|
||||
SELECT
|
||||
_id,
|
||||
name,
|
||||
CASE
|
||||
WHEN _id <= 0 THEN -1
|
||||
ELSE row_number() OVER (PARTITION BY (_id <= 0) ORDER BY sort, _id) - 1
|
||||
END,
|
||||
flags
|
||||
FROM categories;
|
||||
|
||||
-- Dropping the table takes `system_category_delete_trigger` with it
|
||||
DROP TABLE categories;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE
|
||||
ON category
|
||||
BEGIN SELECT CASE
|
||||
WHEN old.id <= 0 THEN
|
||||
RAISE(ABORT, "System category can't be deleted")
|
||||
END;
|
||||
END;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - manga_category
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- The surrogate key is gone: a manga belongs to a category once, and that pair is the identity of the row
|
||||
CREATE TABLE manga_category(
|
||||
manga_id INTEGER NOT NULL,
|
||||
category_id INTEGER NOT NULL,
|
||||
UNIQUE(manga_id, category_id),
|
||||
FOREIGN KEY(category_id) REFERENCES category (id)
|
||||
ON DELETE CASCADE,
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Entries merged in the manga group brought their category rows with them. When the entry is in the library,
|
||||
-- only the copies in it say where it belongs; a copy outside it can still hold the categories it had before
|
||||
-- it was removed. The same pair can appear twice by now, and membership carries nothing else, so the copies
|
||||
-- simply collapse.
|
||||
INSERT INTO manga_category(manga_id, category_id)
|
||||
SELECT DISTINCT coalesce(D.keep_id, MC.manga_id), MC.category_id
|
||||
FROM mangas_categories MC
|
||||
LEFT JOIN duplicate_mangas D
|
||||
ON D._id = MC.manga_id
|
||||
LEFT JOIN manga K
|
||||
ON K.id = D.keep_id
|
||||
WHERE D._id IS NULL OR D.favorite OR K.user_favorite_at IS NULL;
|
||||
|
||||
DROP TABLE mangas_categories;
|
||||
|
||||
CREATE INDEX manga_category_category_id_index ON manga_category(category_id);
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - history
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- `last_read` is when the chapter was last read and `time_read` is how long it has been read for, so
|
||||
-- both are renamed to say so. The chapter it belongs to is already unique, so the constraint indexes it.
|
||||
-- The table keeps its name, so the original is renamed aside and the new one built under
|
||||
-- the real name; renaming a table into place makes SQLite rewrite its stored definition,
|
||||
-- leaving it spelled differently from the same table on a fresh install. The views were
|
||||
-- dropped at the top of this migration, so nothing refers to the name being moved.
|
||||
ALTER TABLE history RENAME TO history_temp;
|
||||
|
||||
CREATE TABLE history(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
chapter_id INTEGER NOT NULL UNIQUE,
|
||||
read_at INTEGER,
|
||||
read_duration INTEGER NOT NULL,
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE,
|
||||
FOREIGN KEY(chapter_id) REFERENCES chapter (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Reaching the entry a row belongs to meant going through its chapter; it names the entry
|
||||
-- itself now, taken from that same chapter. A row whose chapter is already gone cannot name
|
||||
-- one and is dropped, which is what the foreign key would do to it once it is enforced again.
|
||||
-- Some databases hold more than one row for a chapter despite the old unique constraint, and the new one
|
||||
-- rejects them, so they merge the way the history of duplicate chapters does above.
|
||||
INSERT INTO history(id, manga_id, chapter_id, read_at, read_duration)
|
||||
SELECT min(H._id), C.manga_id, H.chapter_id, max(H.last_read), sum(H.time_read)
|
||||
FROM history_temp H
|
||||
JOIN chapter C
|
||||
ON C.id = H.chapter_id
|
||||
GROUP BY H.chapter_id;
|
||||
|
||||
DROP TABLE history_temp;
|
||||
|
||||
CREATE INDEX history_manga_id_index ON history(manga_id);
|
||||
CREATE INDEX history_read_at_index ON history(read_at);
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - excluded_scanlator
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE excluded_scanlator(
|
||||
manga_id INTEGER NOT NULL,
|
||||
scanlator TEXT NOT NULL,
|
||||
UNIQUE(manga_id, scanlator),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Excluding a scanlator twice excludes it once, and merged entries brought their exclusions along, so
|
||||
-- the copies collapse. `excluded_scanlators_manga_id_index` is a prefix of the new constraint.
|
||||
INSERT INTO excluded_scanlator(manga_id, scanlator)
|
||||
SELECT DISTINCT coalesce(D.keep_id, ES.manga_id), ES.scanlator
|
||||
FROM excluded_scanlators ES
|
||||
LEFT JOIN duplicate_mangas D
|
||||
ON D._id = ES.manga_id;
|
||||
|
||||
DROP TABLE excluded_scanlators;
|
||||
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - manga_track
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- `manga_sync` is what tracking was called when it was still called syncing; `sync_id` is the tracker.
|
||||
-- An entry that merged can leave two rows for the same tracker on the survivor, and an entry that didn't can
|
||||
-- still hold two despite the old unique constraint. The new one rejects both, so the one to keep is chosen
|
||||
-- first: one on a copy in the library, then the one furthest read, then the one on the copy ranked first, then
|
||||
-- the oldest. A track is taken whole rather than merged field by field, because two rows on the same tracker
|
||||
-- can point at different remote entries.
|
||||
CREATE TEMP TABLE duplicate_tracks AS
|
||||
SELECT
|
||||
T._id,
|
||||
first_value(T._id) OVER (
|
||||
PARTITION BY coalesce(D.keep_id, T.manga_id), T.sync_id
|
||||
ORDER BY D.favorite DESC, T.last_chapter_read DESC, D.precedence ASC, T._id ASC
|
||||
) AS keep_track_id
|
||||
FROM manga_sync T
|
||||
LEFT JOIN duplicate_mangas D
|
||||
ON D._id = T.manga_id;
|
||||
|
||||
DELETE FROM manga_sync
|
||||
WHERE _id IN (SELECT _id FROM duplicate_tracks WHERE _id != keep_track_id);
|
||||
|
||||
DROP TABLE duplicate_tracks;
|
||||
|
||||
CREATE TABLE manga_track(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
tracker_id INTEGER NOT NULL,
|
||||
remote_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
library_id INTEGER,
|
||||
title TEXT NOT NULL,
|
||||
last_chapter_read REAL NOT NULL,
|
||||
total_chapters INTEGER NOT NULL,
|
||||
status INTEGER NOT NULL,
|
||||
score REAL NOT NULL,
|
||||
start_date INTEGER NOT NULL,
|
||||
finish_date INTEGER NOT NULL,
|
||||
private INTEGER NOT NULL DEFAULT 0,
|
||||
UNIQUE (manga_id, tracker_id),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO manga_track(
|
||||
id, manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read,
|
||||
total_chapters, status, score, start_date, finish_date, private
|
||||
)
|
||||
SELECT
|
||||
T._id, coalesce(D.keep_id, T.manga_id), T.sync_id, T.remote_id, T.remote_url, T.library_id,
|
||||
T.title, T.last_chapter_read, T.total_chapters, T.status, T.score, T.start_date,
|
||||
T.finish_date, T.private
|
||||
FROM manga_sync T
|
||||
LEFT JOIN duplicate_mangas D
|
||||
ON D._id = T.manga_id;
|
||||
|
||||
DROP TABLE duplicate_mangas;
|
||||
|
||||
-- `idx_manga_sync_manga_id` is a prefix of the unique constraint
|
||||
DROP TABLE manga_sync;
|
||||
|
||||
CREATE INDEX manga_track_tracker_id_remote_id_index ON manga_track(tracker_id, remote_id);
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - source
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- The last of the plural table names, and `lang` spelled out. Nothing references this table, so it is a
|
||||
-- straight copy; name comes before language to read the way the row does.
|
||||
CREATE TABLE source(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
language TEXT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO source(id, name, language)
|
||||
SELECT _id, name, lang
|
||||
FROM sources;
|
||||
|
||||
DROP TABLE sources;
|
||||
|
||||
-- The views come back reading the new table
|
||||
CREATE VIEW historyView AS
|
||||
SELECT
|
||||
history.id AS id,
|
||||
manga.id AS mangaId,
|
||||
chapter.id AS chapterId,
|
||||
manga.remote_title AS title,
|
||||
manga.remote_cover AS thumbnailUrl,
|
||||
manga.source_id AS sourceId,
|
||||
manga.user_favorite_at IS NOT NULL AS favorite,
|
||||
manga.state_cover_last_modified AS coverLastModified,
|
||||
chapter.remote_chapter_number AS chapterNumber,
|
||||
history.read_at AS readAt,
|
||||
history.read_duration AS readDuration,
|
||||
max_read.read_at AS maxReadAt,
|
||||
max_read.chapter_id AS maxReadAtChapterId
|
||||
FROM manga
|
||||
JOIN chapter
|
||||
ON manga.id = chapter.manga_id
|
||||
JOIN history
|
||||
ON chapter.id = history.chapter_id
|
||||
JOIN (
|
||||
SELECT manga_id, chapter_id, MAX(read_at) AS read_at
|
||||
FROM history
|
||||
GROUP BY manga_id
|
||||
) AS max_read
|
||||
ON chapter.manga_id = max_read.manga_id;
|
||||
|
||||
CREATE VIEW libraryView AS
|
||||
SELECT
|
||||
M.*,
|
||||
coalesce(C.total, 0) AS totalCount,
|
||||
coalesce(C.readCount, 0) AS readCount,
|
||||
coalesce(C.latestUpload, 0) AS latestUpload,
|
||||
coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
|
||||
coalesce(C.lastRead, 0) AS lastRead,
|
||||
coalesce(C.bookmarkCount, 0) AS bookmarkCount,
|
||||
coalesce(MC.categories, '0') AS categories
|
||||
FROM manga M
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
chapter.manga_id,
|
||||
count(*) AS total,
|
||||
sum(user_read) AS readCount,
|
||||
coalesce(max(chapter.remote_date_upload), 0) AS latestUpload,
|
||||
coalesce(max(history.read_at), 0) AS lastRead,
|
||||
coalesce(max(chapter.state_date_fetch), 0) AS fetchedAt,
|
||||
sum(chapter.user_bookmark) AS bookmarkCount
|
||||
FROM chapter
|
||||
LEFT JOIN excluded_scanlator
|
||||
ON chapter.manga_id = excluded_scanlator.manga_id
|
||||
AND chapter.remote_scanlator = excluded_scanlator.scanlator
|
||||
LEFT JOIN history
|
||||
ON chapter.id = history.chapter_id
|
||||
WHERE excluded_scanlator.scanlator IS NULL
|
||||
AND chapter.manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY chapter.manga_id
|
||||
) AS C
|
||||
ON M.id = C.manga_id
|
||||
LEFT JOIN (
|
||||
SELECT manga_id, group_concat(category_id) AS categories
|
||||
FROM manga_category
|
||||
WHERE manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY manga_id
|
||||
) AS MC
|
||||
ON MC.manga_id = M.id
|
||||
WHERE M.user_favorite_at IS NOT NULL;
|
||||
|
||||
CREATE VIEW updatesView AS
|
||||
SELECT
|
||||
manga.id AS mangaId,
|
||||
manga.remote_title AS mangaTitle,
|
||||
chapter.id AS chapterId,
|
||||
chapter.remote_name AS chapterName,
|
||||
chapter.remote_scanlator AS scanlator,
|
||||
chapter.remote_url AS chapterUrl,
|
||||
chapter.user_read AS read,
|
||||
chapter.user_bookmark AS bookmark,
|
||||
chapter.user_last_page_read AS lastPageRead,
|
||||
manga.source_id AS sourceId,
|
||||
manga.user_favorite_at IS NOT NULL AS favorite,
|
||||
manga.remote_cover AS thumbnailUrl,
|
||||
manga.state_cover_last_modified AS coverLastModified,
|
||||
chapter.remote_date_upload AS dateUpload,
|
||||
chapter.state_date_fetch AS dateFetch,
|
||||
excluded_scanlator.scanlator AS excludedScanlator
|
||||
FROM manga JOIN chapter
|
||||
ON manga.id = chapter.manga_id
|
||||
LEFT JOIN excluded_scanlator
|
||||
ON manga.id = excluded_scanlator.manga_id
|
||||
AND chapter.remote_scanlator = excluded_scanlator.scanlator
|
||||
WHERE user_favorite_at IS NOT NULL
|
||||
AND state_date_fetch > user_favorite_at
|
||||
ORDER BY state_date_fetch DESC;
|
||||
@@ -0,0 +1,59 @@
|
||||
-- Read the library's last read time from history without joining it to every chapter
|
||||
|
||||
DROP VIEW libraryView;
|
||||
|
||||
CREATE VIEW libraryView AS
|
||||
SELECT
|
||||
M.*,
|
||||
coalesce(C.total, 0) AS totalCount,
|
||||
coalesce(C.readCount, 0) AS readCount,
|
||||
coalesce(C.latestUpload, 0) AS latestUpload,
|
||||
coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
|
||||
-- Joining history to every chapter is the slowest part of the query, and only entries with excluded
|
||||
-- scanlators need the chapters to leave theirs out
|
||||
coalesce(
|
||||
CASE WHEN EXISTS (SELECT 0 FROM excluded_scanlator WHERE excluded_scanlator.manga_id = M.id)
|
||||
THEN (
|
||||
SELECT max(history.read_at)
|
||||
FROM history
|
||||
JOIN chapter
|
||||
ON chapter.id = history.chapter_id
|
||||
WHERE history.manga_id = M.id
|
||||
AND NOT EXISTS (
|
||||
SELECT 0 FROM excluded_scanlator
|
||||
WHERE excluded_scanlator.manga_id = M.id
|
||||
AND excluded_scanlator.scanlator = chapter.remote_scanlator
|
||||
)
|
||||
)
|
||||
ELSE (SELECT max(history.read_at) FROM history WHERE history.manga_id = M.id)
|
||||
END,
|
||||
0
|
||||
) AS lastRead,
|
||||
coalesce(C.bookmarkCount, 0) AS bookmarkCount,
|
||||
coalesce(MC.categories, '0') AS categories
|
||||
FROM manga M
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
chapter.manga_id,
|
||||
count(*) AS total,
|
||||
sum(user_read) AS readCount,
|
||||
coalesce(max(chapter.remote_date_upload), 0) AS latestUpload,
|
||||
coalesce(max(chapter.state_date_fetch), 0) AS fetchedAt,
|
||||
sum(chapter.user_bookmark) AS bookmarkCount
|
||||
FROM chapter
|
||||
LEFT JOIN excluded_scanlator
|
||||
ON chapter.manga_id = excluded_scanlator.manga_id
|
||||
AND chapter.remote_scanlator = excluded_scanlator.scanlator
|
||||
WHERE excluded_scanlator.scanlator IS NULL
|
||||
AND chapter.manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY chapter.manga_id
|
||||
) AS C
|
||||
ON M.id = C.manga_id
|
||||
LEFT JOIN (
|
||||
SELECT manga_id, group_concat(category_id) AS categories
|
||||
FROM manga_category
|
||||
WHERE manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY manga_id
|
||||
) AS MC
|
||||
ON MC.manga_id = M.id
|
||||
WHERE M.user_favorite_at IS NOT NULL;
|
||||
@@ -0,0 +1,271 @@
|
||||
import eu.kanade.tachiyomi.source.model.UpdateStrategy;
|
||||
import kotlin.Boolean;
|
||||
import kotlin.String;
|
||||
import kotlin.collections.List;
|
||||
import kotlin.time.Instant;
|
||||
import kotlinx.serialization.json.JsonObject;
|
||||
|
||||
-- A time that isn't known was stored as 0, which reads as the start of 1970 once it is an instant, so
|
||||
-- it is NULL instead. SQLite cannot drop NOT NULL from a column, so the tables holding one are rebuilt.
|
||||
|
||||
-- Every view reads `manga` or `chapter`, so they are dropped here and recreated against the new tables below
|
||||
DROP VIEW historyView;
|
||||
DROP VIEW libraryView;
|
||||
DROP VIEW updatesView;
|
||||
|
||||
-- Each table is renamed aside and rebuilt under its own name, as in migration 17. Renaming a table also
|
||||
-- repoints the foreign keys of the tables that reference it, which would leave them on the copy that is
|
||||
-- dropped; the legacy behavior leaves them naming the table that takes its place.
|
||||
PRAGMA legacy_alter_table = 1;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - manga
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
ALTER TABLE manga RENAME TO manga_temp;
|
||||
|
||||
CREATE TABLE manga(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
source_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
remote_title TEXT NOT NULL,
|
||||
remote_author TEXT,
|
||||
remote_artist TEXT,
|
||||
remote_description TEXT,
|
||||
remote_genre TEXT AS List<String>,
|
||||
remote_status INTEGER NOT NULL,
|
||||
remote_cover TEXT,
|
||||
remote_update_strategy INTEGER AS UpdateStrategy NOT NULL,
|
||||
remote_memo BLOB AS JsonObject NOT NULL,
|
||||
user_favorite_at INTEGER AS Instant,
|
||||
user_notes TEXT NOT NULL,
|
||||
user_reader_flags INTEGER NOT NULL,
|
||||
user_chapter_flags INTEGER NOT NULL,
|
||||
state_chapter_last_update INTEGER AS Instant,
|
||||
state_chapter_next_update INTEGER AS Instant,
|
||||
state_chapter_fetch_interval INTEGER NOT NULL,
|
||||
state_cover_last_modified INTEGER AS Instant,
|
||||
state_initialized INTEGER AS Boolean NOT NULL,
|
||||
UNIQUE(source_id, remote_url)
|
||||
);
|
||||
|
||||
-- `user_favorite_at` keeps its 0: NULL already means the entry isn't in the library, and 0 is an entry
|
||||
-- added before the time was recorded
|
||||
INSERT INTO manga(
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description, remote_genre,
|
||||
remote_status, remote_cover, remote_update_strategy, remote_memo, user_favorite_at, user_notes,
|
||||
user_reader_flags, user_chapter_flags, state_chapter_last_update, state_chapter_next_update,
|
||||
state_chapter_fetch_interval, state_cover_last_modified, state_initialized
|
||||
)
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description, remote_genre,
|
||||
remote_status, remote_cover, remote_update_strategy, remote_memo, user_favorite_at, user_notes,
|
||||
user_reader_flags, user_chapter_flags, nullif(state_chapter_last_update, 0), nullif(state_chapter_next_update, 0),
|
||||
state_chapter_fetch_interval, nullif(state_cover_last_modified, 0), state_initialized
|
||||
FROM manga_temp;
|
||||
|
||||
DROP TABLE manga_temp;
|
||||
|
||||
CREATE INDEX manga_user_favorite_at_index ON manga(user_favorite_at) WHERE user_favorite_at IS NOT NULL;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - chapter
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
ALTER TABLE chapter RENAME TO chapter_temp;
|
||||
|
||||
CREATE TABLE chapter(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
remote_name TEXT NOT NULL,
|
||||
remote_scanlator TEXT,
|
||||
remote_chapter_number REAL NOT NULL,
|
||||
remote_date_upload INTEGER AS Instant,
|
||||
remote_order INTEGER NOT NULL,
|
||||
remote_memo BLOB AS JsonObject NOT NULL,
|
||||
user_read INTEGER AS Boolean NOT NULL,
|
||||
user_bookmark INTEGER AS Boolean NOT NULL,
|
||||
user_last_page_read INTEGER NOT NULL,
|
||||
state_date_fetch INTEGER AS Instant NOT NULL,
|
||||
UNIQUE(manga_id, remote_url),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO chapter(
|
||||
id, manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number, remote_date_upload,
|
||||
remote_order, remote_memo, user_read, user_bookmark, user_last_page_read, state_date_fetch
|
||||
)
|
||||
SELECT
|
||||
id, manga_id, remote_url, remote_name, remote_scanlator, remote_chapter_number, nullif(remote_date_upload, 0),
|
||||
remote_order, remote_memo, user_read, user_bookmark, user_last_page_read, state_date_fetch
|
||||
FROM chapter_temp;
|
||||
|
||||
DROP TABLE chapter_temp;
|
||||
|
||||
CREATE INDEX chapter_state_date_fetch_index ON chapter(state_date_fetch);
|
||||
CREATE INDEX chapter_unread_state_date_fetch_index ON chapter(state_date_fetch) WHERE user_read = 0;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - manga_track
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
ALTER TABLE manga_track RENAME TO manga_track_temp;
|
||||
|
||||
CREATE TABLE manga_track(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
manga_id INTEGER NOT NULL,
|
||||
tracker_id INTEGER NOT NULL,
|
||||
remote_id INTEGER NOT NULL,
|
||||
remote_url TEXT NOT NULL,
|
||||
library_id INTEGER,
|
||||
title TEXT NOT NULL,
|
||||
last_chapter_read REAL NOT NULL,
|
||||
total_chapters INTEGER NOT NULL,
|
||||
status INTEGER NOT NULL,
|
||||
score REAL NOT NULL,
|
||||
start_date INTEGER AS Instant,
|
||||
finish_date INTEGER AS Instant,
|
||||
private INTEGER AS Boolean NOT NULL DEFAULT 0,
|
||||
UNIQUE (manga_id, tracker_id),
|
||||
FOREIGN KEY(manga_id) REFERENCES manga (id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO manga_track(
|
||||
id, manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read, total_chapters,
|
||||
status, score, start_date, finish_date, private
|
||||
)
|
||||
SELECT
|
||||
id, manga_id, tracker_id, remote_id, remote_url, library_id, title, last_chapter_read, total_chapters,
|
||||
status, score, nullif(start_date, 0), nullif(finish_date, 0), private
|
||||
FROM manga_track_temp;
|
||||
|
||||
DROP TABLE manga_track_temp;
|
||||
|
||||
CREATE INDEX manga_track_tracker_id_remote_id_index ON manga_track(tracker_id, remote_id);
|
||||
|
||||
PRAGMA legacy_alter_table = 0;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- table - history
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
-- 0 marked history the user removed, which NULL marks now
|
||||
UPDATE history
|
||||
SET read_at = NULL
|
||||
WHERE read_at = 0;
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
-- views
|
||||
-- ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
CREATE VIEW historyView AS
|
||||
SELECT
|
||||
history.id AS id,
|
||||
manga.id AS mangaId,
|
||||
chapter.id AS chapterId,
|
||||
manga.remote_title AS title,
|
||||
manga.remote_cover AS thumbnailUrl,
|
||||
manga.source_id AS sourceId,
|
||||
manga.user_favorite_at IS NOT NULL AS favorite,
|
||||
manga.state_cover_last_modified AS coverLastModified,
|
||||
chapter.remote_chapter_number AS chapterNumber,
|
||||
history.read_at AS readAt,
|
||||
history.read_duration AS readDuration,
|
||||
max_read.read_at AS maxReadAt,
|
||||
max_read.chapter_id AS maxReadAtChapterId
|
||||
FROM manga
|
||||
JOIN chapter
|
||||
ON manga.id = chapter.manga_id
|
||||
JOIN history
|
||||
ON chapter.id = history.chapter_id
|
||||
JOIN (
|
||||
SELECT manga_id, chapter_id, MAX(read_at) AS read_at
|
||||
FROM history
|
||||
GROUP BY manga_id
|
||||
) AS max_read
|
||||
ON chapter.manga_id = max_read.manga_id;
|
||||
|
||||
-- An entry without chapters or reading history has no latest upload, fetch or read time, which is NULL
|
||||
-- rather than 0 now
|
||||
CREATE VIEW libraryView AS
|
||||
SELECT
|
||||
M.*,
|
||||
coalesce(C.total, 0) AS totalCount,
|
||||
coalesce(C.readCount, 0) AS readCount,
|
||||
C.latestUpload AS latestUpload,
|
||||
C.fetchedAt AS chapterFetchedAt,
|
||||
-- Joining history to every chapter is the slowest part of the query, and only entries with excluded
|
||||
-- scanlators need the chapters to leave theirs out
|
||||
CASE WHEN EXISTS (SELECT 0 FROM excluded_scanlator WHERE excluded_scanlator.manga_id = M.id)
|
||||
THEN (
|
||||
SELECT max(history.read_at)
|
||||
FROM history
|
||||
JOIN chapter
|
||||
ON chapter.id = history.chapter_id
|
||||
WHERE history.manga_id = M.id
|
||||
AND NOT EXISTS (
|
||||
SELECT 0 FROM excluded_scanlator
|
||||
WHERE excluded_scanlator.manga_id = M.id
|
||||
AND excluded_scanlator.scanlator = chapter.remote_scanlator
|
||||
)
|
||||
)
|
||||
ELSE (SELECT max(history.read_at) FROM history WHERE history.manga_id = M.id)
|
||||
END AS lastRead,
|
||||
coalesce(C.bookmarkCount, 0) AS bookmarkCount,
|
||||
coalesce(MC.categories, '0') AS categories
|
||||
FROM manga M
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
chapter.manga_id,
|
||||
count(*) AS total,
|
||||
sum(user_read) AS readCount,
|
||||
max(chapter.remote_date_upload) AS latestUpload,
|
||||
max(chapter.state_date_fetch) AS fetchedAt,
|
||||
sum(chapter.user_bookmark) AS bookmarkCount
|
||||
FROM chapter
|
||||
LEFT JOIN excluded_scanlator
|
||||
ON chapter.manga_id = excluded_scanlator.manga_id
|
||||
AND chapter.remote_scanlator = excluded_scanlator.scanlator
|
||||
WHERE excluded_scanlator.scanlator IS NULL
|
||||
AND chapter.manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY chapter.manga_id
|
||||
) AS C
|
||||
ON M.id = C.manga_id
|
||||
LEFT JOIN (
|
||||
SELECT manga_id, group_concat(category_id) AS categories
|
||||
FROM manga_category
|
||||
WHERE manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY manga_id
|
||||
) AS MC
|
||||
ON MC.manga_id = M.id
|
||||
WHERE M.user_favorite_at IS NOT NULL;
|
||||
|
||||
CREATE VIEW updatesView AS
|
||||
SELECT
|
||||
manga.id AS mangaId,
|
||||
manga.remote_title AS mangaTitle,
|
||||
chapter.id AS chapterId,
|
||||
chapter.remote_name AS chapterName,
|
||||
chapter.remote_scanlator AS scanlator,
|
||||
chapter.remote_url AS chapterUrl,
|
||||
chapter.user_read AS read,
|
||||
chapter.user_bookmark AS bookmark,
|
||||
chapter.user_last_page_read AS lastPageRead,
|
||||
manga.source_id AS sourceId,
|
||||
manga.user_favorite_at IS NOT NULL AS favorite,
|
||||
manga.remote_cover AS thumbnailUrl,
|
||||
manga.state_cover_last_modified AS coverLastModified,
|
||||
chapter.remote_date_upload AS dateUpload,
|
||||
chapter.state_date_fetch AS dateFetch,
|
||||
excluded_scanlator.scanlator AS excludedScanlator
|
||||
FROM manga JOIN chapter
|
||||
ON manga.id = chapter.manga_id
|
||||
LEFT JOIN excluded_scanlator
|
||||
ON manga.id = excluded_scanlator.manga_id
|
||||
AND chapter.remote_scanlator = excluded_scanlator.scanlator
|
||||
WHERE user_favorite_at IS NOT NULL
|
||||
AND state_date_fetch > user_favorite_at
|
||||
ORDER BY state_date_fetch DESC;
|
||||
@@ -1,30 +1,29 @@
|
||||
CREATE VIEW historyView AS
|
||||
SELECT
|
||||
history._id AS id,
|
||||
mangas._id AS mangaId,
|
||||
chapters._id AS chapterId,
|
||||
mangas.title,
|
||||
mangas.thumbnail_url AS thumbnailUrl,
|
||||
mangas.source,
|
||||
mangas.favorite,
|
||||
mangas.cover_last_modified,
|
||||
chapters.chapter_number AS chapterNumber,
|
||||
history.last_read AS readAt,
|
||||
history.time_read AS readDuration,
|
||||
max_last_read.last_read AS maxReadAt,
|
||||
max_last_read.chapter_id AS maxReadAtChapterId
|
||||
FROM mangas
|
||||
JOIN chapters
|
||||
ON mangas._id = chapters.manga_id
|
||||
history.id AS id,
|
||||
manga.id AS mangaId,
|
||||
chapter.id AS chapterId,
|
||||
manga.remote_title AS title,
|
||||
manga.remote_cover AS thumbnailUrl,
|
||||
manga.source_id AS sourceId,
|
||||
manga.user_favorite_at IS NOT NULL AS favorite,
|
||||
manga.state_cover_last_modified AS coverLastModified,
|
||||
chapter.remote_chapter_number AS chapterNumber,
|
||||
history.read_at AS readAt,
|
||||
history.read_duration AS readDuration,
|
||||
max_read.read_at AS maxReadAt,
|
||||
max_read.chapter_id AS maxReadAtChapterId
|
||||
FROM manga
|
||||
JOIN chapter
|
||||
ON manga.id = chapter.manga_id
|
||||
JOIN history
|
||||
ON chapters._id = history.chapter_id
|
||||
ON chapter.id = history.chapter_id
|
||||
JOIN (
|
||||
SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read
|
||||
FROM chapters JOIN history
|
||||
ON chapters._id = history.chapter_id
|
||||
GROUP BY chapters.manga_id
|
||||
) AS max_last_read
|
||||
ON chapters.manga_id = max_last_read.manga_id;
|
||||
SELECT manga_id, chapter_id, MAX(read_at) AS read_at
|
||||
FROM history
|
||||
GROUP BY manga_id
|
||||
) AS max_read
|
||||
ON chapter.manga_id = max_read.manga_id;
|
||||
|
||||
history:
|
||||
SELECT
|
||||
@@ -33,32 +32,36 @@ mangaId,
|
||||
chapterId,
|
||||
title,
|
||||
thumbnailUrl,
|
||||
source,
|
||||
sourceId,
|
||||
favorite,
|
||||
cover_last_modified,
|
||||
coverLastModified,
|
||||
chapterNumber,
|
||||
readAt,
|
||||
readDuration
|
||||
FROM historyView
|
||||
WHERE historyView.readAt > 0
|
||||
WHERE historyView.readAt IS NOT NULL
|
||||
AND maxReadAtChapterId = historyView.chapterId
|
||||
AND lower(historyView.title) LIKE ('%' || :query || '%')
|
||||
ORDER BY readAt DESC;
|
||||
|
||||
getLatestHistory:
|
||||
SELECT
|
||||
id,
|
||||
mangaId,
|
||||
chapterId,
|
||||
title,
|
||||
thumbnailUrl,
|
||||
source,
|
||||
favorite,
|
||||
cover_last_modified,
|
||||
chapterNumber,
|
||||
readAt,
|
||||
readDuration
|
||||
FROM historyView
|
||||
WHERE historyView.readAt > 0
|
||||
ORDER BY readAt DESC
|
||||
history.id AS id,
|
||||
manga.id AS mangaId,
|
||||
chapter.id AS chapterId,
|
||||
manga.remote_title AS title,
|
||||
manga.remote_cover AS thumbnailUrl,
|
||||
manga.source_id AS sourceId,
|
||||
manga.user_favorite_at IS NOT NULL AS favorite,
|
||||
manga.state_cover_last_modified AS coverLastModified,
|
||||
chapter.remote_chapter_number AS chapterNumber,
|
||||
history.read_at AS readAt,
|
||||
history.read_duration AS readDuration
|
||||
FROM history
|
||||
JOIN manga
|
||||
ON manga.id = history.manga_id
|
||||
JOIN chapter
|
||||
ON chapter.id = history.chapter_id
|
||||
WHERE history.read_at IS NOT NULL
|
||||
ORDER BY history.read_at DESC
|
||||
LIMIT 1;
|
||||
|
||||
@@ -3,39 +3,60 @@ SELECT
|
||||
M.*,
|
||||
coalesce(C.total, 0) AS totalCount,
|
||||
coalesce(C.readCount, 0) AS readCount,
|
||||
coalesce(C.latestUpload, 0) AS latestUpload,
|
||||
coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
|
||||
coalesce(C.lastRead, 0) AS lastRead,
|
||||
C.latestUpload AS latestUpload,
|
||||
C.fetchedAt AS chapterFetchedAt,
|
||||
-- Joining history to every chapter is the slowest part of the query, and only entries with excluded
|
||||
-- scanlators need the chapters to leave theirs out
|
||||
CASE WHEN EXISTS (SELECT 0 FROM excluded_scanlator WHERE excluded_scanlator.manga_id = M.id)
|
||||
THEN (
|
||||
SELECT max(history.read_at)
|
||||
FROM history
|
||||
JOIN chapter
|
||||
ON chapter.id = history.chapter_id
|
||||
WHERE history.manga_id = M.id
|
||||
AND NOT EXISTS (
|
||||
SELECT 0 FROM excluded_scanlator
|
||||
WHERE excluded_scanlator.manga_id = M.id
|
||||
AND excluded_scanlator.scanlator = chapter.remote_scanlator
|
||||
)
|
||||
)
|
||||
ELSE (SELECT max(history.read_at) FROM history WHERE history.manga_id = M.id)
|
||||
END AS lastRead,
|
||||
coalesce(C.bookmarkCount, 0) AS bookmarkCount,
|
||||
coalesce(MC.categories, '0') AS categories
|
||||
FROM mangas M
|
||||
FROM manga M
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
chapters.manga_id,
|
||||
chapter.manga_id,
|
||||
count(*) AS total,
|
||||
sum(read) AS readCount,
|
||||
coalesce(max(chapters.date_upload), 0) AS latestUpload,
|
||||
coalesce(max(history.last_read), 0) AS lastRead,
|
||||
coalesce(max(chapters.date_fetch), 0) AS fetchedAt,
|
||||
sum(chapters.bookmark) AS bookmarkCount
|
||||
FROM chapters
|
||||
LEFT JOIN excluded_scanlators
|
||||
ON chapters.manga_id = excluded_scanlators.manga_id
|
||||
AND chapters.scanlator = excluded_scanlators.scanlator
|
||||
LEFT JOIN history
|
||||
ON chapters._id = history.chapter_id
|
||||
WHERE excluded_scanlators.scanlator IS NULL
|
||||
GROUP BY chapters.manga_id
|
||||
sum(user_read) AS readCount,
|
||||
max(chapter.remote_date_upload) AS latestUpload,
|
||||
max(chapter.state_date_fetch) AS fetchedAt,
|
||||
sum(chapter.user_bookmark) AS bookmarkCount
|
||||
FROM chapter
|
||||
LEFT JOIN excluded_scanlator
|
||||
ON chapter.manga_id = excluded_scanlator.manga_id
|
||||
AND chapter.remote_scanlator = excluded_scanlator.scanlator
|
||||
WHERE excluded_scanlator.scanlator IS NULL
|
||||
AND chapter.manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY chapter.manga_id
|
||||
) AS C
|
||||
ON M._id = C.manga_id
|
||||
ON M.id = C.manga_id
|
||||
LEFT JOIN (
|
||||
SELECT manga_id, group_concat(category_id) AS categories
|
||||
FROM mangas_categories
|
||||
FROM manga_category
|
||||
WHERE manga_id IN (SELECT id FROM manga WHERE user_favorite_at IS NOT NULL)
|
||||
GROUP BY manga_id
|
||||
) AS MC
|
||||
ON MC.manga_id = M._id
|
||||
WHERE M.favorite = 1;
|
||||
ON MC.manga_id = M.id
|
||||
WHERE M.user_favorite_at IS NOT NULL;
|
||||
|
||||
library:
|
||||
SELECT *
|
||||
SELECT
|
||||
id, source_id, remote_url, remote_title, remote_author, remote_artist, remote_description,
|
||||
remote_genre, remote_status, remote_cover, remote_update_strategy, remote_memo,
|
||||
user_favorite_at, user_notes, user_reader_flags, user_chapter_flags,
|
||||
state_chapter_last_update, state_chapter_next_update, state_chapter_fetch_interval,
|
||||
state_cover_last_modified, state_initialized, totalCount, readCount, latestUpload,
|
||||
chapterFetchedAt, lastRead, bookmarkCount, categories
|
||||
FROM libraryView;
|
||||
|
||||
@@ -1,61 +1,58 @@
|
||||
import kotlin.Int;
|
||||
|
||||
CREATE VIEW updatesView AS
|
||||
SELECT
|
||||
mangas._id AS mangaId,
|
||||
mangas.title AS mangaTitle,
|
||||
chapters._id AS chapterId,
|
||||
chapters.name AS chapterName,
|
||||
chapters.scanlator,
|
||||
chapters.url AS chapterUrl,
|
||||
chapters.read,
|
||||
chapters.bookmark,
|
||||
chapters.last_page_read,
|
||||
mangas.source,
|
||||
mangas.favorite,
|
||||
mangas.thumbnail_url AS thumbnailUrl,
|
||||
mangas.cover_last_modified AS coverLastModified,
|
||||
chapters.date_upload AS dateUpload,
|
||||
chapters.date_fetch AS datefetch,
|
||||
excluded_scanlators.scanlator AS excludedScanlator
|
||||
FROM mangas JOIN chapters
|
||||
ON mangas._id = chapters.manga_id
|
||||
LEFT JOIN excluded_scanlators
|
||||
ON mangas._id = excluded_scanlators.manga_id
|
||||
AND chapters.scanlator = excluded_scanlators.scanlator
|
||||
WHERE favorite = 1
|
||||
AND date_fetch > date_added
|
||||
ORDER BY date_fetch DESC;
|
||||
|
||||
getRecentUpdates:
|
||||
SELECT *
|
||||
FROM updatesView
|
||||
WHERE dateUpload > :after
|
||||
LIMIT :limit;
|
||||
manga.id AS mangaId,
|
||||
manga.remote_title AS mangaTitle,
|
||||
chapter.id AS chapterId,
|
||||
chapter.remote_name AS chapterName,
|
||||
chapter.remote_scanlator AS scanlator,
|
||||
chapter.remote_url AS chapterUrl,
|
||||
chapter.user_read AS read,
|
||||
chapter.user_bookmark AS bookmark,
|
||||
chapter.user_last_page_read AS lastPageRead,
|
||||
manga.source_id AS sourceId,
|
||||
manga.user_favorite_at IS NOT NULL AS favorite,
|
||||
manga.remote_cover AS thumbnailUrl,
|
||||
manga.state_cover_last_modified AS coverLastModified,
|
||||
chapter.remote_date_upload AS dateUpload,
|
||||
chapter.state_date_fetch AS dateFetch,
|
||||
excluded_scanlator.scanlator AS excludedScanlator
|
||||
FROM manga JOIN chapter
|
||||
ON manga.id = chapter.manga_id
|
||||
LEFT JOIN excluded_scanlator
|
||||
ON manga.id = excluded_scanlator.manga_id
|
||||
AND chapter.remote_scanlator = excluded_scanlator.scanlator
|
||||
WHERE user_favorite_at IS NOT NULL
|
||||
AND state_date_fetch > user_favorite_at
|
||||
ORDER BY state_date_fetch DESC;
|
||||
|
||||
getRecentUpdatesWithFilters:
|
||||
SELECT *
|
||||
SELECT
|
||||
mangaId, mangaTitle, chapterId, chapterName, scanlator, chapterUrl, read, bookmark,
|
||||
lastPageRead, sourceId, favorite, thumbnailUrl, coverLastModified, dateUpload, dateFetch,
|
||||
excludedScanlator
|
||||
FROM updatesView
|
||||
WHERE dateUpload > :after
|
||||
AND (:read IS NULL OR read = :read)
|
||||
-- Started means some progress but not finished, Read means finished chapter, thus:
|
||||
AND (
|
||||
:started IS NULL
|
||||
OR (:started = 1 AND last_page_read > 0 AND read = 0)
|
||||
OR (:started = 0 AND last_page_read = 0 AND read = 0)
|
||||
OR (:started = 1 AND lastPageRead > 0 AND read = 0)
|
||||
OR (:started = 0 AND lastPageRead = 0 AND read = 0)
|
||||
)
|
||||
AND (:bookmarked IS NULL OR bookmark = :bookmarked)
|
||||
AND (
|
||||
(excludedScanlator IS NULL OR :hideExcludedScanlators = 0)
|
||||
)
|
||||
AND (excludedScanlator IS NULL OR :hideExcludedScanlators = 0)
|
||||
AND (
|
||||
-- includedEmpty being true expresses "don't care" state and bypasses the membership filter
|
||||
:includedEmpty
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM mangas_categories
|
||||
WHERE mangas_categories.manga_id = mangaId
|
||||
AND COALESCE(mangas_categories.category_id, 0) IN :includedCategories
|
||||
SELECT 1 FROM manga_category
|
||||
WHERE manga_category.manga_id = mangaId
|
||||
AND manga_category.category_id IN :includedCategories
|
||||
)
|
||||
OR (0 IN :includedCategories AND NOT EXISTS (
|
||||
SELECT 1 FROM mangas_categories mc WHERE mc.manga_id = mangaId)
|
||||
SELECT 1 FROM manga_category WHERE manga_category.manga_id = mangaId)
|
||||
)
|
||||
)
|
||||
AND (
|
||||
@@ -63,21 +60,37 @@ AND (
|
||||
:excludedEmpty
|
||||
OR (
|
||||
NOT EXISTS (
|
||||
SELECT 1 FROM mangas_categories
|
||||
WHERE mangas_categories.manga_id = mangaId
|
||||
AND COALESCE(mangas_categories.category_id, 0) IN :excludedCategories
|
||||
SELECT 1 FROM manga_category
|
||||
WHERE manga_category.manga_id = mangaId
|
||||
AND manga_category.category_id IN :excludedCategories
|
||||
)
|
||||
AND (
|
||||
0 NOT IN :excludedCategories
|
||||
OR EXISTS (SELECT 1 FROM mangas_categories WHERE mangas_categories.manga_id = mangaId)
|
||||
OR EXISTS (SELECT 1 FROM manga_category WHERE manga_category.manga_id = mangaId)
|
||||
)
|
||||
)
|
||||
)
|
||||
ORDER BY dateFetch DESC
|
||||
LIMIT :limit;
|
||||
|
||||
getUpdatesByReadStatus:
|
||||
SELECT *
|
||||
getReadUpdates:
|
||||
SELECT
|
||||
mangaId, mangaTitle, chapterId, chapterName, scanlator, chapterUrl, read, bookmark,
|
||||
lastPageRead, sourceId, favorite, thumbnailUrl, coverLastModified, dateUpload, dateFetch,
|
||||
excludedScanlator
|
||||
FROM updatesView
|
||||
WHERE read = :read
|
||||
WHERE read = 1
|
||||
AND dateUpload > :after
|
||||
ORDER BY dateFetch DESC
|
||||
LIMIT :limit;
|
||||
|
||||
getUnreadUpdates:
|
||||
SELECT
|
||||
mangaId, mangaTitle, chapterId, chapterName, scanlator, chapterUrl, read, bookmark,
|
||||
lastPageRead, sourceId, favorite, thumbnailUrl, coverLastModified, dateUpload, dateFetch,
|
||||
excludedScanlator
|
||||
FROM updatesView
|
||||
WHERE read = 0
|
||||
AND dateUpload > :after
|
||||
ORDER BY dateFetch DESC
|
||||
LIMIT :limit;
|
||||
|
||||
Reference in New Issue
Block a user