@@ -3,7 +3,6 @@ package sqlx
33import (
44 "database/sql"
55 "fmt"
6- "regexp"
76 "testing"
87)
98
@@ -105,6 +104,7 @@ type Test struct {
105104}
106105
107106func (t Test ) Error (err error , msg ... interface {}) {
107+ t .t .Helper ()
108108 if err != nil {
109109 if len (msg ) == 0 {
110110 t .t .Error (err )
@@ -115,6 +115,7 @@ func (t Test) Error(err error, msg ...interface{}) {
115115}
116116
117117func (t Test ) Errorf (err error , format string , args ... interface {}) {
118+ t .t .Helper ()
118119 if err != nil {
119120 t .t .Errorf (format , args ... )
120121 }
@@ -339,7 +340,7 @@ func TestFixBounds(t *testing.T) {
339340 {
340341 name : `found twice test` ,
341342 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)` ,
343+ expect : `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last),(:name, :age, :first, :last) VALUES (:name, :age, :first, :last)` ,
343344 loop : 2 ,
344345 },
345346 {
@@ -354,6 +355,73 @@ func TestFixBounds(t *testing.T) {
354355 expect : `INSERT INTO foo (a,b) values(:a, :b),(:a, :b)` ,
355356 loop : 2 ,
356357 },
358+ {
359+ name : `on duplicate key using VALUES` ,
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)` ,
362+ loop : 2 ,
363+ },
364+ {
365+ name : `single column` ,
366+ query : `INSERT INTO foo (a) VALUES (:a)` ,
367+ expect : `INSERT INTO foo (a) VALUES (:a),(:a)` ,
368+ loop : 2 ,
369+ },
370+ {
371+ name : `call 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())` ,
386+ loop : 2 ,
387+ },
388+ {
389+ name : `table with "values" at the end` ,
390+ query : `INSERT INTO table_values (a, b) VALUES (:a, :b)` ,
391+ expect : `INSERT INTO table_values (a, b) VALUES (:a, :b),(:a, :b)` ,
392+ loop : 2 ,
393+ },
394+ {
395+ name : `multiline indented query` ,
396+ query : `INSERT INTO foo (
397+ a,
398+ b,
399+ c,
400+ d
401+ ) VALUES (
402+ :name,
403+ :age,
404+ :first,
405+ :last
406+ )` ,
407+ expect : `INSERT INTO foo (
408+ a,
409+ b,
410+ c,
411+ d
412+ ) VALUES (
413+ :name,
414+ :age,
415+ :first,
416+ :last
417+ ),(
418+ :name,
419+ :age,
420+ :first,
421+ :last
422+ )` ,
423+ loop : 2 ,
424+ },
357425 }
358426
359427 for _ , tc := range table {
@@ -364,18 +432,4 @@ func TestFixBounds(t *testing.T) {
364432 }
365433 })
366434 }
367-
368- t .Run ("regex changed" , func (t * testing.T ) {
369- var valueBracketRegChanged = regexp .MustCompile (`(VALUES)\s+(\([^(]*.[^(]\))` )
370- saveRegexp := valueBracketReg
371- defer func () {
372- valueBracketReg = saveRegexp
373- }()
374- valueBracketReg = valueBracketRegChanged
375-
376- res := fixBound ("VALUES (:a, :b)" , 2 )
377- if res != "VALUES (:a, :b)" {
378- t .Errorf ("changed regex should return string" )
379- }
380- })
381435}
0 commit comments