From b638d7d6422a6f392914f1dac054284171e6d7a3 Mon Sep 17 00:00:00 2001 From: Caleb Spare Date: Fri, 31 Jan 2025 00:24:47 -0800 Subject: [PATCH] Relax constraint on VALUE clause expansion This lets it be used in non-INSERT scenarios as well. See the test for such an example. --- named.go | 2 +- named_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/named.go b/named.go index 6ac44777..af527aa0 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(`(?i)\WVALUES\s*\(`) func findMatchingClosingBracketIndex(s string) int { count := 0 diff --git a/named_test.go b/named_test.go index 0ee5b85f..c33b9bc5 100644 --- a/named_test.go +++ b/named_test.go @@ -422,6 +422,30 @@ func TestFixBounds(t *testing.T) { )`, loop: 2, }, + { + name: "use of VALUES not for insert", + query: ` +UPDATE t +SET + foo = u.foo, + bar = u.bar +FROM ( + VALUES (:id, :foo, :bar) +) AS u(id, foo, bar) +WHERE t.id = u.id +`, + expect: ` +UPDATE t +SET + foo = u.foo, + bar = u.bar +FROM ( + VALUES (:id, :foo, :bar),(:id, :foo, :bar) +) AS u(id, foo, bar) +WHERE t.id = u.id +`, + loop: 2, + }, } for _, tc := range table {