mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: remove legacy i2a support for ids (#389)
* entc/gen: remove legacy i2a support for ids * integ: add customid tests for fk strings
This commit is contained in:
@@ -57,8 +57,8 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name
|
||||
if {{ $.Receiver }}.ID == 0 {
|
||||
{{- end }}
|
||||
id := _spec.ID.Value.(int64)
|
||||
{{ $.Receiver }}.ID = {{ if $.ID.IsString }}strconv.FormatInt(id, 10){{ else }}{{ $.ID.Type }}(id){{ end }}
|
||||
{{- if and $.ID.UserDefined }}
|
||||
{{ $.Receiver }}.ID = {{ $.ID.Type }}(id)
|
||||
{{- if $.ID.UserDefined }}
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
@@ -44,7 +44,7 @@ func ({{ $receiver }} *{{ $.Name }}) assignValues(values ...interface{}) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
{{ $receiver }}.ID = {{ if $.ID.IsString }}strconv.FormatInt(value.Int64, 10){{ else }}{{ $.ID.Type }}(value.Int64){{ end }}
|
||||
{{ $receiver }}.ID = {{ $.ID.Type }}(value.Int64)
|
||||
{{- end }}
|
||||
values = values[1:]
|
||||
{{- range $i, $f := $.Fields }}
|
||||
@@ -66,7 +66,7 @@ func ({{ $receiver }} *{{ $.Name }}) assignValues(values ...interface{}) error {
|
||||
return fmt.Errorf("unexpected type %T for edge-field {{ $f.Name}}", value)
|
||||
} else if value.Valid {
|
||||
{{ $receiver }}.{{ $f.Name }} = new({{ $f.Type }})
|
||||
*{{ $receiver }}.{{ $f.Name }} = {{ if $f.IsString }}strconv.FormatInt(value.Int64, 10){{ else }}{{ $f.Type }}(value.Int64){{ end }}
|
||||
*{{ $receiver }}.{{ $f.Name }} = {{ $f.Type }}(value.Int64)
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
@@ -6,7 +6,6 @@ in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
{{ define "dialect/sql/predicate/id" -}}
|
||||
func(s *sql.Selector) {
|
||||
{{- if and $.ID.IsString (not $.ID.UserDefined) }}id, _ := strconv.Atoi(id){{- end }}
|
||||
s.Where(sql.EQ(s.C({{ $.ID.Constant }}), id))
|
||||
}
|
||||
{{- end }}
|
||||
@@ -15,7 +14,6 @@ in the LICENSE file in the root directory of this source tree.
|
||||
{{- $op := $.Scope.Op -}}
|
||||
{{- $arg := $.Scope.Arg -}}
|
||||
{{- $storage := $.Scope.Storage -}}
|
||||
{{- $convid := and $.ID.IsString (not $.ID.UserDefined) -}}
|
||||
func(s *sql.Selector) {
|
||||
{{- if $op.Variadic }}
|
||||
// if not arguments were provided, append the FALSE constants,
|
||||
@@ -26,10 +24,8 @@ in the LICENSE file in the root directory of this source tree.
|
||||
}
|
||||
v := make([]interface{}, len({{ $arg }}))
|
||||
for i := range v {
|
||||
{{- if $convid }}v[i], _ = strconv.Atoi({{ $arg }}[i]){{ else }}v[i] = {{ $arg }}[i]{{ end }}
|
||||
v[i] = {{ $arg }}[i]
|
||||
}
|
||||
{{- else if $convid }}
|
||||
id, _ := strconv.Atoi({{ $arg }})
|
||||
{{- end }}
|
||||
s.Where(sql.{{ call $storage.OpCode $op }}(s.C({{ $.ID.Constant }}), {{ if $op.Variadic }}v...{{ else }}id{{ end }}))
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery() *sql.Selector {
|
||||
{{- $n := $ }} {{/* the node we start the query from. */}}
|
||||
{{- $e := $.Scope.Edge }} {{/* the edge we need to genegrate the path to. */}}
|
||||
{{- $receiver := $.Scope.Receiver -}}
|
||||
id := {{ $receiver }}.{{ if and $.ID.IsString (not $.ID.UserDefined) }}id(){{ else }}ID{{ end }}
|
||||
id := {{ $receiver }}.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From({{ $n.Package }}.Table, {{ $n.Package }}.{{ $n.ID.Constant }}, id),
|
||||
sqlgraph.To({{ $e.Type.Package }}.Table, {{ $e.Type.Package }}.{{ $e.Type.ID.Constant }}),
|
||||
@@ -283,16 +283,7 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery() *sql.Selector {
|
||||
fks := make([]driver.Value, 0, len(nodes))
|
||||
nodeids := make(map[{{ $.ID.Type }}]*{{ $.Name }})
|
||||
for i := range nodes {
|
||||
{{- /* Convert string-ids that are stored as int in the database */ -}}
|
||||
{{- if and (not $.ID.UserDefined) $.ID.IsString }}
|
||||
id, err := strconv.Atoi(nodes[i].ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fks = append(fks, id)
|
||||
{{- else }}
|
||||
fks = append(fks, nodes[i].ID)
|
||||
{{- end }}
|
||||
fks = append(fks, nodes[i].ID)
|
||||
nodeids[nodes[i].ID] = nodes[i]
|
||||
}
|
||||
query.withFKs = true
|
||||
@@ -318,14 +309,11 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery() *sql.Selector {
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{- /* Convert string-ids that are stored as int in the database */ -}}
|
||||
{{ define "dialect/sql/query/eagerloading/m2massign" }}
|
||||
{{- $arg := $.Scope.Arg }}
|
||||
{{- $field := $.Scope.Field }}
|
||||
{{- $nulltype := $.Scope.NullType }}
|
||||
{{- if and (not $field.UserDefined) $field.IsString -}}
|
||||
strconv.FormatInt({{ $arg }}.Int64, 10)
|
||||
{{- else if hasPrefix $nulltype "sql" -}}
|
||||
{{- if hasPrefix $nulltype "sql" -}}
|
||||
{{ $field.NullTypeField $arg -}}
|
||||
{{- else -}}
|
||||
{{ if not $field.Nillable }}*{{ end }}{{ $arg }}
|
||||
|
||||
@@ -128,14 +128,6 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }}
|
||||
}
|
||||
{{- with $.Scope.Nodes }}
|
||||
for _, k := range nodes {
|
||||
{{- $id := $e.Type.ID -}}
|
||||
{{- /* Convert string-ids that are stored as int in the database */ -}}
|
||||
{{- if and (not $id.UserDefined) $id.IsString }}
|
||||
k, err := strconv.Atoi(k)
|
||||
if err != nil {
|
||||
return {{ $.Scope.Zero }}, err
|
||||
}
|
||||
{{- end }}
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
Reference in New Issue
Block a user