Extend query builder to compare results from subselects

This commit is contained in:
eikek 2021-06-26 21:12:02 +02:00
parent 7dcb61ef56
commit 988367a281
3 changed files with 29 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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