增加更新
This commit is contained in:
parent
071b27abcf
commit
23d1ca480e
3 changed files with 54 additions and 0 deletions
|
@ -28,6 +28,8 @@ opaque type Query[E] <: Quoted = Quoted
|
|||
|
||||
opaque type Action[E] <: Quoted = Quoted
|
||||
|
||||
opaque type Update[E] <: Action[Long] = Quoted
|
||||
|
||||
opaque type Insert[E] <: Action[Long] = Quoted
|
||||
|
||||
object Insert {
|
||||
|
@ -165,6 +167,28 @@ object EntityQuery {
|
|||
inline def insert(v: E): Insert[E] = {
|
||||
ast.Insert(e, transformCaseClassToAssignments[E](v))
|
||||
}
|
||||
|
||||
inline def update(inline ass: (E => (Any, Any))*): Update[Long] = {
|
||||
ast.Update(e, parseFuncAssign(ass))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private inline def parseFuncAssign[E](
|
||||
inline ass: Seq[(E => (Any, Any))]
|
||||
): List[ast.Assignment] = ${ parseFuncAssignImpl[E]('ass) }
|
||||
|
||||
private def parseFuncAssignImpl[E](x: Expr[Seq[E => (Any, Any)]])(using
|
||||
Quotes,
|
||||
Type[E]
|
||||
): Expr[List[ast.Assignment]] = {
|
||||
import quotes.reflect.*
|
||||
x match {
|
||||
case '{ ${ Varargs(ass) } } =>
|
||||
val assExprs = ass.map { a =>
|
||||
parseAssignment(a)
|
||||
}
|
||||
Expr.ofList(assExprs)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -46,3 +46,19 @@ private[minisql] def parseBody[X](
|
|||
report.errorAndAbort(s"Can only parse function")
|
||||
}
|
||||
}
|
||||
|
||||
private[minisql] def parseAssignment(x: Expr[?])(using
|
||||
Quotes
|
||||
): Expr[ast.Assignment] = {
|
||||
import quotes.reflect.*
|
||||
x.asTerm match {
|
||||
case Lambda(List(ValDef(n, _, _)), IsTuple2(prop, value)) =>
|
||||
'{
|
||||
ast.Assignment(
|
||||
ast.Ident(${ Expr(n) }),
|
||||
${ Parsing.parseExpr(prop) },
|
||||
${ Parsing.parseExpr(value) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,4 +60,18 @@ class MirrorSqlContextSuite extends munit.FunSuite {
|
|||
)
|
||||
assertEquals(o.sql, "SELECT CONCAT(f.name, ' ', f.id) FROM foo f")
|
||||
}
|
||||
|
||||
test("Update") {
|
||||
val name = "new name"
|
||||
val id = 2L
|
||||
val o = testContext.io(
|
||||
Foos
|
||||
.filter(_.id == 1)
|
||||
.update(f => f.name -> lift(name), f => f.id -> lift(id))
|
||||
)
|
||||
assertEquals(
|
||||
o.sql,
|
||||
"UPDATE foo SET name = ?, id = ? WHERE id = 1"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue