Skip to content

Commit

Permalink
named: address bulk insert v1.3.1 issues
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Alan Braithwaite committed Mar 31, 2021
1 parent a1d5e64 commit 6258c9a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions named.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,21 +224,28 @@ func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper)
return bound, arglist, nil
}

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

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

buffer.WriteString(bound[0:loc[1]])
buffer.WriteString(bound[0:loc[0][1]])
for i := 0; i < loop-1; i++ {
buffer.WriteString(",")
buffer.WriteString(bound[loc[0]:loc[1]])
buffer.WriteString(bound[loc[0][2]:loc[0][3]])
}
buffer.WriteString(bound[loc[1]:])
buffer.WriteString(bound[loc[0][1]:])
return buffer.String()
}

Expand Down

0 comments on commit 6258c9a

Please sign in to comment.