From 8f2448289287c51ee3b70fc04f097fdd024541be Mon Sep 17 00:00:00 2001 From: Hyde Zhang Date: Fri, 13 May 2022 21:23:07 +0100 Subject: [PATCH 1/2] fix: allow value list select array bind --- named.go | 2 +- named_test.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/named.go b/named.go index 728aa04d..db068551 100644 --- a/named.go +++ b/named.go @@ -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*\(`) func findMatchingClosingBracketIndex(s string) int { count := 0 diff --git a/named_test.go b/named_test.go index 8481b35b..38751f0e 100644 --- a/named_test.go +++ b/named_test.go @@ -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 ( @@ -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) } }) } From 36bc11d0a84dfad671c2e1662761debdb4e7afbe Mon Sep 17 00:00:00 2001 From: Hyde Zhang Date: Sun, 22 Sep 2024 15:06:19 +0100 Subject: [PATCH 2/2] Update named.go Co-authored-by: jvnuw <99148334+jvnuw@users.noreply.github.com> --- named.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/named.go b/named.go index db068551..5561eba5 100644 --- a/named.go +++ b/named.go @@ -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*\(`) func findMatchingClosingBracketIndex(s string) int { count := 0