-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #687 from jmoiron/pr-501
merge #501 to trunk
- Loading branch information
Showing
3 changed files
with
65 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,6 +182,28 @@ func main() { | |
// as the name -> db mapping, so struct fields are lowercased and the `db` tag | ||
// is taken into consideration. | ||
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason) | ||
|
||
|
||
// batch insert | ||
|
||
// batch insert with structs | ||
personStructs := []Person{ | ||
{FirstName: "Ardie", LastName: "Savea", Email: "[email protected]"}, | ||
{FirstName: "Sonny Bill", LastName: "Williams", Email: "[email protected]"}, | ||
{FirstName: "Ngani", LastName: "Laumape", Email: "[email protected]"}, | ||
} | ||
|
||
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email) | ||
VALUES (:first_name, :last_name, :email)`, personStructs) | ||
|
||
// batch insert with maps | ||
personMaps := []map[string]interface{}{ | ||
{"first_name": "Ardie", "last_name": "Savea", "email": "[email protected]"}, | ||
{"first_name": "Sonny Bill", "last_name": "Williams", "email": "[email protected]"}, | ||
{"first_name": "Ngani", "last_name": "Laumape", "email": "[email protected]"}, | ||
} | ||
|
||
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email) | ||
VALUES (:first_name, :last_name, :email)`, personMaps) | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,7 +195,7 @@ func TestNamedQueries(t *testing.T) { | |
t.Errorf("got %s, expected %s", p.Email, people[0].Email) | ||
} | ||
|
||
// test batch inserts | ||
// test struct batch inserts | ||
sls := []Person{ | ||
{FirstName: "Ardie", LastName: "Savea", Email: "[email protected]"}, | ||
{FirstName: "Sonny Bill", LastName: "Williams", Email: "[email protected]"}, | ||
|
@@ -206,6 +206,29 @@ func TestNamedQueries(t *testing.T) { | |
_, err = db.NamedExec(insert, sls) | ||
test.Error(err) | ||
|
||
// test map batch inserts | ||
slsMap := []map[string]interface{}{ | ||
{"first_name": "Ardie", "last_name": "Savea", "email": "[email protected]"}, | ||
{"first_name": "Sonny Bill", "last_name": "Williams", "email": "[email protected]"}, | ||
{"first_name": "Ngani", "last_name": "Laumape", "email": "[email protected]"}, | ||
} | ||
|
||
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email) | ||
VALUES (:first_name, :last_name, :email)`, slsMap) | ||
test.Error(err) | ||
|
||
type A map[string]interface{} | ||
|
||
typedMap := []A{ | ||
{"first_name": "Ardie", "last_name": "Savea", "email": "[email protected]"}, | ||
{"first_name": "Sonny Bill", "last_name": "Williams", "email": "[email protected]"}, | ||
{"first_name": "Ngani", "last_name": "Laumape", "email": "[email protected]"}, | ||
} | ||
|
||
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email) | ||
VALUES (:first_name, :last_name, :email)`, typedMap) | ||
test.Error(err) | ||
|
||
for _, p := range sls { | ||
dest := Person{} | ||
err = db.Get(&dest, db.Rebind("SELECT * FROM person WHERE email=?"), p.Email) | ||
|