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

{{ define "dialect/sql/create" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $mutation := print $receiver ".mutation"  }}

func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name }}, error) {
	if err := {{ $receiver }}.check(); err != nil {
		return nil, err
	}
	_node, _spec {{ if $.HasValueScanner }}, err {{ end }} := {{ $receiver }}.createSpec()
	{{- if $.HasValueScanner }}
		if err != nil {
			return nil, err
		}
	{{- end }}
	if err := sqlgraph.CreateNode(ctx, {{ $receiver }}.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	{{- if $.HasCompositeID }}
	{{- else if or $.ID.Type.ValueScanner (not $.ID.Type.Numeric) }}
		if _spec.ID.Value != nil {
			{{- /* If the ID type is not a pointer, but implements the ValueScanner interface (e.g. UUID fields). */}}
			{{- if and $.ID.Type.ValueScanner (not $.ID.Type.RType.IsPtr) }}
				if id, ok := _spec.ID.Value.(*{{ $.ID.Type }}); ok {
					_node.ID = *id
			{{- else }}
				if id, ok := _spec.ID.Value.({{ $.ID.Type }}); ok {
					_node.ID = id
			{{- end }}
			{{- if $.ID.Type.ValueScanner }}
				} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
					return nil, err
			{{- else  }}
				} else {
					return nil, fmt.Errorf("unexpected {{ $.Name }}.ID type: %T", _spec.ID.Value)
			{{- end }}
				}
		}
	{{- else }}
		{{- if $.ID.UserDefined }}
			if _spec.ID.Value != _node.ID {
		{{- end }}
				id := _spec.ID.Value.(int64)
				_node.ID = {{ $.ID.Type }}(id)
		{{- if $.ID.UserDefined }}
			}
		{{- end }}
	{{- end }}
	{{- if $.HasOneFieldID }}
		{{ $mutation }}.{{ $.ID.BuilderField }} = &_node.{{ $.ID.StructField }}
		{{ $mutation }}.done = true
	{{- end }}
	return _node, nil
}

func ({{ $receiver }} *{{ $builder }}) createSpec() (*{{ $.Name }}, *sqlgraph.CreateSpec{{ if $.HasValueScanner }}, error{{ end }}) {
	var (
		_node = &{{ $.Name }}{config: {{ $receiver }}.config}
		_spec = sqlgraph.NewCreateSpec({{ $.Package }}.Table, {{ if $.HasOneFieldID }}sqlgraph.NewFieldSpec({{ $.Package }}.{{ $.ID.Constant }}, field.{{ $.ID.Type.ConstName }}){{ else }}nil{{ end }})
	)
	{{- /* Allow mutating the sqlgraph.CreateSpec by ent extensions or user templates.*/}}
	{{- with $tmpls := matchTemplate "dialect/sql/create/spec/*" }}
		{{- range $tmpl := $tmpls }}
			{{- xtemplate $tmpl $ }}
		{{- end }}
	{{- end }}
	{{- if and (not $.HasCompositeID) $.ID.UserDefined }}
		if id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}(); ok {
			_node.ID = id
			_spec.ID.Value = {{ if and $.ID.Type.ValueScanner (not $.ID.Type.RType.IsPtr) }}&{{ end }}id
		}
	{{- end }}
	{{- range $f := $.MutationFields }}
		if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
			{{- if $f.HasValueScanner }}
				vv, err := {{ $f.ValueFunc }}(value)
				if err != nil {
					return nil, nil, err
				}
				_spec.SetField({{ $.Package }}.{{ $f.Constant }}, field.{{ $f.Type.ConstName }}, vv)
			{{- else }}
				_spec.SetField({{ $.Package }}.{{ $f.Constant }}, field.{{ $f.Type.ConstName }}, value)
			{{- end }}
			_node.{{ $f.StructField }} = {{ if $f.NillableValue }}&{{ end }}value
		}
	{{- end }}
	{{- range $e := $.EdgesWithID }}
		if nodes := {{ $mutation }}.{{ $e.StructField }}IDs(); len(nodes) > 0 {
			{{- with extend $ "Edge" $e "Nodes" true "Zero" "nil" }}
				{{ template "dialect/sql/defedge" . }}{{/* defined in sql/update.tmpl */}}
			{{- end }}
			{{- if $e.OwnFK }}
				{{- $fk := $e.ForeignKey }}
				_node.{{ $fk.StructField }} = {{ if $fk.Field.NillableValue }}&{{ end }}nodes[0]
			{{- end }}
			_spec.Edges = append(_spec.Edges, edge)
		}
	{{- end }}
	return _node, _spec{{ if $.HasValueScanner }}, nil{{ end }}
}

{{- /* Allow adding methods to the create-builder by ent extensions or user templates.*/}}
{{- with $tmpls := matchTemplate "dialect/sql/create/additional/*" }}
	{{- range $tmpl := $tmpls }}
		{{- xtemplate $tmpl $ }}
	{{- end }}
{{- end }}

{{ end }}

{{/* Additional fields for the create builder. */}}
{{ define "dialect/sql/create/fields" }}
	{{- with $tmpls := matchTemplate "dialect/sql/create/fields/additional/*" }}
		{{- range $tmpl := $tmpls }}
			{{- xtemplate $tmpl $ }}
		{{- end }}
	{{- end }}
{{- end }}

{{/* Additional fields for the create_bulk builder. */}}
{{ define "dialect/sql/create_bulk/fields" }}
	{{- with $tmpls := matchTemplate "dialect/sql/create_bulk/fields/additional/*" }}
		{{- range $tmpl := $tmpls }}
			{{- xtemplate $tmpl $ }}
		{{- end }}
	{{- end }}
{{- end }}

{{ define "dialect/sql/create_bulk" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}

// Save creates the {{ $.Name }} entities in the database.
func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) ([]*{{ $.Name }}, error) {
	specs := make([]*sqlgraph.CreateSpec, len({{ $receiver }}.builders))
	nodes := make([]*{{ $.Name }}, len({{ $receiver }}.builders))
	mutators := make([]Mutator, len({{ $receiver }}.builders))
	for i := range {{ $receiver }}.builders {
		func(i int, root context.Context) {
			builder := {{ $receiver }}.builders[i]
			{{- if $.HasDefault }}
				builder.defaults()
			{{- end }}
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*{{ $.MutationName }})
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] {{ if $.HasValueScanner }}, err {{ end }}= builder.createSpec()
				{{- if $.HasValueScanner }}
					if err != nil {
						return nil, err
					}
				{{- end }}
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, {{ $receiver }}.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					{{- /* Allow mutating the sqlgraph.BatchCreateSpec by ent extensions or user templates.*/}}
					{{- with $tmpls := matchTemplate "dialect/sql/create_bulk/spec/*" }}
						{{- range $tmpl := $tmpls }}
							{{- xtemplate $tmpl $ }}
						{{- end }}
					{{- end }}
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, {{ $receiver }}.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				{{- if $.HasOneFieldID }}
					mutation.{{ $.ID.BuilderField }} = &nodes[i].{{ $.ID.StructField }}
					{{- if or $.ID.IsString $.ID.IsUUID $.ID.IsBytes $.ID.IsOther }}
						{{- /* Do nothing, because these 4 types must be supplied by the user. */ -}}
					{{- else if or $.ID.Type.ValueScanner }}
						if specs[i].ID.Value != nil {
							if err := nodes[i].ID.Scan(specs[i].ID.Value); err != nil {
								return nil, err
							}
						}
					{{- else }}
						if specs[i].ID.Value != nil {{ if $.ID.UserDefined }}&& nodes[i].ID == 0{{ end }} {
							id := specs[i].ID.Value.(int64)
							nodes[i].ID = {{ $.ID.Type }}(id)
						}
					{{- end }}
				{{- end }}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, {{ $receiver }}.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) SaveX(ctx context.Context) []*{{ $.Name }} {
	v, err := {{ $receiver }}.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func ({{ $receiver }} *{{ $builder }}) Exec(ctx context.Context) error {
	_, err := {{ $receiver }}.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) ExecX(ctx context.Context) {
	if err := {{ $receiver }}.Exec(ctx); err != nil {
		panic(err)
	}
}

{{- /* Allow adding methods to the create_bulk builder by ent extensions or user templates.*/}}
{{- with $tmpls := matchTemplate "dialect/sql/create_bulk/additional/*" }}
	{{- range $tmpl := $tmpls }}
		{{- xtemplate $tmpl $ }}
	{{- end }}
{{- end }}
{{ end }}
