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
6 changes: 5 additions & 1 deletion internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
37 changes: 36 additions & 1 deletion internal/compiler/parse_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_limit_comma/sqlite/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/select_limit_comma/sqlite/go/models.go

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* name: FooLimitComma :many */
SELECT a
FROM foo
LIMIT ?, ?;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE foo (a text);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_limit_comma/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"
}
]
}
Loading