mirror of
https://github.com/Suwayomi/Tachidesk.git
synced 2026-02-02 17:10:45 +01:00
Simplify keyset pagination
This commit is contained in:
@@ -12,12 +12,9 @@ import graphql.schema.DataFetchingEnvironment
|
||||
import org.jetbrains.exposed.sql.Column
|
||||
import org.jetbrains.exposed.sql.Op
|
||||
import org.jetbrains.exposed.sql.SortOrder
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.less
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.andWhere
|
||||
import org.jetbrains.exposed.sql.or
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import suwayomi.tachidesk.graphql.queries.filter.BooleanFilter
|
||||
@@ -34,6 +31,8 @@ import suwayomi.tachidesk.graphql.server.primitives.Cursor
|
||||
import suwayomi.tachidesk.graphql.server.primitives.OrderBy
|
||||
import suwayomi.tachidesk.graphql.server.primitives.PageInfo
|
||||
import suwayomi.tachidesk.graphql.server.primitives.QueryResults
|
||||
import suwayomi.tachidesk.graphql.server.primitives.greaterNotUnique
|
||||
import suwayomi.tachidesk.graphql.server.primitives.lessNotUnique
|
||||
import suwayomi.tachidesk.graphql.server.primitives.maybeSwap
|
||||
import suwayomi.tachidesk.graphql.types.CategoryNodeList
|
||||
import suwayomi.tachidesk.graphql.types.CategoryType
|
||||
@@ -65,32 +64,16 @@ class CategoryQuery {
|
||||
override fun greater(cursor: Cursor): Op<Boolean> {
|
||||
return when (this) {
|
||||
ID -> CategoryTable.id greater cursor.value.toInt()
|
||||
NAME -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-')
|
||||
(CategoryTable.name greater value) or ((CategoryTable.name eq value) and (CategoryTable.id greater id))
|
||||
}
|
||||
ORDER -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-').toInt()
|
||||
(CategoryTable.order greater value) or ((CategoryTable.order eq value) and (CategoryTable.id greater id))
|
||||
}
|
||||
NAME -> greaterNotUnique(CategoryTable.name, CategoryTable.id, cursor, String::toString)
|
||||
ORDER -> greaterNotUnique(CategoryTable.order, CategoryTable.id, cursor, String::toInt)
|
||||
}
|
||||
}
|
||||
|
||||
override fun less(cursor: Cursor): Op<Boolean> {
|
||||
return when (this) {
|
||||
ID -> CategoryTable.id less cursor.value.toInt()
|
||||
NAME -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-')
|
||||
(CategoryTable.name less value) or ((CategoryTable.name eq value) and (CategoryTable.id less id))
|
||||
}
|
||||
ORDER -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-').toInt()
|
||||
(CategoryTable.order less value) or ((CategoryTable.order eq value) and (CategoryTable.id less id))
|
||||
}
|
||||
NAME -> lessNotUnique(CategoryTable.name, CategoryTable.id, cursor, String::toString)
|
||||
ORDER -> lessNotUnique(CategoryTable.order, CategoryTable.id, cursor, String::toInt)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,12 +12,9 @@ import graphql.schema.DataFetchingEnvironment
|
||||
import org.jetbrains.exposed.sql.Column
|
||||
import org.jetbrains.exposed.sql.Op
|
||||
import org.jetbrains.exposed.sql.SortOrder
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.less
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.andWhere
|
||||
import org.jetbrains.exposed.sql.or
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
@@ -37,6 +34,8 @@ import suwayomi.tachidesk.graphql.server.primitives.Cursor
|
||||
import suwayomi.tachidesk.graphql.server.primitives.OrderBy
|
||||
import suwayomi.tachidesk.graphql.server.primitives.PageInfo
|
||||
import suwayomi.tachidesk.graphql.server.primitives.QueryResults
|
||||
import suwayomi.tachidesk.graphql.server.primitives.greaterNotUnique
|
||||
import suwayomi.tachidesk.graphql.server.primitives.lessNotUnique
|
||||
import suwayomi.tachidesk.graphql.server.primitives.maybeSwap
|
||||
import suwayomi.tachidesk.graphql.types.MangaNodeList
|
||||
import suwayomi.tachidesk.graphql.types.MangaType
|
||||
@@ -73,42 +72,18 @@ class MangaQuery {
|
||||
override fun greater(cursor: Cursor): Op<Boolean> {
|
||||
return when (this) {
|
||||
ID -> MangaTable.id greater cursor.value.toInt()
|
||||
TITLE -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-')
|
||||
(MangaTable.title greater value) or ((MangaTable.title eq value) and (MangaTable.id greater id))
|
||||
}
|
||||
IN_LIBRARY_AT -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-').toLong()
|
||||
(MangaTable.inLibraryAt greater value) or ((MangaTable.inLibraryAt eq value) and (MangaTable.id greater id))
|
||||
}
|
||||
LAST_FETCHED_AT -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-').toLong()
|
||||
(MangaTable.lastFetchedAt greater value) or ((MangaTable.lastFetchedAt eq value) and (MangaTable.id greater id))
|
||||
}
|
||||
TITLE -> greaterNotUnique(MangaTable.title, MangaTable.id, cursor, String::toString)
|
||||
IN_LIBRARY_AT -> greaterNotUnique(MangaTable.inLibraryAt, MangaTable.id, cursor, String::toLong)
|
||||
LAST_FETCHED_AT -> greaterNotUnique(MangaTable.lastFetchedAt, MangaTable.id, cursor, String::toLong)
|
||||
}
|
||||
}
|
||||
|
||||
override fun less(cursor: Cursor): Op<Boolean> {
|
||||
return when (this) {
|
||||
ID -> MangaTable.id less cursor.value.toInt()
|
||||
TITLE -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-')
|
||||
(MangaTable.title less value) or ((MangaTable.title eq value) and (MangaTable.id less id))
|
||||
}
|
||||
IN_LIBRARY_AT -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-').toLong()
|
||||
(MangaTable.inLibraryAt less value) or ((MangaTable.inLibraryAt eq value) and (MangaTable.id less id))
|
||||
}
|
||||
LAST_FETCHED_AT -> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = cursor.value.substringAfter('-').toLong()
|
||||
(MangaTable.lastFetchedAt less value) or ((MangaTable.lastFetchedAt eq value) and (MangaTable.id less id))
|
||||
}
|
||||
TITLE -> lessNotUnique(MangaTable.title, MangaTable.id, cursor, String::toString)
|
||||
IN_LIBRARY_AT -> lessNotUnique(MangaTable.inLibraryAt, MangaTable.id, cursor, String::toLong)
|
||||
LAST_FETCHED_AT -> lessNotUnique(MangaTable.lastFetchedAt, MangaTable.id, cursor, String::toLong)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package suwayomi.tachidesk.graphql.server.primitives
|
||||
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import org.jetbrains.exposed.sql.Column
|
||||
import org.jetbrains.exposed.sql.Op
|
||||
import org.jetbrains.exposed.sql.SortOrder
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.less
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.or
|
||||
|
||||
interface OrderBy<T> {
|
||||
val column: Column<out Comparable<*>>
|
||||
@@ -29,3 +35,15 @@ fun SortOrder?.maybeSwap(value: Any?): SortOrder {
|
||||
this ?: SortOrder.ASC
|
||||
}
|
||||
}
|
||||
|
||||
fun <T: Comparable<T>> greaterNotUnique(column: Column<T>, idColumn: Column<EntityID<Int>>, cursor: Cursor, toValue: (String) -> T): Op<Boolean> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = toValue(cursor.value.substringAfter('-'))
|
||||
return (column greater value) or ((column eq value) and (idColumn greater id))
|
||||
}
|
||||
|
||||
fun <T: Comparable<T>> lessNotUnique(column: Column<T>, idColumn: Column<EntityID<Int>>, cursor: Cursor, toValue: (String) -> T): Op<Boolean> {
|
||||
val id = cursor.value.substringBefore('-').toInt()
|
||||
val value = toValue(cursor.value.substringAfter('-'))
|
||||
return (column less value) or ((column eq value) and (idColumn less id))
|
||||
}
|
||||
@@ -37,8 +37,8 @@ class MangaType(
|
||||
val inLibrary: Boolean,
|
||||
val inLibraryAt: Long,
|
||||
val realUrl: String?,
|
||||
var lastFetchedAt: Long?,
|
||||
var chaptersLastFetchedAt: Long?
|
||||
var lastFetchedAt: Long?, //todo
|
||||
var chaptersLastFetchedAt: Long? //todo
|
||||
) : Node {
|
||||
constructor(row: ResultRow) : this(
|
||||
row[MangaTable.id].value,
|
||||
|
||||
Reference in New Issue
Block a user