Add filterOpt
This commit is contained in:
parent
fefcf86b8c
commit
9a7f66a268
4 changed files with 49 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
|||
version = "3.8.3"
|
||||
version = "3.9.8"
|
||||
style = defaultWithAlign
|
||||
runner.dialect=scala3
|
||||
maxColumn = 80
|
||||
|
@ -13,4 +13,4 @@ runner.optimizer.forceConfigStyleMinArgCount = 2
|
|||
newlines.beforeCurlyLambdaParams = multilineWithCaseOnly
|
||||
indentOperator.topLevelOnly = true
|
||||
rewrite.imports.sort = original
|
||||
docstrings.style = keep
|
||||
docstrings.style = Asterisk
|
||||
|
|
|
@ -142,6 +142,24 @@ object Query {
|
|||
expandFields[E](e)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter with pred `f` if x is Some(...), otherwise true
|
||||
*/
|
||||
inline def filterOpt[E1](
|
||||
x: Option[E1]
|
||||
)(
|
||||
inline f: (E, E1) => Boolean
|
||||
)(using ParamEncoder[Option[E1]]): Query[E] = {
|
||||
|
||||
transform2(e)(f) { (p, id1, id2, body) =>
|
||||
ast.Filter(
|
||||
p,
|
||||
id1,
|
||||
ast.OptionForall(quotedLift(x), id2, body)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
inline def leftJoin[E1](inline e1: Query[E1]): Joined[E, Option[E1]] =
|
||||
Joined[E, Option[E1]](JoinType.LeftJoin, e, e1)
|
||||
|
||||
|
@ -289,6 +307,12 @@ private inline def transform[A, B](inline q1: Quoted)(
|
|||
fast(q1, f.param0, f.body)
|
||||
}
|
||||
|
||||
private inline def transform2[A, A1, B](inline q1: Quoted)(
|
||||
inline f: (A, A1) => B
|
||||
)(inline fast: (Ast, Ident, Ident, Ast) => Ast): Quoted = {
|
||||
fast(q1, f.param0, f.param1, f.body)
|
||||
}
|
||||
|
||||
inline def alias(inline from: String, inline to: String): PropertyAlias =
|
||||
PropertyAlias(List(from), to)
|
||||
|
||||
|
|
|
@ -148,7 +148,7 @@ trait Context[I <: Idiom, N <: NamingStrategy] { selft =>
|
|||
val (sql, params) = stmt.expand(lifts)
|
||||
(
|
||||
sql = sql,
|
||||
params = params.map(_.value.get.asInstanceOf),
|
||||
params = params.map(_.value.get.asInstanceOf[(Any, Encoder[?])]),
|
||||
mapper = mapper
|
||||
)
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ trait Context[I <: Idiom, N <: NamingStrategy] { selft =>
|
|||
val (sql, params) = stmt.expand(lifts)
|
||||
(
|
||||
sql = sql,
|
||||
params = params.map(_.value.get.asInstanceOf),
|
||||
params = params.map(_.value.get.asInstanceOf[(Any, Encoder[?])]),
|
||||
mapper = (rows) => rows.traverse(extractor.extract)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -185,4 +185,17 @@ class MirrorSqlContextSuite extends munit.FunSuite {
|
|||
"SELECT SUM(f.age) FROM foo f"
|
||||
)
|
||||
}
|
||||
|
||||
test("filterOpt - Some value") {
|
||||
val maybeId: Option[Long] = Some(5L)
|
||||
val o = testContext.io(
|
||||
Foos.filterOpt(maybeId)((f, id) => f.id == id)
|
||||
)
|
||||
assertEquals(
|
||||
o.sql,
|
||||
"SELECT f.id, f.name, f.age FROM foo f WHERE ? IS NULL OR f.id = ?"
|
||||
)
|
||||
println(o.params)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue