-
-
Notifications
You must be signed in to change notification settings - Fork 219
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
Setting Where condition of SelectQuery,UpdateQuery centrally #497
Comments
We could add something like this: type Query interface {
// note how WhereGroup works with Query instead of SelectQuery/UpdateQuery etc
WhereGroup(string, func(Query) Query) Query
}
q := db.NewSelect().Query()
// q has type Query
q = q.WhereGroup("AND", func(query Query) Query {
return query.Where("u.organization_id = ?", oID)
}) This would require writing adapters for |
Thanks, that will be enough. This will be a breaking change, it will be better to add a new function with name I can send a PR if it is ok? |
I suggest to add new API so it should be backwards compatible, e.g. func (q *SelectQuery) Query() Query {
return &selectQueryAdapter{q: q}
}
type selectQueryAdapter struct {
q *SelectQuery
}
func (q *selectQueryAdapter) WhereGroup(sep string, fn func(Query) Query) Query {
// Call the original WhereGroup
return q.q.WhereGroup(sep, func(q *SelectQuery) *SelectQuery {
return fn(q).(*SelectQuery)
})
} @derkan does that make sense? |
This will be insufficient, because for example in What about adding this wrapper and type Condition interface {
Query
Where(query string, args ...interface{}) Condition
WhereGroup(sep string, fn func(Condition) Condition) Condition
WhereOr(query string, args ...interface{}) Condition
WhereDeleted() Condition
WhereAllWithDeleted() Condition
WherePK(cols ...string) Condition
Query() interface{}
}
type selectCondition struct {
*SelectQuery
}
func (q *selectCondition) WhereGroup(sep string, fn func(Condition) Condition) Condition {
q.SelectQuery.WhereGroup(sep, func(qs *SelectQuery) *SelectQuery {
return fn(&selectCondition{qs}).(*selectCondition).SelectQuery
})
return q
}
func (q *selectCondition) Where(query string, args ...interface{}) Condition {
q.SelectQuery.Where(query, args...)
return q
}
func (q *selectCondition) WhereOr(query string, args ...interface{}) Condition {
q.SelectQuery.WhereOr(query, args...)
return q
}
func (q *selectCondition) WhereDeleted() Condition {
q.SelectQuery.WhereDeleted()
return q
}
func (q *selectCondition) WhereAllWithDeleted() Condition {
q.SelectQuery.WhereAllWithDeleted()
return q
}
func (q *selectCondition) WherePK(cols ...string) Condition {
q.SelectQuery.WherePK(cols...)
return q
}
func (q *selectCondition) Query() interface{} {
return q.SelectQuery
}
func (q *SelectQuery) Query() Condition {
return &selectCondition{q}
}
Usage will be like: func orgFilter(oID uuid.UUID, q bun.Condition) bun.Condition {
q.WhereGroup("AND", func(query bun.Condition) bun.Condition {
return query.Where("u.organization_id = ?", oID)
})
return q
}
q := db.NewSelect().Model(&users)
orgFilter(oID, q.Query())
if _, err = q.Where("id = ?", uuid.New()).Exec(ctx); err != nil {
panic(err)
} I added orgFilter(oID, db.NewSelect().Model(&users).Query()).Query().(*bun.SelectQuery).
Where("id = ?", uuid.New()).
Exec(ctx)... |
Let's:
Otherwise, looks good 👍 |
Hi All,
I wanted to filter
SelectQuery
,UpdateQuery
,DeleteQuery
typed queries in a single place. I've tried interfaces&switch type but always had boilerplate code. Later tried generics in go 1.18, but again got boilerplate code.Is there a way to get rid of boilerplate code at
userFilter
function in the following example:The text was updated successfully, but these errors were encountered: