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:
@@ -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 {
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
|
||||
package node
|
||||
|
||||
import (
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
)
|
||||
|
||||
const (
|
||||
// Label holds the string label denoting the node type in the database.
|
||||
Label = "node"
|
||||
@@ -47,3 +52,49 @@ func ValidColumn(column string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Order defines the ordering method for the Node 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()
|
||||
}
|
||||
|
||||
// ByValue orders the results by the value field.
|
||||
func ByValue(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldValue, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByPrevID orders the results by the prev_id field.
|
||||
func ByPrevID(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField(FieldPrevID, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByPrevField orders the results by prev field.
|
||||
func ByPrevField(field string, opts ...sql.OrderTermOption) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, newPrevStep(), sql.OrderByField(field, opts...))
|
||||
}
|
||||
}
|
||||
|
||||
// ByNextField orders the results by next field.
|
||||
func ByNextField(field string, opts ...sql.OrderTermOption) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, newNextStep(), sql.OrderByField(field, opts...))
|
||||
}
|
||||
}
|
||||
func newPrevStep() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, true, PrevTable, PrevColumn),
|
||||
)
|
||||
}
|
||||
func newNextStep() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, false, NextTable, NextColumn),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -151,11 +151,7 @@ func HasPrev() predicate.Node {
|
||||
// HasPrevWith applies the HasEdge predicate on the "prev" edge with a given conditions (other predicates).
|
||||
func HasPrevWith(preds ...predicate.Node) predicate.Node {
|
||||
return predicate.Node(func(s *sql.Selector) {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, true, PrevTable, PrevColumn),
|
||||
)
|
||||
step := newPrevStep()
|
||||
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
|
||||
for _, p := range preds {
|
||||
p(s)
|
||||
@@ -178,11 +174,7 @@ func HasNext() predicate.Node {
|
||||
// HasNextWith applies the HasEdge predicate on the "next" edge with a given conditions (other predicates).
|
||||
func HasNextWith(preds ...predicate.Node) predicate.Node {
|
||||
return predicate.Node(func(s *sql.Selector) {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, false, NextTable, NextColumn),
|
||||
)
|
||||
step := newNextStep()
|
||||
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
|
||||
for _, p := range preds {
|
||||
p(s)
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
type NodeQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []OrderFunc
|
||||
order []node.Order
|
||||
inters []Interceptor
|
||||
predicates []predicate.Node
|
||||
withPrev *NodeQuery
|
||||
@@ -59,7 +59,7 @@ func (nq *NodeQuery) Unique(unique bool) *NodeQuery {
|
||||
}
|
||||
|
||||
// Order specifies how the records should be ordered.
|
||||
func (nq *NodeQuery) Order(o ...OrderFunc) *NodeQuery {
|
||||
func (nq *NodeQuery) Order(o ...node.Order) *NodeQuery {
|
||||
nq.order = append(nq.order, o...)
|
||||
return nq
|
||||
}
|
||||
@@ -297,7 +297,7 @@ func (nq *NodeQuery) Clone() *NodeQuery {
|
||||
return &NodeQuery{
|
||||
config: nq.config,
|
||||
ctx: nq.ctx.Clone(),
|
||||
order: append([]OrderFunc{}, nq.order...),
|
||||
order: append([]node.Order{}, nq.order...),
|
||||
inters: append([]Interceptor{}, nq.inters...),
|
||||
predicates: append([]predicate.Node{}, nq.predicates...),
|
||||
withPrev: nq.withPrev.Clone(),
|
||||
|
||||
Reference in New Issue
Block a user