mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve package gdb/glog
This commit is contained in:
@ -56,6 +56,10 @@ type Model struct {
|
||||
// ModelHandler is a function that handles given Model and returns a new Model that is custom modified.
|
||||
type ModelHandler func(m *Model) *Model
|
||||
|
||||
// ChunkHandler is a function that is used in function Chunk, which handles given Result and error.
|
||||
// It returns true if it wants continue chunking, or else it returns false to stop chunking.
|
||||
type ChunkHandler func(result Result, err error) bool
|
||||
|
||||
// whereHolder is the holder for where condition preparing.
|
||||
type whereHolder struct {
|
||||
operator int // Operator for this holder.
|
||||
|
||||
@ -101,27 +101,27 @@ func (m *Model) getFieldsFiltered() string {
|
||||
return newFields
|
||||
}
|
||||
|
||||
// Chunk iterates the query result with given size and callback function.
|
||||
func (m *Model) Chunk(limit int, callback func(result Result, err error) bool) {
|
||||
// Chunk iterates the query result with given `size` and `handler` function.
|
||||
func (m *Model) Chunk(size int, handler ChunkHandler) {
|
||||
page := m.start
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
model := m
|
||||
for {
|
||||
model = model.Page(page, limit)
|
||||
model = model.Page(page, size)
|
||||
data, err := model.All()
|
||||
if err != nil {
|
||||
callback(nil, err)
|
||||
handler(nil, err)
|
||||
break
|
||||
}
|
||||
if len(data) == 0 {
|
||||
break
|
||||
}
|
||||
if callback(data, err) == false {
|
||||
if handler(data, err) == false {
|
||||
break
|
||||
}
|
||||
if len(data) < limit {
|
||||
if len(data) < size {
|
||||
break
|
||||
}
|
||||
page++
|
||||
|
||||
@ -77,6 +77,7 @@ func (r Result) List() List {
|
||||
|
||||
// Array retrieves and returns specified column values as slice.
|
||||
// The parameter `field` is optional is the column field is only one.
|
||||
// The default `field` is the first field name of the first item in `Result` if parameter `field` is not given.
|
||||
func (r Result) Array(field ...string) []Value {
|
||||
array := make([]Value, len(r))
|
||||
if len(r) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user