优化完善
This commit is contained in:
parent
c95fd7e5da
commit
7c50f2b1a1
|
@ -34,7 +34,7 @@ func PostComments(args ...any) ([]uint64, error) {
|
||||||
r, err := model.Finds[models.Comments](ctx, model.Conditions(
|
r, err := model.Finds[models.Comments](ctx, model.Conditions(
|
||||||
model.Where(model.SqlBuilder{
|
model.Where(model.SqlBuilder{
|
||||||
{"comment_approved", "1"},
|
{"comment_approved", "1"},
|
||||||
{"comment_post_ID", "=", number.ToString(postId), "int"},
|
{"comment_post_ID", "=", number.IntToString(postId), "int"},
|
||||||
}),
|
}),
|
||||||
model.Fields("comment_ID"),
|
model.Fields("comment_ID"),
|
||||||
model.Order(model.SqlBuilder{
|
model.Order(model.SqlBuilder{
|
||||||
|
|
|
@ -50,20 +50,20 @@ func (h *Handle) BodyClass() string {
|
||||||
if cate.Slug[0] != '%' {
|
if cate.Slug[0] != '%' {
|
||||||
class = append(class, str.Join("category-", cate.Slug))
|
class = append(class, str.Join("category-", cate.Slug))
|
||||||
}
|
}
|
||||||
class = append(class, str.Join("category-", number.ToString(cate.Terms.TermId)))
|
class = append(class, str.Join("category-", number.IntToString(cate.Terms.TermId)))
|
||||||
|
|
||||||
case constraints.Author:
|
case constraints.Author:
|
||||||
class = append(class, "archive", "author")
|
class = append(class, "archive", "author")
|
||||||
author := h.Index.Param.Author
|
author := h.Index.Param.Author
|
||||||
user, _ := cache.GetUserByName(h.C, author)
|
user, _ := cache.GetUserByName(h.C, author)
|
||||||
class = append(class, str.Join("author-", number.ToString(user.Id)))
|
class = append(class, str.Join("author-", number.IntToString(user.Id)))
|
||||||
if user.UserLogin[0] != '%' {
|
if user.UserLogin[0] != '%' {
|
||||||
class = append(class, str.Join("author-", user.UserLogin))
|
class = append(class, str.Join("author-", user.UserLogin))
|
||||||
}
|
}
|
||||||
|
|
||||||
case constraints.Detail:
|
case constraints.Detail:
|
||||||
class = append(class, "post-template-default", "single", "single-post")
|
class = append(class, "post-template-default", "single", "single-post")
|
||||||
class = append(class, str.Join("postid-", number.ToString(h.Detail.Post.Id)))
|
class = append(class, str.Join("postid-", number.IntToString(h.Detail.Post.Id)))
|
||||||
if len(h.themeMods.ThemeSupport.PostFormats) > 0 {
|
if len(h.themeMods.ThemeSupport.PostFormats) > 0 {
|
||||||
class = append(class, "single-format-standard")
|
class = append(class, "single-format-standard")
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ func pagination[T Model](db dbQuery, ctx context.Context, q *QueryCondition, pag
|
||||||
err = er
|
err = er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
qx.From = str.Join("( ", sq, " ) ", "table", number.ToString(rand.Int()))
|
qx.From = str.Join("( ", sq, " ) ", "table", number.IntToString(rand.Int()))
|
||||||
qx = QueryCondition{
|
qx = QueryCondition{
|
||||||
From: qx.From,
|
From: qx.From,
|
||||||
In: qx.In,
|
In: qx.In,
|
||||||
|
|
|
@ -36,7 +36,7 @@ func finds[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r []T,
|
||||||
err = db.Select(ctx, &r, sq, args...)
|
err = db.Select(ctx, &r, sq, args...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = parseRelation(true, db, ctx, &r, q)
|
err = ParseRelation(true, db, ctx, &r, q)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,14 +317,14 @@ func gets[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r T, err
|
||||||
err = db.Get(ctx, &r, s, args...)
|
err = db.Get(ctx, &r, s, args...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = parseRelation(false, db, ctx, &r, q)
|
err = ParseRelation(false, db, ctx, &r, q)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRelation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) (err error) {
|
func ParseRelation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) (err error) {
|
||||||
fn, fns := Relation(isMultiple, db, ctx, r, q)
|
before, after := Relation(isMultiple, db, ctx, r, q)
|
||||||
for _, f := range fn {
|
for _, fn := range before {
|
||||||
f()
|
fn()
|
||||||
}
|
}
|
||||||
s, args, err := BuildQuerySql(q)
|
s, args, err := BuildQuerySql(q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -339,8 +339,8 @@ func parseRelation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *Q
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, f := range fns {
|
for _, fn := range after {
|
||||||
err = f()
|
err = fn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,13 +25,13 @@ type Relationship struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Relation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) ([]func(), []func() error) {
|
func Relation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) ([]func(), []func() error) {
|
||||||
var fn []func()
|
var beforeFn []func()
|
||||||
var fns []func() error
|
var afterFn []func() error
|
||||||
for _, f := range q.RelationFn {
|
for _, f := range q.RelationFn {
|
||||||
getVal, isJoin, qq, ff := f()
|
getVal, isJoin, qq, relationship := f()
|
||||||
idFn, assignment, rr, rrs, ship := ff()
|
idFn, assignmentFn, rr, rrs, ship := relationship()
|
||||||
if isJoin {
|
if isJoin {
|
||||||
fn = append(fn, func() {
|
beforeFn = append(beforeFn, func() {
|
||||||
tables := strings.Split(ship.Table, " ")
|
tables := strings.Split(ship.Table, " ")
|
||||||
from := strings.Split(q.From, " ")
|
from := strings.Split(q.From, " ")
|
||||||
on := ""
|
on := ""
|
||||||
|
@ -46,13 +46,12 @@ func Relation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryC
|
||||||
if !getVal {
|
if !getVal {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fns = append(fns, func() error {
|
afterFn = append(afterFn, func() error {
|
||||||
ids := idFn(r)
|
ids := idFn(r)
|
||||||
if len(ids) < 1 {
|
if len(ids) < 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
{
|
|
||||||
if qq == nil {
|
if qq == nil {
|
||||||
qq = &QueryCondition{
|
qq = &QueryCondition{
|
||||||
Fields: "*",
|
Fields: "*",
|
||||||
|
@ -73,17 +72,19 @@ func Relation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryC
|
||||||
if qq.From == "" {
|
if qq.From == "" {
|
||||||
qq.From = ship.Table
|
qq.From = ship.Table
|
||||||
}
|
}
|
||||||
}
|
err = ParseRelation(isMultiple || ship.RelationType == "hasMany", db, ctx, helper.Or(isMultiple, rrs, rr), qq)
|
||||||
err = parseRelation(isMultiple || ship.RelationType == "hasMany", db, ctx, helper.Or(isMultiple, rrs, rr), qq)
|
if err != nil {
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
err = nil
|
||||||
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
assignment(r, helper.Or(isMultiple, rrs, rr))
|
}
|
||||||
|
assignmentFn(r, helper.Or(isMultiple, rrs, rr))
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return fn, fns
|
return beforeFn, afterFn
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetWithID[T, V any](fn func(*T) V) func(any) []any {
|
func GetWithID[T, V any](fn func(*T) V) func(any) []any {
|
||||||
|
@ -98,47 +99,52 @@ func GetWithID[T, V any](fn func(*T) V) func(any) []any {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetHasOne[T, V any, K comparable](fn func(*T, *V), idFn func(*T) K, iddFn func(*V) K) func(any, any) {
|
// SetHasOne mIdFn is main , pIdFn is part
|
||||||
return func(m, v any) {
|
//
|
||||||
|
// eg: post has a user. mIdFn is post's userId, iddFn is user's id
|
||||||
|
func SetHasOne[T, V any, K comparable](assignmentFn func(*T, *V), mIdFn func(*T) K, pIdFn func(*V) K) func(any, any) {
|
||||||
|
return func(m, p any) {
|
||||||
one, ok := m.(*T)
|
one, ok := m.(*T)
|
||||||
if ok {
|
if ok {
|
||||||
fn(one, v.(*V))
|
assignmentFn(one, p.(*V))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r := m.(*[]T)
|
mSlice := m.(*[]T)
|
||||||
vv := v.(*[]V)
|
pSLice := p.(*[]V)
|
||||||
mm := slice.SimpleToMap(*vv, func(v V) K {
|
mm := slice.SimpleToMap(*pSLice, func(v V) K {
|
||||||
return iddFn(&v)
|
return pIdFn(&v)
|
||||||
})
|
})
|
||||||
for i := 0; i < len(*r); i++ {
|
for i := 0; i < len(*mSlice); i++ {
|
||||||
val := &(*r)[i]
|
m := &(*mSlice)[i]
|
||||||
id := idFn(val)
|
id := mIdFn(m)
|
||||||
v, ok := mm[id]
|
p, ok := mm[id]
|
||||||
if ok {
|
if ok {
|
||||||
fn(val, &v)
|
assignmentFn(m, &p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetHasMany[T, V any, K comparable](fn func(*T, *[]V), idFn func(*T) K, iddFn func(*V) K) func(any, any) {
|
// SetHasMany
|
||||||
return func(m, v any) {
|
// eg: post has many comments,pIdFn is comment's postId, mIdFn is post's id
|
||||||
|
func SetHasMany[T, V any, K comparable](assignmentFn func(*T, *[]V), pIdFn func(*T) K, mIdFn func(*V) K) func(any, any) {
|
||||||
|
return func(m, p any) {
|
||||||
one, ok := m.(*T)
|
one, ok := m.(*T)
|
||||||
if ok {
|
if ok {
|
||||||
fn(one, v.(*[]V))
|
assignmentFn(one, p.(*[]V))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r := m.(*[]T)
|
r := m.(*[]T)
|
||||||
vv := v.(*[]V)
|
vv := p.(*[]V)
|
||||||
mm := slice.GroupBy(*vv, func(t V) (K, V) {
|
mm := slice.GroupBy(*vv, func(t V) (K, V) {
|
||||||
return iddFn(&t), t
|
return mIdFn(&t), t
|
||||||
})
|
})
|
||||||
for i := 0; i < len(*r); i++ {
|
for i := 0; i < len(*r); i++ {
|
||||||
val := &(*r)[i]
|
m := &(*r)[i]
|
||||||
id := idFn(val)
|
id := pIdFn(m)
|
||||||
v, ok := mm[id]
|
p, ok := mm[id]
|
||||||
if ok {
|
if ok {
|
||||||
fn(val, &v)
|
assignmentFn(m, &p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user