Skip to content
Open
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
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/upsert_named_params/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/upsert_named_params/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions internal/endtoend/testdata/upsert_named_params/sqlite/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions internal/endtoend/testdata/upsert_named_params/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- name: UpsertAuthor :one
INSERT INTO authors (
id,
name,
bio
) VALUES (
@id,
@name,
sqlc.narg('bio')
) ON CONFLICT(id) DO UPDATE SET
name = @name,
bio = sqlc.narg('bio')
RETURNING *;

-- name: UpsertAuthorDoUpdateWhere :one
INSERT INTO authors (
id,
name,
bio
) VALUES (
@id,
@name,
sqlc.narg('bio')
) ON CONFLICT(id) DO UPDATE SET
name = @name,
bio = sqlc.narg('bio')
WHERE excluded.name != @name
RETURNING *;

-- name: UpsertAuthorConflictWhere :one
INSERT INTO authors (
id,
name,
bio
) VALUES (
@id,
@name,
sqlc.narg('bio')
) ON CONFLICT(id) DO UPDATE SET
name = @name,
bio = sqlc.narg('bio')
WHERE excluded.bio != sqlc.narg('bio')
RETURNING *;

-- name: UpsertAuthorBothWhere :one
INSERT INTO authors (
id,
name,
bio
) VALUES (
@id,
@name,
sqlc.narg('bio')
) ON CONFLICT(id) DO UPDATE SET
name = @name,
bio = sqlc.narg('bio')
WHERE excluded.name != @name
AND excluded.bio != sqlc.narg('bio')
RETURNING *;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE authors (
id text primary key,
name text not null,
bio text
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/upsert_named_params/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "sqlite",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
60 changes: 60 additions & 0 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,69 @@ func (c *cc) convertInsert_stmtContext(n *parser.Insert_stmtContext) ast.Node {
}
}

insert.OnConflictClause = c.convertUpsert_clauseContext(n.Upsert_clause())

return insert
}

func (c *cc) convertUpsert_clauseContext(n parser.IUpsert_clauseContext) *ast.OnConflictClause {
if n == nil {
return nil
}

// ON CONFLICT DO NOTHING
if n.NOTHING_() != nil {
return &ast.OnConflictClause{
Action: ast.OnConflictActionNothing,
}
}

// Build Infer clause from ON CONFLICT (col, ...) target columns
var infer *ast.InferClause
indexCols := n.AllIndexed_column()
if len(indexCols) > 0 {
indexElems := &ast.List{}
for _, col := range indexCols {
name := identifier(col.GetText())
indexElems.Items = append(indexElems.Items, &ast.IndexElem{
Name: &name,
})
}
infer = &ast.InferClause{
IndexElems: indexElems,
}
}

// Build DO UPDATE SET col = expr target list
targetList := &ast.List{}
cols := n.AllColumn_name()
exprs := n.AllExpr()
for i, col := range cols {
if i >= len(exprs) {
break
}
colName := identifier(col.GetText())
target := &ast.ResTarget{
Name: &colName,
Val: c.convert(exprs[i]),
}
targetList.Items = append(targetList.Items, target)
}

// Handle optional WHERE clause
var whereClause ast.Node
if n.WHERE_(0) != nil && len(exprs) > len(cols) {
whereClause = c.convert(exprs[len(exprs)-1])
}

return &ast.OnConflictClause{
Action: ast.OnConflictActionUpdate,
Infer: infer,
TargetList: targetList,
WhereClause: whereClause,
}
}

func (c *cc) convertColumnNames(cols []parser.IColumn_nameContext) *ast.List {
list := &ast.List{Items: []ast.Node{}}
for _, c := range cols {
Expand Down
Loading