mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
385 lines
14 KiB
Cheetah
385 lines
14 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 */}}
|
|
|
|
{{/* Additional fields for the builder. */}}
|
|
{{ define "dialect/sql/update/fields" }}
|
|
{{- with $tmpls := matchTemplate "dialect/sql/update/fields/additional/*" }}
|
|
{{- range $tmpl := $tmpls }}
|
|
{{- xtemplate $tmpl $ }}
|
|
{{- end }}
|
|
{{- end }}
|
|
{{- end }}
|
|
|
|
{{ define "dialect/sql/update" }}
|
|
{{ $pkg := $.Scope.Package }}
|
|
{{ $builder := pascal $.Scope.Builder }}
|
|
{{ $receiver := $.Scope.Receiver }}
|
|
{{ $mutation := print $receiver ".mutation" }}
|
|
{{ $one := hasSuffix $builder "One" }}
|
|
{{- $zero := 0 }}{{ if $one }}{{ $zero = "nil" }}{{ end }}
|
|
|
|
{{- /* Allow adding methods to the update-builder by ent extensions or user templates.*/}}
|
|
{{- with $tmpls := matchTemplate "dialect/sql/update/additional/*" }}
|
|
{{- range $tmpl := $tmpls }}
|
|
{{- xtemplate $tmpl $ }}
|
|
{{- end }}
|
|
{{- end }}
|
|
|
|
func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (_node {{ if $one }}*{{ $.Name }}{{ else }}int{{ end }}, err error) {
|
|
{{- if $.HasUpdateCheckers }}
|
|
if err := {{ $receiver }}.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
{{- end }}
|
|
_spec := sqlgraph.NewUpdateSpec({{ $.Package }}.Table, {{ $.Package }}.Columns,
|
|
{{- if $.HasOneFieldID -}}
|
|
sqlgraph.NewFieldSpec({{ $.Package }}.{{ $.ID.Constant }}, field.{{ $.ID.Type.ConstName }})
|
|
{{- else -}}
|
|
{{- range $id := $.EdgeSchema.ID -}}
|
|
sqlgraph.NewFieldSpec({{ $.Package }}.{{ $id.Constant }}, field.{{ $id.Type.ConstName }}),
|
|
{{- 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`)}
|
|
}
|
|
{{- if $.ID.HasValueScanner }}
|
|
vv, err := {{ $.ID.ValueFunc }}(id)
|
|
if err != nil {
|
|
return {{ $zero }}, err
|
|
}
|
|
_spec.Node.ID.Value = vv
|
|
{{- else }}
|
|
_spec.Node.ID.Value = id
|
|
{{- end }}
|
|
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 $f.IsBlobNoColumn }}{{ continue }}{{ end }}
|
|
{{- if or (not $f.Immutable) $f.UpdateDefault }}
|
|
if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
|
|
{{- if $f.HasValueScanner }}
|
|
vv, err := {{ $f.ValueFunc }}(value)
|
|
if err != nil {
|
|
return {{ $zero }}, err
|
|
}
|
|
_spec.SetField({{ $.Package }}.{{ $f.Constant }}, field.{{ $f.Type.ConstName }}, vv)
|
|
{{- else }}
|
|
_spec.SetField({{ $.Package }}.{{ $f.Constant }}, field.{{ $f.Type.ConstName }}, value)
|
|
{{- end }}
|
|
}
|
|
{{- if $f.SupportsMutationAdd }}
|
|
if value, ok := {{ $mutation }}.{{ $f.MutationAdded }}(); ok {
|
|
{{- if $f.HasValueScanner }}
|
|
vv, err := {{ $f.ValueFunc }}(value)
|
|
if err != nil {
|
|
return {{ $zero }}, err
|
|
}
|
|
_spec.AddField({{ $.Package }}.{{ $f.Constant }}, field.{{ $f.Type.ConstName }}, vv)
|
|
{{- else }}
|
|
_spec.AddField({{ $.Package }}.{{ $f.Constant }}, field.{{ $f.Type.ConstName }}, value)
|
|
{{- end }}
|
|
}
|
|
{{- end }}
|
|
{{- if $f.SupportsMutationAppend }}
|
|
if value, ok := {{ $mutation }}.{{ $f.MutationAppended }}(); ok {
|
|
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
|
sqljson.Append(u, {{ $.Package }}.{{ $f.Constant }}, value)
|
|
})
|
|
}
|
|
{{- end }}
|
|
{{- end }}
|
|
{{- if $f.Optional }}
|
|
if {{ $mutation }}.{{ $f.StructField }}Cleared() {
|
|
_spec.ClearField({{ $.Package }}.{{ $f.Constant }}, field.{{ $f.Type.ConstName }})
|
|
}
|
|
{{- end }}
|
|
{{- end }}
|
|
{{- range $e := $.EdgesWithID }}
|
|
{{- if $e.Immutable }}
|
|
{{- /* Skip to the next one as immutable edges cannot be updated. */}}
|
|
{{- continue }}
|
|
{{- end }}
|
|
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 and $one $.HasBlobFields }}
|
|
_blobs := ent.NewBlobs({{ $mutation }}.blobOpeners.{{ $.Name }})
|
|
{{- range $f := $.BlobFields }}
|
|
{{- if not $f.IsBlobLazy }}
|
|
if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
|
|
{{- if $f.HasValueScanner }}
|
|
_blobDV, err := {{ $f.ValueFunc }}(value)
|
|
if err != nil {
|
|
return {{ $zero }}, fmt.Errorf("{{ $pkg }}: encoding {{ $f.Name }}: %w", err)
|
|
}
|
|
var _blobData []byte
|
|
switch v := _blobDV.(type) {
|
|
case []byte:
|
|
_blobData = v
|
|
case string:
|
|
_blobData = []byte(v)
|
|
default:
|
|
return {{ $zero }}, fmt.Errorf("{{ $pkg }}: encoding {{ $f.Name }}: expected []byte or string, got %T", _blobDV)
|
|
}
|
|
_blobs.Set({{ $.Package }}.{{ $f.Constant }}, _blobData,
|
|
{{ if $f.HasBlobKey }}{{ $.Package }}.{{ $f.BlobKeyName }}{{ else }}defaultBlobKey{{ end }},
|
|
func(k string) {
|
|
_spec.SetField("{{ $f.BlobKeyColumn }}", field.TypeString, k)
|
|
},
|
|
)
|
|
{{- else if $f.IsBlobGoString }}
|
|
_blobs.Set({{ $.Package }}.{{ $f.Constant }}, []byte(value),
|
|
{{ if $f.HasBlobKey }}{{ $.Package }}.{{ $f.BlobKeyName }}{{ else }}defaultBlobKey{{ end }},
|
|
func(k string) {
|
|
_spec.SetField("{{ $f.BlobKeyColumn }}", field.TypeString, k)
|
|
},
|
|
)
|
|
{{- else }}
|
|
_blobs.Set({{ $.Package }}.{{ $f.Constant }}, value,
|
|
{{ if $f.HasBlobKey }}{{ $.Package }}.{{ $f.BlobKeyName }}{{ else }}defaultBlobKey{{ end }},
|
|
func(k string) {
|
|
_spec.SetField("{{ $f.BlobKeyColumn }}", field.TypeString, k)
|
|
},
|
|
)
|
|
{{- end }}
|
|
}
|
|
{{- if $f.Optional }}
|
|
if {{ $mutation }}.{{ $f.StructField }}Cleared() {
|
|
_blobs.SetCleared({{ $.Package }}.{{ $f.Constant }}, func() {
|
|
_spec.ClearField("{{ $f.BlobKeyColumn }}", field.TypeString)
|
|
})
|
|
}
|
|
{{- end }}
|
|
{{- else }}
|
|
if r, ok := {{ $mutation }}.{{ $f.StructField }}(); ok {
|
|
_blobData, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return {{ $zero }}, fmt.Errorf("{{ $pkg }}: reading {{ $f.Name }}: %w", err)
|
|
}
|
|
_blobs.Set({{ $.Package }}.{{ $f.Constant }}, _blobData,
|
|
{{ if $f.HasBlobKey }}{{ $.Package }}.{{ $f.BlobKeyName }}{{ else }}defaultBlobKey{{ end }},
|
|
func(k string) {
|
|
_spec.SetField("{{ $f.BlobKeyColumn }}", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
{{- if $f.Optional }}
|
|
if {{ $mutation }}.{{ $f.MutationCleared }}() {
|
|
_blobs.SetCleared({{ $.Package }}.{{ $f.Constant }}, func() {
|
|
_spec.ClearField("{{ $f.BlobKeyColumn }}", field.TypeString)
|
|
})
|
|
}
|
|
{{- end }}
|
|
{{- end }}
|
|
{{- end }}
|
|
_blobResult, err := _blobs.Update(ctx, &sqlgraph.BlobSpec{
|
|
Driver: {{ $receiver }}.driver,
|
|
Table: {{ $.Package }}.Table,
|
|
Columns: map[string]string{
|
|
{{- range $f := $.BlobFields }}
|
|
{{ $.Package }}.{{ $f.Constant }}: "{{ $f.BlobKeyColumn }}",
|
|
{{- end }}
|
|
},
|
|
Predicate: func(s *sql.Selector) {
|
|
s.Where(sql.EQ({{ $.Package }}.{{ $.ID.Constant }}, _spec.Node.ID.Value))
|
|
},
|
|
})
|
|
if err != nil {
|
|
return {{ $zero }}, err
|
|
}
|
|
{{- end }}
|
|
{{- if $one }}
|
|
_node = &{{ $.Name }}{config: {{ $receiver }}.config}
|
|
_spec.Assign = _node.assignValues
|
|
_spec.ScanValues = _node.scanValues
|
|
{{- end }}
|
|
{{- if $one }}
|
|
if err = sqlgraph.UpdateNode(ctx, {{ $receiver }}.driver, _spec); err != nil {
|
|
{{- else }}
|
|
if _node, 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}
|
|
}
|
|
{{- if and $one $.HasBlobFields }}
|
|
return {{ $zero }}, errors.Join(err, _blobResult.Rollback(ctx))
|
|
{{- else }}
|
|
return {{ $zero }}, err
|
|
{{- end }}
|
|
}
|
|
{{ $mutation }}.done = true
|
|
{{- if and $one $.HasBlobFields }}
|
|
if txd, ok := {{ $receiver }}.driver.(*txDriver); ok {
|
|
txd.mu.Lock()
|
|
txd.onCommit = append(txd.onCommit, func(next Committer) Committer {
|
|
return CommitFunc(func(ctx context.Context, tx *Tx) error {
|
|
if err := next.Commit(ctx, tx); err != nil {
|
|
return err
|
|
}
|
|
return _blobResult.Commit(ctx)
|
|
})
|
|
})
|
|
txd.onRollback = append(txd.onRollback, func(next Rollbacker) Rollbacker {
|
|
return RollbackFunc(func(ctx context.Context, tx *Tx) error {
|
|
err := next.Rollback(ctx, tx)
|
|
return errors.Join(err, _blobResult.Rollback(ctx))
|
|
})
|
|
})
|
|
txd.mu.Unlock()
|
|
} else {
|
|
if err := _blobResult.Commit(ctx); err != nil {
|
|
return {{ $zero }}, err
|
|
}
|
|
}
|
|
{{- end }}
|
|
{{- if and $one $.HasLoadOnScanFields }}
|
|
{{- /* Blob values are consistent after write — use mutation value for mutated fields
|
|
instead of re-reading from storage. For DualWrite fields, assignValues already
|
|
populated the value from the SQL column. For non-DualWrite fields not mutated,
|
|
read from blob storage. */}}
|
|
_blobReader := ent.NewBlobStore({{ $mutation }}.blobOpeners.{{ $.Name }})
|
|
{{- range $f := $.LoadOnScanFields }}
|
|
if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
|
|
_node.{{ $f.StructField }} = value
|
|
}
|
|
{{- if $f.IsBlobNoColumn }} else if _node.{{ $f.BlobKeyColumn }} != nil && *_node.{{ $f.BlobKeyColumn }} != "" {
|
|
data, err := _blobReader.Read(ctx, {{ $.Package }}.{{ $f.Constant }}, *_node.{{ $f.BlobKeyColumn }})
|
|
if err != nil {
|
|
return nil, errors.Join(fmt.Errorf("loading {{ $f.Name }} after update: %w", err), _blobReader.Close())
|
|
}
|
|
{{- if $f.HasValueScanner }}
|
|
sv := {{ $f.ScanValueFunc }}()
|
|
if err := sv.Scan(data); err != nil {
|
|
return nil, errors.Join(fmt.Errorf("scanning {{ $f.Name }} after update: %w", err), _blobReader.Close())
|
|
}
|
|
v, err := {{ $f.FromValueFunc }}(sv)
|
|
if err != nil {
|
|
return nil, errors.Join(fmt.Errorf("scanning {{ $f.Name }} after update: %w", err), _blobReader.Close())
|
|
}
|
|
_node.{{ $f.StructField }} = v
|
|
{{- else if $f.IsBlobGoString }}
|
|
_node.{{ $f.StructField }} = {{ $f.Type }}(data)
|
|
{{- else }}
|
|
_node.{{ $f.StructField }} = data
|
|
{{- end }}
|
|
}
|
|
{{- end }}
|
|
{{- end }}
|
|
if err := _blobReader.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
{{- end }}
|
|
return _node, nil
|
|
}
|
|
{{ end }}
|
|
|
|
{{ define "dialect/sql/defedge" }}
|
|
{{- $e := $.Scope.Edge -}}
|
|
{{- $receiver := $.Scope.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.NewFieldSpec({{ $e.Type.Package }}.{{ $e.Type.ID.Constant }}, field.{{ $e.Type.ID.Type.ConstName }}),
|
|
},
|
|
}
|
|
{{- /* 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 }}
|