diff --git a/internal/cmd/shim.go b/internal/cmd/shim.go index 9987a1783d..355003667b 100644 --- a/internal/cmd/shim.go +++ b/internal/cmd/shim.go @@ -255,9 +255,13 @@ func pluginTypeArg(a core.TypeArg) *plugin.TypeArg { } func pluginQueryParam(p compiler.Parameter) *plugin.Parameter { + col := pluginQueryColumn(p.Column) + if col.Name == "" { + col.Name = p.InferredName + } return &plugin.Parameter{ Number: int32(p.Number), - Column: pluginQueryColumn(p.Column), + Column: col, Name: p.Name, } } diff --git a/internal/compiler/parse_core.go b/internal/compiler/parse_core.go index 2e1dfc7042..e428b350b5 100644 --- a/internal/compiler/parse_core.go +++ b/internal/compiler/parse_core.go @@ -68,8 +68,14 @@ func (c *Compiler) parseQueryCore(raw *ast.RawStmt, src string, pre *preprocess. if err != nil { return nil, err } + limits := limitParamNames(raw) for _, p := range res.Parameters { - params = append(params, Parameter{Number: p.Number, Column: coreParamColumn(p, namedParams), Name: placeholders[p.Number]}) + params = append(params, Parameter{ + Number: p.Number, + Column: coreParamColumn(p, namedParams), + Name: placeholders[p.Number], + InferredName: limits[p.Number], + }) } expanded, err = source.Mutate(rawSQL, c.expandCore(raw, res.Stars)) if err != nil { @@ -176,6 +182,35 @@ func placeholderNames(root ast.Node) (map[int]string, error) { return names, nil } +// limitParamNames maps the number of each placeholder that is a whole LIMIT +// or OFFSET to "limit" or "offset", the names the legacy compiler gives them. +// The database reports no name for these placeholders, so the analysis core +// leaves them unnamed and they only name the parameter in generated code +// (see Parameter.InferredName). SQLite's LIMIT x, y arrives as +// LIMIT y OFFSET x, so the comma form is named by the clause each +// placeholder ends up in, not by its position. +func limitParamNames(root ast.Node) map[int]string { + names := map[int]string{} + add := func(n ast.Node, name string) { + if ref, ok := n.(*ast.ParamRef); ok { + names[ref.Number] = name + } + } + astutils.Apply(root, func(c *astutils.Cursor) bool { + switch n := c.Node().(type) { + case *ast.SelectStmt: + add(n.LimitCount, "limit") + add(n.LimitOffset, "offset") + case *ast.UpdateStmt: + add(n.LimitCount, "limit") + case *ast.DeleteStmt: + add(n.LimitCount, "limit") + } + return true + }, nil) + return names +} + func coreParamColumn(p core.Parameter, params *named.ParamSet) *Column { col := &Column{ Name: p.Name, diff --git a/internal/compiler/query.go b/internal/compiler/query.go index 339126405c..d2bd7c697c 100644 --- a/internal/compiler/query.go +++ b/internal/compiler/query.go @@ -68,4 +68,9 @@ type Parameter struct { // {name:Type}, by which the query binds the parameter. It is empty for // a positional placeholder. Name string + // InferredName names the parameter in generated code when its column + // has no name, as "limit" and "offset" do under the analysis core. It + // is kept off the column so that `sqlc analyze` still prints the name + // the analysis reports, which for these placeholders is none. + InferredName string } diff --git a/internal/endtoend/testdata/select_limit_comma/sqlite/go/db.go b/internal/endtoend/testdata/select_limit_comma/sqlite/go/db.go new file mode 100644 index 0000000000..32faa9f2b8 --- /dev/null +++ b/internal/endtoend/testdata/select_limit_comma/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, ...any) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...any) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...any) *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/select_limit_comma/sqlite/go/models.go b/internal/endtoend/testdata/select_limit_comma/sqlite/go/models.go new file mode 100644 index 0000000000..f05c6b5688 --- /dev/null +++ b/internal/endtoend/testdata/select_limit_comma/sqlite/go/models.go @@ -0,0 +1,13 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 + +package querytest + +import ( + "database/sql" +) + +type Foo struct { + A sql.NullString +} diff --git a/internal/endtoend/testdata/select_limit_comma/sqlite/go/query.sql.go b/internal/endtoend/testdata/select_limit_comma/sqlite/go/query.sql.go new file mode 100644 index 0000000000..e5dc2ae329 --- /dev/null +++ b/internal/endtoend/testdata/select_limit_comma/sqlite/go/query.sql.go @@ -0,0 +1,45 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" +) + +const fooLimitComma = `-- name: FooLimitComma :many +SELECT a +FROM foo +LIMIT ?, ? +` + +type FooLimitCommaParams struct { + Offset int64 + Limit int64 +} + +func (q *Queries) FooLimitComma(ctx context.Context, arg FooLimitCommaParams) ([]sql.NullString, error) { + rows, err := q.db.QueryContext(ctx, fooLimitComma, arg.Offset, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []sql.NullString + for rows.Next() { + var a sql.NullString + if err := rows.Scan(&a); err != nil { + return nil, err + } + items = append(items, a) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/select_limit_comma/sqlite/query.sql b/internal/endtoend/testdata/select_limit_comma/sqlite/query.sql new file mode 100644 index 0000000000..7ea3db0e31 --- /dev/null +++ b/internal/endtoend/testdata/select_limit_comma/sqlite/query.sql @@ -0,0 +1,4 @@ +/* name: FooLimitComma :many */ +SELECT a +FROM foo +LIMIT ?, ?; diff --git a/internal/endtoend/testdata/select_limit_comma/sqlite/schema.sql b/internal/endtoend/testdata/select_limit_comma/sqlite/schema.sql new file mode 100644 index 0000000000..022bbd6f91 --- /dev/null +++ b/internal/endtoend/testdata/select_limit_comma/sqlite/schema.sql @@ -0,0 +1 @@ +CREATE TABLE foo (a text); diff --git a/internal/endtoend/testdata/select_limit_comma/sqlite/sqlc.json b/internal/endtoend/testdata/select_limit_comma/sqlite/sqlc.json new file mode 100644 index 0000000000..f8e8051087 --- /dev/null +++ b/internal/endtoend/testdata/select_limit_comma/sqlite/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "engine": "sqlite", + "path": "go", + "name": "querytest", + "schema": "schema.sql", + "queries": "query.sql" + } + ] +} \ No newline at end of file