dialect/sql/sqlgraph: support sql.Scanner types when scanning IDs (#1987)

Fixed https://github.com/ent/ent/issues/1985
This commit is contained in:
Ariel Mashraki
2021-09-27 17:49:57 +03:00
committed by GitHub
parent 0c4e9c78a7
commit 4306643d16
21 changed files with 2903 additions and 25 deletions

View File

@@ -4,7 +4,7 @@ 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 */}}
{{/* gotype: entgo.io/ent/entc/gen.Type */}}
{{ define "dialect/sql/create" }}
{{ $builder := pascal $.Scope.Builder }}
@@ -19,9 +19,14 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name
}
return nil, err
}
{{- if and $.ID.UserDefined (not $.ID.Type.Numeric) }}
{{- if and $.ID.UserDefined (or $.ID.Type.ValueScanner (not $.ID.Type.Numeric)) }}
if _spec.ID.Value != nil {
_node.ID = _spec.ID.Value.({{ $.ID.Type }})
{{- /* 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) }}
_node.ID = *_spec.ID.Value.(*{{ $.ID.Type }})
{{- else }}
_node.ID = _spec.ID.Value.({{ $.ID.Type }})
{{- end }}
}
{{- else }}
{{- if $.ID.UserDefined }}
@@ -56,7 +61,7 @@ func ({{ $receiver }} *{{ $builder }}) createSpec() (*{{ $.Name }}, *sqlgraph.Cr
{{- if $.ID.UserDefined }}
if id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}(); ok {
_node.ID = id
_spec.ID.Value = id
_spec.ID.Value = {{ if and $.ID.Type.ValueScanner (not $.ID.Type.RType.IsPtr) }}&{{ end }}id
}
{{- end }}
{{- range $f := $.MutationFields }}