Skip to content

Commit 68949f7

Browse files
authored
Merge pull request jmoiron#501 from J-Zeitler/map_batch
add support for batch insert/exec with maps in addition to structs
2 parents 03c8d81 + 429af82 commit 68949f7

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ _testmain.go
2323
*.exe
2424
tags
2525
environ
26+
27+
.idea

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ func main() {
182182
// as the name -> db mapping, so struct fields are lowercased and the `db` tag
183183
// is taken into consideration.
184184
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)
185+
186+
187+
// batch insert
188+
189+
// batch insert with structs
190+
personStructs := []Person{
191+
{FirstName: "Ardie", LastName: "Savea", Email: "[email protected]"},
192+
{FirstName: "Sonny Bill", LastName: "Williams", Email: "[email protected]"},
193+
{FirstName: "Ngani", LastName: "Laumape", Email: "[email protected]"},
194+
}
195+
196+
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
197+
VALUES (:first_name, :last_name, :email)`, personStructs)
198+
199+
// batch insert with maps
200+
personMaps := []map[string]interface{}{
201+
{"first_name": "Ardie", "last_name": "Savea", "email": "[email protected]"},
202+
{"first_name": "Sonny Bill", "last_name": "Williams", "email": "[email protected]"},
203+
{"first_name": "Ngani", "last_name": "Laumape", "email": "[email protected]"},
204+
}
205+
206+
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
207+
VALUES (:first_name, :last_name, :email)`, personMaps)
185208
}
186209
```
187-

named.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interfa
157157
// type, given a list of names to pull out of the struct. Used by public
158158
// BindStruct interface.
159159
func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
160+
if maparg, ok := arg.(map[string]interface{}); ok {
161+
return bindMapArgs(names, maparg)
162+
}
163+
160164
arglist := make([]interface{}, 0, len(names))
161165

162166
// grab the indirected value of arg

named_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestNamedQueries(t *testing.T) {
195195
t.Errorf("got %s, expected %s", p.Email, people[0].Email)
196196
}
197197

198-
// test batch inserts
198+
// test struct batch inserts
199199
sls := []Person{
200200
{FirstName: "Ardie", LastName: "Savea", Email: "[email protected]"},
201201
{FirstName: "Sonny Bill", LastName: "Williams", Email: "[email protected]"},
@@ -206,6 +206,17 @@ func TestNamedQueries(t *testing.T) {
206206
_, err = db.NamedExec(insert, sls)
207207
test.Error(err)
208208

209+
// test map batch inserts
210+
slsMap := []map[string]interface{}{
211+
{"first_name": "Ardie", "last_name": "Savea", "email": "[email protected]"},
212+
{"first_name": "Sonny Bill", "last_name": "Williams", "email": "[email protected]"},
213+
{"first_name": "Ngani", "last_name": "Laumape", "email": "[email protected]"},
214+
}
215+
216+
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
217+
VALUES (:first_name, :last_name, :email)`, slsMap)
218+
test.Error(err)
219+
209220
for _, p := range sls {
210221
dest := Person{}
211222
err = db.Get(&dest, db.Rebind("SELECT * FROM person WHERE email=?"), p.Email)

0 commit comments

Comments
 (0)