improve package gdb/glog

This commit is contained in:
John Guo
2021-08-01 10:33:33 +08:00
parent 0acd118c03
commit fa64df6f91
4 changed files with 12 additions and 7 deletions

View File

@ -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.

View File

@ -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++

View File

@ -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 {

View File

@ -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)