Add insert
This commit is contained in:
parent
13633e0b20
commit
60db6033d6
3 changed files with 63 additions and 3 deletions
|
@ -16,3 +16,16 @@ object UpdateMeta {
|
||||||
|
|
||||||
inline def unsafe[E](inline ex: List[String]): UpdateMeta[E] = ex
|
inline def unsafe[E](inline ex: List[String]): UpdateMeta[E] = ex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opaque type InsertMeta[E] <: List[String] = List[String]
|
||||||
|
|
||||||
|
object InsertMeta {
|
||||||
|
|
||||||
|
inline def noExclude[E]: InsertMeta[E] = unsafe(Nil)
|
||||||
|
|
||||||
|
inline def apply[E](using m: Mirror.ProductOf[E])(
|
||||||
|
inline exclude: List[Tuple.Union[m.MirroredElemLabels] & String]
|
||||||
|
): InsertMeta[E] = unsafe(exclude)
|
||||||
|
|
||||||
|
inline def unsafe[E](inline ex: List[String]): InsertMeta[E] = ex
|
||||||
|
}
|
||||||
|
|
|
@ -165,8 +165,14 @@ object EntityQuery {
|
||||||
transform(e)(f)(Filter.apply)
|
transform(e)(f)(Filter.apply)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline def insert(v: E): Insert[E] = {
|
inline def insert(
|
||||||
ast.Insert(e, transformCaseClassToAssignments[E](v, Nil))
|
v: E
|
||||||
|
)(using inline m: InsertMeta[E] = InsertMeta.noExclude[E]): Insert[E] = {
|
||||||
|
ast.Insert(e, transformCaseClassToAssignments[E](v, m))
|
||||||
|
}
|
||||||
|
|
||||||
|
inline def insert(inline ass: (E => (Any, Any))*): Insert[Long] = {
|
||||||
|
ast.Insert(e, parseFuncAssign(ass))
|
||||||
}
|
}
|
||||||
|
|
||||||
inline def update(
|
inline def update(
|
||||||
|
|
|
@ -30,7 +30,7 @@ class MirrorSqlContextSuite extends munit.FunSuite {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
test("Insert") {
|
test("Insert - basic") {
|
||||||
val v: Foo = Foo(0L, "foo", 1)
|
val v: Foo = Foo(0L, "foo", 1)
|
||||||
|
|
||||||
val o = testContext.io(Foos.insert(v))
|
val o = testContext.io(Foos.insert(v))
|
||||||
|
@ -40,6 +40,47 @@ class MirrorSqlContextSuite extends munit.FunSuite {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test("Insert - with excluded fields") {
|
||||||
|
inline given InsertMeta[Foo] = InsertMeta[Foo](List("age"))
|
||||||
|
|
||||||
|
val v: Foo = Foo(0L, "foo", 1)
|
||||||
|
|
||||||
|
val o = testContext.io(Foos.insert(v))
|
||||||
|
assertEquals(
|
||||||
|
o.sql,
|
||||||
|
"INSERT INTO foo (id,name) VALUES (?, ?)"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
test("Insert - with explicit assignments") {
|
||||||
|
val name = "test"
|
||||||
|
val age = 42
|
||||||
|
|
||||||
|
val o = testContext.io(
|
||||||
|
Foos.insert(
|
||||||
|
f => f.name -> lift(name),
|
||||||
|
f => f.age -> lift(age)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
o.sql,
|
||||||
|
"INSERT INTO foo (name,age) VALUES (?, ?)"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
test("Insert - with mixed explicit and excluded") {
|
||||||
|
given InsertMeta[Foo] = InsertMeta[Foo](List("id"))
|
||||||
|
val name = "test"
|
||||||
|
|
||||||
|
val o = testContext.io(
|
||||||
|
Foos.insert(f => f.name -> lift(name))
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
o.sql,
|
||||||
|
"INSERT INTO foo (name) VALUES (?)"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
test("InsertReturningGenerated") {
|
test("InsertReturningGenerated") {
|
||||||
|
|
||||||
val o = testContext.io(Foos.insert(foo0).returningGenerated(_.id))
|
val o = testContext.io(Foos.insert(foo0).returningGenerated(_.id))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue