From 9f1f67aba6f229eb627339069eebcf75d54f5e39 Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:40:47 -0800 Subject: [PATCH 1/8] fix(mssql): report a named query sqlc cannot analyze A statement the converter does not handle, such as MERGE or EXEC, was skipped, so a query named on one silently went missing from the generated code. Skipping is right for a schema, which holds statements sqlc has no use for, but a statement carrying a query name is now reported as unsupported, at the statement. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../mssql_unsupported_statement/mssql/exec.json | 1 + .../mssql_unsupported_statement/mssql/query.sql | 8 ++++++++ .../mssql/schema.sql | 12 ++++++++++++ .../mssql_unsupported_statement/mssql/sqlc.yaml | 10 ++++++++++ .../mssql/stderr.txt | 2 ++ internal/engine/mssql/parse.go | 17 +++++++++++++++++ 6 files changed, 50 insertions(+) create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement/mssql/exec.json create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement/mssql/query.sql create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement/mssql/schema.sql create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement/mssql/sqlc.yaml create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement/mssql/stderr.txt diff --git a/internal/endtoend/testdata/mssql_unsupported_statement/mssql/exec.json b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/exec.json new file mode 100644 index 0000000000..f4e21983b6 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/exec.json @@ -0,0 +1 @@ +{"contexts": ["base"]} diff --git a/internal/endtoend/testdata/mssql_unsupported_statement/mssql/query.sql b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/query.sql new file mode 100644 index 0000000000..00a92a27ec --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/query.sql @@ -0,0 +1,8 @@ +-- name: ListAuthors :many +SELECT id, name FROM authors; + +-- name: UpsertAuthor :exec +MERGE authors AS t +USING (SELECT @id AS id, @name AS name) s ON t.id = s.id +WHEN MATCHED THEN UPDATE SET name = s.name +WHEN NOT MATCHED THEN INSERT (name) VALUES (s.name); diff --git a/internal/endtoend/testdata/mssql_unsupported_statement/mssql/schema.sql b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/schema.sql new file mode 100644 index 0000000000..00557ccd53 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE authors ( + id BIGINT IDENTITY(1,1) PRIMARY KEY, + name NVARCHAR(100) NOT NULL +); + +CREATE TABLE books ( + id BIGINT IDENTITY(1,1) PRIMARY KEY, + author_id BIGINT NOT NULL, + title NVARCHAR(200) NOT NULL +); + +CREATE PROCEDURE list_authors AS SELECT id, name FROM authors; diff --git a/internal/endtoend/testdata/mssql_unsupported_statement/mssql/sqlc.yaml b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/sqlc.yaml new file mode 100644 index 0000000000..94b0dc8896 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "mssql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "database/sql" diff --git a/internal/endtoend/testdata/mssql_unsupported_statement/mssql/stderr.txt b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/stderr.txt new file mode 100644 index 0000000000..531a736e9f --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement/mssql/stderr.txt @@ -0,0 +1,2 @@ +# package querytest +query.sql:5:1: Merge statements are not supported diff --git a/internal/engine/mssql/parse.go b/internal/engine/mssql/parse.go index e222708ece..6007003e97 100644 --- a/internal/engine/mssql/parse.go +++ b/internal/engine/mssql/parse.go @@ -3,13 +3,17 @@ package mssql import ( "bytes" "context" + "fmt" "io" + "regexp" + "strings" "unicode/utf8" "github.com/sqlc-dev/teesql/parser" "github.com/sqlc-dev/sqlc/internal/source" "github.com/sqlc-dev/sqlc/internal/sql/ast" + "github.com/sqlc-dev/sqlc/internal/sql/sqlerr" ) func NewParser() *Parser { @@ -47,6 +51,16 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { converter := &cc{toByte: toByte} out := converter.convert(stmt) if _, ok := out.(*ast.TODO); ok { + // A schema holds statements sqlc has no use for, which are + // skipped. A named query sqlc cannot analyze is an error, + // rather than a query missing from the generated code. + if namedQuery.Match(blob[loc:end]) { + kind := strings.TrimSuffix(strings.TrimPrefix(fmt.Sprintf("%T", stmt), "*ast."), "Statement") + return nil, &sqlerr.Error{ + Message: fmt.Sprintf("%s statements are not supported", kind), + Location: start, + } + } loc = end continue } @@ -65,6 +79,9 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { return stmts, nil } +// namedQuery matches the comment that names a query. +var namedQuery = regexp.MustCompile(`(?m)^\s*--\s*name:\s*\S+`) + // byteOffsets returns a function mapping a UTF-16 code-unit offset — how // teesql records source spans, mirroring ScriptDom — to a byte offset in // blob. For ASCII input the two are identical and the identity is returned. From 0f527029a9453e4af44f69e07fe4f31172ba66ec Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:52:55 -0800 Subject: [PATCH 2/8] fix(mssql): report an unsupported query named in a block comment A named query on a statement sqlc cannot analyze is reported rather than dropped, but the check only recognized a -- name: comment, so a MERGE named with /* name: Foo :exec */, which sqlc also reads, was still dropped silently. Both comment styles are now recognized. Addresses review comment r4557025570 on #1. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../mssql/exec.json | 1 + .../mssql/query.sql | 7 +++++++ .../mssql/schema.sql | 12 ++++++++++++ .../mssql/sqlc.yaml | 10 ++++++++++ .../mssql/stderr.txt | 2 ++ internal/engine/mssql/parse.go | 5 +++-- 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/exec.json create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/query.sql create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/schema.sql create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/sqlc.yaml create mode 100644 internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/stderr.txt diff --git a/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/exec.json b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/exec.json new file mode 100644 index 0000000000..f4e21983b6 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/exec.json @@ -0,0 +1 @@ +{"contexts": ["base"]} diff --git a/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/query.sql b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/query.sql new file mode 100644 index 0000000000..a4c10b1c8b --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/query.sql @@ -0,0 +1,7 @@ +/* name: ListAuthors :many */ +SELECT id, name FROM authors; + +/* name: DeleteAll :exec */ +MERGE authors AS t +USING (SELECT @id AS id) s ON t.id = s.id +WHEN MATCHED THEN DELETE; diff --git a/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/schema.sql b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/schema.sql new file mode 100644 index 0000000000..00557ccd53 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE authors ( + id BIGINT IDENTITY(1,1) PRIMARY KEY, + name NVARCHAR(100) NOT NULL +); + +CREATE TABLE books ( + id BIGINT IDENTITY(1,1) PRIMARY KEY, + author_id BIGINT NOT NULL, + title NVARCHAR(200) NOT NULL +); + +CREATE PROCEDURE list_authors AS SELECT id, name FROM authors; diff --git a/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/sqlc.yaml b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/sqlc.yaml new file mode 100644 index 0000000000..94b0dc8896 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "mssql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "database/sql" diff --git a/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/stderr.txt b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/stderr.txt new file mode 100644 index 0000000000..531a736e9f --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_statement_block_comment/mssql/stderr.txt @@ -0,0 +1,2 @@ +# package querytest +query.sql:5:1: Merge statements are not supported diff --git a/internal/engine/mssql/parse.go b/internal/engine/mssql/parse.go index 6007003e97..be878a43ca 100644 --- a/internal/engine/mssql/parse.go +++ b/internal/engine/mssql/parse.go @@ -79,8 +79,9 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { return stmts, nil } -// namedQuery matches the comment that names a query. -var namedQuery = regexp.MustCompile(`(?m)^\s*--\s*name:\s*\S+`) +// namedQuery matches the comment that names a query, in either comment +// style. +var namedQuery = regexp.MustCompile(`(?m)^\s*(--|/\*)\s*name:\s*\S+`) // byteOffsets returns a function mapping a UTF-16 code-unit offset — how // teesql records source spans, mirroring ScriptDom — to a byte offset in From 982cee5ae2b69806330ce58beded5e820c5b71a6 Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:54:49 -0800 Subject: [PATCH 3/8] fix(mssql): keep the placeholder in TOP (@n) PERCENT TOP n PERCENT was skipped as no row count, which dropped a placeholder in it from the query's parameters, so the generated code did not pass it and the query failed with an undeclared variable. The percentage is now analyzed as the float SQL Server takes it as. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../analyze_top_percent/mssql/exec.json | 5 +++ .../analyze_top_percent/mssql/query.sql | 2 + .../analyze_top_percent/mssql/schema.sql | 4 ++ .../analyze_top_percent/mssql/stdout.json | 38 +++++++++++++++++++ internal/engine/mssql/convert.go | 8 +++- 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 internal/endtoend/testdata/analyze_top_percent/mssql/exec.json create mode 100644 internal/endtoend/testdata/analyze_top_percent/mssql/query.sql create mode 100644 internal/endtoend/testdata/analyze_top_percent/mssql/schema.sql create mode 100644 internal/endtoend/testdata/analyze_top_percent/mssql/stdout.json diff --git a/internal/endtoend/testdata/analyze_top_percent/mssql/exec.json b/internal/endtoend/testdata/analyze_top_percent/mssql/exec.json new file mode 100644 index 0000000000..253b05abbf --- /dev/null +++ b/internal/endtoend/testdata/analyze_top_percent/mssql/exec.json @@ -0,0 +1,5 @@ +{ + "command": "analyze", + "args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"], + "contexts": ["base"] +} diff --git a/internal/endtoend/testdata/analyze_top_percent/mssql/query.sql b/internal/endtoend/testdata/analyze_top_percent/mssql/query.sql new file mode 100644 index 0000000000..82a8380d35 --- /dev/null +++ b/internal/endtoend/testdata/analyze_top_percent/mssql/query.sql @@ -0,0 +1,2 @@ +-- name: TopPercent :many +SELECT TOP (@pct) PERCENT id, name FROM authors ORDER BY id; diff --git a/internal/endtoend/testdata/analyze_top_percent/mssql/schema.sql b/internal/endtoend/testdata/analyze_top_percent/mssql/schema.sql new file mode 100644 index 0000000000..d48c43b40e --- /dev/null +++ b/internal/endtoend/testdata/analyze_top_percent/mssql/schema.sql @@ -0,0 +1,4 @@ +CREATE TABLE authors ( + id BIGINT IDENTITY(1,1) PRIMARY KEY, + name NVARCHAR(100) NOT NULL +); diff --git a/internal/endtoend/testdata/analyze_top_percent/mssql/stdout.json b/internal/endtoend/testdata/analyze_top_percent/mssql/stdout.json new file mode 100644 index 0000000000..deee4f1830 --- /dev/null +++ b/internal/endtoend/testdata/analyze_top_percent/mssql/stdout.json @@ -0,0 +1,38 @@ +[ + { + "name": "TopPercent", + "cmd": ":many", + "columns": [ + { + "name": "id", + "type": { + "name": "bigint" + }, + "table": "authors" + }, + { + "name": "name", + "type": { + "name": "nvarchar", + "args": [ + { + "int": 100 + } + ] + }, + "table": "authors" + } + ], + "params": [ + { + "number": 1, + "column": { + "name": "pct", + "type": { + "name": "float" + } + } + } + ] + } +] diff --git a/internal/engine/mssql/convert.go b/internal/engine/mssql/convert.go index bdef335bc6..7d1a5b2908 100644 --- a/internal/engine/mssql/convert.go +++ b/internal/engine/mssql/convert.go @@ -224,9 +224,13 @@ func (c *cc) convertQuerySpecification(n *tsql.QuerySpecification) *ast.SelectSt } } - // TOP n; TOP n PERCENT does not translate to a row count. - if n.TopRowFilter != nil && !n.TopRowFilter.Percent { + // TOP n. TOP n PERCENT is no row count, but a float a placeholder for + // it is typed as. + if n.TopRowFilter != nil { stmt.LimitCount = c.convertScalarExpression(n.TopRowFilter.Expression) + if n.TopRowFilter.Percent { + stmt.LimitCount = &ast.TypeCast{Arg: stmt.LimitCount, TypeName: &ast.TypeName{Name: "float"}} + } } if n.UniqueRowFilter == "Distinct" { From deffbd5717d6a653362837f0f7bafee0ecbd5a1e Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:57:58 -0800 Subject: [PATCH 4/8] fix(validate): allow an INSERT whose VALUES has no FROM list Checking a :copyfrom query read the FROM list of its INSERT ... VALUES without checking it was there. PostgreSQL's converter gives VALUES an empty one, but SQL Server's leaves it nil, so any :copyfrom query on SQL Server panicked. It now reaches the generator, which reports that :copyfrom is not supported for the driver. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../mssql_copyfrom_unsupported/mssql/exec.json | 1 + .../mssql_copyfrom_unsupported/mssql/query.sql | 2 ++ .../mssql_copyfrom_unsupported/mssql/schema.sql | 1 + .../mssql_copyfrom_unsupported/mssql/sqlc.yaml | 10 ++++++++++ .../mssql_copyfrom_unsupported/mssql/stderr.txt | 2 ++ internal/sql/validate/cmd.go | 2 +- 6 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/exec.json create mode 100644 internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/query.sql create mode 100644 internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/schema.sql create mode 100644 internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/sqlc.yaml create mode 100644 internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/stderr.txt diff --git a/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/exec.json b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/exec.json new file mode 100644 index 0000000000..f4e21983b6 --- /dev/null +++ b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/exec.json @@ -0,0 +1 @@ +{"contexts": ["base"]} diff --git a/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/query.sql b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/query.sql new file mode 100644 index 0000000000..8d1d2a89ef --- /dev/null +++ b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/query.sql @@ -0,0 +1,2 @@ +-- name: CopyBooks :copyfrom +INSERT INTO books (id, title) VALUES (@id, @title); diff --git a/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/schema.sql b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/schema.sql new file mode 100644 index 0000000000..9e32b120d1 --- /dev/null +++ b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/schema.sql @@ -0,0 +1 @@ +CREATE TABLE books (id INT NOT NULL, title NVARCHAR(200) NOT NULL); diff --git a/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/sqlc.yaml b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/sqlc.yaml new file mode 100644 index 0000000000..94b0dc8896 --- /dev/null +++ b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "mssql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "database/sql" diff --git a/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/stderr.txt b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/stderr.txt new file mode 100644 index 0000000000..3509d2bee5 --- /dev/null +++ b/internal/endtoend/testdata/mssql_copyfrom_unsupported/mssql/stderr.txt @@ -0,0 +1,2 @@ +# package querytest +error generating code: :copyfrom is only supported by pgx and github.com/go-sql-driver/mysql diff --git a/internal/sql/validate/cmd.go b/internal/sql/validate/cmd.go index e9d9dd03cb..0837965405 100644 --- a/internal/sql/validate/cmd.go +++ b/internal/sql/validate/cmd.go @@ -27,7 +27,7 @@ func validateCopyfrom(n ast.Node) error { if !ok { return nil } - if len(sel.FromClause.Items) > 0 { + if sel.FromClause != nil && len(sel.FromClause.Items) > 0 { return errors.New(":copyfrom is not compatible with INSERT INTO ... SELECT") } if sel.ValuesLists == nil || len(sel.ValuesLists.Items) != 1 { From 4c03a9a899abd899b2a77e25dad407e073a0b87a Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:59:23 -0800 Subject: [PATCH 5/8] fix(golang): reject :execlastid for SQL Server go-mssqldb never reports a last insert ID: its LastInsertId always returns an error, so the code generated for an :execlastid query failed every time it ran. Generating one is now an error that points at the OUTPUT INSERTED clause SQL Server returns the ID with. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/reference/query-annotations.md | 3 +++ internal/codegen/golang/gen.go | 10 ++++++++++ .../mssql_execlastid_unsupported/mssql/exec.json | 1 + .../mssql_execlastid_unsupported/mssql/query.sql | 2 ++ .../mssql_execlastid_unsupported/mssql/schema.sql | 1 + .../mssql_execlastid_unsupported/mssql/sqlc.yaml | 10 ++++++++++ .../mssql_execlastid_unsupported/mssql/stderr.txt | 2 ++ 7 files changed, 29 insertions(+) create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/exec.json create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/query.sql create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/schema.sql create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/sqlc.yaml create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/stderr.txt diff --git a/docs/reference/query-annotations.md b/docs/reference/query-annotations.md index 4fabe05aae..57eed12d4c 100644 --- a/docs/reference/query-annotations.md +++ b/docs/reference/query-annotations.md @@ -77,6 +77,9 @@ func (q *Queries) InsertAuthor(ctx context.Context, name string) (int64, error) } ``` +SQL Server's driver reports no last insert ID, so `:execlastid` is not +supported there. Return the ID with `OUTPUT INSERTED.id` and `:one` instead. + ## `:many` The generated method will return a slice of records via diff --git a/internal/codegen/golang/gen.go b/internal/codegen/golang/gen.go index 725f5970cf..343bc123db 100644 --- a/internal/codegen/golang/gen.go +++ b/internal/codegen/golang/gen.go @@ -212,6 +212,16 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, tctx.SQLDriver = opts.SQLDriverGoSQLDriverMySQL } + // go-mssqldb never reports a last insert ID; SQL Server returns one with + // OUTPUT INSERTED. + if req.Settings.Engine == engineMSSQL { + for _, q := range queries { + if q.Cmd == metadata.CmdExecLastId { + return nil, fmt.Errorf("query %q: :execlastid is not supported for SQL Server, whose driver reports no last insert ID; use OUTPUT INSERTED. with :one", q.MethodName) + } + } + } + if tctx.UsesBatch && !tctx.SQLDriver.IsPGX() { return nil, errors.New(":batch* commands are only supported by pgx") } diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/exec.json b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/exec.json new file mode 100644 index 0000000000..f4e21983b6 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/exec.json @@ -0,0 +1 @@ +{"contexts": ["base"]} diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/query.sql b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/query.sql new file mode 100644 index 0000000000..d1caf2a305 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/query.sql @@ -0,0 +1,2 @@ +-- name: CreateBook :execlastid +INSERT INTO books (id, title) VALUES (@id, @title); diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/schema.sql b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/schema.sql new file mode 100644 index 0000000000..9e32b120d1 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/schema.sql @@ -0,0 +1 @@ +CREATE TABLE books (id INT NOT NULL, title NVARCHAR(200) NOT NULL); diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/sqlc.yaml b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/sqlc.yaml new file mode 100644 index 0000000000..94b0dc8896 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "mssql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "database/sql" diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/stderr.txt b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/stderr.txt new file mode 100644 index 0000000000..73b2d2edf0 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/mssql/stderr.txt @@ -0,0 +1,2 @@ +# package querytest +error generating code: query "CreateBook": :execlastid is not supported for SQL Server, whose driver reports no last insert ID; use OUTPUT INSERTED. with :one From aef45f655deec337f4f8702710de6494a68c9162 Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:28:29 -0800 Subject: [PATCH 6/8] fix(mssql): report a placeholder the converter drops A placeholder in a construct the converter does not follow, as TABLESAMPLE (@n PERCENT) or CONVERT's style, was dropped from the query's parameters, generating code that passed the query too few arguments. Every @name in a named query's text, outside strings, quoted names and comments, must now be one of its parameters, and one that is not is reported where it is. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../mssql/exec.json | 1 + .../mssql/query.sql | 5 ++ .../mssql/schema.sql | 1 + .../mssql/sqlc.yaml | 10 +++ .../mssql/stderr.txt | 2 + internal/engine/mssql/parse.go | 76 +++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/exec.json create mode 100644 internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/query.sql create mode 100644 internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/schema.sql create mode 100644 internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/sqlc.yaml create mode 100644 internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/stderr.txt diff --git a/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/exec.json b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/exec.json new file mode 100644 index 0000000000..f4e21983b6 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/exec.json @@ -0,0 +1 @@ +{"contexts": ["base"]} diff --git a/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/query.sql b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/query.sql new file mode 100644 index 0000000000..6376e1f909 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/query.sql @@ -0,0 +1,5 @@ +-- name: Ids :many +SELECT id FROM t WHERE name = '@not_a_param' /* @nor_this */ AND id > @min; + +-- name: Sample :many +SELECT id FROM t WITH (INDEX(@ix)); diff --git a/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/schema.sql b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/schema.sql new file mode 100644 index 0000000000..15ae02025c --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/schema.sql @@ -0,0 +1 @@ +CREATE TABLE t (id INT NOT NULL, name NVARCHAR(50) NULL); diff --git a/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/sqlc.yaml b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/sqlc.yaml new file mode 100644 index 0000000000..94b0dc8896 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "mssql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "database/sql" diff --git a/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/stderr.txt b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/stderr.txt new file mode 100644 index 0000000000..8303d18af0 --- /dev/null +++ b/internal/endtoend/testdata/mssql_unsupported_placeholder/mssql/stderr.txt @@ -0,0 +1,2 @@ +# package querytest +query.sql:5:30: placeholder @ix is not supported where it is used diff --git a/internal/engine/mssql/parse.go b/internal/engine/mssql/parse.go index be878a43ca..3d6d9d5f56 100644 --- a/internal/engine/mssql/parse.go +++ b/internal/engine/mssql/parse.go @@ -65,6 +65,12 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { continue } + if namedQuery.Match(blob[loc:end]) { + if err := checkPlaceholders(blob, start, end, converter.namedParams); err != nil { + return nil, err + } + } + stmts = append(stmts, ast.Statement{ Raw: &ast.RawStmt{ Stmt: out, @@ -83,6 +89,76 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { // style. var namedQuery = regexp.MustCompile(`(?m)^\s*(--|/\*)\s*name:\s*\S+`) +// checkPlaceholders reports a placeholder in a query's text that did not +// become one of its parameters, which a construct the converter does not +// follow drops, leaving generated code that passes the query too few +// arguments. +func checkPlaceholders(blob []byte, start, end int, params map[string]namedParam) error { + for _, p := range placeholders(blob[start:end]) { + if _, ok := params[strings.ToLower(p.name)]; !ok { + return &sqlerr.Error{ + Message: fmt.Sprintf("placeholder @%s is not supported where it is used", p.name), + Location: start + p.offset, + } + } + } + return nil +} + +type placeholder struct { + name string + offset int +} + +// placeholders are the @name placeholders in sql, leaving out @@ system +// variables and anything inside a string, a quoted or bracketed +// identifier or a comment. +func placeholders(sql []byte) []placeholder { + var out []placeholder + skipTo := func(i int, close string) int { + if j := bytes.Index(sql[i:], []byte(close)); j >= 0 { + return i + j + len(close) + } + return len(sql) + } + for i := 0; i < len(sql); { + switch c := sql[i]; { + case c == '\'' || c == '"': + // A doubled quote inside is an escaped one, which the scan + // steps over as the end of one string and the start of the + // next. + i = skipTo(i+1, string(c)) + case c == '[': + i = skipTo(i+1, "]") + case c == '-' && i+1 < len(sql) && sql[i+1] == '-': + i = skipTo(i, "\n") + case c == '/' && i+1 < len(sql) && sql[i+1] == '*': + i = skipTo(i+2, "*/") + case c == '@': + j := i + 1 + for j < len(sql) && isNameByte(sql[j]) { + j++ + } + if i+1 < len(sql) && sql[i+1] == '@' { + i = j + continue + } + if j > i+1 { + out = append(out, placeholder{name: string(sql[i+1 : j]), offset: i}) + } + i = j + default: + i++ + } + } + return out +} + +func isNameByte(b byte) bool { + return b == '_' || b == '#' || b == '$' || b == '@' || + 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' || '0' <= b && b <= '9' || b >= utf8.RuneSelf +} + // byteOffsets returns a function mapping a UTF-16 code-unit offset — how // teesql records source spans, mirroring ScriptDom — to a byte offset in // blob. For ASCII input the two are identical and the identity is returned. From 3908e041dc761077669c522f72334bac57bb1cad Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:31:00 -0800 Subject: [PATCH 7/8] fix(mssql): keep the placeholder in CONVERT's style A style given as a placeholder, as CONVERT(nvarchar(30), at, @style), was dropped. It is now passed as an int, through a function returning the converted value, which may then be NULL; a literal style is left as it was. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../analyze_convert_style/mssql/exec.json | 5 +++ .../analyze_convert_style/mssql/query.sql | 2 + .../analyze_convert_style/mssql/schema.sql | 1 + .../analyze_convert_style/mssql/stdout.json | 42 +++++++++++++++++++ internal/engine/mssql/convert.go | 12 +++++- internal/engine/mssql/dialect/functions.jsonl | 1 + 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 internal/endtoend/testdata/analyze_convert_style/mssql/exec.json create mode 100644 internal/endtoend/testdata/analyze_convert_style/mssql/query.sql create mode 100644 internal/endtoend/testdata/analyze_convert_style/mssql/schema.sql create mode 100644 internal/endtoend/testdata/analyze_convert_style/mssql/stdout.json diff --git a/internal/endtoend/testdata/analyze_convert_style/mssql/exec.json b/internal/endtoend/testdata/analyze_convert_style/mssql/exec.json new file mode 100644 index 0000000000..253b05abbf --- /dev/null +++ b/internal/endtoend/testdata/analyze_convert_style/mssql/exec.json @@ -0,0 +1,5 @@ +{ + "command": "analyze", + "args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"], + "contexts": ["base"] +} diff --git a/internal/endtoend/testdata/analyze_convert_style/mssql/query.sql b/internal/endtoend/testdata/analyze_convert_style/mssql/query.sql new file mode 100644 index 0000000000..3e4f34f892 --- /dev/null +++ b/internal/endtoend/testdata/analyze_convert_style/mssql/query.sql @@ -0,0 +1,2 @@ +-- name: Styled :many +SELECT CONVERT(nvarchar(30), at, @style) AS styled, CONVERT(varchar(10), at, 23) AS fixed FROM t; diff --git a/internal/endtoend/testdata/analyze_convert_style/mssql/schema.sql b/internal/endtoend/testdata/analyze_convert_style/mssql/schema.sql new file mode 100644 index 0000000000..64963bfc13 --- /dev/null +++ b/internal/endtoend/testdata/analyze_convert_style/mssql/schema.sql @@ -0,0 +1 @@ +CREATE TABLE t (id INT NOT NULL, name NVARCHAR(50) NULL, at DATETIME2 NOT NULL); diff --git a/internal/endtoend/testdata/analyze_convert_style/mssql/stdout.json b/internal/endtoend/testdata/analyze_convert_style/mssql/stdout.json new file mode 100644 index 0000000000..e8ba8dfda0 --- /dev/null +++ b/internal/endtoend/testdata/analyze_convert_style/mssql/stdout.json @@ -0,0 +1,42 @@ +[ + { + "name": "Styled", + "cmd": ":many", + "columns": [ + { + "name": "styled", + "type": { + "name": "nvarchar", + "nullable": true, + "args": [ + { + "int": 30 + } + ] + } + }, + { + "name": "fixed", + "type": { + "name": "varchar", + "args": [ + { + "int": 10 + } + ] + } + } + ], + "params": [ + { + "number": 1, + "column": { + "name": "style", + "type": { + "name": "int" + } + } + } + ] + } +] diff --git a/internal/engine/mssql/convert.go b/internal/engine/mssql/convert.go index 7d1a5b2908..e4d29eeac9 100644 --- a/internal/engine/mssql/convert.go +++ b/internal/engine/mssql/convert.go @@ -565,8 +565,18 @@ func (c *cc) convertScalarExpression(expr tsql.ScalarExpression) ast.Node { Location: c.loc(e), } case *tsql.ConvertCall: + arg := c.convertScalarExpression(e.Parameter) + // A style given as other than a literal, as @style, is an int + // passed through "convert style", which may return NULL. + if _, literal := e.Style.(*tsql.IntegerLiteral); e.Style != nil && !literal { + arg = &ast.FuncCall{ + Funcname: &ast.List{Items: []ast.Node{&ast.String{Str: "convert style"}}}, + Args: &ast.List{Items: []ast.Node{arg, &ast.TypeCast{Arg: c.convertScalarExpression(e.Style), TypeName: &ast.TypeName{Name: "int"}}}}, + Location: c.loc(e), + } + } return &ast.TypeCast{ - Arg: c.convertScalarExpression(e.Parameter), + Arg: arg, TypeName: dataTypeName(e.DataType), Location: c.loc(e), } diff --git a/internal/engine/mssql/dialect/functions.jsonl b/internal/engine/mssql/dialect/functions.jsonl index e0cd45408c..72d3a5db86 100644 --- a/internal/engine/mssql/dialect/functions.jsonl +++ b/internal/engine/mssql/dialect/functions.jsonl @@ -74,3 +74,4 @@ {"name": "rtrim", "args": [{"type": "nvarchar"}], "returns": "nvarchar"} {"name": "trim", "args": [{"type": "varchar"}], "returns": "varchar"} {"name": "trim", "args": [{"type": "nvarchar"}], "returns": "nvarchar"} +{"name": "convert style", "args": [{"type": "any"}, {"type": "int"}], "returns": "$1", "nullable": true} From 41ae5fab4b448f35fa4e1164c96733e0f06c7b23 Mon Sep 17 00:00:00 2001 From: EliSauder <24995216+EliSauder@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:47:26 -0800 Subject: [PATCH 8/8] refactor(mssql): supply driver execution capabilities to Go generation Expose optional engine-owned driver capabilities through compiler results. Apply LastInsertId restrictions only to built-in Go generation, preserving other engines and external generators. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- internal/cmd/generate.go | 6 +- internal/codegen/golang/gen.go | 21 +++- internal/compiler/compile.go | 6 +- internal/compiler/result.go | 6 +- .../mssql_execlastid_plugin/mssql/exec.json | 4 + .../mssql/gen/codegen.json | 118 ++++++++++++++++++ .../mssql_execlastid_plugin/mssql/query.sql | 2 + .../mssql_execlastid_plugin/mssql/schema.sql | 1 + .../mssql_execlastid_plugin/mssql/sqlc.json | 28 +++++ .../default_package/exec.json | 3 + .../default_package/query.sql | 2 + .../default_package/schema.sql | 1 + .../default_package/sqlc.yaml | 9 ++ .../default_package/stderr.txt | 2 + internal/engine/mssql/capabilities.go | 17 +++ internal/opts/execution.go | 14 +++ 16 files changed, 229 insertions(+), 11 deletions(-) create mode 100644 internal/endtoend/testdata/mssql_execlastid_plugin/mssql/exec.json create mode 100644 internal/endtoend/testdata/mssql_execlastid_plugin/mssql/gen/codegen.json create mode 100644 internal/endtoend/testdata/mssql_execlastid_plugin/mssql/query.sql create mode 100644 internal/endtoend/testdata/mssql_execlastid_plugin/mssql/schema.sql create mode 100644 internal/endtoend/testdata/mssql_execlastid_plugin/mssql/sqlc.json create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/exec.json create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/query.sql create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/schema.sql create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/sqlc.yaml create mode 100644 internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/stderr.txt create mode 100644 internal/engine/mssql/capabilities.go create mode 100644 internal/opts/execution.go diff --git a/internal/cmd/generate.go b/internal/cmd/generate.go index fbdb084409..d8af330261 100644 --- a/internal/cmd/generate.go +++ b/internal/cmd/generate.go @@ -343,7 +343,11 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql OutputPair, case sql.Gen.Go != nil: out = combo.Go.Out - handler = ext.HandleFunc(golang.Generate) + generator := golang.Generator{} + if result.ExecutionCapabilities != nil { + generator.ExecutionCapabilities = result.ExecutionCapabilities.ExecutionCapabilities(sql.Gen.Go.SqlPackage, sql.Gen.Go.SqlDriver) + } + handler = ext.HandleFunc(generator.Generate) opts, err := json.Marshal(sql.Gen.Go) if err != nil { return "", nil, fmt.Errorf("opts marshal failed: %w", err) diff --git a/internal/codegen/golang/gen.go b/internal/codegen/golang/gen.go index 343bc123db..261d8d70b6 100644 --- a/internal/codegen/golang/gen.go +++ b/internal/codegen/golang/gen.go @@ -14,6 +14,7 @@ import ( "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" "github.com/sqlc-dev/sqlc/internal/codegen/sdk" "github.com/sqlc-dev/sqlc/internal/metadata" + sqlopts "github.com/sqlc-dev/sqlc/internal/opts" "github.com/sqlc-dev/sqlc/internal/plugin" ) @@ -110,7 +111,17 @@ func (t *tmplCtx) codegenQueryRetval(q Query) (string, error) { } } +// Generator receives execution restrictions from the engine/driver integration, +// rather than deciding them from the request's engine name. +type Generator struct { + ExecutionCapabilities sqlopts.ExecutionCapabilities +} + func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) { + return (Generator{}).Generate(ctx, req) +} + +func (g Generator) Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) { options, err := opts.Parse(req) if err != nil { return nil, err @@ -138,7 +149,7 @@ func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.Generat return nil, err } - return generate(req, options, enums, structs, queries) + return generate(req, options, enums, structs, queries, g.ExecutionCapabilities) } func validate(options *opts.Options, enums []Enum, structs []Struct, queries []Query) error { @@ -168,7 +179,7 @@ func validate(options *opts.Options, enums []Enum, structs []Struct, queries []Q return nil } -func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.GenerateResponse, error) { +func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query, capabilities sqlopts.ExecutionCapabilities) (*plugin.GenerateResponse, error) { i := &importer{ Options: options, Engine: req.Settings.Engine, @@ -212,12 +223,10 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, tctx.SQLDriver = opts.SQLDriverGoSQLDriverMySQL } - // go-mssqldb never reports a last insert ID; SQL Server returns one with - // OUTPUT INSERTED. - if req.Settings.Engine == engineMSSQL { + if capabilities.LastInsertIDUnsupportedReason != "" { for _, q := range queries { if q.Cmd == metadata.CmdExecLastId { - return nil, fmt.Errorf("query %q: :execlastid is not supported for SQL Server, whose driver reports no last insert ID; use OUTPUT INSERTED. with :one", q.MethodName) + return nil, fmt.Errorf("query %q: :execlastid %s", q.MethodName, capabilities.LastInsertIDUnsupportedReason) } } } diff --git a/internal/compiler/compile.go b/internal/compiler/compile.go index b6f703d1cb..478c3e539f 100644 --- a/internal/compiler/compile.go +++ b/internal/compiler/compile.go @@ -240,9 +240,11 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) { return nil, fmt.Errorf("no queries contained in paths %s", strings.Join(c.conf.Queries, ",")) } + capabilities, _ := c.parser.(opts.ExecutionCapabilitiesProvider) return &Result{ - Catalog: c.catalog, - Queries: q, + ExecutionCapabilities: capabilities, + Catalog: c.catalog, + Queries: q, }, nil } diff --git a/internal/compiler/result.go b/internal/compiler/result.go index 3647da630f..80273e9e75 100644 --- a/internal/compiler/result.go +++ b/internal/compiler/result.go @@ -1,10 +1,12 @@ package compiler import ( + "github.com/sqlc-dev/sqlc/internal/opts" "github.com/sqlc-dev/sqlc/internal/sql/catalog" ) type Result struct { - Catalog *catalog.Catalog - Queries []*Query + Catalog *catalog.Catalog + Queries []*Query + ExecutionCapabilities opts.ExecutionCapabilitiesProvider } diff --git a/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/exec.json b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/exec.json new file mode 100644 index 0000000000..e38046cf57 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/exec.json @@ -0,0 +1,4 @@ +{ + "contexts": ["base"], + "process": "sqlc-gen-json" +} diff --git a/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/gen/codegen.json b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/gen/codegen.json new file mode 100644 index 0000000000..78f6da7218 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/gen/codegen.json @@ -0,0 +1,118 @@ +{ + "settings": { + "version": "2", + "engine": "mssql", + "schema": [ + "schema.sql" + ], + "queries": [ + "query.sql" + ], + "codegen": { + "out": "gen", + "plugin": "jsonb", + "options": "eyJmaWxlbmFtZSI6ImNvZGVnZW4uanNvbiIsImluZGVudCI6IiAgIn0=", + "env": [], + "process": { + "cmd": "sqlc-gen-json" + }, + "wasm": null + } + }, + "catalog": { + "comment": "", + "default_schema": "public", + "name": "", + "schemas": [ + { + "comment": "", + "name": "public", + "tables": [], + "enums": [], + "composite_types": [] + }, + { + "comment": "", + "name": "public", + "tables": [ + { + "rel": { + "catalog": "", + "schema": "public", + "name": "books" + }, + "columns": [ + { + "name": "id", + "not_null": true, + "is_array": false, + "comment": "", + "length": -1, + "is_named_param": false, + "is_func_call": false, + "scope": "", + "table": { + "catalog": "", + "schema": "public", + "name": "books" + }, + "table_alias": "", + "type": { + "catalog": "", + "schema": "", + "name": "int" + }, + "is_sqlc_slice": false, + "embed_table": null, + "original_name": "", + "unsigned": false, + "array_dims": 0, + "type_expr": { + "name": "int", + "nullable": false, + "args": [] + } + } + ], + "comment": "" + } + ], + "enums": [], + "composite_types": [] + }, + { + "comment": "", + "name": "sys", + "tables": [], + "enums": [], + "composite_types": [] + }, + { + "comment": "", + "name": "information_schema", + "tables": [], + "enums": [], + "composite_types": [] + } + ] + }, + "queries": [ + { + "text": "INSERT INTO books (id) VALUES (1);", + "name": "CreateBook", + "cmd": ":execlastid", + "columns": [], + "params": [], + "comments": [], + "filename": "query.sql", + "insert_into_table": { + "catalog": "", + "schema": "", + "name": "books" + } + } + ], + "sqlc_version": "v1.31.1", + "plugin_options": "eyJmaWxlbmFtZSI6ImNvZGVnZW4uanNvbiIsImluZGVudCI6IiAgIn0=", + "global_options": "" +} diff --git a/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/query.sql b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/query.sql new file mode 100644 index 0000000000..ef81004a63 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/query.sql @@ -0,0 +1,2 @@ +-- name: CreateBook :execlastid +INSERT INTO books (id) VALUES (1); diff --git a/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/schema.sql b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/schema.sql new file mode 100644 index 0000000000..5111ae138e --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/schema.sql @@ -0,0 +1 @@ +CREATE TABLE books (id INT NOT NULL); diff --git a/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/sqlc.json b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/sqlc.json new file mode 100644 index 0000000000..652d367695 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_plugin/mssql/sqlc.json @@ -0,0 +1,28 @@ +{ + "version": "2", + "sql": [ + { + "engine": "mssql", + "schema": "schema.sql", + "queries": "query.sql", + "codegen": [ + { + "out": "gen", + "plugin": "jsonb", + "options": { + "indent": " ", + "filename": "codegen.json" + } + } + ] + } + ], + "plugins": [ + { + "name": "jsonb", + "process": { + "cmd": "sqlc-gen-json" + } + } + ] +} diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/exec.json b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/exec.json new file mode 100644 index 0000000000..2e996ca79d --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/exec.json @@ -0,0 +1,3 @@ +{ + "contexts": ["base"] +} diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/query.sql b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/query.sql new file mode 100644 index 0000000000..ef81004a63 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/query.sql @@ -0,0 +1,2 @@ +-- name: CreateBook :execlastid +INSERT INTO books (id) VALUES (1); diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/schema.sql b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/schema.sql new file mode 100644 index 0000000000..5111ae138e --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/schema.sql @@ -0,0 +1 @@ +CREATE TABLE books (id INT NOT NULL); diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/sqlc.yaml b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/sqlc.yaml new file mode 100644 index 0000000000..3ccfc8f56a --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/sqlc.yaml @@ -0,0 +1,9 @@ +version: "2" +sql: + - engine: "mssql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" diff --git a/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/stderr.txt b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/stderr.txt new file mode 100644 index 0000000000..73b2d2edf0 --- /dev/null +++ b/internal/endtoend/testdata/mssql_execlastid_unsupported/default_package/stderr.txt @@ -0,0 +1,2 @@ +# package querytest +error generating code: query "CreateBook": :execlastid is not supported for SQL Server, whose driver reports no last insert ID; use OUTPUT INSERTED. with :one diff --git a/internal/engine/mssql/capabilities.go b/internal/engine/mssql/capabilities.go new file mode 100644 index 0000000000..d443b52716 --- /dev/null +++ b/internal/engine/mssql/capabilities.go @@ -0,0 +1,17 @@ +package mssql + +import "github.com/sqlc-dev/sqlc/internal/opts" + +func (p *Parser) ExecutionCapabilities(sqlPackage, sqlDriver string) opts.ExecutionCapabilities { + if sqlPackage != "" && sqlPackage != "database/sql" { + return opts.ExecutionCapabilities{} + } + if sqlDriver != "" && sqlDriver != "github.com/microsoft/go-mssqldb" { + return opts.ExecutionCapabilities{} + } + // The default Go binding uses go-mssqldb, whose LastInsertId always + // errors. Other bindings may return identities through a different API. + return opts.ExecutionCapabilities{ + LastInsertIDUnsupportedReason: "is not supported for SQL Server, whose driver reports no last insert ID; use OUTPUT INSERTED. with :one", + } +} diff --git a/internal/opts/execution.go b/internal/opts/execution.go new file mode 100644 index 0000000000..9da96b1d57 --- /dev/null +++ b/internal/opts/execution.go @@ -0,0 +1,14 @@ +package opts + +// ExecutionCapabilities describes restrictions of a particular driver binding. +// Empty fields retain the generator's historical behavior. +type ExecutionCapabilities struct { + LastInsertIDUnsupportedReason string +} + +// ExecutionCapabilitiesProvider is optional: engines without one do not add +// restrictions. Package and driver describe the selected execution binding, +// not the database's ability to express equivalent SQL. +type ExecutionCapabilitiesProvider interface { + ExecutionCapabilities(sqlPackage, sqlDriver string) ExecutionCapabilities +}