query 套娃测试

This commit is contained in:
xing 2023-05-24 21:41:49 +08:00
parent 9580678b55
commit 60fd016b5f
3 changed files with 73 additions and 1 deletions

View File

@ -88,6 +88,7 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
args = append(args, ss[1]) args = append(args, ss[1])
case 3, 4: case 3, 4:
w.parseWhereField(ss, &s) w.parseWhereField(ss, &s)
s.WriteString(" ")
s.WriteString(ss[1]) s.WriteString(ss[1])
if w.parseIn(ss, &s, &c, &args, in) { if w.parseIn(ss, &s, &c, &args, in) {
s.WriteString(" and ") s.WriteString(" and ")

View File

@ -38,6 +38,7 @@ type post struct {
PostMimeType string `gorm:"column:post_mime_type" db:"post_mime_type" json:"post_mime_type" form:"post_mime_type"` PostMimeType string `gorm:"column:post_mime_type" db:"post_mime_type" json:"post_mime_type" form:"post_mime_type"`
CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"` CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"`
User *user User *user
Ships *[]TermRelationships
PostMeta *[]models.PostMeta PostMeta *[]models.PostMeta
} }
@ -45,6 +46,7 @@ type TermRelationships struct {
ObjectID uint64 `db:"object_id"` ObjectID uint64 `db:"object_id"`
TermTaxonomyId uint64 `db:"term_taxonomy_id"` TermTaxonomyId uint64 `db:"term_taxonomy_id"`
TermOrder int64 `db:"term_order"` TermOrder int64 `db:"term_order"`
TermTaxonomy *[]TermTaxonomy
} }
type user struct { type user struct {

View File

@ -5,6 +5,63 @@ import (
"testing" "testing"
) )
type TermTaxonomy struct {
TermTaxonomyId uint64 `gorm:"column:term_taxonomy_id" db:"term_taxonomy_id" json:"term_taxonomy_id" form:"term_taxonomy_id"`
TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"`
Taxonomy string `gorm:"column:taxonomy" db:"taxonomy" json:"taxonomy" form:"taxonomy"`
Description string `gorm:"column:description" db:"description" json:"description" form:"description"`
Parent uint64 `gorm:"column:parent" db:"parent" json:"parent" form:"parent"`
Count int64 `gorm:"column:count" db:"count" json:"count" form:"count"`
Term *models.Terms
}
var termMyHasOneTerm = RelationHasOne(func(m *TermTaxonomy) uint64 {
return m.TermTaxonomyId
}, func(p *models.Terms) uint64 {
return p.TermId
}, func(m *TermTaxonomy, p *models.Terms) {
m.Term = p
}, Relationship{
RelationType: HasOne,
Table: "wp_terms",
ForeignKey: "term_id",
Local: "term_id",
})
var postHasManyShip = RelationHasMany(func(m *post) uint64 {
return m.Id
}, func(p *TermRelationships) uint64 {
return p.ObjectID
}, func(m *post, i *[]TermRelationships) {
m.Ships = i
}, Relationship{
RelationType: HasMany,
Table: "wp_term_relationships",
ForeignKey: "object_id",
Local: "ID",
})
var shipHasManyTermMy = RelationHasMany(func(m *TermRelationships) uint64 {
return m.TermTaxonomyId
}, func(p *TermTaxonomy) uint64 {
return p.TermTaxonomyId
}, func(m *TermRelationships, i *[]TermTaxonomy) {
m.TermTaxonomy = i
}, Relationship{
RelationType: HasMany,
Table: "wp_term_taxonomy",
ForeignKey: "term_taxonomy_id",
Local: "term_taxonomy_id",
})
func (w TermTaxonomy) PrimaryKey() string {
return "term_taxonomy_id"
}
func (w TermTaxonomy) Table() string {
return "wp_term_taxonomy"
}
func postAuthorId(p *post) uint64 { func postAuthorId(p *post) uint64 {
return p.PostAuthor return p.PostAuthor
} }
@ -94,6 +151,12 @@ func TestGets2(t *testing.T) {
Fields("posts.*"), Fields("posts.*"),
From("wp_posts posts"), From("wp_posts posts"),
WithFn(true, true, nil, Meta2()), WithFn(true, true, nil, Meta2()),
WithFn(true, false, Conditions(
WithFn(true, false, Conditions(
WithFn(true, false, nil, termMyHasOneTerm),
), shipHasManyTermMy),
), postHasManyShip),
//WithFn(true, false, nil, term),
) )
got, err := Gets[post](ctx, q) got, err := Gets[post](ctx, q)
_ = got _ = got
@ -106,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}), 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"),
@ -114,6 +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, nil, termMyHasOneTerm),
), shipHasManyTermMy),
), postHasManyShip),
//WithFn(true, false, nil, term),
) )
got, err := Finds[post](ctx, q) got, err := Finds[post](ctx, q)
_ = got _ = got