Skip to content

Commit b20d679

Browse files
committed
Allow multi-level depth function calls
1 parent 5317814 commit b20d679

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

named.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,29 +224,47 @@ 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 valuesReg = regexp.MustCompile(`(?i)VALUES\s*\(`)
228228

229-
func fixBound(bound string, loop int) string {
229+
func findMatchingClosingBracketIndex(s string) int {
230+
count := 0
231+
for i, ch := range s {
232+
if ch == '(' {
233+
count++
234+
}
235+
if ch == ')' {
236+
count--
237+
if count == 0 {
238+
return i
239+
}
240+
}
241+
}
242+
return 0
243+
}
230244

231-
loc := valueBracketReg.FindAllStringSubmatchIndex(bound, -1)
245+
func fixBound(bound string, loop int) string {
246+
loc := valuesReg.FindStringIndex(bound)
232247
// defensive guard when "VALUES (...)" not found
233248
if len(loc) < 1 {
234249
return bound
235250
}
236-
// defensive guard. loc should be len 4 representing the starting and
237-
// ending index for the whole regex match and the starting + ending
238-
// index for the single inside group
239-
if len(loc[0]) != 4 {
251+
252+
openingBracketIndex := loc[1] - 1
253+
index := findMatchingClosingBracketIndex(bound[openingBracketIndex:])
254+
// defensive guard. must have closing bracket
255+
if index == 0 {
240256
return bound
241257
}
258+
closingBracketIndex := openingBracketIndex + index + 1
259+
242260
var buffer bytes.Buffer
243261

244-
buffer.WriteString(bound[0:loc[0][1]])
262+
buffer.WriteString(bound[0:closingBracketIndex])
245263
for i := 0; i < loop-1; i++ {
246264
buffer.WriteString(",")
247-
buffer.WriteString(bound[loc[0][2]:loc[0][3]])
265+
buffer.WriteString(bound[openingBracketIndex:closingBracketIndex])
248266
}
249-
buffer.WriteString(bound[loc[0][1]:])
267+
buffer.WriteString(bound[closingBracketIndex:])
250268
return buffer.String()
251269
}
252270

named_test.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sqlx
33
import (
44
"database/sql"
55
"fmt"
6-
"regexp"
76
"testing"
87
)
98

@@ -358,20 +357,32 @@ func TestFixBounds(t *testing.T) {
358357
},
359358
{
360359
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)`,
360+
query: `INSERT INTO foo (a,b) VALUES (:a, :b) ON DUPLICATE KEY UPDATE a=VALUES(a)`,
361+
expect: `INSERT INTO foo (a,b) VALUES (:a, :b),(:a, :b) ON DUPLICATE KEY UPDATE a=VALUES(a)`,
363362
loop: 2,
364363
},
365364
{
366365
name: `single column`,
367-
query: `INSERT INTO foo (a) VALUES(:a)`,
368-
expect: `INSERT INTO foo (a) VALUES(:a),(:a)`,
366+
query: `INSERT INTO foo (a) VALUES (:a)`,
367+
expect: `INSERT INTO foo (a) VALUES (:a),(:a)`,
369368
loop: 2,
370369
},
371370
{
372371
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())`,
372+
query: `INSERT INTO foo (a, b) VALUES (:a, NOW())`,
373+
expect: `INSERT INTO foo (a, b) VALUES (:a, NOW()),(:a, NOW())`,
374+
loop: 2,
375+
},
376+
{
377+
name: `two level depth function call`,
378+
query: `INSERT INTO foo (a, b) VALUES (:a, YEAR(NOW()))`,
379+
expect: `INSERT INTO foo (a, b) VALUES (:a, YEAR(NOW())),(:a, YEAR(NOW()))`,
380+
loop: 2,
381+
},
382+
{
383+
name: `missing closing bracket`,
384+
query: `INSERT INTO foo (a, b) VALUES (:a, YEAR(NOW())`,
385+
expect: `INSERT INTO foo (a, b) VALUES (:a, YEAR(NOW())`,
375386
loop: 2,
376387
},
377388
}
@@ -384,18 +395,4 @@ func TestFixBounds(t *testing.T) {
384395
}
385396
})
386397
}
387-
388-
t.Run("regex changed", func(t *testing.T) {
389-
var valueBracketRegChanged = regexp.MustCompile(`(VALUES)\s+(\([^(]*.[^(]\))`)
390-
saveRegexp := valueBracketReg
391-
defer func() {
392-
valueBracketReg = saveRegexp
393-
}()
394-
valueBracketReg = valueBracketRegChanged
395-
396-
res := fixBound("VALUES (:a, :b)", 2)
397-
if res != "VALUES (:a, :b)" {
398-
t.Errorf("changed regex should return string")
399-
}
400-
})
401398
}

0 commit comments

Comments
 (0)