Skip to content

Commit

Permalink
fix cols and distinct conflicts (go-xorm#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored May 4, 2018
1 parent 12e0367 commit 0f33965
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions session_cols.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (m columnMap) contain(colName string) bool {
return false
}

func (m *columnMap) add(colName string) bool {
if m.contain(colName) {
return false
}
*m = append(*m, colName)
return true
}

func setColumnInt(bean interface{}, col *core.Column, t int64) {
v, err := col.ValueOf(bean)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func (statement *Statement) Select(str string) *Statement {
func (statement *Statement) Cols(columns ...string) *Statement {
cols := col2NewCols(columns...)
for _, nc := range cols {
statement.columnMap = append(statement.columnMap, nc)
statement.columnMap.add(nc)
}

newColumns := statement.colmap2NewColsWithQuote()
Expand Down
23 changes: 23 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/go-xorm/core"
"github.com/stretchr/testify/assert"
)

var colStrTests = []struct {
Expand Down Expand Up @@ -180,3 +181,25 @@ func createTestStatement() *Statement {
}
return nil
}

func TestDistinctAndCols(t *testing.T) {
type DistinctAndCols struct {
Id int64
Name string
}

assert.NoError(t, prepareEngine())
assertSync(t, new(DistinctAndCols))

cnt, err := testEngine.Insert(&DistinctAndCols{
Name: "test",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

var names []string
err = testEngine.Table("distinct_and_cols").Cols("name").Distinct("name").Find(&names)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(names))
assert.EqualValues(t, "test", names[0])
}

0 comments on commit 0f33965

Please sign in to comment.