fix Count error when have Select statement

This commit is contained in:
jiangyanfeng 2018-12-08 17:09:30 +08:00 committed by Lunny Xiao
parent e6786ce052
commit 7b921a0f51
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 8 additions and 0 deletions

View File

@ -30,6 +30,10 @@ func (session *Session) Count(bean ...interface{}) (int64, error) {
args = session.statement.RawParams
}
if len(session.statement.selectStr) > 0 {
sqlStr = "SELECT COUNT(*) FROM ("+sqlStr+") _TEMP_"
}
var total int64
err = session.queryRow(sqlStr, args...).Scan(&total)
if err == sql.ErrNoRows || err == nil {

View File

@ -182,6 +182,10 @@ func TestCount(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
total, err = testEngine.Select(colName).Where(cond).Count(new(UserinfoCount))
assert.NoError(t, err)
assert.EqualValues(t, 1, total)
total, err = testEngine.Where(cond).Count(new(UserinfoCount))
assert.NoError(t, err)
assert.EqualValues(t, 1, total)