Skip to content

Commit fb19451

Browse files
authored
Insert.select on conflict (#68)
fixes #62 again seems like redundant casts to `V[Expr]`
1 parent f5b9aa7 commit fb19451

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

docs/reference.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4587,6 +4587,45 @@ Buyer.insert
45874587
45884588
45894589
4590+
----
4591+
4592+
with `insert.select`
4593+
4594+
```scala
4595+
Buyer.insert
4596+
.select(
4597+
identity,
4598+
Buyer.select
4599+
.filter(_.id === 1)
4600+
.map(b => b.copy(name = b.name + "."))
4601+
)
4602+
.onConflictIgnore(_.id)
4603+
```
4604+
4605+
4606+
*
4607+
```sql
4608+
INSERT INTO
4609+
buyer (id, name, date_of_birth)
4610+
SELECT
4611+
buyer0.id AS id,
4612+
(buyer0.name || ?) AS name,
4613+
buyer0.date_of_birth AS date_of_birth
4614+
FROM
4615+
buyer buyer0
4616+
WHERE
4617+
(buyer0.id = ?) ON CONFLICT (id) DO NOTHING
4618+
```
4619+
4620+
4621+
4622+
*
4623+
```scala
4624+
0
4625+
```
4626+
4627+
4628+
45904629
### OnConflict.ignore.returningEmpty
45914630
45924631
@@ -4779,6 +4818,47 @@ Buyer.insert
47794818
47804819
47814820
4821+
----
4822+
4823+
with `insert.select`
4824+
4825+
```scala
4826+
Buyer.insert
4827+
.select(
4828+
identity,
4829+
Buyer.select
4830+
.filter(_.id === 1)
4831+
.map(b => b.copy(name = b.name + "."))
4832+
)
4833+
.onConflictUpdate(_.id)(_.dateOfBirth := LocalDate.parse("2023-10-09"))
4834+
```
4835+
4836+
4837+
*
4838+
```sql
4839+
INSERT INTO
4840+
buyer (id, name, date_of_birth)
4841+
SELECT
4842+
buyer1.id AS id,
4843+
(buyer1.name || ?) AS name,
4844+
buyer1.date_of_birth AS date_of_birth
4845+
FROM
4846+
buyer buyer1
4847+
WHERE
4848+
(buyer1.id = ?) ON CONFLICT (id) DO
4849+
UPDATE
4850+
SET date_of_birth = ?
4851+
```
4852+
4853+
4854+
4855+
*
4856+
```scala
4857+
1
4858+
```
4859+
4860+
4861+
47824862
----
47834863
47844864
@@ -4793,7 +4873,7 @@ Buyer.select
47934873
*
47944874
```scala
47954875
Seq(
4796-
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-10")),
4876+
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-09")),
47974877
Buyer[Sc](2, "叉烧包", LocalDate.parse("1923-11-12")),
47984878
Buyer[Sc](3, "Li Haoyi", LocalDate.parse("1965-08-09"))
47994879
)

scalasql/query/src/InsertSelect.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import scalasql.core.SqlStr.{Renderable, SqlStringSyntax}
77
* A SQL `INSERT SELECT` query
88
*/
99
trait InsertSelect[V[_[_]], C, R, R2]
10-
extends Returning.InsertBase[V[Expr]]
10+
extends Returning.InsertBase[V[Column]]
1111
with Query.ExecuteUpdate[Int]
1212

1313
object InsertSelect {
1414
class Impl[V[_[_]], C, R, R2](insert: Insert[V, R], columns: C, select: Select[C, R2])(
1515
implicit dialect: DialectTypeMappers
1616
) extends InsertSelect[V, C, R, R2] {
1717
import dialect.{dialectSelf => _, _}
18-
protected def expr = WithSqlExpr.get(insert).asInstanceOf[V[Expr]]
18+
protected def expr = WithSqlExpr.get(insert)
1919

2020
def table = insert.table
2121

scalasql/src/dialects/OnConflictOps.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ trait OnConflictOps {
1616

1717
implicit def OnConflictableInsertSelect[V[_[_]], C, R, R2](
1818
query: InsertSelect[V, C, R, R2]
19-
): OnConflict[V[Expr], Int] = {
20-
new OnConflict[V[Expr], Int](query, WithSqlExpr.get(query), query.table)
19+
): OnConflict[V[Column], Int] = {
20+
new OnConflict[V[Column], Int](query, WithSqlExpr.get(query), query.table)
2121
}
2222

2323
}

scalasql/test/src/query/OnConflictTests.scala

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ trait OnConflictTests extends ScalaSqlSuite {
5454
docs = "with `insert.values`"
5555
)
5656

57+
checker(
58+
query = Text {
59+
Buyer.insert
60+
.select(
61+
identity,
62+
Buyer.select
63+
.filter(_.id === 1)
64+
.map(b => b.copy(name = b.name + "."))
65+
)
66+
.onConflictIgnore(_.id)
67+
},
68+
sql = """INSERT INTO
69+
buyer (id, name, date_of_birth)
70+
SELECT
71+
buyer0.id AS id,
72+
(buyer0.name || ?) AS name,
73+
buyer0.date_of_birth AS date_of_birth
74+
FROM
75+
buyer buyer0
76+
WHERE
77+
(buyer0.id = ?) ON CONFLICT (id) DO NOTHING""",
78+
value = 0,
79+
docs = "with `insert.select`"
80+
)
81+
5782
test("returningEmpty") - {
5883
checker(
5984
query = Text {
@@ -179,10 +204,37 @@ trait OnConflictTests extends ScalaSqlSuite {
179204
docs = "with `insert.values`"
180205
)
181206

207+
checker(
208+
query = Text {
209+
Buyer.insert
210+
.select(
211+
identity,
212+
Buyer.select
213+
.filter(_.id === 1)
214+
.map(b => b.copy(name = b.name + "."))
215+
)
216+
.onConflictUpdate(_.id)(_.dateOfBirth := LocalDate.parse("2023-10-09"))
217+
},
218+
sql = """INSERT INTO
219+
buyer (id, name, date_of_birth)
220+
SELECT
221+
buyer1.id AS id,
222+
(buyer1.name || ?) AS name,
223+
buyer1.date_of_birth AS date_of_birth
224+
FROM
225+
buyer buyer1
226+
WHERE
227+
(buyer1.id = ?) ON CONFLICT (id) DO
228+
UPDATE
229+
SET date_of_birth = ?""",
230+
value = 1,
231+
docs = "with `insert.select`"
232+
)
233+
182234
checker(
183235
query = Text { Buyer.select },
184236
value = Seq(
185-
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-10")),
237+
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-09")),
186238
Buyer[Sc](2, "叉烧包", LocalDate.parse("1923-11-12")),
187239
Buyer[Sc](3, "Li Haoyi", LocalDate.parse("1965-08-09"))
188240
),

0 commit comments

Comments
 (0)