fix reset colmap when counting distinct cols (#2096)
when using distinct cols with FindAndCount, reset statement.ColumnMap will make the counting sql an syntax error, this pr try fix this. is this pr ok for merge? error sql logging: ```sql [SQL] SELECT DISTINCT `Fid` FROM `table_demo` WHERE Fkey = ? [val] [SQL] SELECT count(DISTINCT ) FROM `table_demo` WHERE Fkey = ? [val] ``` after fix: ```sql [SQL] SELECT DISTINCT `Fid` FROM `table_demo` WHERE Fkey = ? [val] [SQL] SELECT count(DISTINCT `Fid`) FROM `table_demo` WHERE Fkey = ? [val] ``` Co-authored-by: finelog <kaicltw@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/2096 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: finelog <finelog@noreply.gitea.io> Co-committed-by: finelog <finelog@noreply.gitea.io>
This commit is contained in:
parent
e4e830bc78
commit
7802393d01
|
@ -711,6 +711,36 @@ func TestFindAndCountWithGroupBy(t *testing.T) {
|
||||||
assert.EqualValues(t, 2, len(results))
|
assert.EqualValues(t, 2, len(results))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindAndCountWithDistinct(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
type FindAndCountWithDistinct struct {
|
||||||
|
Id int64
|
||||||
|
Age int `xorm:"index"`
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync(new(FindAndCountWithDistinct)))
|
||||||
|
|
||||||
|
_, err := testEngine.Insert([]FindAndCountWithDistinct{
|
||||||
|
{
|
||||||
|
Name: "test1",
|
||||||
|
Age: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "test2",
|
||||||
|
Age: 20,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var results []FindAndCountWithDistinct
|
||||||
|
cnt, err := testEngine.Distinct("`age`").FindAndCount(&results)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 2, cnt)
|
||||||
|
assert.EqualValues(t, 2, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
type FindMapDevice struct {
|
type FindMapDevice struct {
|
||||||
Deviceid string `xorm:"pk"`
|
Deviceid string `xorm:"pk"`
|
||||||
Status int
|
Status int
|
||||||
|
|
|
@ -57,7 +57,7 @@ func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...inte
|
||||||
if session.statement.SelectStr != "" {
|
if session.statement.SelectStr != "" {
|
||||||
session.statement.SelectStr = ""
|
session.statement.SelectStr = ""
|
||||||
}
|
}
|
||||||
if len(session.statement.ColumnMap) > 0 {
|
if len(session.statement.ColumnMap) > 0 && !session.statement.IsDistinct {
|
||||||
session.statement.ColumnMap = []string{}
|
session.statement.ColumnMap = []string{}
|
||||||
}
|
}
|
||||||
if session.statement.OrderStr != "" {
|
if session.statement.OrderStr != "" {
|
||||||
|
|
Loading…
Reference in New Issue