Skip to content

Insert.select on conflict #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4587,6 +4587,45 @@ Buyer.insert



----

with `insert.select`

```scala
Buyer.insert
.select(
identity,
Buyer.select
.filter(_.id === 1)
.map(b => b.copy(name = b.name + "."))
)
.onConflictIgnore(_.id)
```


*
```sql
INSERT INTO
buyer (id, name, date_of_birth)
SELECT
buyer0.id AS id,
(buyer0.name || ?) AS name,
buyer0.date_of_birth AS date_of_birth
FROM
buyer buyer0
WHERE
(buyer0.id = ?) ON CONFLICT (id) DO NOTHING
```



*
```scala
0
```



### OnConflict.ignore.returningEmpty


Expand Down Expand Up @@ -4779,6 +4818,47 @@ Buyer.insert



----

with `insert.select`

```scala
Buyer.insert
.select(
identity,
Buyer.select
.filter(_.id === 1)
.map(b => b.copy(name = b.name + "."))
)
.onConflictUpdate(_.id)(_.dateOfBirth := LocalDate.parse("2023-10-09"))
```


*
```sql
INSERT INTO
buyer (id, name, date_of_birth)
SELECT
buyer1.id AS id,
(buyer1.name || ?) AS name,
buyer1.date_of_birth AS date_of_birth
FROM
buyer buyer1
WHERE
(buyer1.id = ?) ON CONFLICT (id) DO
UPDATE
SET date_of_birth = ?
```



*
```scala
1
```



----


Expand All @@ -4793,7 +4873,7 @@ Buyer.select
*
```scala
Seq(
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-10")),
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-09")),
Buyer[Sc](2, "叉烧包", LocalDate.parse("1923-11-12")),
Buyer[Sc](3, "Li Haoyi", LocalDate.parse("1965-08-09"))
)
Expand Down
4 changes: 2 additions & 2 deletions scalasql/query/src/InsertSelect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import scalasql.core.SqlStr.{Renderable, SqlStringSyntax}
* A SQL `INSERT SELECT` query
*/
trait InsertSelect[V[_[_]], C, R, R2]
extends Returning.InsertBase[V[Expr]]
extends Returning.InsertBase[V[Column]]
with Query.ExecuteUpdate[Int]

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

def table = insert.table

Expand Down
4 changes: 2 additions & 2 deletions scalasql/src/dialects/OnConflictOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ trait OnConflictOps {

implicit def OnConflictableInsertSelect[V[_[_]], C, R, R2](
query: InsertSelect[V, C, R, R2]
): OnConflict[V[Expr], Int] = {
new OnConflict[V[Expr], Int](query, WithSqlExpr.get(query), query.table)
): OnConflict[V[Column], Int] = {
new OnConflict[V[Column], Int](query, WithSqlExpr.get(query), query.table)
}

}
54 changes: 53 additions & 1 deletion scalasql/test/src/query/OnConflictTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ trait OnConflictTests extends ScalaSqlSuite {
docs = "with `insert.values`"
)

checker(
query = Text {
Buyer.insert
.select(
identity,
Buyer.select
.filter(_.id === 1)
.map(b => b.copy(name = b.name + "."))
)
.onConflictIgnore(_.id)
},
sql = """INSERT INTO
buyer (id, name, date_of_birth)
SELECT
buyer0.id AS id,
(buyer0.name || ?) AS name,
buyer0.date_of_birth AS date_of_birth
FROM
buyer buyer0
WHERE
(buyer0.id = ?) ON CONFLICT (id) DO NOTHING""",
value = 0,
docs = "with `insert.select`"
)

test("returningEmpty") - {
checker(
query = Text {
Expand Down Expand Up @@ -179,10 +204,37 @@ trait OnConflictTests extends ScalaSqlSuite {
docs = "with `insert.values`"
)

checker(
query = Text {
Buyer.insert
.select(
identity,
Buyer.select
.filter(_.id === 1)
.map(b => b.copy(name = b.name + "."))
)
.onConflictUpdate(_.id)(_.dateOfBirth := LocalDate.parse("2023-10-09"))
},
sql = """INSERT INTO
buyer (id, name, date_of_birth)
SELECT
buyer1.id AS id,
(buyer1.name || ?) AS name,
buyer1.date_of_birth AS date_of_birth
FROM
buyer buyer1
WHERE
(buyer1.id = ?) ON CONFLICT (id) DO
UPDATE
SET date_of_birth = ?""",
value = 1,
docs = "with `insert.select`"
)

checker(
query = Text { Buyer.select },
value = Seq(
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-10")),
Buyer[Sc](1, "TEST BUYER CONFLICT", LocalDate.parse("2023-10-09")),
Buyer[Sc](2, "叉烧包", LocalDate.parse("1923-11-12")),
Buyer[Sc](3, "Li Haoyi", LocalDate.parse("1965-08-09"))
),
Expand Down