Add test for count with orderby and limit (#880)

* add test for count with orderby and limit

* fix the bug
This commit is contained in:
Lunny Xiao 2018-04-10 12:52:47 +08:00 committed by GitHub
parent 99ab88dbcf
commit 590c04df2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 8 deletions

View File

@ -161,7 +161,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
}
args = append(session.statement.joinArgs, condArgs...)
sqlStr, err = session.statement.genSelectSQL(columnStr, condSQL, true)
sqlStr, err = session.statement.genSelectSQL(columnStr, condSQL, true, true)
if err != nil {
return err
}

View File

@ -70,7 +70,7 @@ func (session *Session) genQuerySQL(sqlorArgs ...interface{}) (string, []interfa
}
args := append(session.statement.joinArgs, condArgs...)
sqlStr, err := session.statement.genSelectSQL(columnStr, condSQL, true)
sqlStr, err := session.statement.genSelectSQL(columnStr, condSQL, true, true)
if err != nil {
return "", nil, err
}

View File

@ -158,3 +158,28 @@ func TestSQLCount(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 0, total)
}
func TestCountWithOthers(t *testing.T) {
assert.NoError(t, prepareEngine())
type CountWithOthers struct {
Id int64
Name string
}
assertSync(t, new(CountWithOthers))
_, err := testEngine.Insert(&CountWithOthers{
Name: "orderby",
})
assert.NoError(t, err)
_, err = testEngine.Insert(&CountWithOthers{
Name: "limit",
})
assert.NoError(t, err)
total, err := testEngine.OrderBy("id desc").Limit(1).Count(new(CountWithOthers))
assert.NoError(t, err)
assert.EqualValues(t, 2, total)
}

View File

@ -971,7 +971,7 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{},
return "", nil, err
}
sqlStr, err := statement.genSelectSQL(columnStr, condSQL, true)
sqlStr, err := statement.genSelectSQL(columnStr, condSQL, true, true)
if err != nil {
return "", nil, err
}
@ -1001,7 +1001,7 @@ func (statement *Statement) genCountSQL(beans ...interface{}) (string, []interfa
selectSQL = "count(*)"
}
}
sqlStr, err := statement.genSelectSQL(selectSQL, condSQL, false)
sqlStr, err := statement.genSelectSQL(selectSQL, condSQL, false, false)
if err != nil {
return "", nil, err
}
@ -1026,7 +1026,7 @@ func (statement *Statement) genSumSQL(bean interface{}, columns ...string) (stri
return "", nil, err
}
sqlStr, err := statement.genSelectSQL(sumSelect, condSQL, true)
sqlStr, err := statement.genSelectSQL(sumSelect, condSQL, true, true)
if err != nil {
return "", nil, err
}
@ -1034,7 +1034,7 @@ func (statement *Statement) genSumSQL(bean interface{}, columns ...string) (stri
return sqlStr, append(statement.joinArgs, condArgs...), nil
}
func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit bool) (a string, err error) {
func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, needOrderBy bool) (a string, err error) {
var distinct string
if statement.IsDistinct && !strings.HasPrefix(columnStr, "count") {
distinct = "DISTINCT "
@ -1101,9 +1101,10 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit bo
}
var orderStr string
if len(statement.OrderStr) > 0 {
if needOrderBy && len(statement.OrderStr) > 0 {
orderStr = " ORDER BY " + statement.OrderStr
}
var groupStr string
if len(statement.GroupByStr) > 0 {
groupStr = " GROUP BY " + statement.GroupByStr
@ -1129,7 +1130,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit bo
if statement.HavingStr != "" {
a = fmt.Sprintf("%v %v", a, statement.HavingStr)
}
if statement.OrderStr != "" {
if needOrderBy && statement.OrderStr != "" {
a = fmt.Sprintf("%v ORDER BY %v", a, statement.OrderStr)
}
if needLimit {