query remote relation model

This commit is contained in:
xing 2023-05-27 14:42:14 +08:00
parent 95e3298b7e
commit 1c33665e34
3 changed files with 45 additions and 27 deletions

View File

@ -41,6 +41,7 @@ type post struct {
Ships *[]TermRelationships
PostMeta *[]models.PostMeta
TermTaxonomy *[]TermTaxonomy
Terms *[]models.Terms
}
type TermRelationships struct {

View File

@ -45,6 +45,7 @@ func parseBeforeJoin(qq *QueryCondition, ship Relationship) {
if ship.Middle != nil {
parseBeforeJoin(qq, *ship.Middle)
local = ship.Local
fromTable = ship.Middle.Table
} else {
fromTable = qq.From
}
@ -64,37 +65,34 @@ func parseBeforeJoin(qq *QueryCondition, ship Relationship) {
}
func parseAfterJoin(ids [][]any, qq *QueryCondition, ship Relationship) bool {
func parseAfterJoin(fromTable string, ids [][]any, qq *QueryCondition, ship Relationship) bool {
tables := strings.Split(ship.Middle.Table, " ")
from := strings.Split(qq.From, " ")
from := strings.Split(fromTable, " ")
on := ""
if ship.On != "" {
on = fmt.Sprintf("and %s", on)
}
foreignKey := ship.ForeignKey
local := ship.Local
if ship.RelationType == HasMany {
foreignKey = ship.Local
local = ship.ForeignKey
}
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.Middle != nil {
return parseAfterJoin(ids, qq, *ship.Middle.Middle)
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.Local), "in", ""},
tables[len(tables)-1], ship.Middle.ForeignKey), "in", ""},
)
qq.Where = ww
}
if qq.Fields == "" || qq.Fields == "*" {
qq.Fields = str.Join(from[len(from)-1], ".", "*")
qq.Fields = str.Join(from[len(from)-1], ".", "*", ",", tables[len(tables)-1], ".", ship.Middle.ForeignKey)
}
qq.In = ids
return ship.Middle.RelationType == HasMany
@ -139,7 +137,7 @@ func Relation(isPlural bool, db dbQuery, ctx context.Context, r any, q *QueryCon
in := [][]any{ids}
if ok {
if ship.Middle != nil {
isPlural = parseAfterJoin(in, qq, ship)
isPlural = parseAfterJoin(qq.From, in, qq, ship)
} else {
ww = append(ww, SqlBuilder{{
ship.ForeignKey, "in", "",

View File

@ -2,6 +2,7 @@ package model
import (
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/helper/slice"
"testing"
)
@ -117,13 +118,30 @@ func PostMetas() (func(any) []any, func(any, any), any, any, Relationship) {
}
}
var term = RelationHasMany(func(m *post) uint64 {
var postHaveManyTerms = RelationHasMany(func(m *post) uint64 {
return m.Id
}, func(p *TermTaxonomy) uint64 {
return p.TermTaxonomyId
}, func(m *post, i *[]TermTaxonomy) {
m.TermTaxonomy = i
}, 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",
@ -131,8 +149,9 @@ var term = RelationHasMany(func(m *post) uint64 {
Middle: &Relationship{
RelationType: HasMany,
Table: "wp_term_relationships",
ForeignKey: "ID",
Local: "object_id",
ForeignKey: "object_id",
Local: "ID",
},
},
})
@ -175,7 +194,7 @@ func TestGets2(t *testing.T) {
WithFn(true, false, nil, termMyHasOneTerm),
), shipHasManyTermMy),
), postHasManyShip),
//WithFn(true, false, nil, term),
WithFn(true, false, nil, postHaveManyTerms),
)
got, err := Gets[post](ctx, q)
_ = got
@ -196,12 +215,12 @@ func TestGets2(t *testing.T) {
Fields("posts.*"),
From("wp_posts posts"),
WithFn(true, false, nil, Meta2()),
WithFn(true, false, Conditions(
/*WithFn(true, false, Conditions(
WithFn(true, false, Conditions(
WithFn(true, false, nil, termMyHasOneTerm),
), shipHasManyTermMy),
), postHasManyShip),
//WithFn(true, false, nil, term),
), postHasManyShip),*/
WithFn(true, false, nil, postHaveManyTerms),
)
got, err := Finds[post](ctx, q)
_ = got