mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
chore: More storio to sqldelight migration
- insert manga - update manga
This commit is contained in:
parent
28e551de36
commit
5ed2934b73
15 changed files with 204 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
|||
package yokai.data
|
||||
|
||||
import app.cash.sqldelight.ExecutableQuery
|
||||
import app.cash.sqldelight.Query
|
||||
import app.cash.sqldelight.coroutines.asFlow
|
||||
import app.cash.sqldelight.coroutines.mapToList
|
||||
|
@ -38,6 +39,13 @@ class AndroidDatabaseHandler(
|
|||
return dispatch(inTransaction) { block(db).executeAsOne() }
|
||||
}
|
||||
|
||||
override suspend fun <T : Any> awaitOneExecutable(
|
||||
inTransaction: Boolean,
|
||||
block: suspend Database.() -> ExecutableQuery<T>,
|
||||
): T {
|
||||
return dispatch(inTransaction) { block(db).executeAsOne() }
|
||||
}
|
||||
|
||||
override suspend fun <T : Any> awaitOneOrNull(
|
||||
inTransaction: Boolean,
|
||||
block: suspend Database.() -> Query<T>
|
||||
|
@ -45,6 +53,13 @@ class AndroidDatabaseHandler(
|
|||
return dispatch(inTransaction) { block(db).executeAsOneOrNull() }
|
||||
}
|
||||
|
||||
override suspend fun <T : Any> awaitOneOrNullExecutable(
|
||||
inTransaction: Boolean,
|
||||
block: suspend Database.() -> ExecutableQuery<T>,
|
||||
): T? {
|
||||
return dispatch(inTransaction) { block(db).executeAsOneOrNull() }
|
||||
}
|
||||
|
||||
override fun <T : Any> subscribeToList(block: Database.() -> Query<T>): Flow<List<T>> {
|
||||
return block(db).asFlow().mapToList(queryDispatcher)
|
||||
}
|
||||
|
|
|
@ -6,13 +6,13 @@ import java.util.*
|
|||
|
||||
// TODO: Move to yokai.data.DatabaseAdapter
|
||||
|
||||
val updateStrategyAdapter = object : ColumnAdapter<UpdateStrategy, Int> {
|
||||
val updateStrategyAdapter = object : ColumnAdapter<UpdateStrategy, Long> {
|
||||
private val enumValues by lazy { UpdateStrategy.entries }
|
||||
|
||||
override fun decode(databaseValue: Int): UpdateStrategy =
|
||||
enumValues.getOrElse(databaseValue) { UpdateStrategy.ALWAYS_UPDATE }
|
||||
override fun decode(databaseValue: Long): UpdateStrategy =
|
||||
enumValues.getOrElse(databaseValue.toInt()) { UpdateStrategy.ALWAYS_UPDATE }
|
||||
|
||||
override fun encode(value: UpdateStrategy): Int = value.ordinal
|
||||
override fun encode(value: UpdateStrategy): Long = value.ordinal.toLong()
|
||||
}
|
||||
|
||||
val dateAdapter = object : ColumnAdapter<Date, Long> {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package yokai.data
|
||||
|
||||
import app.cash.sqldelight.ExecutableQuery
|
||||
import app.cash.sqldelight.Query
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
|
@ -16,11 +17,21 @@ interface DatabaseHandler {
|
|||
block: suspend Database.() -> Query<T>
|
||||
): T
|
||||
|
||||
suspend fun <T : Any> awaitOneExecutable(
|
||||
inTransaction: Boolean = false,
|
||||
block: suspend Database.() -> ExecutableQuery<T>,
|
||||
): T
|
||||
|
||||
suspend fun <T : Any> awaitOneOrNull(
|
||||
inTransaction: Boolean = false,
|
||||
block: suspend Database.() -> Query<T>
|
||||
): T?
|
||||
|
||||
suspend fun <T : Any> awaitOneOrNullExecutable(
|
||||
inTransaction: Boolean = false,
|
||||
block: suspend Database.() -> ExecutableQuery<T>,
|
||||
): T?
|
||||
|
||||
fun <T : Any> subscribeToList(block: Database.() -> Query<T>): Flow<List<T>>
|
||||
|
||||
fun <T : Any> subscribeToOne(block: Database.() -> Query<T>): Flow<T>
|
||||
|
|
|
@ -29,3 +29,32 @@ CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1;
|
|||
findAll:
|
||||
SELECT *
|
||||
FROM mangas;
|
||||
|
||||
insert:
|
||||
INSERT INTO mangas (source, url, artist, author, description, genre, title, status, thumbnail_url, favorite, last_update, initialized, viewer, hide_title, chapter_flags, date_added, filtered_scanlators, update_strategy)
|
||||
VALUES (:source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favorite, :lastUpdate, :initialized, :viewer, :hideTitle, :chapterFlags, :dateAdded, :filteredScanlators, :updateStrategy);
|
||||
|
||||
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),
|
||||
initialized = coalesce(:initialized, initialized),
|
||||
viewer = coalesce(:viewer, viewer),
|
||||
hide_title = coalesce(:hideTitle, hide_title),
|
||||
chapter_flags = coalesce(:chapterFlags, chapter_flags),
|
||||
date_added = coalesce(:dateAdded, date_added),
|
||||
filtered_scanlators = coalesce(:filteredScanlators, filtered_scanlators),
|
||||
update_strategy = coalesce(:updateStrategy, update_strategy)
|
||||
WHERE _id = :mangaId;
|
||||
|
||||
selectLastInsertedRowId:
|
||||
SELECT last_insert_rowid();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue