entc/gen: add fluent-api for order options (#3449)

This commit is contained in:
Ariel Mashraki
2023-04-09 10:37:42 +03:00
committed by GitHub
parent 6165bdea32
commit 064c9118b7
436 changed files with 8086 additions and 1652 deletions

View File

@@ -67,6 +67,7 @@ func NewTxContext(parent context.Context, tx *Tx) context.Context {
}
// OrderFunc applies an ordering on the sql selector.
// Deprecated: Use Asc/Desc functions or the package builders instead.
type OrderFunc func(*sql.Selector)
var (
@@ -87,7 +88,7 @@ func checkColumn(table, column string) error {
}
// Asc applies the given fields in ASC order.
func Asc(fields ...string) OrderFunc {
func Asc(fields ...string) func(*sql.Selector) {
return func(s *sql.Selector) {
for _, f := range fields {
if err := checkColumn(s.TableName(), f); err != nil {
@@ -99,7 +100,7 @@ func Asc(fields ...string) OrderFunc {
}
// Desc applies the given fields in DESC order.
func Desc(fields ...string) OrderFunc {
func Desc(fields ...string) func(*sql.Selector) {
return func(s *sql.Selector) {
for _, f := range fields {
if err := checkColumn(s.TableName(), f); err != nil {

View File

@@ -6,6 +6,10 @@
package group
import (
"entgo.io/ent/dialect/sql"
)
const (
// Label holds the string label denoting the group type in the database.
Label = "group"
@@ -32,3 +36,16 @@ func ValidColumn(column string) bool {
}
return false
}
// Order defines the ordering method for the Group queries.
type Order func(*sql.Selector)
// ByID orders the results by the id field.
func ByID(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldID, opts...).ToFunc()
}
// ByMaxUsers orders the results by the max_users field.
func ByMaxUsers(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldMaxUsers, opts...).ToFunc()
}

View File

@@ -22,7 +22,7 @@ import (
type GroupQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []group.Order
inters []Interceptor
predicates []predicate.Group
// additional query fields.
@@ -59,7 +59,7 @@ func (gq *GroupQuery) Unique(unique bool) *GroupQuery {
}
// Order specifies how the records should be ordered.
func (gq *GroupQuery) Order(o ...OrderFunc) *GroupQuery {
func (gq *GroupQuery) Order(o ...group.Order) *GroupQuery {
gq.order = append(gq.order, o...)
return gq
}
@@ -253,7 +253,7 @@ func (gq *GroupQuery) Clone() *GroupQuery {
return &GroupQuery{
config: gq.config,
ctx: gq.ctx.Clone(),
order: append([]OrderFunc{}, gq.order...),
order: append([]group.Order{}, gq.order...),
inters: append([]Interceptor{}, gq.inters...),
predicates: append([]predicate.Group{}, gq.predicates...),
// clone intermediate query.

View File

@@ -6,6 +6,11 @@
package pet
import (
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
)
const (
// Label holds the string label denoting the pet type in the database.
Label = "pet"
@@ -55,3 +60,35 @@ func ValidColumn(column string) bool {
}
return false
}
// Order defines the ordering method for the Pet queries.
type Order func(*sql.Selector)
// ByID orders the results by the id field.
func ByID(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldID, opts...).ToFunc()
}
// ByAge orders the results by the age field.
func ByAge(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldAge, opts...).ToFunc()
}
// ByLicensedAt orders the results by the licensed_at field.
func ByLicensedAt(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldLicensedAt, opts...).ToFunc()
}
// ByOwnerField orders the results by owner field.
func ByOwnerField(field string, opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newOwnerStep(), sql.OrderByField(field, opts...))
}
}
func newOwnerStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(OwnerInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, OwnerTable, OwnerColumn),
)
}

View File

@@ -173,11 +173,7 @@ func HasOwner() predicate.Pet {
// HasOwnerWith applies the HasEdge predicate on the "owner" edge with a given conditions (other predicates).
func HasOwnerWith(preds ...predicate.User) predicate.Pet {
return predicate.Pet(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(OwnerInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, OwnerTable, OwnerColumn),
)
step := newOwnerStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)

View File

@@ -23,7 +23,7 @@ import (
type PetQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []pet.Order
inters []Interceptor
predicates []predicate.Pet
withOwner *UserQuery
@@ -62,7 +62,7 @@ func (pq *PetQuery) Unique(unique bool) *PetQuery {
}
// Order specifies how the records should be ordered.
func (pq *PetQuery) Order(o ...OrderFunc) *PetQuery {
func (pq *PetQuery) Order(o ...pet.Order) *PetQuery {
pq.order = append(pq.order, o...)
return pq
}
@@ -278,7 +278,7 @@ func (pq *PetQuery) Clone() *PetQuery {
return &PetQuery{
config: pq.config,
ctx: pq.ctx.Clone(),
order: append([]OrderFunc{}, pq.order...),
order: append([]pet.Order{}, pq.order...),
inters: append([]Interceptor{}, pq.inters...),
predicates: append([]predicate.Pet{}, pq.predicates...),
withOwner: pq.withOwner.Clone(),

View File

@@ -6,6 +6,11 @@
package user
import (
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
)
const (
// Label holds the string label denoting the user type in the database.
Label = "user"
@@ -51,3 +56,58 @@ func ValidColumn(column string) bool {
}
return false
}
// Order defines the ordering method for the User queries.
type Order func(*sql.Selector)
// ByID orders the results by the id field.
func ByID(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldID, opts...).ToFunc()
}
// ByName orders the results by the name field.
func ByName(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldName, opts...).ToFunc()
}
// ByPetsCount orders the results by pets count.
func ByPetsCount(opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newPetsStep(), opts...)
}
}
// ByPets orders the results by pets terms.
func ByPets(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newPetsStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
// ByFriendsCount orders the results by friends count.
func ByFriendsCount(opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newFriendsStep(), opts...)
}
}
// ByFriends orders the results by friends terms.
func ByFriends(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newFriendsStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
func newPetsStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(PetsInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, PetsTable, PetsColumn),
)
}
func newFriendsStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(Table, FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, FriendsTable, FriendsPrimaryKey...),
)
}

View File

@@ -141,11 +141,7 @@ func HasPets() predicate.User {
// HasPetsWith applies the HasEdge predicate on the "pets" edge with a given conditions (other predicates).
func HasPetsWith(preds ...predicate.Pet) predicate.User {
return predicate.User(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(PetsInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, PetsTable, PetsColumn),
)
step := newPetsStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)
@@ -168,11 +164,7 @@ func HasFriends() predicate.User {
// HasFriendsWith applies the HasEdge predicate on the "friends" edge with a given conditions (other predicates).
func HasFriendsWith(preds ...predicate.User) predicate.User {
return predicate.User(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(Table, FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, FriendsTable, FriendsPrimaryKey...),
)
step := newFriendsStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)

View File

@@ -24,7 +24,7 @@ import (
type UserQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []user.Order
inters []Interceptor
predicates []predicate.User
withPets *PetQuery
@@ -63,7 +63,7 @@ func (uq *UserQuery) Unique(unique bool) *UserQuery {
}
// Order specifies how the records should be ordered.
func (uq *UserQuery) Order(o ...OrderFunc) *UserQuery {
func (uq *UserQuery) Order(o ...user.Order) *UserQuery {
uq.order = append(uq.order, o...)
return uq
}
@@ -301,7 +301,7 @@ func (uq *UserQuery) Clone() *UserQuery {
return &UserQuery{
config: uq.config,
ctx: uq.ctx.Clone(),
order: append([]OrderFunc{}, uq.order...),
order: append([]user.Order{}, uq.order...),
inters: append([]Interceptor{}, uq.inters...),
predicates: append([]predicate.User{}, uq.predicates...),
withPets: uq.withPets.Clone(),