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
3 changes: 3 additions & 0 deletions docs/reference/query-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -212,6 +223,14 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
tctx.SQLDriver = opts.SQLDriverGoSQLDriverMySQL
}

if capabilities.LastInsertIDUnsupportedReason != "" {
for _, q := range queries {
if q.Cmd == metadata.CmdExecLastId {
return nil, fmt.Errorf("query %q: :execlastid %s", q.MethodName, capabilities.LastInsertIDUnsupportedReason)
}
}
}

if tctx.UsesBatch && !tctx.SQLDriver.IsPGX() {
return nil, errors.New(":batch* commands are only supported by pgx")
}
Expand Down
6 changes: 4 additions & 2 deletions internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions internal/compiler/result.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "analyze",
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
"contexts": ["base"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: Styled :many
SELECT CONVERT(nvarchar(30), at, @style) AS styled, CONVERT(varchar(10), at, 23) AS fixed FROM t;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE t (id INT NOT NULL, name NVARCHAR(50) NULL, at DATETIME2 NOT NULL);
42 changes: 42 additions & 0 deletions internal/endtoend/testdata/analyze_convert_style/mssql/stdout.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "analyze",
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
"contexts": ["base"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: TopPercent :many
SELECT TOP (@pct) PERCENT id, name FROM authors ORDER BY id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE authors (
id BIGINT IDENTITY(1,1) PRIMARY KEY,
name NVARCHAR(100) NOT NULL
);
38 changes: 38 additions & 0 deletions internal/endtoend/testdata/analyze_top_percent/mssql/stdout.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"contexts": ["base"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: CopyBooks :copyfrom
INSERT INTO books (id, title) VALUES (@id, @title);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE books (id INT NOT NULL, title NVARCHAR(200) NOT NULL);
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# package querytest
error generating code: :copyfrom is only supported by pgx and github.com/go-sql-driver/mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"contexts": ["base"],
"process": "sqlc-gen-json"
}
Original file line number Diff line number Diff line change
@@ -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": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: CreateBook :execlastid
INSERT INTO books (id) VALUES (1);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE books (id INT NOT NULL);
28 changes: 28 additions & 0 deletions internal/endtoend/testdata/mssql_execlastid_plugin/mssql/sqlc.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
Loading