From 1c33665e34fe1552678f1891fcd816c096c15f04 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 27 May 2023 14:42:14 +0800 Subject: [PATCH] query remote relation model --- model/query_test.go | 1 + model/relation.go | 20 ++++++++--------- model/relation_test.go | 51 +++++++++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/model/query_test.go b/model/query_test.go index 6ce4967..0b14e93 100644 --- a/model/query_test.go +++ b/model/query_test.go @@ -41,6 +41,7 @@ type post struct { Ships *[]TermRelationships PostMeta *[]models.PostMeta TermTaxonomy *[]TermTaxonomy + Terms *[]models.Terms } type TermRelationships struct { diff --git a/model/relation.go b/model/relation.go index 70a8dd6..8823afa 100644 --- a/model/relation.go +++ b/model/relation.go @@ -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", "", diff --git a/model/relation_test.go b/model/relation_test.go index 93d7841..d0b9023 100644 --- a/model/relation_test.go +++ b/model/relation_test.go @@ -2,6 +2,7 @@ package model import ( "github.com/fthvgb1/wp-go/app/pkg/models" + "github.com/fthvgb1/wp-go/helper/slice" "testing" ) @@ -117,22 +118,40 @@ 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_term_taxonomy taxonomy", - ForeignKey: "term_taxonomy_id", - Local: "term_taxonomy_id", + Table: "wp_terms", + ForeignKey: "term_id", + Local: "term_id", Middle: &Relationship{ - RelationType: HasMany, - Table: "wp_term_relationships", - ForeignKey: "ID", - Local: "object_id", + 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", + }, }, }) @@ -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