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:
@@ -6,7 +6,7 @@ in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
{{/* gotype: entgo.io/ent/entc/gen.Type */}}
|
||||
|
||||
{{/* constants needed for sql dialects. */}}
|
||||
{{/* Constants needed for sql dialects. */}}
|
||||
{{ define "dialect/sql/meta/constants" }}
|
||||
{{- range $t := $.RelatedTypes }}
|
||||
{{- $withEID := $.HasCompositeID }}
|
||||
@@ -71,7 +71,7 @@ in the LICENSE file in the root directory of this source tree.
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{/* functions needed for sql dialects. */}}
|
||||
{{/* Functions needed for sql dialects. */}}
|
||||
{{ define "dialect/sql/meta/functions" }}
|
||||
// ValidColumn reports if the column name is valid (part of the table columns).
|
||||
func ValidColumn(column string) bool {
|
||||
@@ -90,3 +90,72 @@ func ValidColumn(column string) bool {
|
||||
return false
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{/* Type-safe order-by builders. */}}
|
||||
{{ define "dialect/sql/meta/order" }}
|
||||
{{ if $.HasOneFieldID }}
|
||||
// {{ $.ID.OrderName }} orders the results by the {{ $.ID.Name }} field.
|
||||
func {{ $.ID.OrderName }}(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField({{ $.ID.Constant }}, opts...).ToFunc()
|
||||
}
|
||||
{{ end }}
|
||||
{{- range $f := $.Fields }}
|
||||
{{- if $f.Type.Comparable }}
|
||||
// {{ $f.OrderName }} orders the results by the {{ $f.Name }} field.
|
||||
func {{ $f.OrderName }}(opts ...sql.OrderTermOption) Order {
|
||||
return sql.OrderByField({{ $f.Constant }}, opts...).ToFunc()
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- range $e := $.Edges }}
|
||||
{{- if $e.Unique }}
|
||||
// {{ $e.OrderFieldName }} orders the results by {{ $e.Name }} field.
|
||||
func {{ $e.OrderFieldName }}(field string, opts ...sql.OrderTermOption) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, new{{ pascal $e.Name }}Step(), sql.OrderByField(field, opts...))
|
||||
}
|
||||
}
|
||||
{{- else }}
|
||||
// {{ $e.OrderCountName }} orders the results by {{ $e.Name }} count.
|
||||
func {{ $e.OrderCountName }}(opts ...sql.OrderTermOption) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborsCount(s, new{{ pascal $e.Name }}Step(), opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// {{ $e.OrderTermsName }} orders the results by {{ $e.Name }} terms.
|
||||
func {{ $e.OrderTermsName }}(term sql.OrderTerm, terms ...sql.OrderTerm) Order {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, new{{ pascal $e.Name }}Step(), append([]sql.OrderTerm{term}, terms...)...)
|
||||
}
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- range $e := $.Edges }}
|
||||
func new{{ pascal $e.Name }}Step() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
{{- if $.HasCompositeID }}
|
||||
{{- /* Query that goes from the edge schema. */}}
|
||||
sqlgraph.From(Table, {{ $e.ColumnConstant }}),
|
||||
sqlgraph.To({{ $e.InverseTableConstant }}, {{ print $e.Type.Name "FieldID" }}),
|
||||
{{- else }}
|
||||
sqlgraph.From(Table, {{ $.ID.Constant }}),
|
||||
{{- if $e.Type.HasOneFieldID }}
|
||||
{{- $refid := $.ID.Constant }}{{ if ne $e.Type.ID.StorageKey $.ID.StorageKey }}{{ $refid = print $e.Type.Name "FieldID" }}{{ end }}
|
||||
sqlgraph.To({{ if ne $.Table $e.Type.Table }}{{ $e.InverseTableConstant }}{{ else }}Table{{ end }}, {{ $refid }}),
|
||||
{{- else }}
|
||||
{{- /* Query that goes to the edge schema. */}}
|
||||
sqlgraph.To({{ if ne $.Table $e.Type.Table }}{{ $e.InverseTableConstant }}{{ else }}Table{{ end }}, {{ $e.ColumnConstant }}),
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
sqlgraph.Edge(sqlgraph.{{ $e.Rel.Type }}, {{ $e.IsInverse }}, {{ $e.TableConstant }},
|
||||
{{- if $e.M2M -}}
|
||||
{{ $e.PKConstant }}...
|
||||
{{- else -}}
|
||||
{{ $e.ColumnConstant }}
|
||||
{{- end -}}
|
||||
),
|
||||
)
|
||||
}
|
||||
{{- end }}
|
||||
{{ end }}
|
||||
Reference in New Issue
Block a user