Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bindArray on SELECT from VALUES list #811

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion named.go
Expand Up @@ -224,7 +224,7 @@ func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper)
return bound, arglist, nil
}

var valuesReg = regexp.MustCompile(`\)\s*(?i)VALUES\s*\(`)
var valuesReg = regexp.MustCompile(`[\)|\(]\s*(?i)VALUES\s*\(`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see test cases justifying ) and ( but not |. Unless anyone wants to add a test demonstrating the usefulness of allowing a leading |, maybe this PR could get unblocked by removing that option?

Suggested change
var valuesReg = regexp.MustCompile(`[\)|\(]\s*(?i)VALUES\s*\(`)
var valuesReg = regexp.MustCompile(`[)(]\s*(?i)VALUES\s*\(`)

Note there is no need to escape parentheses within a character class, since capture groups cannot exist inside character classes.


func findMatchingClosingBracketIndex(s string) int {
count := 0
Expand Down
14 changes: 13 additions & 1 deletion named_test.go
Expand Up @@ -391,6 +391,18 @@ func TestFixBounds(t *testing.T) {
expect: `INSERT INTO table_values (a, b) VALUES (:a, :b),(:a, :b)`,
loop: 2,
},
{
name: `table named "values"`,
query: `INSERT INTO values (a, b) VALUES (:a, :b)`,
expect: `INSERT INTO values (a, b) VALUES (:a, :b),(:a, :b)`,
loop: 2,
},
{
name: `select from values list`,
query: `SELECT * FROM (VALUES (:a, :b))`,
expect: `SELECT * FROM (VALUES (:a, :b),(:a, :b))`,
loop: 2,
},
{
name: `multiline indented query`,
query: `INSERT INTO foo (
Expand Down Expand Up @@ -428,7 +440,7 @@ func TestFixBounds(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
res := fixBound(tc.query, tc.loop)
if res != tc.expect {
t.Errorf("mismatched results")
t.Errorf("mismatched results. Expected: %s, got: %s", tc.expect, res)
}
})
}
Expand Down