小优化,查询添加 column函数
This commit is contained in:
parent
f28c41c84a
commit
4ec03cba9b
|
@ -2,22 +2,26 @@ package dao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/number"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/model"
|
"github.com/fthvgb1/wp-go/model"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RecentComments
|
// RecentComments
|
||||||
// param context.Context
|
// param context.Context
|
||||||
func RecentComments(a ...any) (r []models.Comments, err error) {
|
func RecentComments(a ...any) (r []models.Comments, err error) {
|
||||||
ctx := a[0].(context.Context)
|
ctx := a[0].(context.Context)
|
||||||
return model.Find[models.Comments](ctx, model.SqlBuilder{
|
return model.Finds[models.Comments](ctx, model.Conditions(
|
||||||
{"comment_approved", "1"},
|
model.Where(model.SqlBuilder{
|
||||||
{"post_status", "publish"},
|
{"comment_approved", "1"},
|
||||||
}, "comment_ID,comment_author,comment_post_ID,post_title", "", model.SqlBuilder{{"comment_date_gmt", "desc"}}, model.SqlBuilder{
|
{"post_status", "publish"},
|
||||||
{"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
|
// PostComments
|
||||||
|
@ -26,13 +30,17 @@ func RecentComments(a ...any) (r []models.Comments, err error) {
|
||||||
func PostComments(args ...any) ([]uint64, error) {
|
func PostComments(args ...any) ([]uint64, error) {
|
||||||
ctx := args[0].(context.Context)
|
ctx := args[0].(context.Context)
|
||||||
postId := args[1].(uint64)
|
postId := args[1].(uint64)
|
||||||
r, err := model.Find[models.Comments](ctx, model.SqlBuilder{
|
r, err := model.Finds[models.Comments](ctx, model.Conditions(
|
||||||
{"comment_approved", "1"},
|
model.Where(model.SqlBuilder{
|
||||||
{"comment_post_ID", "=", strconv.FormatUint(postId, 10), "int"},
|
{"comment_approved", "1"},
|
||||||
}, "comment_ID", "", model.SqlBuilder{
|
{"comment_post_ID", "=", number.ToString(postId), "int"},
|
||||||
{"comment_date_gmt", "asc"},
|
}),
|
||||||
{"comment_ID", "asc"},
|
model.Fields("comment_ID"),
|
||||||
}, nil, nil, 0)
|
model.Order(model.SqlBuilder{
|
||||||
|
{"comment_date_gmt", "asc"},
|
||||||
|
{"comment_ID", "asc"},
|
||||||
|
})),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
"strings"
|
"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) {
|
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...)
|
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