Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bulk insert with one field #710

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (?)",
},
{
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test for #709

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())",
Copy link
Author

@podhmo podhmo Mar 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supports only no-arguments function (e.g. 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)
}
}
}