fix bug when buffersize with iterate (#941)
Merge branch 'master' into lunny/fix_buffer_iterate Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 fix test fix bug fix bug when buffersize with iterate SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Co-authored-by: Guillermo Prandi <guillep2k@noreply.gitea.io> Reviewed-on: https://gitea.com/xorm/xorm/pulls/941
This commit is contained in:
parent
bd20ffba3b
commit
14a0c19a0c
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import "reflect"
|
import (
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
// IterFunc only use by Iterate
|
// IterFunc only use by Iterate
|
||||||
type IterFunc func(idx int, bean interface{}) error
|
type IterFunc func(idx int, bean interface{}) error
|
||||||
|
@ -60,10 +62,6 @@ func (session *Session) BufferSize(size int) *Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
|
func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
|
||||||
if session.isAutoClose {
|
|
||||||
defer session.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
var bufferSize = session.statement.bufferSize
|
var bufferSize = session.statement.bufferSize
|
||||||
var limit = session.statement.LimitN
|
var limit = session.statement.LimitN
|
||||||
if limit > 0 && bufferSize > limit {
|
if limit > 0 && bufferSize > limit {
|
||||||
|
@ -73,9 +71,14 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
|
||||||
v := rValue(bean)
|
v := rValue(bean)
|
||||||
sliceType := reflect.SliceOf(v.Type())
|
sliceType := reflect.SliceOf(v.Type())
|
||||||
var idx = 0
|
var idx = 0
|
||||||
for {
|
session.autoResetStatement = false
|
||||||
|
defer func() {
|
||||||
|
session.autoResetStatement = true
|
||||||
|
}()
|
||||||
|
|
||||||
|
for bufferSize > 0 {
|
||||||
slice := reflect.New(sliceType)
|
slice := reflect.New(sliceType)
|
||||||
if err := session.Limit(bufferSize, start).find(slice.Interface(), bean); err != nil {
|
if err := session.NoCache().Limit(bufferSize, start).find(slice.Interface(), bean); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,13 +89,13 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
|
||||||
idx++
|
idx++
|
||||||
}
|
}
|
||||||
|
|
||||||
start = start + slice.Elem().Len()
|
if bufferSize > slice.Elem().Len() {
|
||||||
if limit > 0 && idx+bufferSize > limit {
|
break
|
||||||
bufferSize = limit - idx
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if bufferSize <= 0 || slice.Elem().Len() < bufferSize || idx == limit {
|
start = start + slice.Elem().Len()
|
||||||
break
|
if limit > 0 && start+bufferSize > limit {
|
||||||
|
bufferSize = limit - start
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -89,4 +89,15 @@ func TestBufferIterate(t *testing.T) {
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 7, cnt)
|
assert.EqualValues(t, 7, cnt)
|
||||||
|
|
||||||
|
cnt = 0
|
||||||
|
err = testEngine.Where("id <= 10").BufferSize(2).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
|
||||||
|
user := bean.(*UserBufferIterate)
|
||||||
|
assert.EqualValues(t, cnt+1, user.Id)
|
||||||
|
assert.EqualValues(t, true, user.IsMan)
|
||||||
|
cnt++
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 10, cnt)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue