mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
entc/gen: add fluent-api for order options (#3449)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
// ByName orders the results by the name field.
|
||||
func ByName(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldName, opts...).ToFunc()
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
type GroupQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []OrderFunc
|
||||
order []group.Order
|
||||
inters []Interceptor
|
||||
predicates []predicate.Group
|
||||
// intermediate query (i.e. traversal path).
|
||||
@@ -56,7 +56,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
|
||||
}
|
||||
@@ -250,7 +250,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.
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"entgo.io/ent/dialect/sql"
|
||||
)
|
||||
|
||||
const (
|
||||
// Label holds the string label denoting the user type in the database.
|
||||
Label = "user"
|
||||
@@ -43,3 +47,26 @@ 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 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()
|
||||
}
|
||||
|
||||
// ByAddress orders the results by the address field.
|
||||
func ByAddress(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldAddress, opts...).ToFunc()
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
type UserQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []OrderFunc
|
||||
order []user.Order
|
||||
inters []Interceptor
|
||||
predicates []predicate.User
|
||||
// intermediate query (i.e. traversal path).
|
||||
@@ -56,7 +56,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
|
||||
}
|
||||
@@ -250,7 +250,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...),
|
||||
// clone intermediate query.
|
||||
|
||||
Reference in New Issue
Block a user