mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: add fluent-api for order options (#3449)
This commit is contained in:
@@ -6,6 +6,11 @@
|
||||
|
||||
package car
|
||||
|
||||
import (
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
)
|
||||
|
||||
const (
|
||||
// Label holds the string label denoting the car type in the database.
|
||||
Label = "car"
|
||||
@@ -55,3 +60,35 @@ func ValidColumn(column string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Order defines the ordering method for the Car 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()
|
||||
}
|
||||
|
||||
// ByModel orders the results by the model field.
|
||||
func ByModel(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldModel, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByRegisteredAt orders the results by the registered_at field.
|
||||
func ByRegisteredAt(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldRegisteredAt, 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),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -188,11 +188,7 @@ func HasOwner() predicate.Car {
|
||||
// HasOwnerWith applies the HasEdge predicate on the "owner" edge with a given conditions (other predicates).
|
||||
func HasOwnerWith(preds ...predicate.User) predicate.Car {
|
||||
return predicate.Car(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)
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
type CarQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []OrderFunc
|
||||
order []car.Order
|
||||
inters []Interceptor
|
||||
predicates []predicate.Car
|
||||
withOwner *UserQuery
|
||||
@@ -59,7 +59,7 @@ func (cq *CarQuery) Unique(unique bool) *CarQuery {
|
||||
}
|
||||
|
||||
// Order specifies how the records should be ordered.
|
||||
func (cq *CarQuery) Order(o ...OrderFunc) *CarQuery {
|
||||
func (cq *CarQuery) Order(o ...car.Order) *CarQuery {
|
||||
cq.order = append(cq.order, o...)
|
||||
return cq
|
||||
}
|
||||
@@ -275,7 +275,7 @@ func (cq *CarQuery) Clone() *CarQuery {
|
||||
return &CarQuery{
|
||||
config: cq.config,
|
||||
ctx: cq.ctx.Clone(),
|
||||
order: append([]OrderFunc{}, cq.order...),
|
||||
order: append([]car.Order{}, cq.order...),
|
||||
inters: append([]Interceptor{}, cq.inters...),
|
||||
predicates: append([]predicate.Car{}, cq.predicates...),
|
||||
withOwner: cq.withOwner.Clone(),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
|
||||
package group
|
||||
|
||||
import (
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
)
|
||||
|
||||
const (
|
||||
// Label holds the string label denoting the group type in the database.
|
||||
Label = "group"
|
||||
@@ -50,3 +55,37 @@ var (
|
||||
// NameValidator is a validator for the "name" field. It is called by the builders before save.
|
||||
NameValidator func(string) error
|
||||
)
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// ByName orders the results by the name field.
|
||||
func ByName(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldName, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByUsersCount orders the results by users count.
|
||||
func ByUsersCount(opts ...sql.OrderTermOption) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborsCount(s, newUsersStep(), opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// ByUsers orders the results by users terms.
|
||||
func ByUsers(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, newUsersStep(), append([]sql.OrderTerm{term}, terms...)...)
|
||||
}
|
||||
}
|
||||
func newUsersStep() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(UsersInverseTable, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2M, false, UsersTable, UsersPrimaryKey...),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -141,11 +141,7 @@ func HasUsers() predicate.Group {
|
||||
// HasUsersWith applies the HasEdge predicate on the "users" edge with a given conditions (other predicates).
|
||||
func HasUsersWith(preds ...predicate.User) predicate.Group {
|
||||
return predicate.Group(func(s *sql.Selector) {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(UsersInverseTable, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2M, false, UsersTable, UsersPrimaryKey...),
|
||||
)
|
||||
step := newUsersStep()
|
||||
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
|
||||
for _, p := range preds {
|
||||
p(s)
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
type GroupQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []OrderFunc
|
||||
order []group.Order
|
||||
inters []Interceptor
|
||||
predicates []predicate.Group
|
||||
withUsers *UserQuery
|
||||
@@ -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
|
||||
}
|
||||
@@ -275,7 +275,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...),
|
||||
withUsers: gq.withUsers.Clone(),
|
||||
|
||||
@@ -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"
|
||||
@@ -64,3 +69,63 @@ var (
|
||||
// DefaultName holds the default value on creation for the "name" field.
|
||||
DefaultName string
|
||||
)
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// ByAge orders the results by the age field.
|
||||
func ByAge(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldAge, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByName orders the results by the name field.
|
||||
func ByName(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldName, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByCarsCount orders the results by cars count.
|
||||
func ByCarsCount(opts ...sql.OrderTermOption) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborsCount(s, newCarsStep(), opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// ByCars orders the results by cars terms.
|
||||
func ByCars(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, newCarsStep(), append([]sql.OrderTerm{term}, terms...)...)
|
||||
}
|
||||
}
|
||||
|
||||
// ByGroupsCount orders the results by groups count.
|
||||
func ByGroupsCount(opts ...sql.OrderTermOption) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborsCount(s, newGroupsStep(), opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// ByGroups orders the results by groups terms.
|
||||
func ByGroups(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, newGroupsStep(), append([]sql.OrderTerm{term}, terms...)...)
|
||||
}
|
||||
}
|
||||
func newCarsStep() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(CarsInverseTable, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, CarsTable, CarsColumn),
|
||||
)
|
||||
}
|
||||
func newGroupsStep() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(GroupsInverseTable, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2M, true, GroupsTable, GroupsPrimaryKey...),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -186,11 +186,7 @@ func HasCars() predicate.User {
|
||||
// HasCarsWith applies the HasEdge predicate on the "cars" edge with a given conditions (other predicates).
|
||||
func HasCarsWith(preds ...predicate.Car) predicate.User {
|
||||
return predicate.User(func(s *sql.Selector) {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(CarsInverseTable, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, CarsTable, CarsColumn),
|
||||
)
|
||||
step := newCarsStep()
|
||||
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
|
||||
for _, p := range preds {
|
||||
p(s)
|
||||
@@ -213,11 +209,7 @@ func HasGroups() predicate.User {
|
||||
// HasGroupsWith applies the HasEdge predicate on the "groups" edge with a given conditions (other predicates).
|
||||
func HasGroupsWith(preds ...predicate.Group) predicate.User {
|
||||
return predicate.User(func(s *sql.Selector) {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(GroupsInverseTable, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2M, true, GroupsTable, GroupsPrimaryKey...),
|
||||
)
|
||||
step := newGroupsStep()
|
||||
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
|
||||
for _, p := range preds {
|
||||
p(s)
|
||||
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
type UserQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []OrderFunc
|
||||
order []user.Order
|
||||
inters []Interceptor
|
||||
predicates []predicate.User
|
||||
withCars *CarQuery
|
||||
@@ -61,7 +61,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
|
||||
}
|
||||
@@ -299,7 +299,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...),
|
||||
withCars: uq.withCars.Clone(),
|
||||
|
||||
Reference in New Issue
Block a user