小优化,查询添加 column函数
This commit is contained in:
parent
f28c41c84a
commit
4ec03cba9b
|
@ -2,22 +2,26 @@ package dao
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/fthvgb1/wp-go/helper/number"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// RecentComments
|
||||
// param context.Context
|
||||
func RecentComments(a ...any) (r []models.Comments, err error) {
|
||||
ctx := a[0].(context.Context)
|
||||
return model.Find[models.Comments](ctx, model.SqlBuilder{
|
||||
return model.Finds[models.Comments](ctx, model.Conditions(
|
||||
model.Where(model.SqlBuilder{
|
||||
{"comment_approved", "1"},
|
||||
{"post_status", "publish"},
|
||||
}, "comment_ID,comment_author,comment_post_ID,post_title", "", model.SqlBuilder{{"comment_date_gmt", "desc"}}, model.SqlBuilder{
|
||||
{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"},
|
||||
}, nil, 10)
|
||||
}),
|
||||
model.Fields("comment_ID,comment_author,comment_post_ID,post_title"),
|
||||
model.Order(model.SqlBuilder{{"comment_date_gmt", "desc"}}),
|
||||
model.Join(model.SqlBuilder{{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}}),
|
||||
model.Limit(10),
|
||||
))
|
||||
}
|
||||
|
||||
// PostComments
|
||||
|
@ -26,13 +30,17 @@ func RecentComments(a ...any) (r []models.Comments, err error) {
|
|||
func PostComments(args ...any) ([]uint64, error) {
|
||||
ctx := args[0].(context.Context)
|
||||
postId := args[1].(uint64)
|
||||
r, err := model.Find[models.Comments](ctx, model.SqlBuilder{
|
||||
r, err := model.Finds[models.Comments](ctx, model.Conditions(
|
||||
model.Where(model.SqlBuilder{
|
||||
{"comment_approved", "1"},
|
||||
{"comment_post_ID", "=", strconv.FormatUint(postId, 10), "int"},
|
||||
}, "comment_ID", "", model.SqlBuilder{
|
||||
{"comment_post_ID", "=", number.ToString(postId), "int"},
|
||||
}),
|
||||
model.Fields("comment_ID"),
|
||||
model.Order(model.SqlBuilder{
|
||||
{"comment_date_gmt", "asc"},
|
||||
{"comment_ID", "asc"},
|
||||
}, nil, nil, 0)
|
||||
})),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -169,3 +170,29 @@ func Pagination[T Model](ctx context.Context, q *QueryCondition) ([]T, int, erro
|
|||
func DBPagination[T Model](db dbQuery, ctx context.Context, q *QueryCondition) ([]T, int, error) {
|
||||
return pagination[T](db, ctx, q.where, q.fields, q.group, q.page, q.limit, q.order, q.join, q.having, q.in...)
|
||||
}
|
||||
|
||||
func Column[V Model, T any](ctx context.Context, fn func(V) (T, bool), q *QueryCondition) (r []T, err error) {
|
||||
res, err := finds[V](globalBb, ctx, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r = slice.FilterAndMap(res, fn)
|
||||
return
|
||||
}
|
||||
func DBColumn[V Model, T any](db dbQuery, ctx context.Context, fn func(V) (T, bool), q *QueryCondition) (r []T, err error) {
|
||||
res, err := finds[V](db, ctx, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r = slice.FilterAndMap(res, fn)
|
||||
return
|
||||
}
|
||||
|
||||
func column[V Model, T any](db dbQuery, ctx context.Context, fn func(V) (T, bool), q *QueryCondition) (r []T, err error) {
|
||||
res, err := finds[V](db, ctx, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r = slice.FilterAndMap(res, fn)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -230,3 +230,46 @@ func TestPagination(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestColumn(t *testing.T) {
|
||||
type args[V Model, T any] struct {
|
||||
ctx context.Context
|
||||
fn func(V) (T, bool)
|
||||
q *QueryCondition
|
||||
}
|
||||
type testCase[V Model, T any] struct {
|
||||
name string
|
||||
args args[V, T]
|
||||
wantR []T
|
||||
wantErr bool
|
||||
}
|
||||
tests := []testCase[post, uint64]{
|
||||
{
|
||||
name: "t1",
|
||||
args: args[post, uint64]{
|
||||
ctx: ctx,
|
||||
fn: func(t post) (uint64, bool) {
|
||||
return t.Id, true
|
||||
},
|
||||
q: Conditions(
|
||||
Where(SqlBuilder{
|
||||
{"ID", "<", "200", "int"},
|
||||
}),
|
||||
),
|
||||
},
|
||||
wantR: []uint64{63, 64, 190, 193},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotR, err := Column[post](tt.args.ctx, tt.args.fn, tt.args.q)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Column() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(gotR, tt.wantR) {
|
||||
t.Errorf("Column() gotR = %v, want %v", gotR, tt.wantR)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user