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
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/mix_param_types/sqlite/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/mix_param_types/sqlite/go/models.go

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

59 changes: 59 additions & 0 deletions internal/endtoend/testdata/mix_param_types/sqlite/go/test.sql.go

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

5 changes: 5 additions & 0 deletions internal/endtoend/testdata/mix_param_types/sqlite/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE bar (
id integer not null,
name text not null,
phone text not null
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/mix_param_types/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "test.sql",
"engine": "sqlite"
}
]
}
8 changes: 8 additions & 0 deletions internal/endtoend/testdata/mix_param_types/sqlite/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- name: CountOne :one
SELECT count(1) FROM bar WHERE id = sqlc.arg(id) AND name <> ?;

-- name: CountTwo :one
SELECT count(1) FROM bar WHERE id = ? AND name <> sqlc.arg(name);

-- name: CountThree :one
SELECT count(1) FROM bar WHERE id > ? AND phone <> sqlc.arg(phone) AND name <> ?;
8 changes: 8 additions & 0 deletions internal/sql/named/param_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (p *ParamSet) Add(param Param) int {
return argn
}

// AddAnonymous allocates the next available parameter position without
// associating a name. Used for bare ? placeholders that need explicit numbering.
func (p *ParamSet) AddAnonymous() int {
argn := p.nextArgNum()
p.positionToName[argn] = ""
return argn
}

// FetchMerge fetches an indexed parameter, and merges `mergeP` into it
// Returns: the merged parameter and whether it was a named parameter
func (p *ParamSet) FetchMerge(idx int, mergeP Param) (param Param, isNamed bool) {
Expand Down
31 changes: 31 additions & 0 deletions internal/sql/rewrite/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ func NamedParameters(engine config.Engine, raw *ast.RawStmt, numbs map[int]bool,
foundFunc := astutils.Search(raw, named.IsParamFunc)
foundSign := astutils.Search(raw, named.IsParamSign)
hasNamedParameterSupport := engine != config.EngineMySQL

// SQLite uses numbered ?N for sqlc.arg() but also accepts bare ?. When both
// coexist we must number all placeholders in text order so that positional
// argument binding matches the generated ?N values.
foundBare := astutils.Search(raw, isBareParamRef)
hasMixedParams := engine == config.EngineSQLite &&
len(foundBare.Items) > 0 &&
len(foundFunc.Items)+len(foundSign.Items) > 0
if hasMixedParams {
numbs = nil
}

allParams := named.NewParamSet(numbs, hasNamedParameterSupport)

if len(foundFunc.Items)+len(foundSign.Items) == 0 {
Expand Down Expand Up @@ -183,10 +195,29 @@ func NamedParameters(engine config.Engine, raw *ast.RawStmt, numbs map[int]bool,
})
return false

case hasMixedParams && isBareParamRef(node):
ref := node.(*ast.ParamRef)
argn := allParams.AddAnonymous()
cr.Replace(&ast.ParamRef{
Number: argn,
Location: ref.Location,
})
edits = append(edits, source.Edit{
Location: ref.Location - raw.StmtLocation,
Old: "?",
New: fmt.Sprintf("?%d", argn),
})
return false

default:
return true
}
}, nil)

return node.(*ast.RawStmt), allParams, edits
}

func isBareParamRef(node ast.Node) bool {
ref, ok := node.(*ast.ParamRef)
return ok && !ref.Dollar
}
Loading