Add the sql concat function to query builder

This commit is contained in:
Eike Kettner 2021-01-18 21:48:05 +01:00
parent 249f9e6e2a
commit 3e28ce1254
3 changed files with 11 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package docspell.store.qb
import cats.data.NonEmptyList
sealed trait DBFunction {}
object DBFunction {
@ -31,6 +33,8 @@ object DBFunction {
case class Sum(expr: SelectExpr) extends DBFunction
case class Concat(exprs: NonEmptyList[SelectExpr]) extends DBFunction
sealed trait Operator
object Operator {
case object Plus extends Operator

View File

@ -98,6 +98,9 @@ trait DSL extends DoobieMeta {
def substring(expr: SelectExpr, start: Int, length: Int): DBFunction =
DBFunction.Substring(expr, start, length)
def concat(expr: SelectExpr, exprs: SelectExpr*): DBFunction =
DBFunction.Concat(Nel.of(expr, exprs: _*))
def lit[A](value: A)(implicit P: Put[A]): SelectExpr.SelectLit[A] =
SelectExpr.SelectLit(value, None)

View File

@ -32,6 +32,10 @@ object DBFunctionBuilder extends CommonBuilder {
case DBFunction.Substring(expr, start, len) =>
sql"SUBSTRING(" ++ SelectExprBuilder.build(expr) ++ fr" FROM $start FOR $len)"
case DBFunction.Concat(exprs) =>
val inner = exprs.map(SelectExprBuilder.build).toList.reduce(_ ++ comma ++ _)
sql"CONCAT(" ++ inner ++ sql")"
case DBFunction.Calc(op, left, right) =>
SelectExprBuilder.build(left) ++
buildOperator(op) ++