Compare commits
No commits in common. "b41b6e0a09440a93b59de1ca0cbb25284ee1f996" and "bafd839637e02c4c717c5c6358ee8209408c5cdf" have entirely different histories.
b41b6e0a09
...
bafd839637
|
@ -40,9 +40,6 @@ type post struct {
|
||||||
User *user
|
User *user
|
||||||
Ships *[]TermRelationships
|
Ships *[]TermRelationships
|
||||||
PostMeta *[]models.PostMeta
|
PostMeta *[]models.PostMeta
|
||||||
TermTaxonomy *[]TermTaxonomy
|
|
||||||
Terms *[]models.Terms
|
|
||||||
CommentMetas *[]CommentMeta
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TermRelationships struct {
|
type TermRelationships struct {
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/helper"
|
"github.com/fthvgb1/wp-go/helper"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
||||||
"golang.org/x/exp/constraints"
|
"golang.org/x/exp/constraints"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -37,79 +36,25 @@ type Relationship struct {
|
||||||
ForeignKey string
|
ForeignKey string
|
||||||
Local string
|
Local string
|
||||||
On string
|
On string
|
||||||
Middle *Relationship
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseBeforeJoin(qq *QueryCondition, ship Relationship) {
|
|
||||||
var fromTable, foreignKey, local string
|
|
||||||
if ship.Middle != nil {
|
|
||||||
parseBeforeJoin(qq, *ship.Middle)
|
|
||||||
local = ship.Local
|
|
||||||
fromTable = ship.Middle.Table
|
|
||||||
} else {
|
|
||||||
fromTable = qq.From
|
|
||||||
}
|
|
||||||
foreignKey = ship.ForeignKey
|
|
||||||
local = ship.Local
|
|
||||||
tables := strings.Split(ship.Table, " ")
|
|
||||||
from := strings.Split(fromTable, " ")
|
|
||||||
on := ""
|
|
||||||
if ship.On != "" {
|
|
||||||
on = fmt.Sprintf("and %s", on)
|
|
||||||
}
|
|
||||||
qq.Join = append(qq.Join, []string{
|
|
||||||
"left join", ship.Table,
|
|
||||||
fmt.Sprintf("%s.%s=%s.%s %s",
|
|
||||||
tables[len(tables)-1], foreignKey, from[len(from)-1], local, on,
|
|
||||||
)})
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseAfterJoin(fromTable string, ids [][]any, qq *QueryCondition, ship Relationship) bool {
|
|
||||||
tables := strings.Split(ship.Middle.Table, " ")
|
|
||||||
from := strings.Split(fromTable, " ")
|
|
||||||
on := ""
|
|
||||||
if ship.On != "" {
|
|
||||||
on = fmt.Sprintf("and %s", on)
|
|
||||||
}
|
|
||||||
foreignKey := ship.ForeignKey
|
|
||||||
local := ship.Local
|
|
||||||
qq.Join = append(qq.Join, []string{
|
|
||||||
"left join", ship.Middle.Table,
|
|
||||||
fmt.Sprintf("%s.%s=%s.%s %s",
|
|
||||||
tables[len(tables)-1], foreignKey, from[len(from)-1], local, on,
|
|
||||||
),
|
|
||||||
})
|
|
||||||
if ship.Middle != nil && ship.Middle.Middle != nil {
|
|
||||||
return parseAfterJoin(tables[len(tables)-1], ids, qq, *ship.Middle)
|
|
||||||
} else {
|
|
||||||
from := strings.Split(qq.From, " ")
|
|
||||||
ww, ok := qq.Where.(SqlBuilder)
|
|
||||||
if ok {
|
|
||||||
ww = append(ww, []string{fmt.Sprintf("%s.%s",
|
|
||||||
tables[len(tables)-1], ship.Middle.ForeignKey), "in", ""},
|
|
||||||
)
|
|
||||||
qq.Where = ww
|
|
||||||
}
|
|
||||||
if qq.Fields == "" || qq.Fields == "*" {
|
|
||||||
qq.Fields = str.Join(from[len(from)-1], ".", "*", ",", tables[len(tables)-1], ".", ship.Middle.ForeignKey)
|
|
||||||
}
|
|
||||||
qq.In = ids
|
|
||||||
return ship.Middle.RelationType == HasMany
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Relation(isPlural bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) ([]func(), []func() error) {
|
func Relation(isPlural bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) ([]func(), []func() error) {
|
||||||
var beforeFn []func()
|
var beforeFn []func()
|
||||||
var afterFn []func() error
|
var afterFn []func() error
|
||||||
qx := helper.GetContextVal(ctx, "ancestorsQueryCondition", q)
|
|
||||||
|
|
||||||
for _, f := range q.RelationFn {
|
for _, f := range q.RelationFn {
|
||||||
getVal, isJoin, qq, relationship := f()
|
getVal, isJoin, qq, relationship := f()
|
||||||
idFn, assignmentFn, rr, rrs, ship := relationship()
|
idFn, assignmentFn, rr, rrs, ship := relationship()
|
||||||
if isJoin {
|
if isJoin {
|
||||||
beforeFn = append(beforeFn, func() {
|
beforeFn = append(beforeFn, func() {
|
||||||
parseBeforeJoin(qx, ship)
|
tables := strings.Split(ship.Table, " ")
|
||||||
|
from := strings.Split(q.From, " ")
|
||||||
|
on := ""
|
||||||
|
if ship.On != "" {
|
||||||
|
on = fmt.Sprintf("and %s", on)
|
||||||
|
}
|
||||||
|
qq := helper.GetContextVal(ctx, "ancestorsQueryCondition", q)
|
||||||
|
qq.Join = append(qq.Join, []string{
|
||||||
|
"left join", ship.Table, fmt.Sprintf("%s.%s=%s.%s %s", tables[len(tables)-1], ship.ForeignKey, from[len(from)-1], ship.Local, on)})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if !getVal {
|
if !getVal {
|
||||||
|
@ -126,25 +71,20 @@ func Relation(isPlural bool, db dbQuery, ctx context.Context, r any, q *QueryCon
|
||||||
Fields: "*",
|
Fields: "*",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if qq.From == "" {
|
|
||||||
qq.From = ship.Table
|
|
||||||
}
|
|
||||||
var w any = qq.Where
|
var w any = qq.Where
|
||||||
if w == nil {
|
if w == nil {
|
||||||
qq.Where = SqlBuilder{}
|
w = SqlBuilder{}
|
||||||
}
|
}
|
||||||
ww, ok := qq.Where.(SqlBuilder)
|
ww, ok := w.(SqlBuilder)
|
||||||
in := [][]any{ids}
|
|
||||||
if ok {
|
if ok {
|
||||||
if ship.Middle != nil {
|
ww = append(ww, SqlBuilder{{
|
||||||
isPlural = parseAfterJoin(qq.From, in, qq, ship)
|
ship.ForeignKey, "in", "",
|
||||||
} else {
|
}}...)
|
||||||
ww = append(ww, SqlBuilder{{
|
qq.In = [][]any{ids}
|
||||||
ship.ForeignKey, "in", "",
|
qq.Where = ww
|
||||||
}}...)
|
}
|
||||||
qq.In = in
|
if qq.From == "" {
|
||||||
qq.Where = ww
|
qq.From = ship.Table
|
||||||
}
|
|
||||||
}
|
}
|
||||||
err = ParseRelation(isPlural || ship.RelationType == HasMany, db, ctx, helper.Or(isPlural, rrs, rr), qq)
|
err = ParseRelation(isPlural || ship.RelationType == HasMany, db, ctx, helper.Or(isPlural, rrs, rr), qq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fthvgb1/wp-go/app/pkg/models"
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,13 +15,6 @@ type TermTaxonomy struct {
|
||||||
Term *models.Terms
|
Term *models.Terms
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommentMeta struct {
|
|
||||||
MetaId uint64 `db:"meta_id"`
|
|
||||||
CommentId uint64 `db:"comment_id"`
|
|
||||||
MetaKey string `db:"meta_key"`
|
|
||||||
MetaValue string `db:"meta_value"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var termMyHasOneTerm = RelationHasOne(func(m *TermTaxonomy) uint64 {
|
var termMyHasOneTerm = RelationHasOne(func(m *TermTaxonomy) uint64 {
|
||||||
return m.TermTaxonomyId
|
return m.TermTaxonomyId
|
||||||
}, func(p *models.Terms) uint64 {
|
}, func(p *models.Terms) uint64 {
|
||||||
|
@ -125,71 +117,6 @@ func PostMetas() (func(any) []any, func(any, any), any, any, Relationship) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var postHaveManyTerms = RelationHasMany(func(m *post) uint64 {
|
|
||||||
return m.Id
|
|
||||||
}, func(p *struct {
|
|
||||||
ObjectId uint64 `db:"object_id"`
|
|
||||||
models.Terms
|
|
||||||
}) uint64 {
|
|
||||||
return p.ObjectId
|
|
||||||
}, func(m *post, i *[]struct {
|
|
||||||
ObjectId uint64 `db:"object_id"`
|
|
||||||
models.Terms
|
|
||||||
}) {
|
|
||||||
v := slice.Map(*i, func(t struct {
|
|
||||||
ObjectId uint64 `db:"object_id"`
|
|
||||||
models.Terms
|
|
||||||
}) models.Terms {
|
|
||||||
return t.Terms
|
|
||||||
})
|
|
||||||
m.Terms = &v
|
|
||||||
}, Relationship{
|
|
||||||
RelationType: HasOne,
|
|
||||||
Table: "wp_terms",
|
|
||||||
ForeignKey: "term_id",
|
|
||||||
Local: "term_id",
|
|
||||||
Middle: &Relationship{
|
|
||||||
RelationType: HasOne,
|
|
||||||
Table: "wp_term_taxonomy taxonomy",
|
|
||||||
ForeignKey: "term_taxonomy_id",
|
|
||||||
Local: "term_taxonomy_id",
|
|
||||||
Middle: &Relationship{
|
|
||||||
RelationType: HasMany,
|
|
||||||
Table: "wp_term_relationships",
|
|
||||||
ForeignKey: "object_id",
|
|
||||||
Local: "ID",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
var postHaveManyCommentMetas = func() RelationFn {
|
|
||||||
type metas struct {
|
|
||||||
CommentPostID uint64 `db:"comment_post_ID"`
|
|
||||||
CommentMeta
|
|
||||||
}
|
|
||||||
return RelationHasMany(func(m *post) uint64 {
|
|
||||||
return m.Id
|
|
||||||
}, func(p *metas) uint64 {
|
|
||||||
return p.CommentPostID
|
|
||||||
}, func(m *post, i *[]metas) {
|
|
||||||
v := slice.Map(*i, func(t metas) CommentMeta {
|
|
||||||
return t.CommentMeta
|
|
||||||
})
|
|
||||||
m.CommentMetas = &v
|
|
||||||
}, Relationship{
|
|
||||||
RelationType: HasOne,
|
|
||||||
Table: "wp_commentmeta",
|
|
||||||
ForeignKey: "comment_id",
|
|
||||||
Local: "comment_ID",
|
|
||||||
Middle: &Relationship{
|
|
||||||
RelationType: HasMany,
|
|
||||||
Table: "wp_comments comments",
|
|
||||||
ForeignKey: "comment_post_ID",
|
|
||||||
Local: "ID",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}()
|
|
||||||
|
|
||||||
func Meta2() RelationFn {
|
func Meta2() RelationFn {
|
||||||
return RelationHasMany(postId, metasPostId, func(m *post, i *[]models.PostMeta) {
|
return RelationHasMany(postId, metasPostId, func(m *post, i *[]models.PostMeta) {
|
||||||
m.PostMeta = i
|
m.PostMeta = i
|
||||||
|
@ -229,7 +156,7 @@ func TestGets2(t *testing.T) {
|
||||||
WithFn(true, false, nil, termMyHasOneTerm),
|
WithFn(true, false, nil, termMyHasOneTerm),
|
||||||
), shipHasManyTermMy),
|
), shipHasManyTermMy),
|
||||||
), postHasManyShip),
|
), postHasManyShip),
|
||||||
WithFn(true, false, nil, postHaveManyTerms),
|
//WithFn(true, false, nil, term),
|
||||||
)
|
)
|
||||||
got, err := Gets[post](ctx, q)
|
got, err := Gets[post](ctx, q)
|
||||||
_ = got
|
_ = got
|
||||||
|
@ -242,7 +169,7 @@ func TestGets2(t *testing.T) {
|
||||||
{
|
{
|
||||||
q := Conditions(
|
q := Conditions(
|
||||||
Where(SqlBuilder{{"posts.id", "in", ""}}),
|
Where(SqlBuilder{{"posts.id", "in", ""}}),
|
||||||
In([]any{190, 3022, 291, 2858}),
|
In([]any{190, 3022, 291}),
|
||||||
WithCtx(&ctx),
|
WithCtx(&ctx),
|
||||||
WithFn(true, false, Conditions(
|
WithFn(true, false, Conditions(
|
||||||
Fields("ID,user_login,user_pass"),
|
Fields("ID,user_login,user_pass"),
|
||||||
|
@ -250,13 +177,12 @@ func TestGets2(t *testing.T) {
|
||||||
Fields("posts.*"),
|
Fields("posts.*"),
|
||||||
From("wp_posts posts"),
|
From("wp_posts posts"),
|
||||||
WithFn(true, false, nil, Meta2()),
|
WithFn(true, false, nil, Meta2()),
|
||||||
/*WithFn(true, false, Conditions(
|
WithFn(true, false, Conditions(
|
||||||
WithFn(true, false, Conditions(
|
WithFn(true, false, Conditions(
|
||||||
WithFn(true, false, nil, termMyHasOneTerm),
|
WithFn(true, false, nil, termMyHasOneTerm),
|
||||||
), shipHasManyTermMy),
|
), shipHasManyTermMy),
|
||||||
), postHasManyShip),*/
|
), postHasManyShip),
|
||||||
WithFn(true, false, nil, postHaveManyTerms),
|
//WithFn(true, false, nil, term),
|
||||||
WithFn(true, false, nil, postHaveManyCommentMetas),
|
|
||||||
)
|
)
|
||||||
got, err := Finds[post](ctx, q)
|
got, err := Finds[post](ctx, q)
|
||||||
_ = got
|
_ = got
|
||||||
|
|
Loading…
Reference in New Issue
Block a user