fix cols and distinct conflicts (#927)

This commit is contained in:
Lunny Xiao 2018-05-04 13:09:54 +08:00 committed by GitHub
parent 12e0367559
commit 0f339654dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View File

@ -47,6 +47,14 @@ func (m columnMap) contain(colName string) bool {
return false 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) { func setColumnInt(bean interface{}, col *core.Column, t int64) {
v, err := col.ValueOf(bean) v, err := col.ValueOf(bean)
if err != nil { if err != nil {

View File

@ -624,7 +624,7 @@ func (statement *Statement) Select(str string) *Statement {
func (statement *Statement) Cols(columns ...string) *Statement { func (statement *Statement) Cols(columns ...string) *Statement {
cols := col2NewCols(columns...) cols := col2NewCols(columns...)
for _, nc := range cols { for _, nc := range cols {
statement.columnMap = append(statement.columnMap, nc) statement.columnMap.add(nc)
} }
newColumns := statement.colmap2NewColsWithQuote() newColumns := statement.colmap2NewColsWithQuote()

View File

@ -10,6 +10,7 @@ import (
"testing" "testing"
"github.com/go-xorm/core" "github.com/go-xorm/core"
"github.com/stretchr/testify/assert"
) )
var colStrTests = []struct { var colStrTests = []struct {
@ -180,3 +181,25 @@ func createTestStatement() *Statement {
} }
return nil 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])
}