Skip to content

Commit

Permalink
add more tolerance to fixBound regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed Apr 8, 2021
1 parent 1723f86 commit 874a5d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion named.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper)
return bound, arglist, nil
}

var valueBracketReg = regexp.MustCompile(`VALUES\s+(\([^(]*.[^(]\))`)
var valueBracketReg = regexp.MustCompile(`(?i)VALUES\s*(\([^(]*.[^(]\))`)

func fixBound(bound string, loop int) string {

loc := valueBracketReg.FindAllStringSubmatchIndex(bound, -1)
// Either no VALUES () found or more than one found??
if len(loc) != 1 {
Expand Down
12 changes: 12 additions & 0 deletions named_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ func TestFixBounds(t *testing.T) {
expect: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last) VALUES (:name, :age, :first, :last)`,
loop: 2,
},
{
name: `nospace`,
query: `INSERT INTO foo (a,b) VALUES(:a, :b)`,
expect: `INSERT INTO foo (a,b) VALUES(:a, :b),(:a, :b)`,
loop: 2,
},
{
name: `lowercase`,
query: `INSERT INTO foo (a,b) values(:a, :b)`,
expect: `INSERT INTO foo (a,b) values(:a, :b),(:a, :b)`,
loop: 2,
},
}

for _, tc := range table {
Expand Down

0 comments on commit 874a5d4

Please sign in to comment.