fix cols and distinct conflicts (#927)
This commit is contained in:
parent
12e0367559
commit
0f339654dd
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/go-xorm/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var colStrTests = []struct {
|
||||
|
@ -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])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue