From fa64df6f918f3affe18ed9b1370454ba30845bc3 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sun, 1 Aug 2021 10:33:33 +0800 Subject: [PATCH] improve package gdb/glog --- database/gdb/gdb_model.go | 4 ++++ database/gdb/gdb_model_select.go | 12 ++++++------ database/gdb/gdb_type_result.go | 1 + os/glog/glog_z_unit_chaining_test.go | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/database/gdb/gdb_model.go b/database/gdb/gdb_model.go index cd7900d9b..195e551cd 100644 --- a/database/gdb/gdb_model.go +++ b/database/gdb/gdb_model.go @@ -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. diff --git a/database/gdb/gdb_model_select.go b/database/gdb/gdb_model_select.go index e1690743e..78fc25735 100644 --- a/database/gdb/gdb_model_select.go +++ b/database/gdb/gdb_model_select.go @@ -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++ diff --git a/database/gdb/gdb_type_result.go b/database/gdb/gdb_type_result.go index dd2764880..cfe1ba92a 100644 --- a/database/gdb/gdb_type_result.go +++ b/database/gdb/gdb_type_result.go @@ -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 { diff --git a/os/glog/glog_z_unit_chaining_test.go b/os/glog/glog_z_unit_chaining_test.go index 1be9d3a86..f72f88180 100644 --- a/os/glog/glog_z_unit_chaining_test.go +++ b/os/glog/glog_z_unit_chaining_test.go @@ -140,7 +140,7 @@ func Test_StackWithFilter(t *testing.T) { t.Assert(err, nil) defer gfile.Remove(path) - Path(path).File(file).StackWithFilter("gogf").Stdout(false).Error(1, 2, 3) + Path(path).File(file).StackWithFilter("/gf/").Stdout(false).Error(1, 2, 3) content := gfile.GetContents(gfile.Join(path, file)) t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 1) t.Assert(gstr.Count(content, "1 2 3"), 1)