FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(sqlite): correct parameter binding when mixing sqlc.arg() with bare "?" by abgoyal · Pull Request #4471 · sqlc-dev/sqlc · GitHub

/ sqlc Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (5) .json  (1) .sql  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
        • db.go
        • models.go
        • test.sql.go
      • schema.sql
      • sqlc.json
      • test.sql

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.

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

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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"
}
]
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL