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

@@ -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,56 @@ 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()
}
// ByParentID orders the results by the parent_id field.
func ByParentID(opts ...sql.OrderTermOption) Order {
return sql.OrderByField(FieldParentID, opts...).ToFunc()
}
// ByParentField orders the results by parent field.
func ByParentField(field string, opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newParentStep(), sql.OrderByField(field, opts...))
}
}
// ByChildrenCount orders the results by children count.
func ByChildrenCount(opts ...sql.OrderTermOption) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newChildrenStep(), opts...)
}
}
// ByChildren orders the results by children terms.
func ByChildren(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newChildrenStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
func newParentStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(Table, FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, ParentTable, ParentColumn),
)
}
func newChildrenStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(Table, FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, ChildrenTable, ChildrenColumn),
)
}

View File

@@ -151,11 +151,7 @@ func HasParent() predicate.Node {
// HasParentWith applies the HasEdge predicate on the "parent" edge with a given conditions (other predicates).
func HasParentWith(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.M2O, true, ParentTable, ParentColumn),
)
step := newParentStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)
@@ -178,11 +174,7 @@ func HasChildren() predicate.Node {
// HasChildrenWith applies the HasEdge predicate on the "children" edge with a given conditions (other predicates).
func HasChildrenWith(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.O2M, false, ChildrenTable, ChildrenColumn),
)
step := newChildrenStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)

View File

@@ -23,7 +23,7 @@ import (
type NodeQuery struct {
config
ctx *QueryContext
order []OrderFunc
order []node.Order
inters []Interceptor
predicates []predicate.Node
withParent *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...),
withParent: nq.withParent.Clone(),