Skip to content

Commit

Permalink
v2.0.1
Browse files Browse the repository at this point in the history
* Fixed issue when `ScanStruct(s)` was used with `SelectDistinct` and caused a panic.
  • Loading branch information
doug-martin committed May 27, 2015
1 parent 3e0b8f8 commit 39ce306
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.0.1

* Fixed issue when `ScanStruct(s)` was used with `SelectDistinct` and caused a panic.

## v2.0.0

* When scanning a struct or slice of structs, the struct(s) will be parsed for the column names to select. [#9](https://github.com/doug-martin/goqu/pull/9) - [@technotronicoz](https://github.com/TechnotronicOz)
Expand Down
24 changes: 24 additions & 0 deletions dataset_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (me *datasetTest) TestScanStructs() {
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

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

sqlmock.ExpectQuery(`SELECT "test" FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
Expand All @@ -32,6 +36,15 @@ func (me *datasetTest) TestScanStructs() {
assert.Equal(t, items[1].Address, "211 Test Addr")
assert.Equal(t, items[1].Name, "Test2")

items = items[0:0]
assert.NoError(t, db.From("items").SelectDistinct("name").ScanStructs(&items))
assert.Len(t, items, 2)
assert.Equal(t, items[0].Address, "111 Test Addr")
assert.Equal(t, items[0].Name, "Test1")

assert.Equal(t, items[1].Address, "211 Test Addr")
assert.Equal(t, items[1].Name, "Test2")

items = items[0:0]
assert.EqualError(t, db.From("items").ScanStructs(items), "goqu: Type must be a pointer to a slice when calling ScanStructs")
assert.EqualError(t, db.From("items").ScanStructs(&dsTestActionItem{}), "goqu: Type must be a pointer to a slice when calling ScanStructs")
Expand Down Expand Up @@ -78,6 +91,10 @@ func (me *datasetTest) TestScanStruct() {
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))

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

sqlmock.ExpectQuery(`SELECT "test" FROM "items" LIMIT 1`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
Expand All @@ -90,6 +107,13 @@ func (me *datasetTest) TestScanStruct() {
assert.Equal(t, item.Address, "111 Test Addr")
assert.Equal(t, item.Name, "Test1")

item = dsTestActionItem{}
found, err = db.From("items").SelectDistinct("name").ScanStruct(&item)
assert.NoError(t, err)
assert.True(t, found)
assert.Equal(t, item.Address, "111 Test Addr")
assert.Equal(t, item.Name, "Test1")

_, err = db.From("items").ScanStruct(item)
assert.EqualError(t, err, "goqu: Type must be a pointer to a struct when calling ScanStruct")
_, err = db.From("items").ScanStruct([]dsTestActionItem{})
Expand Down
10 changes: 6 additions & 4 deletions dataset_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ func (me *Dataset) ClearSelect() *Dataset {
//Returns true if using default SELECT *
func (me *Dataset) isDefaultSelect() bool {
ret := false
selects := me.clauses.Select.Columns()
if len(selects) == 1 {
if l, ok := selects[0].(LiteralExpression); ok && l.Literal() == "*" {
ret = true
if me.clauses.Select != nil {
selects := me.clauses.Select.Columns()
if len(selects) == 1 {
if l, ok := selects[0].(LiteralExpression); ok && l.Literal() == "*" {
ret = true
}
}
}
return ret
Expand Down
42 changes: 41 additions & 1 deletion dataset_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (me *datasetTest) TestSelect() {
assert.Equal(t, sql, `SELECT * FROM "test"`)
}

func (me *datasetTest) TestDistinctSelect() {
func (me *datasetTest) TestSelectDistinct() {
t := me.T()
ds1 := From("test")

Expand All @@ -121,6 +121,46 @@ func (me *datasetTest) TestDistinctSelect() {
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT DISTINCT "id" AS "other_id", COUNT(*) AS "count" FROM "test"`)

type myStruct struct {
Name string
Address string `db:"address"`
EmailAddress string `db:"email_address"`
}
sql, _, err = ds1.SelectDistinct(&myStruct{}).ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT DISTINCT "address", "email_address", "name" FROM "test"`)

sql, _, err = ds1.SelectDistinct(myStruct{}).ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT DISTINCT "address", "email_address", "name" FROM "test"`)

type myStruct2 struct {
myStruct
Zipcode string `db:"zipcode"`
}

sql, _, err = ds1.SelectDistinct(&myStruct2{}).ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT DISTINCT "address", "email_address", "name", "zipcode" FROM "test"`)

sql, _, err = ds1.SelectDistinct(myStruct2{}).ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT DISTINCT "address", "email_address", "name", "zipcode" FROM "test"`)

var myStructs []myStruct
sql, _, err = ds1.SelectDistinct(&myStructs).ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT DISTINCT "address", "email_address", "name" FROM "test"`)

sql, _, err = ds1.SelectDistinct(myStructs).ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT DISTINCT "address", "email_address", "name" FROM "test"`)

//should not change original
sql, _, err = ds1.ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT * FROM "test"`)

//should not change original
sql, _, err = ds1.ToSql()
assert.NoError(t, err)
Expand Down

0 comments on commit 39ce306

Please sign in to comment.