dialect/sql: support capturing predicates in selectors

This allows custom predicates mutating the root querying and still respect the AND/OR/NOT semantics
This commit is contained in:
Ariel Mashraki
2023-06-21 19:19:54 +03:00
committed by Ariel Mashraki
parent 4787899669
commit 808edd134d
132 changed files with 475 additions and 2600 deletions

View File

@@ -128,32 +128,15 @@ func NameContainsFold(v string) predicate.User {
// And groups predicates with the AND operator between them.
func And(predicates ...predicate.User) predicate.User {
return predicate.User(func(s *sql.Selector) {
s1 := s.Clone().SetP(nil)
for _, p := range predicates {
p(s1)
}
s.Where(s1.P())
})
return predicate.User(sql.AndPredicates(predicates...))
}
// Or groups predicates with the OR operator between them.
func Or(predicates ...predicate.User) predicate.User {
return predicate.User(func(s *sql.Selector) {
s1 := s.Clone().SetP(nil)
for i, p := range predicates {
if i > 0 {
s1.Or()
}
p(s1)
}
s.Where(s1.P())
})
return predicate.User(sql.OrPredicates(predicates...))
}
// Not applies the not operator on the given predicate.
func Not(p predicate.User) predicate.User {
return predicate.User(func(s *sql.Selector) {
p(s.Not())
})
return predicate.User(sql.NotPredicates(p))
}