entc/gen: add support for upsert/on-conflict feature-flag

This commit is contained in:
Ariel Mashraki
2021-08-02 19:19:46 +03:00
committed by Ariel Mashraki
parent a5c931ed13
commit 09c4306378
95 changed files with 9556 additions and 279 deletions

View File

@@ -82,8 +82,33 @@ func ({{ $receiver }} *{{ $builder }}) createSpec() (*{{ $.Name }}, *sqlgraph.Cr
return _node, _spec
}
{{- /* 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 }}
@@ -113,8 +138,15 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) ([]*{{ $.Name }
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, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
if err = sqlgraph.BatchCreate(ctx, {{ $receiver }}.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{err.Error(), err}
}
@@ -128,14 +160,10 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) ([]*{{ $.Name }
{{- if and $.ID.UserDefined (or $.ID.IsString $.ID.IsUUID $.ID.IsBytes) }}
{{- /* Do nothing, because these 2 types must be supplied by the user. */ -}}
{{- else }}
{{- if $.ID.UserDefined }}
if nodes[i].ID == 0 {
{{- end }}
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)
{{- if $.ID.UserDefined }}
}
{{- end }}
}
{{- end }}
return nodes[i], nil
})
@@ -174,4 +202,11 @@ func ({{ $receiver }} *{{ $builder }}) ExecX(ctx context.Context) {
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 }}