{{/*
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/update" }}
{{ $pkg := $.Scope.Package }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $mutation := print $receiver ".mutation" }}
{{ $one := hasSuffix $builder "One" }}
{{- $zero := 0 }}{{ if $one }}{{ $zero = "nil" }}{{ end }}
{{- $ret := "n" }}{{ if $one }}{{ $ret = "_node" }}{{ end }}

func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }} {{ if $one }}*{{ $.Name }}{{ else }}int{{ end }}, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table: {{ $.Package }}.Table,
			Columns: {{ $.Package }}.Columns,
			ID: &sqlgraph.FieldSpec{
				Type: field.{{ $.ID.Type.ConstName }},
				Column: {{ $.Package }}.{{ $.ID.Constant }},
			},
		},
	}
	{{- if $one }}
		id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}()
		if !ok {
			return {{ $zero }}, &ValidationError{Name: "ID", err: fmt.Errorf("missing {{ $.Name }}.ID for update")}
		}
		_spec.Node.ID.Value = id
		if fields := {{ $receiver }}.fields; len(fields) > 0 {
			_spec.Node.Columns = make([]string, 0, len(fields))
			_spec.Node.Columns = append(_spec.Node.Columns, {{ $.Package }}.{{ $.ID.Constant }})
			for _, f := range fields {
				if !{{ $.Package }}.ValidColumn(f) {
					return nil, &ValidationError{Name: f, err: fmt.Errorf("{{ $pkg }}: invalid field %q for query", f)}
				}
				if f != {{ $.Package }}.{{ $.ID.Constant }} {
					_spec.Node.Columns = append(_spec.Node.Columns, f)
				}
			}
		}
	{{- end }}
	if ps := {{ $mutation }}.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	{{- range $f := $.MutationFields }}
			{{- if or (not $f.Immutable) $f.UpdateDefault }}
				if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
					_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
						Type: field.{{ $f.Type.ConstName }},
						Value: value,
						Column: {{ $.Package }}.{{ $f.Constant }},
					})
				}
				{{- if $f.SupportsMutationAdd }}
					if value, ok := {{ $mutation }}.Added{{ $f.StructField }}(); ok {
						_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
							Type: field.{{ $f.Type.ConstName }},
							Value: value,
							Column: {{ $.Package }}.{{ $f.Constant }},
						})
					}
				{{- end }}
			{{- end }}
			{{- if $f.Optional }}
				if {{ $mutation }}.{{ $f.StructField }}Cleared() {
					_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
						Type: field.{{ $f.Type.ConstName }},
						Column: {{ $.Package }}.{{ $f.Constant }},
					})
				}
			{{- end }}
	{{- end }}
	{{- range $e := $.Edges }}
		if {{ $mutation }}.{{ $e.MutationCleared }}() {
			{{- with extend $ "Edge" $e }}
				{{ template "dialect/sql/defedge" . }}
			{{- end }}
			_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
		}
		{{- if not $e.Unique }}
			if nodes := {{ $mutation }}.Removed{{ $e.StructField }}IDs(); len(nodes) > 0 && !{{ $mutation }}.{{ $e.MutationCleared }}() {
				{{- with extend $ "Edge" $e "Nodes" true "Zero" $zero }}
					{{ template "dialect/sql/defedge" . }}
				{{- end }}
				_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
			}
		{{- end }}
		if nodes := {{ $mutation }}.{{ $e.StructField }}IDs(); len(nodes) > 0 {
			{{- with extend $ "Edge" $e "Nodes" true "Zero" $zero }}
				{{ template "dialect/sql/defedge" . }}
			{{- end }}
			_spec.Edges.Add = append(_spec.Edges.Add, edge)
		}
	{{- end }}
	{{- /* Allow mutating the sqlgraph.UpdateSpec by ent extensions or user templates.*/}}
	{{- with $tmpls := matchTemplate "dialect/sql/update/spec/*" }}
		{{- range $tmpl := $tmpls }}
			{{- xtemplate $tmpl $ }}
		{{- end }}
	{{- end }}
	{{- if $one }}
		{{ $ret }} = &{{ $.Name }}{config: {{ $receiver }}.config}
		_spec.Assign = {{ $ret }}.assignValues
		_spec.ScanValues = {{ $ret }}.scanValues
	{{- end }}
	{{- if $one }}
		if err = sqlgraph.UpdateNode(ctx, {{ $receiver }}.driver, _spec); err != nil {
	{{- else }}
		if {{ $ret }}, err = sqlgraph.UpdateNodes(ctx, {{ $receiver }}.driver, _spec); err != nil {
	{{- end }}
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{ {{ $.Package }}.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return {{ $zero }}, err
	}
	return {{ $ret }}, nil
}
{{ end }}

{{ define "dialect/sql/defedge" }}
	{{- $e := $.Scope.Edge -}}
	edge := &sqlgraph.EdgeSpec{
		Rel: sqlgraph.{{ $e.Rel.Type }},
		Inverse: {{ $e.IsInverse }},
		Table: {{ $.Package }}.{{ $e.TableConstant }},
		Columns: {{ if $e.M2M }}{{ $.Package }}.{{ $e.PKConstant }}{{ else }}[]string{ {{ $.Package }}.{{ $e.ColumnConstant }} }{{ end }},
		Bidi: {{ $e.Bidi }},
		Target: &sqlgraph.EdgeTarget{
			IDSpec: &sqlgraph.FieldSpec{
				Type: field.{{ $e.Type.ID.Type.ConstName }},
				Column: {{ $e.Type.Package }}.{{ $e.Type.ID.Constant }},
			},
		},
	}
	{{- /* Allow mutating the sqlgraph.EdgeSpec by ent extensions or user templates.*/}}
	{{- with $tmpls := matchTemplate "dialect/sql/defedge/spec/*" }}
		{{- range $tmpl := $tmpls }}
			{{- xtemplate $tmpl $ }}
		{{- end }}
	{{- end }}
	{{- with $.Scope.Nodes }}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
	{{- end }}
{{- end }}
