Skip to content

Commit 285cfa8

Browse files
cmoogkyleconroy
andcommitted
patches param replacement matching for double digits (#294)
* patches param replacement matching for double digits * adds test case for 11 params with mysql Co-authored-by: Kyle Conroy <[email protected]>
1 parent 9f21299 commit 285cfa8

File tree

5 files changed

+67
-21
lines changed

5 files changed

+67
-21
lines changed

examples/booktest/mysql/models.go

Lines changed: 13 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/booktest/mysql/query.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ INSERT INTO books (
2525
title,
2626
yr,
2727
available,
28-
tags
28+
tags,
29+
is_hardcover,
30+
rating,
31+
front_cover_img,
32+
back_cover_img
2933
) VALUES (
3034
?,
3135
?,
3236
?,
3337
?,
3438
?,
3539
?,
40+
?,
41+
?,
42+
?,
43+
?,
3644
?
3745
);
3846

examples/booktest/mysql/query.sql.go

Lines changed: 27 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/booktest/mysql/schema.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ CREATE TABLE books (
1919
title text NOT NULL DEFAULT '',
2020
yr integer NOT NULL DEFAULT 2000,
2121
available datetime NOT NULL DEFAULT NOW(),
22-
tags text NOT NULL DEFAULT ''
22+
tags text NOT NULL DEFAULT '',
23+
is_hardcover BOOL NOT NULL DEFAULT 0,
24+
rating decimal(13, 2),
25+
front_cover_img varchar(255),
26+
back_cover_img varchar(255)
2327
-- CONSTRAINT FOREIGN KEY (author_id) REFERENCES authors(author_id)
2428
) ENGINE=InnoDB;
2529

internal/mysql/param.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mysql
33
import (
44
"fmt"
55
"regexp"
6+
"sort"
67
"strings"
78

89
"github.com/kyleconroy/sqlc/internal/dinosql"
@@ -174,7 +175,18 @@ func paramName(col sqlparser.ColIdent, originalName string) string {
174175
}
175176

176177
func replaceParamStrs(query string, params []*Param) (string, error) {
177-
for _, p := range params {
178+
/*
179+
To ensure that ":v1" does not replace ":v12", we need to sort
180+
the params in decending order by length of the string.
181+
But, the original order of the params must be preserved.
182+
*/
183+
paramsCopy := make([]*Param, len(params))
184+
copy(paramsCopy, params)
185+
sort.Slice(paramsCopy, func(i, j int) bool {
186+
return len(paramsCopy[i].OriginalName) > len(paramsCopy[j].OriginalName)
187+
})
188+
189+
for _, p := range paramsCopy {
178190
re, err := regexp.Compile(fmt.Sprintf("(%v)", regexp.QuoteMeta(p.OriginalName)))
179191
if err != nil {
180192
return "", err

0 commit comments

Comments
 (0)