diff --git a/internal/endtoend/testdata/upsert_named_params/sqlite/go/db.go b/internal/endtoend/testdata/upsert_named_params/sqlite/go/db.go new file mode 100644 index 0000000000..80dd6ab1f6 --- /dev/null +++ b/internal/endtoend/testdata/upsert_named_params/sqlite/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/upsert_named_params/sqlite/go/models.go b/internal/endtoend/testdata/upsert_named_params/sqlite/go/models.go new file mode 100644 index 0000000000..9841d6e811 --- /dev/null +++ b/internal/endtoend/testdata/upsert_named_params/sqlite/go/models.go @@ -0,0 +1,15 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 + +package querytest + +import ( + "database/sql" +) + +type Author struct { + ID string + Name string + Bio sql.NullString +} diff --git a/internal/endtoend/testdata/upsert_named_params/sqlite/go/query.sql.go b/internal/endtoend/testdata/upsert_named_params/sqlite/go/query.sql.go new file mode 100644 index 0000000000..c447fe2192 --- /dev/null +++ b/internal/endtoend/testdata/upsert_named_params/sqlite/go/query.sql.go @@ -0,0 +1,127 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" +) + +const upsertAuthor = `-- name: UpsertAuthor :one +INSERT INTO authors ( + id, + name, + bio +) VALUES ( + ?1, + ?2, + ?3 +) ON CONFLICT(id) DO UPDATE SET + name = ?2, + bio = ?3 +RETURNING id, name, bio +` + +type UpsertAuthorParams struct { + ID string + Name string + Bio sql.NullString +} + +func (q *Queries) UpsertAuthor(ctx context.Context, arg UpsertAuthorParams) (Author, error) { + row := q.db.QueryRowContext(ctx, upsertAuthor, arg.ID, arg.Name, arg.Bio) + var i Author + err := row.Scan(&i.ID, &i.Name, &i.Bio) + return i, err +} + +const upsertAuthorBothWhere = `-- name: UpsertAuthorBothWhere :one +INSERT INTO authors ( + id, + name, + bio +) VALUES ( + ?1, + ?2, + ?3 +) ON CONFLICT(id) DO UPDATE SET + name = ?2, + bio = ?3 +WHERE excluded.name != ?2 +AND excluded.bio != ?3 +RETURNING id, name, bio +` + +type UpsertAuthorBothWhereParams struct { + ID string + Name string + Bio sql.NullString +} + +func (q *Queries) UpsertAuthorBothWhere(ctx context.Context, arg UpsertAuthorBothWhereParams) (Author, error) { + row := q.db.QueryRowContext(ctx, upsertAuthorBothWhere, arg.ID, arg.Name, arg.Bio) + var i Author + err := row.Scan(&i.ID, &i.Name, &i.Bio) + return i, err +} + +const upsertAuthorConflictWhere = `-- name: UpsertAuthorConflictWhere :one +INSERT INTO authors ( + id, + name, + bio +) VALUES ( + ?1, + ?2, + ?3 +) ON CONFLICT(id) DO UPDATE SET + name = ?2, + bio = ?3 +WHERE excluded.bio != ?3 +RETURNING id, name, bio +` + +type UpsertAuthorConflictWhereParams struct { + ID string + Name string + Bio sql.NullString +} + +func (q *Queries) UpsertAuthorConflictWhere(ctx context.Context, arg UpsertAuthorConflictWhereParams) (Author, error) { + row := q.db.QueryRowContext(ctx, upsertAuthorConflictWhere, arg.ID, arg.Name, arg.Bio) + var i Author + err := row.Scan(&i.ID, &i.Name, &i.Bio) + return i, err +} + +const upsertAuthorDoUpdateWhere = `-- name: UpsertAuthorDoUpdateWhere :one +INSERT INTO authors ( + id, + name, + bio +) VALUES ( + ?1, + ?2, + ?3 +) ON CONFLICT(id) DO UPDATE SET + name = ?2, + bio = ?3 +WHERE excluded.name != ?2 +RETURNING id, name, bio +` + +type UpsertAuthorDoUpdateWhereParams struct { + ID string + Name string + Bio sql.NullString +} + +func (q *Queries) UpsertAuthorDoUpdateWhere(ctx context.Context, arg UpsertAuthorDoUpdateWhereParams) (Author, error) { + row := q.db.QueryRowContext(ctx, upsertAuthorDoUpdateWhere, arg.ID, arg.Name, arg.Bio) + var i Author + err := row.Scan(&i.ID, &i.Name, &i.Bio) + return i, err +} diff --git a/internal/endtoend/testdata/upsert_named_params/sqlite/query.sql b/internal/endtoend/testdata/upsert_named_params/sqlite/query.sql new file mode 100644 index 0000000000..d39513945b --- /dev/null +++ b/internal/endtoend/testdata/upsert_named_params/sqlite/query.sql @@ -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 *; diff --git a/internal/endtoend/testdata/upsert_named_params/sqlite/schema.sql b/internal/endtoend/testdata/upsert_named_params/sqlite/schema.sql new file mode 100644 index 0000000000..4fd8a23bee --- /dev/null +++ b/internal/endtoend/testdata/upsert_named_params/sqlite/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id text primary key, + name text not null, + bio text +); diff --git a/internal/endtoend/testdata/upsert_named_params/sqlite/sqlc.json b/internal/endtoend/testdata/upsert_named_params/sqlite/sqlc.json new file mode 100644 index 0000000000..1f9d43df5d --- /dev/null +++ b/internal/endtoend/testdata/upsert_named_params/sqlite/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "engine": "sqlite", + "path": "go", + "name": "querytest", + "schema": "schema.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/engine/sqlite/convert.go b/internal/engine/sqlite/convert.go index e9868f5be6..53dcc6f2dc 100644 --- a/internal/engine/sqlite/convert.go +++ b/internal/engine/sqlite/convert.go @@ -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 {