Count will ignore order by as before (#2307)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2307
This commit is contained in:
parent
4109ce1e23
commit
9aab1f689c
|
@ -125,6 +125,11 @@ func TestWithTableName(t *testing.T) {
|
|||
total, err = testEngine.OrderBy("count(`id`) desc").Count(CountWithTableName{})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 2, total)
|
||||
|
||||
// the orderby will be ignored by count because some databases will return errors if the orderby columns not in group by
|
||||
total, err = testEngine.OrderBy("`name`").Count(CountWithTableName{})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 2, total)
|
||||
}
|
||||
|
||||
func TestCountWithSelectCols(t *testing.T) {
|
||||
|
|
|
@ -35,7 +35,7 @@ func (statement *Statement) GenQuerySQL(sqlOrArgs ...interface{}) (string, []int
|
|||
}
|
||||
|
||||
buf := builder.NewWriter()
|
||||
if err := statement.writeSelect(buf, statement.genSelectColumnStr(), true); err != nil {
|
||||
if err := statement.writeSelect(buf, statement.genSelectColumnStr(), true, true); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return buf.String(), buf.Args(), nil
|
||||
|
@ -66,7 +66,7 @@ func (statement *Statement) GenSumSQL(bean interface{}, columns ...string) (stri
|
|||
}
|
||||
|
||||
buf := builder.NewWriter()
|
||||
if err := statement.writeSelect(buf, strings.Join(sumStrs, ", "), true); err != nil {
|
||||
if err := statement.writeSelect(buf, strings.Join(sumStrs, ", "), true, true); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return buf.String(), buf.Args(), nil
|
||||
|
@ -122,7 +122,7 @@ func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{},
|
|||
}
|
||||
|
||||
buf := builder.NewWriter()
|
||||
if err := statement.writeSelect(buf, columnStr, true); err != nil {
|
||||
if err := statement.writeSelect(buf, columnStr, true, true); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return buf.String(), buf.Args(), nil
|
||||
|
@ -153,12 +153,6 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
|
|||
selectSQL = "count(*)"
|
||||
}
|
||||
}
|
||||
var subQuerySelect string
|
||||
if statement.GroupByStr != "" {
|
||||
subQuerySelect = statement.GroupByStr
|
||||
} else {
|
||||
subQuerySelect = selectSQL
|
||||
}
|
||||
|
||||
buf := builder.NewWriter()
|
||||
if statement.GroupByStr != "" {
|
||||
|
@ -167,7 +161,14 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
|
|||
}
|
||||
}
|
||||
|
||||
if err := statement.writeSelect(buf, subQuerySelect, false); err != nil {
|
||||
var subQuerySelect string
|
||||
if statement.GroupByStr != "" {
|
||||
subQuerySelect = statement.GroupByStr
|
||||
} else {
|
||||
subQuerySelect = selectSQL
|
||||
}
|
||||
|
||||
if err := statement.writeSelect(buf, subQuerySelect, false, false); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
|
@ -364,7 +365,7 @@ func (statement *Statement) writeOracleLimit(w *builder.BytesWriter, columnStr s
|
|||
return err
|
||||
}
|
||||
|
||||
func (statement *Statement) writeSelect(buf *builder.BytesWriter, columnStr string, needLimit bool) error {
|
||||
func (statement *Statement) writeSelect(buf *builder.BytesWriter, columnStr string, needLimit, needOrderBy bool) error {
|
||||
if err := statement.writeSelectColumns(buf, columnStr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -380,8 +381,10 @@ func (statement *Statement) writeSelect(buf *builder.BytesWriter, columnStr stri
|
|||
if err := statement.writeHaving(buf); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := statement.writeOrderBys(buf); err != nil {
|
||||
return err
|
||||
if needOrderBy {
|
||||
if err := statement.writeOrderBys(buf); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
dialect := statement.dialect
|
||||
|
@ -519,7 +522,7 @@ func (statement *Statement) GenFindSQL(autoCond builder.Cond) (string, []interfa
|
|||
statement.cond = statement.cond.And(autoCond)
|
||||
|
||||
buf := builder.NewWriter()
|
||||
if err := statement.writeSelect(buf, statement.genSelectColumnStr(), true); err != nil {
|
||||
if err := statement.writeSelect(buf, statement.genSelectColumnStr(), true, true); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return buf.String(), buf.Args(), nil
|
||||
|
|
Loading…
Reference in New Issue