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

@@ -6,6 +6,11 @@
package card
import (
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
)
const (
// Label holds the string label denoting the card type in the database.
Label = "card"
@@ -55,3 +60,35 @@ func ValidColumn(column string) bool {
}
return false
}
// Order defines the ordering method for the Card 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()
}
// ByExpired orders the results by the expired field.
func ByExpired(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldExpired, opts...).ToFunc()
}
// ByNumber orders the results by the number field.
func ByNumber(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldNumber, 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.O2O, true, OwnerTable, OwnerColumn),
)
}

View File

@@ -188,11 +188,7 @@ func HasOwner() predicate.Card {
// HasOwnerWith applies the HasEdge predicate on the "owner" edge with a given conditions (other predicates).
func HasOwnerWith(preds ...predicate.User) predicate.Card {
return predicate.Card(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(OwnerInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.O2O, 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 CardQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []card.Order
inters []Interceptor
predicates []predicate.Card
withOwner *UserQuery
@@ -59,7 +59,7 @@ func (cq *CardQuery) Unique(unique bool) *CardQuery {
}
// Order specifies how the records should be ordered.
func (cq *CardQuery) Order(o ...OrderFunc) *CardQuery {
func (cq *CardQuery) Order(o ...card.Order) *CardQuery {
cq.order = append(cq.order, o...)
return cq
}
@@ -275,7 +275,7 @@ func (cq *CardQuery) Clone() *CardQuery {
return &CardQuery{
config: cq.config,
ctx: cq.ctx.Clone(),
order: append([]OrderFunc{}, cq.order...),
order: append([]card.Order{}, cq.order...),
inters: append([]Interceptor{}, cq.inters...),
predicates: append([]predicate.Card{}, cq.predicates...),
withOwner: cq.withOwner.Clone(),

View File

@@ -66,6 +66,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 (
@@ -85,7 +86,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 {
@@ -97,7 +98,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,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"
@@ -44,3 +49,35 @@ 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()
}
// 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()
}
// ByCardField orders the results by card field.
func ByCardField(field string, opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newCardStep(), sql.OrderByField(field, opts...))
}
}
func newCardStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(CardInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.O2O, false, CardTable, CardColumn),
)
}

View File

@@ -186,11 +186,7 @@ func HasCard() predicate.User {
// HasCardWith applies the HasEdge predicate on the "card" edge with a given conditions (other predicates).
func HasCardWith(preds ...predicate.Card) predicate.User {
return predicate.User(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(CardInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.O2O, false, CardTable, CardColumn),
)
step := newCardStep()
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
withCard *CardQuery
@@ -59,7 +59,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
}
@@ -275,7 +275,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...),
withCard: uq.withCard.Clone(),