Skip to content

Commit 6258c9a

Browse files
author
Alan Braithwaite
committed
named: address bulk insert v1.3.1 issues
This change-set is how I addressed the issues of bulk inserts not being handled cleanly when they're not at the end of the query. It works by first adding VALUES to the expression (not sure if this is universally applicable to all supported SQL dialects) then adding a submatch to the regular expression for the fields we want to repeat. Using the submatch, we're able to repeat the fields that we're concerned with to expand the insertion parameters.
1 parent a1d5e64 commit 6258c9a

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

named.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,28 @@ func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper)
224224
return bound, arglist, nil
225225
}
226226

227-
var valueBracketReg = regexp.MustCompile(`\([^(]*.[^(]\)\s*$`)
227+
var valueBracketReg = regexp.MustCompile(`VALUES\s+(\([^(]*.[^(]\))`)
228228

229229
func fixBound(bound string, loop int) string {
230-
loc := valueBracketReg.FindStringIndex(bound)
231-
if len(loc) != 2 {
230+
loc := valueBracketReg.FindAllStringSubmatchIndex(bound, -1)
231+
// Either no VALUES () found or more than one found??
232+
if len(loc) != 1 {
233+
return bound
234+
}
235+
// defensive guard. loc should be len 4 representing the starting and
236+
// ending index for the whole regex match and the starting + ending
237+
// index for the single inside group
238+
if len(loc[0]) != 4 {
232239
return bound
233240
}
234241
var buffer bytes.Buffer
235242

236-
buffer.WriteString(bound[0:loc[1]])
243+
buffer.WriteString(bound[0:loc[0][1]])
237244
for i := 0; i < loop-1; i++ {
238245
buffer.WriteString(",")
239-
buffer.WriteString(bound[loc[0]:loc[1]])
246+
buffer.WriteString(bound[loc[0][2]:loc[0][3]])
240247
}
241-
buffer.WriteString(bound[loc[1]:])
248+
buffer.WriteString(bound[loc[0][1]:])
242249
return buffer.String()
243250
}
244251

0 commit comments

Comments
 (0)