Introduce a sql literal and constants in query builder

The h2 jdbc driver could not translate the union query in QCollective
when the `kind` was set via a constant value. Using literals works
here. Renamed the corresponding elements in the query builder.
This commit is contained in:
Eike Kettner 2021-01-25 00:16:59 +01:00
parent bb744668ca
commit 394aeeccb6
6 changed files with 27 additions and 16 deletions

View File

@ -101,8 +101,11 @@ trait DSL extends DoobieMeta {
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)
def const[A](value: A)(implicit P: Put[A]): SelectExpr.SelectConstant[A] =
SelectExpr.SelectConstant(value, None)
def lit(value: String): SelectExpr.SelectLiteral =
SelectExpr.SelectLiteral(value, None)
def plus(left: SelectExpr, right: SelectExpr): DBFunction =
DBFunction.Calc(DBFunction.Operator.Plus, left, right)
@ -291,7 +294,7 @@ trait DSL extends DoobieMeta {
DBFunction.Calc(
DBFunction.Operator.Minus,
SelectExpr.SelectFun(dbf, None),
SelectExpr.SelectLit(value, None)
SelectExpr.SelectConstant(value, None)
)
}
}

View File

@ -18,9 +18,14 @@ object SelectExpr {
copy(alias = Some(a))
}
case class SelectLit[A](value: A, alias: Option[String])(implicit val P: Put[A])
case class SelectConstant[A](value: A, alias: Option[String])(implicit val P: Put[A])
extends SelectExpr {
def as(a: String): SelectLit[A] =
def as(a: String): SelectConstant[A] =
copy(alias = Some(a))
}
case class SelectLiteral(value: String, alias: Option[String]) extends SelectExpr {
def as(a: String): SelectLiteral =
copy(alias = Some(a))
}

View File

@ -12,9 +12,12 @@ object SelectExprBuilder extends CommonBuilder {
case SelectExpr.SelectColumn(col, alias) =>
column(col) ++ appendAs(alias)
case s @ SelectExpr.SelectLit(value, aliasOpt) =>
case s @ SelectExpr.SelectConstant(value, aliasOpt) =>
ConditionBuilder.buildValue(value)(s.P) ++ appendAs(aliasOpt)
case SelectExpr.SelectLiteral(value, alias) =>
Fragment.const(value) ++ appendAs(alias)
case SelectExpr.SelectFun(fun, alias) =>
DBFunctionBuilder.build(fun) ++ appendAs(alias)

View File

@ -28,17 +28,17 @@ object QCollective {
val created = Column[Timestamp]("created", TableDef(""))
union(
Select(
select(ro.name.s, lit(1).as("kind"), ro.created.as(created)),
select(ro.name.s, lit("1").as("kind"), ro.created.as(created)),
from(ro),
ro.cid === collective
),
Select(
select(rp.name.s, lit(2).as("kind"), rp.created.as(created)),
select(rp.name.s, lit("2").as("kind"), rp.created.as(created)),
from(rp),
rp.cid === collective
),
Select(
select(re.name.s, lit(3).as("kind"), re.created.as(created)),
select(re.name.s, lit("3").as("kind"), re.created.as(created)),
from(re),
re.cid === collective
)

View File

@ -138,7 +138,7 @@ object QItem {
i.source.s,
i.incoming.s,
i.created.s,
coalesce(Attachs.num.s, lit(0)).s,
coalesce(Attachs.num.s, const(0)).s,
org.oid.s,
org.name.s,
pers0.pid.s,
@ -302,10 +302,10 @@ object QItem {
val basicFields = Nel.of(
count(i.id).as("fc"),
lit(0).as("favg"),
lit(0).as("fsum"),
lit(0).as("fmax"),
lit(0).as("fmin")
const(0).as("favg"),
const(0).as("fsum"),
const(0).as("fmax"),
const(0).as("fmin")
)
val valueNum = cast(cv.value.s, "decimal").s
val numericFields = Nel.of(

View File

@ -118,8 +118,8 @@ object QJob {
private def stuckTriggerValue(t: RJob.Table, initialPause: Duration, now: Timestamp) =
plus(
coalesce(t.startedmillis.s, lit(now.toMillis)).s,
mult(power(2, t.retries.s).s, lit(initialPause.millis)).s
coalesce(t.startedmillis.s, const(now.toMillis)).s,
mult(power(2, t.retries.s).s, const(initialPause.millis)).s
)
def selectNextJob(