File tree Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,14 @@ func (m columnMap) contain(colName string) bool {
47
47
return false
48
48
}
49
49
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
+
50
58
func setColumnInt (bean interface {}, col * core.Column , t int64 ) {
51
59
v , err := col .ValueOf (bean )
52
60
if err != nil {
Original file line number Diff line number Diff line change @@ -624,7 +624,7 @@ func (statement *Statement) Select(str string) *Statement {
624
624
func (statement * Statement ) Cols (columns ... string ) * Statement {
625
625
cols := col2NewCols (columns ... )
626
626
for _ , nc := range cols {
627
- statement .columnMap = append ( statement . columnMap , nc )
627
+ statement .columnMap . add ( nc )
628
628
}
629
629
630
630
newColumns := statement .colmap2NewColsWithQuote ()
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import (
10
10
"testing"
11
11
12
12
"github.com/go-xorm/core"
13
+ "github.com/stretchr/testify/assert"
13
14
)
14
15
15
16
var colStrTests = []struct {
@@ -180,3 +181,25 @@ func createTestStatement() *Statement {
180
181
}
181
182
return nil
182
183
}
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
+ }
You can’t perform that action at this time.
0 commit comments