From 603105e2ea480a39ba20904e0d05383a62d35836 Mon Sep 17 00:00:00 2001 From: Mitchell Syer Date: Wed, 24 May 2023 06:30:54 -0400 Subject: [PATCH] Fix StringFilter (#554) --- .../graphql/queries/filter/Filter.kt | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt index 96458393..531c9eca 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt @@ -131,7 +131,7 @@ interface ScalarFilter { val notIn: List? } -interface ComparableScalarFilter> : ScalarFilter { +interface ComparableScalarFilter?> : ScalarFilter { val lessThan: T? val lessThanOrEqualTo: T? val greaterThan: T? @@ -255,12 +255,30 @@ data class StringListFilter( ) : ListScalarFilter> @Suppress("UNCHECKED_CAST") -fun andFilterWithCompareString( - column: Column, +fun andFilterWithCompareString( + column: Column, filter: StringFilter? ): Op? { filter ?: return null val opAnd = OpAnd() + + opAnd.andWhere(filter.isNull) { if (it) column.isNull() else column.isNotNull() } + opAnd.andWhere(filter.equalTo) { column eq it as S } + opAnd.andWhere(filter.notEqualTo) { column neq it as S } + opAnd.andWhere(filter.distinctFrom) { DistinctFromOp.distinctFrom(column, it as S) } + opAnd.andWhere(filter.notDistinctFrom) { DistinctFromOp.notDistinctFrom(column, it as S) } + if (!filter.`in`.isNullOrEmpty()) { + opAnd.andWhere(filter.`in`) { column inList it as List } + } + if (!filter.notIn.isNullOrEmpty()) { + opAnd.andWhere(filter.notIn) { column notInList it as List } + } + + opAnd.andWhere(filter.lessThan) { column less it } + opAnd.andWhere(filter.lessThanOrEqualTo) { column lessEq it } + opAnd.andWhere(filter.greaterThan) { column greater it } + opAnd.andWhere(filter.greaterThanOrEqualTo) { column greaterEq it } + opAnd.andWhere(filter.includes) { column like "%$it%" } opAnd.andWhere(filter.notIncludes) { column notLike "%$it%" } opAnd.andWhere(filter.includesInsensitive) { ILikeEscapeOp.iLike(column, "%$it%") } @@ -281,11 +299,11 @@ fun andFilterWithCompareString( opAnd.andWhere(filter.likeInsensitive) { ILikeEscapeOp.iLike(column, it) } opAnd.andWhere(filter.notLikeInsensitive) { ILikeEscapeOp.iNotLike(column, it) } - opAnd.andWhere(filter.distinctFromInsensitive) { DistinctFromOp.distinctFrom(column.upperCase(), it.uppercase() as T) } - opAnd.andWhere(filter.notDistinctFromInsensitive) { DistinctFromOp.notDistinctFrom(column.upperCase(), it.uppercase() as T) } + opAnd.andWhere(filter.distinctFromInsensitive) { DistinctFromOp.distinctFrom(column.upperCase(), it.uppercase() as S) } + opAnd.andWhere(filter.notDistinctFromInsensitive) { DistinctFromOp.notDistinctFrom(column.upperCase(), it.uppercase() as S) } - opAnd.andWhere(filter.inInsensitive) { column.upperCase() inList (it.map { it.uppercase() } as List) } - opAnd.andWhere(filter.notInInsensitive) { column.upperCase() notInList (it.map { it.uppercase() } as List) } + opAnd.andWhere(filter.inInsensitive) { column.upperCase() inList (it.map { it.uppercase() } as List) } + opAnd.andWhere(filter.notInInsensitive) { column.upperCase() notInList (it.map { it.uppercase() } as List) } opAnd.andWhere(filter.lessThanInsensitive) { column.upperCase() less it.uppercase() } opAnd.andWhere(filter.lessThanOrEqualToInsensitive) { column.upperCase() lessEq it.uppercase() } @@ -296,7 +314,7 @@ fun andFilterWithCompareString( } class OpAnd(var op: Op? = null) { - fun andWhere(value: T?, andPart: SqlExpressionBuilder.(T) -> Op) { + fun andWhere(value: T?, andPart: SqlExpressionBuilder.(T & Any) -> Op) { value ?: return val expr = Op.build { andPart(value) } op = if (op == null) expr else (op!! and expr)