From 988367a2817155ee9d7d519711d6ca46025aa1aa Mon Sep 17 00:00:00 2001 From: eikek Date: Sat, 26 Jun 2021 21:12:02 +0200 Subject: [PATCH] Extend query builder to compare results from subselects --- .../main/scala/docspell/store/qb/Condition.scala | 3 +++ .../src/main/scala/docspell/store/qb/DSL.scala | 13 +++++++++++++ .../docspell/store/qb/impl/ConditionBuilder.scala | 13 +++++++++++++ 3 files changed, 29 insertions(+) diff --git a/modules/store/src/main/scala/docspell/store/qb/Condition.scala b/modules/store/src/main/scala/docspell/store/qb/Condition.scala index 9a329033..cfe156a2 100644 --- a/modules/store/src/main/scala/docspell/store/qb/Condition.scala +++ b/modules/store/src/main/scala/docspell/store/qb/Condition.scala @@ -19,6 +19,9 @@ object Condition { val P: Put[A] ) extends Condition + case class CompareSelect(sel: SelectExpr, op: Operator, subSelect: Select) + extends Condition + case class CompareCol[A](col1: Column[A], op: Operator, col2: Column[A]) extends Condition diff --git a/modules/store/src/main/scala/docspell/store/qb/DSL.scala b/modules/store/src/main/scala/docspell/store/qb/DSL.scala index d6f32d9c..671d4e71 100644 --- a/modules/store/src/main/scala/docspell/store/qb/DSL.scala +++ b/modules/store/src/main/scala/docspell/store/qb/DSL.scala @@ -215,6 +215,19 @@ trait DSL extends DoobieMeta { def in(subsel: Select): Condition = Condition.InSubSelect(col, subsel) + def >(subsel: Select): Condition = + Condition.CompareSelect(col.s, Operator.Gt, subsel) + def <(subsel: Select): Condition = + Condition.CompareSelect(col.s, Operator.Lt, subsel) + def >=(subsel: Select): Condition = + Condition.CompareSelect(col.s, Operator.Gte, subsel) + def <=(subsel: Select): Condition = + Condition.CompareSelect(col.s, Operator.Lte, subsel) + def ===(subsel: Select): Condition = + Condition.CompareSelect(col.s, Operator.Eq, subsel) + def !==(subsel: Select): Condition = + Condition.CompareSelect(col.s, Operator.Neq, subsel) + def notIn(subsel: Select): Condition = in(subsel).negate diff --git a/modules/store/src/main/scala/docspell/store/qb/impl/ConditionBuilder.scala b/modules/store/src/main/scala/docspell/store/qb/impl/ConditionBuilder.scala index c9e50575..665ca476 100644 --- a/modules/store/src/main/scala/docspell/store/qb/impl/ConditionBuilder.scala +++ b/modules/store/src/main/scala/docspell/store/qb/impl/ConditionBuilder.scala @@ -100,6 +100,19 @@ object ConditionBuilder { } c1Frag ++ operator(op) ++ c2Frag + case Condition.CompareSelect(col, op, subsel) => + val opFrag = operator(op) + val colFrag = op match { + case Operator.LowerLike => + lower(col) + case Operator.LowerEq => + lower(col) + case _ => + SelectExprBuilder.build(col) + } + val sub = SelectBuilder(subsel) + colFrag ++ opFrag ++ sql"(" ++ sub ++ sql")" + case Condition.InSubSelect(col, subsel) => val sub = SelectBuilder(subsel) SelectExprBuilder.column(col) ++ sql" IN (" ++ sub ++ parenClose