Skip to content

Commit

Permalink
Merge 0f772c8 into a1d5e64
Browse files Browse the repository at this point in the history
  • Loading branch information
podhmo committed Mar 13, 2021
2 parents a1d5e64 + 0f772c8 commit 68336a3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion named.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper)
return bound, arglist, nil
}

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

func fixBound(bound string, loop int) string {
loc := valueBracketReg.FindStringIndex(bound)
Expand Down
80 changes: 80 additions & 0 deletions named_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Test struct {
}

func (t Test) Error(err error, msg ...interface{}) {
t.t.Helper()
if err != nil {
if len(msg) == 0 {
t.t.Error(err)
Expand All @@ -114,6 +115,7 @@ func (t Test) Error(err error, msg ...interface{}) {
}

func (t Test) Errorf(err error, format string, args ...interface{}) {
t.t.Helper()
if err != nil {
t.t.Errorf(format, args...)
}
Expand Down Expand Up @@ -296,3 +298,81 @@ func TestNamedQueries(t *testing.T) {

})
}

func TestNamedBulkInsert(t *testing.T) {
type Val struct {
K string `db:"k"`
V int `db:"v"`
}

vs := []Val{{K: "x"}, {K: "y"}, {K: "z"}}
table := []struct {
values []interface{}
q, expected string
}{
{
values: []interface{}{vs[0]},
q: "INSERT INTO val (k) VALUES (:k)",
expected: "INSERT INTO val (k) VALUES (?)",
},
{
values: []interface{}{vs[0], vs[1]},
q: "INSERT INTO val (k) VALUES (:k)",
expected: "INSERT INTO val (k) VALUES (?),(?)",
},
{
values: []interface{}{vs[0]},
q: "INSERT INTO val (k,v) VALUES (:k,:v)",
expected: "INSERT INTO val (k,v) VALUES (?,?)",
},
{
values: []interface{}{vs[0], vs[1]},
q: "INSERT INTO val (k,v) VALUES (:k,:v)",
expected: "INSERT INTO val (k,v) VALUES (?,?),(?,?)",
},
{
values: []interface{}{vs[0]},
q: "INSERT INTO val (k,v) VALUES ( :k, :v )",
expected: "INSERT INTO val (k,v) VALUES ( ?, ? )",
},
{
values: []interface{}{vs[0], vs[1]},
q: "INSERT INTO val (k,v) VALUES ( :k, :v )",
expected: "INSERT INTO val (k,v) VALUES ( ?, ? ),( ?, ? )",
},
{
values: []interface{}{vs[0], vs[1]},
q: func() string {
_, _, now := defaultSchema.Postgres()
return fmt.Sprintf("INSERT INTO val (k, v, added_at) VALUES (:k, :v, %v)", now)
}(),
expected: "INSERT INTO val (k, v, added_at) VALUES (?, ?, now()),(?, ?, now())",
},
{
values: []interface{}{vs[0], vs[1]},
q: func() string {
_, _, now := defaultSchema.MySQL()
return fmt.Sprintf("INSERT INTO val (k, v, added_at) VALUES (:k, :v, %v)", now)
}(),
expected: "INSERT INTO val (k, v, added_at) VALUES (?, ?, now()),(?, ?, now())",
},
{
values: []interface{}{vs[0], vs[1]},
q: func() string {
_, _, now := defaultSchema.Sqlite3()
return fmt.Sprintf("INSERT INTO val (k, v, added_at) VALUES (:k, :v, %v)", now)
}(),
expected: "INSERT INTO val (k, v, added_at) VALUES (?, ?, CURRENT_TIMESTAMP),(?, ?, CURRENT_TIMESTAMP)",
},
}

for _, test := range table {
actual, _, err := Named(test.q, test.values)
if err != nil {
t.Fatalf("unexpected error %+v, when len(values) == %d", err, len(test.values))
}
if test.expected != actual {
t.Errorf("expected query is (len(values) == %d)\n\t%q\nbut actual result is\n\t%q", len(test.values), test.expected, actual)
}
}
}

0 comments on commit 68336a3

Please sign in to comment.