Skip to content

Commit

Permalink
Generate valid code for MySQL named parameters (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy authored Nov 23, 2020
1 parent db397b7 commit 3d23cd9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.

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

57 changes: 49 additions & 8 deletions internal/endtoend/testdata/params_duplicate/mysql/go/query.sql.go

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

8 changes: 7 additions & 1 deletion internal/endtoend/testdata/params_duplicate/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* name: SelectUserArg :many */
/* name: SelectUserByID :many */
SELECT first_name from
users where (sqlc.arg(id) = id OR sqlc.arg(id) = 0);

/* name: SelectUserByName :many */
SELECT first_name
FROM users
WHERE first_name = sqlc.arg(name)
OR last_name = sqlc.arg(name);

/* name: SelectUserQuestion :many */
SELECT first_name from
users where (? = id OR ? = 0);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE users (
id integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name varchar(255) NOT NULL,
first_name varchar(255),
last_name varchar(255)
) ENGINE=InnoDB;
4 changes: 3 additions & 1 deletion internal/sql/rewrite/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func NamedParameters(engine config.Engine, raw *ast.RawStmt) (*ast.RawStmt, map[
return raw, map[int]string{}, nil
}

hasNamedParameterSupport := engine != config.EngineMySQL

args := map[string]int{}
argn := 0
var edits []source.Edit
Expand All @@ -58,7 +60,7 @@ func NamedParameters(engine config.Engine, raw *ast.RawStmt) (*ast.RawStmt, map[
case named.IsParamFunc(node):
fun := node.(*ast.FuncCall)
param, isConst := flatten(fun.Args)
if num, ok := args[param]; ok {
if num, ok := args[param]; ok && hasNamedParameterSupport {
cr.Replace(&ast.ParamRef{
Number: num,
Location: fun.Location,
Expand Down

0 comments on commit 3d23cd9

Please sign in to comment.