From b4cc570e8a23b87d03c2124298edb60550aed1c9 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 26 Feb 2023 21:55:03 +0800 Subject: [PATCH] =?UTF-8?q?map=E7=9A=84=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/query.go | 45 +++++++++++++++++++++++++++------ model/query_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/model/query.go b/model/query.go index fb2268e..d5ea473 100644 --- a/model/query.go +++ b/model/query.go @@ -10,6 +10,9 @@ import ( ) func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []T, total int, err error) { + if q.Page < 1 { + return + } qx := QueryCondition{ Where: q.Where, Having: q.Having, @@ -46,14 +49,42 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r [ return } q.Offset = offset - sq, args, err := BuildQuerySql[T](q) - if err != nil { - return - } - err = db.Select(ctx, &r, sq, args...) - if err != nil { - return + m := ctx.Value("handle=>") + if m != nil { + mm, ok := m.(string) + if ok && mm == "toMap" { + v := ctx.Value("map") + mx, er := findToStringMap[T](db, ctx, q) + if er != nil { + err = er + return + } + vv := v.(*[]map[string]string) + *vv = mx + return + } } + r, err = finds[T](db, ctx, q) + return +} + +func paginationToMap[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) { + ctx = context.WithValue(ctx, "handle=>", "toMap") + ctx = context.WithValue(ctx, "map", &r) + _, total, err = pagination[T](db, ctx, q) + return +} + +func PaginationToMap[T Model](ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) { + ctx = context.WithValue(ctx, "handle=>", "toMap") + ctx = context.WithValue(ctx, "map", &r) + _, total, err = pagination[T](globalBb, ctx, q) + return +} +func PaginationToMapFromDB[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) { + ctx = context.WithValue(ctx, "handle=>", "toMap") + ctx = context.WithValue(ctx, "map", &r) + _, total, err = pagination[T](db, ctx, q) return } diff --git a/model/query_test.go b/model/query_test.go index a44f726..e2aff9e 100644 --- a/model/query_test.go +++ b/model/query_test.go @@ -516,3 +516,64 @@ func Test_pagination(t *testing.T) { }) } } + +func Test_paginationToMap(t *testing.T) { + type args struct { + db dbQuery + ctx context.Context + q QueryCondition + } + tests := []struct { + name string + args args + wantR []map[string]string + wantTotal int + wantErr bool + }{ + { + name: "t1", + args: args{ + db: glob, + ctx: ctx, + q: QueryCondition{ + Fields: "ID", + Limit: 2, + Page: 1, + Where: SqlBuilder{{"ID < 200"}}, + }, + }, + wantR: []map[string]string{{"ID": "63"}, {"ID": "64"}}, + wantTotal: 4, + }, + { + name: "t2", + args: args{ + db: glob, + ctx: ctx, + q: QueryCondition{ + Fields: "ID", + Limit: 2, + Page: 2, + Where: SqlBuilder{{"ID < 200"}}, + }, + }, + wantR: []map[string]string{{"ID": "190"}, {"ID": "193"}}, + wantTotal: 4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotR, gotTotal, err := paginationToMap[post](tt.args.db, tt.args.ctx, tt.args.q) + if (err != nil) != tt.wantErr { + t.Errorf("paginationToMap() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotR, tt.wantR) { + t.Errorf("paginationToMap() gotR = %v, want %v", gotR, tt.wantR) + } + if gotTotal != tt.wantTotal { + t.Errorf("paginationToMap() gotTotal = %v, want %v", gotTotal, tt.wantTotal) + } + }) + } +}