{{/* 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. */}} {{ define "dialect/sql/create" }} {{ $builder := pascal $.Scope.Builder }} {{ $receiver := receiver $builder }} {{ $mutation := print $receiver ".mutation" }} func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name }}, error) { var ( {{ $.Receiver }} = &{{ $.Name }}{config: {{ $receiver }}.config} _spec = &sqlgraph.CreateSpec{ Table: {{ $.Package }}.Table, ID: &sqlgraph.FieldSpec{ Type: field.{{ $.ID.Type.ConstName }}, Column: {{ $.Package }}.{{ $.ID.Constant }}, }, } ) {{- if $.ID.UserDefined }} if id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}(); ok { {{ $.Receiver }}.ID = id _spec.ID.Value = id } {{- end }} {{- range $_, $f := $.Fields }} if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.{{ $f.Type.ConstName }}, Value: value, Column: {{ $.Package }}.{{ $f.Constant }}, }) {{ $.Receiver }}.{{ $f.StructField }} = {{ if $f.Nillable }}&{{ end }}value } {{- end }} {{- range $_, $e := $.Edges }} 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 }} _spec.Edges = append(_spec.Edges, edge) } {{- end }} if err := sqlgraph.CreateNode(ctx, {{ $receiver }}.driver, _spec); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return nil, err } {{- if and $.ID.UserDefined (or $.ID.IsString $.ID.IsUUID) }} {{- /* Do nothing, because these 2 types must be supplied by the user. */ -}} {{- else }} {{- if $.ID.UserDefined }} if {{ $.Receiver }}.ID == 0 { {{- end }} id := _spec.ID.Value.(int64) {{ $.Receiver }}.ID = {{ $.ID.Type }}(id) {{- if $.ID.UserDefined }} } {{- end }} {{- end }} return {{ $.Receiver }}, nil } {{ end }}