Browse Source

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: #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: #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: #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: #941
pull/1513/head
Lunny Xiao 5 months ago
parent
commit
14a0c19a0c
2 changed files with 26 additions and 12 deletions
  1. +15
    -12
      session_iterate.go
  2. +11
    -0
      session_iterate_test.go

+ 15
- 12
session_iterate.go View File

@ -4,7 +4,9 @@
package xorm
import "reflect"
import (
"reflect"
)
// IterFunc only use by Iterate
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 {
if session.isAutoClose {
defer session.Close()
}
var bufferSize = session.statement.bufferSize
var limit = session.statement.LimitN
if limit > 0 && bufferSize > limit {
@ -73,9 +71,14 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
v := rValue(bean)
sliceType := reflect.SliceOf(v.Type())
var idx = 0
for {
session.autoResetStatement = false
defer func() {
session.autoResetStatement = true
}()
for bufferSize > 0 {
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
}
@ -86,13 +89,13 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
idx++
}
start = start + slice.Elem().Len()
if limit > 0 && idx+bufferSize > limit {
bufferSize = limit - idx
if bufferSize > slice.Elem().Len() {
break
}
if bufferSize <= 0 || slice.Elem().Len() < bufferSize || idx == limit {
break
start = start + slice.Elem().Len()
if limit > 0 && start+bufferSize > limit {
bufferSize = limit - start
}
}

+ 11
- 0
session_iterate_test.go View File

@ -89,4 +89,15 @@ func TestBufferIterate(t *testing.T) {
})
assert.NoError(t, err)
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…
Cancel
Save