{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}

{{/* gotype: entgo.io/ent/entc/gen.Type */}}

{{/* Constants needed for sql dialects. */}}
{{ define "dialect/sql/meta/constants" }}
	{{- range $t := $.RelatedTypes }}
		{{- $withEID := $.HasCompositeID }}
		{{- if and (not $withEID) $t.HasOneFieldID }}
			{{- if ne $t.ID.StorageKey $.ID.StorageKey }}{{ $withEID = true }}{{ end }}
		{{- end }}
		{{- if $withEID }}
			// {{ $t.Name }}FieldID holds the string denoting the ID field of the {{ $t.Name }}.
			{{ $t.Name }}FieldID = "{{ $t.ID.StorageKey }}"
		{{- end }}
	{{- end }}
	// Table holds the table name of the {{ lower $.Name }} in the database.
	Table = "{{ $.Table }}"
	{{- range $e := $.Edges }}
		// {{ $e.TableConstant }} is the table that holds the {{ $e.Name }} relation/edge.
		{{- if $e.M2M }} The primary key declared below.{{ end }}
		{{ $e.TableConstant }} = "{{ $e.Rel.Table }}"
		{{- if ne $.Table $e.Type.Table }}
			// {{ $e.InverseTableConstant }} is the table name for the {{ $e.Type.Name }} entity.
			// It exists in this package in order to avoid circular dependency with the "{{ $e.Type.Package }}" package.
			{{ $e.InverseTableConstant }} = "{{ $e.Type.Table }}"
		{{- end }}
		{{- if not $e.M2M }}
			// {{ $e.ColumnConstant }} is the table column denoting the {{ $e.Name }} relation/edge.
			{{ $e.ColumnConstant }} = "{{ $e.Rel.Column }}"
		{{- end }}
	{{- end }}
{{ end }}

{{/* Variables needed for sql dialects. */}}
{{ define "dialect/sql/meta/variables" }}
	// Columns holds all SQL columns for {{ lower $.Name }} fields.
	var Columns = []string{
		{{- if $.HasOneFieldID }}
			{{ $.ID.Constant }},
		{{- end }}
		{{- range $f := $.Fields }}
			{{ $f.Constant }},
		{{- end }}
	}
	{{/* If any of the edges owns a foreign-key */}}
	{{ with $.UnexportedForeignKeys }}
		// ForeignKeys holds the SQL foreign-keys that are owned by the "{{ $.Table }}"
		// table and are not defined as standalone fields in the schema.
		var ForeignKeys = []string{
			{{- range $fk := . }}
				"{{ $fk.Edge.Rel.Column }}",
			{{- end }}
		}
	{{ end }}

	{{ with $.NumM2M }}
		var (
			{{- range $e := $.Edges }}
				{{- if $e.M2M }}
					// {{ $e.PKConstant }} and {{ $e.ColumnConstant }}2 are the table columns denoting the
					// primary key for the {{ $e.Name }} relation (M2M).
					{{ $e.PKConstant }} = []string{"{{ index $e.Rel.Columns 0 }}", "{{ index $e.Rel.Columns 1 }}"}
				{{- end }}
			{{- end }}
		)
	{{ end }}
{{ end }}

{{/* 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 {
	for i := range Columns {
		if column == Columns[i] {
			return true
		}
	}
	{{- with $.UnexportedForeignKeys }}
		for i := range ForeignKeys {
			if column == ForeignKeys[i] {
				return true
			}
		}
	{{- end }}
	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 }}