Skip to content

Commit 0f33965

Browse files
authored
fix cols and distinct conflicts (go-xorm#927)
1 parent 12e0367 commit 0f33965

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

session_cols.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ func (m columnMap) contain(colName string) bool {
4747
return false
4848
}
4949

50+
func (m *columnMap) add(colName string) bool {
51+
if m.contain(colName) {
52+
return false
53+
}
54+
*m = append(*m, colName)
55+
return true
56+
}
57+
5058
func setColumnInt(bean interface{}, col *core.Column, t int64) {
5159
v, err := col.ValueOf(bean)
5260
if err != nil {

statement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ func (statement *Statement) Select(str string) *Statement {
624624
func (statement *Statement) Cols(columns ...string) *Statement {
625625
cols := col2NewCols(columns...)
626626
for _, nc := range cols {
627-
statement.columnMap = append(statement.columnMap, nc)
627+
statement.columnMap.add(nc)
628628
}
629629

630630
newColumns := statement.colmap2NewColsWithQuote()

statement_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/go-xorm/core"
13+
"github.com/stretchr/testify/assert"
1314
)
1415

1516
var colStrTests = []struct {
@@ -180,3 +181,25 @@ func createTestStatement() *Statement {
180181
}
181182
return nil
182183
}
184+
185+
func TestDistinctAndCols(t *testing.T) {
186+
type DistinctAndCols struct {
187+
Id int64
188+
Name string
189+
}
190+
191+
assert.NoError(t, prepareEngine())
192+
assertSync(t, new(DistinctAndCols))
193+
194+
cnt, err := testEngine.Insert(&DistinctAndCols{
195+
Name: "test",
196+
})
197+
assert.NoError(t, err)
198+
assert.EqualValues(t, 1, cnt)
199+
200+
var names []string
201+
err = testEngine.Table("distinct_and_cols").Cols("name").Distinct("name").Find(&names)
202+
assert.NoError(t, err)
203+
assert.EqualValues(t, 1, len(names))
204+
assert.EqualValues(t, "test", names[0])
205+
}

0 commit comments

Comments
 (0)