Fix find and count bug #1618

Merged
lunny merged 1 commits from lunny/fix_find_and_count_bug into master 2 years ago
  1. 9
      session_find.go
  2. 6
      session_find_test.go

9
session_find.go

@ -11,6 +11,7 @@ import ( @@ -11,6 +11,7 @@ import (
"xorm.io/builder"
"xorm.io/xorm/caches"
"xorm.io/xorm/internal/statements"
"xorm.io/xorm/internal/utils"
"xorm.io/xorm/schemas"
)
@ -387,6 +388,12 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in @@ -387,6 +388,12 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
slices := reflect.New(reflect.SliceOf(t))
beans := slices.Interface()
statement := session.statement
session.statement = statements.NewStatement(
session.engine.dialect,
session.engine.tagParser,
session.engine.DatabaseTZ,
)
if len(table.PrimaryKeys) == 1 {
ff := make([]interface{}, 0, len(ides))
for _, ie := range ides {
@ -409,6 +416,8 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in @@ -409,6 +416,8 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
return err
}
session.statement = statement
vs := reflect.Indirect(reflect.ValueOf(beans))
for i := 0; i < vs.Len(); i++ {
rv := vs.Index(i)

6
session_find_test.go

@ -493,6 +493,12 @@ func TestFindAndCountOneFunc(t *testing.T) { @@ -493,6 +493,12 @@ func TestFindAndCountOneFunc(t *testing.T) {
assert.EqualValues(t, 2, cnt)
var results = make([]FindAndCountStruct, 0, 2)
cnt, err = testEngine.Limit(1).FindAndCount(&results)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(results))
assert.EqualValues(t, 2, cnt)
results = make([]FindAndCountStruct, 0, 2)
cnt, err = testEngine.FindAndCount(&results)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(results))

Loading…
Cancel
Save