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

@@ -65,6 +65,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 (
@@ -83,7 +84,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 {
@@ -95,7 +96,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

@@ -13,6 +13,7 @@ import (
"entgo.io/ent/dialect/sql"
"entgo.io/ent/examples/encryptfield/ent"
"entgo.io/ent/examples/encryptfield/ent/predicate"
"entgo.io/ent/examples/encryptfield/ent/user"
)
// The Query interface represents an operation that queries a graph.
@@ -28,7 +29,7 @@ type Query interface {
// Unique configures the query builder to filter duplicate records.
Unique(bool)
// Order specifies how the records should be ordered.
Order(...ent.OrderFunc)
Order(...func(*sql.Selector))
// WhereP appends storage-level predicates to the query builder. Using this method, users
// can use type-assertion to append predicates that do not depend on any generated package.
WhereP(...func(*sql.Selector))
@@ -102,44 +103,48 @@ func (f TraverseUser) Traverse(ctx context.Context, q ent.Query) error {
func NewQuery(q ent.Query) (Query, error) {
switch q := q.(type) {
case *ent.UserQuery:
return &query[*ent.UserQuery, predicate.User]{typ: ent.TypeUser, tq: q}, nil
return &query[*ent.UserQuery, predicate.User, user.Order]{typ: ent.TypeUser, tq: q}, nil
default:
return nil, fmt.Errorf("unknown query type %T", q)
}
}
type query[T any, P ~func(*sql.Selector)] struct {
type query[T any, P ~func(*sql.Selector), R ~func(*sql.Selector)] struct {
typ string
tq interface {
Limit(int) T
Offset(int) T
Unique(bool) T
Order(...ent.OrderFunc) T
Order(...R) T
Where(...P) T
}
}
func (q query[T, P]) Type() string {
func (q query[T, P, R]) Type() string {
return q.typ
}
func (q query[T, P]) Limit(limit int) {
func (q query[T, P, R]) Limit(limit int) {
q.tq.Limit(limit)
}
func (q query[T, P]) Offset(offset int) {
func (q query[T, P, R]) Offset(offset int) {
q.tq.Offset(offset)
}
func (q query[T, P]) Unique(unique bool) {
func (q query[T, P, R]) Unique(unique bool) {
q.tq.Unique(unique)
}
func (q query[T, P]) Order(orders ...ent.OrderFunc) {
q.tq.Order(orders...)
func (q query[T, P, R]) Order(orders ...func(*sql.Selector)) {
rs := make([]R, len(orders))
for i := range orders {
rs[i] = orders[i]
}
q.tq.Order(rs...)
}
func (q query[T, P]) WhereP(ps ...func(*sql.Selector)) {
func (q query[T, P, R]) WhereP(ps ...func(*sql.Selector)) {
p := make([]P, len(ps))
for i := range ps {
p[i] = ps[i]

View File

@@ -8,6 +8,7 @@ package user
import (
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
)
const (
@@ -49,3 +50,21 @@ var (
Hooks [1]ent.Hook
Interceptors [1]ent.Interceptor
)
// 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()
}
// ByNickname orders the results by the nickname field.
func ByNickname(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldNickname, opts...).ToFunc()
}

View File

@@ -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.