-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add WhereHas and WhereDoesntHave helpers for relationship filtering #7642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,197 @@ | ||||||||||||||||||||||
| package callbacks | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import ( | ||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| "gorm.io/gorm" | ||||||||||||||||||||||
| "gorm.io/gorm/clause" | ||||||||||||||||||||||
| "gorm.io/gorm/schema" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func whereHasDb(db *gorm.DB) *gorm.DB { | ||||||||||||||||||||||
| tx := db.Session(&gorm.Session{Context: db.Statement.Context, NewDB: true, SkipHooks: db.Statement.SkipHooks, Initialized: true}) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return tx | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func newWhereHas(db *gorm.DB, isDoesntHave bool, relationName string, conds []interface{}, s *schema.Schema) (*clause.Where, error) { | ||||||||||||||||||||||
| var err error | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| rel, ok := s.Relationships.Relations[relationName] | ||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||
| return nil, fmt.Errorf("relation %s not found", relationName) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| tx := whereHasDb(db) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| reflectResults := rel.FieldSchema.MakeSlice().Elem() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| tx = tx.Model(reflectResults.Addr().Interface()).Select("id") | ||||||||||||||||||||||
propel-code-bot[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||
| if err = tx.Statement.Parse(tx.Statement.Model); err != nil { | ||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if rel.Type == schema.Many2Many { | ||||||||||||||||||||||
| tx, err = existsMany2many(db, tx, rel, conds) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } else if rel.Type == schema.BelongsTo { | ||||||||||||||||||||||
| tx, err = existsBelongsTo(db, tx, rel, conds) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } else if rel.Type == schema.HasMany { | ||||||||||||||||||||||
| tx, err = existsHasMany(db, tx, rel, conds) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } else if rel.Type == schema.HasOne { | ||||||||||||||||||||||
| tx, err = existsHasOne(db, tx, rel, conds) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| cond := "EXISTS(?)" | ||||||||||||||||||||||
| if isDoesntHave { | ||||||||||||||||||||||
| cond = "NOT " + cond | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| cl := clause.Where{ | ||||||||||||||||||||||
| Exprs: []clause.Expression{ | ||||||||||||||||||||||
| clause.Expr{ | ||||||||||||||||||||||
| SQL: cond, | ||||||||||||||||||||||
| Vars: []interface{}{tx}, | ||||||||||||||||||||||
| WithoutParentheses: false, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return &cl, nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func existsHasOne(mainQuery *gorm.DB, existsQuery *gorm.DB, rel *schema.Relationship, conds []interface{}) (*gorm.DB, error) { | ||||||||||||||||||||||
| return existsHasMany(mainQuery, existsQuery, rel, conds) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func existsHasMany(mainQuery *gorm.DB, existsQuery *gorm.DB, rel *schema.Relationship, conds []interface{}) (*gorm.DB, error) { | ||||||||||||||||||||||
| for _, reference := range rel.References { | ||||||||||||||||||||||
| if reference.PrimaryKey != nil { | ||||||||||||||||||||||
| existsQuery.Statement.AddClause(clause.Where{ | ||||||||||||||||||||||
| Exprs: []clause.Expression{ | ||||||||||||||||||||||
| clause.Eq{ | ||||||||||||||||||||||
| Column: clause.Column{Table: existsQuery.Statement.Table, Name: reference.ForeignKey.DBName}, | ||||||||||||||||||||||
| Value: clause.Column{Table: mainQuery.Statement.Table, Name: reference.PrimaryKey.DBName}, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| existsQuery.Statement.AddClause(clause.Where{ | ||||||||||||||||||||||
| Exprs: []clause.Expression{ | ||||||||||||||||||||||
| clause.Eq{ | ||||||||||||||||||||||
| Column: clause.Column{Table: existsQuery.Statement.Table, Name: reference.ForeignKey.DBName}, | ||||||||||||||||||||||
| Value: reference.PrimaryValue, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| existsQuery = applyConds(existsQuery, conds) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return existsQuery, nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func existsBelongsTo(mainQuery *gorm.DB, existsQuery *gorm.DB, rel *schema.Relationship, conds []interface{}) (*gorm.DB, error) { | ||||||||||||||||||||||
| for _, reference := range rel.References { | ||||||||||||||||||||||
| existsQuery.Statement.AddClause(clause.Where{ | ||||||||||||||||||||||
| Exprs: []clause.Expression{ | ||||||||||||||||||||||
| clause.Eq{ | ||||||||||||||||||||||
| Column: clause.Column{Table: existsQuery.Statement.Table, Name: reference.PrimaryKey.DBName}, | ||||||||||||||||||||||
| Value: clause.Column{Table: mainQuery.Statement.Table, Name: reference.ForeignKey.DBName}, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| existsQuery = applyConds(existsQuery, conds) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return existsQuery, nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func existsMany2many(mainQuery *gorm.DB, existsQuery *gorm.DB, rel *schema.Relationship, conds []interface{}) (*gorm.DB, error) { | ||||||||||||||||||||||
| if rel.JoinTable != nil { | ||||||||||||||||||||||
| var parentTableField *schema.Reference = nil | ||||||||||||||||||||||
| var primaryTableField *schema.Reference = nil | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for _, reference := range rel.References { | ||||||||||||||||||||||
| if !reference.OwnPrimaryKey { | ||||||||||||||||||||||
| parentTableField = reference | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| primaryTableField = reference | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if parentTableField == nil { | ||||||||||||||||||||||
| return nil, fmt.Errorf("relation %s has no parent table field", rel.Name) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if primaryTableField == nil { | ||||||||||||||||||||||
| return nil, fmt.Errorf("relation %s has no primary table field", rel.Name) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| fromClause := clause.From{ | ||||||||||||||||||||||
| Tables: nil, | ||||||||||||||||||||||
| Joins: []clause.Join{ | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| Type: clause.InnerJoin, | ||||||||||||||||||||||
| Table: clause.Table{Name: rel.JoinTable.Table}, | ||||||||||||||||||||||
| ON: clause.Where{ | ||||||||||||||||||||||
| Exprs: []clause.Expression{ | ||||||||||||||||||||||
| clause.Eq{ | ||||||||||||||||||||||
| Column: clause.Column{Table: rel.JoinTable.Table, Name: parentTableField.ForeignKey.DBName}, | ||||||||||||||||||||||
| Value: clause.Column{Table: existsQuery.Statement.Table, Name: parentTableField.PrimaryKey.DBName}, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| existsQuery.Statement.AddClause(fromClause) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| existsQuery.Statement.AddClause(clause.Where{ | ||||||||||||||||||||||
| Exprs: []clause.Expression{ | ||||||||||||||||||||||
| clause.Eq{ | ||||||||||||||||||||||
| Column: clause.Column{Table: rel.JoinTable.Table, Name: primaryTableField.ForeignKey.DBName}, | ||||||||||||||||||||||
| Value: clause.Column{Table: mainQuery.Statement.Table, Name: primaryTableField.PrimaryKey.DBName}, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| existsQuery = applyConds(existsQuery, conds) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return existsQuery, nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func applyConds(existsQuery *gorm.DB, conds []interface{}) *gorm.DB { | ||||||||||||||||||||||
| inlineConds := make([]interface{}, 0) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for _, cond := range conds { | ||||||||||||||||||||||
| if fc, ok := cond.(func(*gorm.DB) *gorm.DB); ok { | ||||||||||||||||||||||
| existsQuery = fc(existsQuery) | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| inlineConds = append(inlineConds, cond) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if len(inlineConds) > 0 { | ||||||||||||||||||||||
| existsQuery = existsQuery.Where(inlineConds[0], inlineConds[1:]...) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+204
to
+206
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [BestPractice] Array bounds safety issue: Suggested Change
Suggested change
⚡ Committable suggestion Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Context for Agents
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the code is inspired by the code in the file callbacks/preload.go (line 288) and this code is correct, does not cause errors, and I think readability is better than the proposed version. |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| existsQuery.WhereHas("Some") | ||||||||||||||||||||||
propel-code-bot[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return existsQuery | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.