{{/*
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.typeScope */}}

{{ define "dialect/sql/predicate/id" -}}
	sql.FieldEQ({{ $.ID.Constant }}, id)
{{- end }}

{{ define "dialect/sql/predicate/id/ops" -}}
	{{- $op := $.Scope.Op -}}
	{{- $storage := $.Scope.Storage -}}
	sql.Field{{ call $storage.OpCode $op }}({{ $.ID.Constant }}{{ if not $op.Niladic }},{{ if $op.Variadic }}ids...{{ else }}id{{ end }}{{ end }})
{{- end }}

{{ define "dialect/sql/predicate/field" -}}
	{{- $f := $.Scope.Field -}}
	{{- $arg := $.Scope.Arg -}}
	sql.FieldEQ({{ $f.Constant }}, {{ $arg }})
{{- end }}

{{ define "dialect/sql/predicate/field/ops" -}}
	{{- $f := $.Scope.Field -}}
	{{- $op := $.Scope.Op -}}
	{{- $arg := $.Scope.Arg -}}
	{{- $storage := $.Scope.Storage -}}
	sql.Field{{ call $storage.OpCode $op }}({{ $f.Constant }}{{ if not $op.Niladic }}, {{ $arg }}{{ if $op.Variadic }}...{{ end }}{{ end }})
{{- end }}

{{ define "dialect/sql/predicate/edge/has" -}}
	{{- $e := $.Scope.Edge -}}
	func(s *sql.Selector) {
		step := sqlgraph.NewStep(
			{{- if $.HasCompositeID }}
				{{- /* Query that goes from the edge schema. */}}
				sqlgraph.From(Table, {{ $e.ColumnConstant }}),
			{{- else }}
				sqlgraph.From(Table, {{ $.ID.Constant }}),
			{{- end }}
			sqlgraph.Edge(sqlgraph.{{ $e.Rel.Type }}, {{ $e.IsInverse }}, {{ $e.TableConstant }},
				{{- if $e.M2M -}}
					{{ $e.PKConstant }}...
				{{- else -}}
					{{ $e.ColumnConstant }}
				{{- end -}}
			),
		)
		{{- /* Allow mutating the sqlgraph.Step by ent extensions or user templates.*/}}
		{{- with $tmpls := matchTemplate "dialect/sql/predicate/edge/has/*" }}
			{{- range $tmpl := $tmpls }}
				{{- xtemplate $tmpl $ }}
			{{- end }}
		{{- end }}
		{{- /* sqlgraph.To is meaningless on Has<E> (without predicates), because unlike Has<E>With no predicates are applied on the "other" table. */}}
		sqlgraph.HasNeighbors(s, step)
	}
{{- end }}

{{ define "dialect/sql/predicate/edge/haswith" -}}
	{{- $e := $.Scope.Edge -}}
	func(s *sql.Selector) {
		step := new{{ pascal $e.Name }}Step()
		{{- /* Allow mutating the sqlgraph.Step by ent extensions or user templates.*/}}
		{{- with $tmpls := matchTemplate "dialect/sql/predicate/edge/haswith/*" }}
			{{- range $tmpl := $tmpls }}
				{{- xtemplate $tmpl $ }}
			{{- end }}
		{{- end }}
		sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
			for _, p := range preds {
				p(s)
			}
		})
	}
{{- end }}

{{ define "dialect/sql/predicate/and" -}}
	func(s *sql.Selector) {
		s1 := s.Clone().SetP(nil)
		for _, p := range predicates {
			p(s1)
		}
		s.Where(s1.P())
	}
{{- end }}

{{ define "dialect/sql/predicate/or" -}}
	func(s *sql.Selector) {
		s1 := s.Clone().SetP(nil)
		for i, p := range predicates {
			if i > 0 {
				s1.Or()
			}
			p(s1)
		}
		s.Where(s1.P())
	}
{{- end }}

{{ define "dialect/sql/predicate/not" -}}
	func(s *sql.Selector) {
		p(s.Not())
	}
{{- end }}
