mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Also, added additional example where an edge schema has another edge to a type that holds an information about the relationship. The only reason this example exists is to allow users to reduce the storage occupied by the join-table and allow connect (via M2O) multiple edge-schemas to an 'information'/'description' node.
204 lines
7.1 KiB
Cheetah
204 lines
7.1 KiB
Cheetah
{{/*
|
|
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,
|
|
{{- if $.HasOneFieldID }}
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.{{ $.ID.Type.ConstName }},
|
|
Column: {{ $.Package }}.{{ $.ID.Constant }},
|
|
},
|
|
{{- else }}
|
|
CompositeID: []*sqlgraph.FieldSpec{
|
|
{{- range $id := $.EdgeSchema.ID }}
|
|
{
|
|
Type: field.{{ $id.Type.ConstName }},
|
|
Column: {{ $.Package }}.{{ $id.Constant }},
|
|
},
|
|
{{- end }}
|
|
},
|
|
{{- end }}
|
|
},
|
|
}
|
|
{{- if $one }}
|
|
{{- if $.HasOneFieldID }}
|
|
id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}()
|
|
if !ok {
|
|
return {{ $zero }}, &ValidationError{Name: "{{ $.ID.Name }}", err: errors.New(`{{ $pkg }}: missing "{{ $.Name }}.{{ $.ID.Name }}" 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)
|
|
}
|
|
}
|
|
}
|
|
{{- else }}{{/* Composite ID. */}}
|
|
{{- range $i, $id := $.EdgeSchema.ID }}
|
|
if id, ok := {{ $mutation }}.{{ $id.MutationGet }}(); !ok {
|
|
return {{ $zero }}, &ValidationError{Name: "{{ $id.Name }}", err: errors.New(`{{ $pkg }}: missing "{{ $.Name }}.{{ $id.Name }}" for update`)}
|
|
} else {
|
|
_spec.Node.CompositeID[{{ $i }}].Value = id
|
|
}
|
|
{{- end }}
|
|
if fields := {{ $receiver }}.fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, len(fields))
|
|
for i, f := range fields {
|
|
if !{{ $.Package }}.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("{{ $pkg }}: invalid field %q for query", f)}
|
|
}
|
|
_spec.Node.Columns[i] = f
|
|
}
|
|
}
|
|
{{- end }}
|
|
{{- 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 := $.EdgesWithID }}
|
|
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{msg: err.Error(), wrap: err}
|
|
}
|
|
return {{ $zero }}, err
|
|
}
|
|
return {{ $ret }}, nil
|
|
}
|
|
{{ end }}
|
|
|
|
{{ define "dialect/sql/defedge" }}
|
|
{{- $e := $.Scope.Edge -}}
|
|
{{- $receiver := pascal $.Scope.Builder | receiver -}}
|
|
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 }}
|
|
{{- with $e.Through }}
|
|
{{- if .HasDefault }}
|
|
createE := &{{ .CreateName }}{config: {{ $receiver }}.config, mutation: new{{ .MutationName }}({{ $receiver }}.config, OpCreate)}
|
|
{{- /* Skip error handling here as this check was already handled. */}}
|
|
{{ if or $.NumHooks $.NumPolicy }}_ = {{ end }}createE.defaults()
|
|
_, specE := createE.createSpec()
|
|
edge.Target.Fields = specE.Fields
|
|
{{- if and .HasOneFieldID .ID.Default }}
|
|
if specE.ID.Value != nil {
|
|
edge.Target.Fields = append(edge.Target.Fields, specE.ID)
|
|
}
|
|
{{- end }}
|
|
{{- end }}
|
|
{{- end }}
|
|
{{- end }}
|