Skip to content

Commit

Permalink
Merge pull request #14 from andymoon/master
Browse files Browse the repository at this point in the history
Version 3.1
  • Loading branch information
doug-martin committed Sep 1, 2015
2 parents 95fc6b7 + 1d3b96d commit 54c8a8d
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 185 deletions.
8 changes: 8 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v3.1.0

* Version 3.1 [#14](https://github.com/doug-martin/goqu/pull/14) - [@andymoon](https://github.com/andymoon)
* Fix an issue with a nil pointer access on the inserts and updates.
* Allowing ScanStructs to take a struct with an embedded pointer to a struct.
* Change to check if struct is Anonymous when recursing through an embedded struct.
* Updated to use the lastest version of github.com/DATA-DOG/go-sqlmock.

## v3.0.1

* Add literal bytes and update to c2fo testify [#15](https://github.com/doug-martin/goqu/pull/15) - [@TechnotronicOz](https://github.com/TechnotronicOz)
Expand Down
21 changes: 19 additions & 2 deletions crud_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func assignVals(i interface{}, results []Record, cm columnMap) error {
case reflect.Slice:
for _, result := range results {
row := reflect.Indirect(reflect.New(t))
initEmbeddedPtr(row)
for name, data := range cm {
src, ok := result[name]
if ok {
Expand All @@ -259,6 +260,18 @@ func assignVals(i interface{}, results []Record, cm columnMap) error {
return nil
}

func initEmbeddedPtr(value reflect.Value) {
for i := 0; i < value.NumField(); i++ {
v := value.Field(i)
kind := v.Kind()
t := value.Type().Field(i)
if t.Anonymous && kind == reflect.Ptr {
z := reflect.New(t.Type.Elem())
v.Set(z)
}
}
}

func getColumnMap(i interface{}) (columnMap, error) {
val := reflect.Indirect(reflect.ValueOf(i))
t, valKind, _ := getTypeInfo(i, val)
Expand All @@ -276,8 +289,12 @@ func createColumnMap(t reflect.Type) columnMap {
var subColMaps []columnMap
for i := 0; i < n; i++ {
f := t.Field(i)
if f.Anonymous && f.Type.Kind() == reflect.Struct {
subColMaps = append(subColMaps, createColumnMap(f.Type))
if f.Anonymous && (f.Type.Kind() == reflect.Struct || f.Type.Kind() == reflect.Ptr) {
if f.Type.Kind() == reflect.Ptr {
subColMaps = append(subColMaps, createColumnMap(f.Type.Elem()))
} else {
subColMaps = append(subColMaps, createColumnMap(f.Type))
}
} else {
columnName := f.Tag.Get("db")
if columnName == "" {
Expand Down
42 changes: 21 additions & 21 deletions crud_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
)
Expand All @@ -31,7 +31,7 @@ type crudExecTest struct {

func (me *crudExecTest) TestWithError() {
t := me.T()
mDb, err := sqlmock.New()
mDb, _, err := sqlmock.New()
assert.NoError(t, err)
db := New("db-mock", mDb)
expectedErr := fmt.Errorf("crud exec error")
Expand All @@ -51,29 +51,29 @@ func (me *crudExecTest) TestWithError() {

func (me *crudExecTest) TestScanStructs() {
t := me.T()
mDb, err := sqlmock.New()
mDb, mock, err := sqlmock.New()
assert.NoError(t, err)

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WillReturnError(fmt.Errorf("query error"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name", "phone_number", "age"}).FromCSVString("111 Test Addr,Test1,111-111-1111,20\n211 Test Addr,Test2,222-222-2222,30"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name", "phone_number", "age"}).FromCSVString("111 Test Addr,Test1,111-111-1111,20\n211 Test Addr,Test2,222-222-2222,30"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

Expand Down Expand Up @@ -141,21 +141,21 @@ func (me *crudExecTest) TestScanStructs() {

func (me *crudExecTest) TestScanStruct() {
t := me.T()
mDb, err := sqlmock.New()
mDb, mock, err := sqlmock.New()
assert.NoError(t, err)

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WillReturnError(fmt.Errorf("query error"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name", "phone_number", "age"}).FromCSVString("111 Test Addr,Test1,111-111-1111,20"))

sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))

Expand Down Expand Up @@ -199,17 +199,17 @@ func (me *crudExecTest) TestScanStruct() {

func (me *crudExecTest) TestScanVals() {
t := me.T()
mDb, err := sqlmock.New()
mDb, mock, err := sqlmock.New()
assert.NoError(t, err)

sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
mock.ExpectQuery(`SELECT "id" FROM "items"`).
WillReturnError(fmt.Errorf("query error"))

sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
mock.ExpectQuery(`SELECT "id" FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2"))

sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
mock.ExpectQuery(`SELECT "id" FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2"))

Expand All @@ -234,13 +234,13 @@ func (me *crudExecTest) TestScanVals() {

func (me *crudExecTest) TestScanVal() {
t := me.T()
mDb, err := sqlmock.New()
mDb, mock, err := sqlmock.New()
assert.NoError(t, err)

sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
mock.ExpectQuery(`SELECT "id" FROM "items"`).
WillReturnError(fmt.Errorf("query error"))

sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
mock.ExpectQuery(`SELECT "id" FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1"))

Expand Down
Loading

0 comments on commit 54c8a8d

Please sign in to comment.