Skip to content

Commit 5317814

Browse files
committed
Fix to allow single column, one level function call and using VALUES after ON DUPLICATE KEY
1 parent 1810e7a commit 5317814

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

named.go

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

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

229229
func fixBound(bound string, loop int) string {
230230

231231
loc := valueBracketReg.FindAllStringSubmatchIndex(bound, -1)
232-
// Either no VALUES () found or more than one found??
233-
if len(loc) != 1 {
232+
// defensive guard when "VALUES (...)" not found
233+
if len(loc) < 1 {
234234
return bound
235235
}
236236
// defensive guard. loc should be len 4 representing the starting and

named_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type Test struct {
105105
}
106106

107107
func (t Test) Error(err error, msg ...interface{}) {
108+
t.t.Helper()
108109
if err != nil {
109110
if len(msg) == 0 {
110111
t.t.Error(err)
@@ -115,6 +116,7 @@ func (t Test) Error(err error, msg ...interface{}) {
115116
}
116117

117118
func (t Test) Errorf(err error, format string, args ...interface{}) {
119+
t.t.Helper()
118120
if err != nil {
119121
t.t.Errorf(format, args...)
120122
}
@@ -339,7 +341,7 @@ func TestFixBounds(t *testing.T) {
339341
{
340342
name: `found twice test`,
341343
query: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last) VALUES (:name, :age, :first, :last)`,
342-
expect: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last) VALUES (:name, :age, :first, :last)`,
344+
expect: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last),(:name, :age, :first, :last) VALUES (:name, :age, :first, :last)`,
343345
loop: 2,
344346
},
345347
{
@@ -354,6 +356,24 @@ func TestFixBounds(t *testing.T) {
354356
expect: `INSERT INTO foo (a,b) values(:a, :b),(:a, :b)`,
355357
loop: 2,
356358
},
359+
{
360+
name: `on duplicate key using VALUES`,
361+
query: `INSERT INTO foo (a,b) VALUES(:a, :b) ON DUPLICATE KEY UPDATE a=VALUES(a)`,
362+
expect: `INSERT INTO foo (a,b) VALUES(:a, :b),(:a, :b) ON DUPLICATE KEY UPDATE a=VALUES(a)`,
363+
loop: 2,
364+
},
365+
{
366+
name: `single column`,
367+
query: `INSERT INTO foo (a) VALUES(:a)`,
368+
expect: `INSERT INTO foo (a) VALUES(:a),(:a)`,
369+
loop: 2,
370+
},
371+
{
372+
name: `call now`,
373+
query: `INSERT INTO foo (a, b) VALUES(:a, NOW())`,
374+
expect: `INSERT INTO foo (a, b) VALUES(:a, NOW()),(:a, NOW())`,
375+
loop: 2,
376+
},
357377
}
358378

359379
for _, tc := range table {

0 commit comments

Comments
 (0)