entc/gen/template: allow nested template access Receiver (#4354)

This commit is contained in:
Giau. Tran Minh
2025-03-17 17:16:05 +07:00
committed by GitHub
parent 6109670fa2
commit 088bfd75ad
17 changed files with 43 additions and 34 deletions

View File

@@ -16,7 +16,7 @@ in the LICENSE file in the root directory of this source tree.
{{ define "helper/sqlock" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $receiver := $.Scope.Receiver }}
// ForUpdate locks the selected rows against concurrent updates, and prevent them from being
// updated, deleted or "selected ... for update" by other sessions, until the transaction is

View File

@@ -18,7 +18,7 @@ in the LICENSE file in the root directory of this source tree.
{{/* Template for adding the "executing" the list of modifiers on the sql.Selector. */}}
{{ define "dialect/sql/query/selector/modify" }}
{{- if or ($.FeatureEnabled "sql/lock") ($.FeatureEnabled "sql/modifier") }}
{{- $receiver := pascal $.Scope.Builder | receiver }}
{{- $receiver := $.Scope.Receiver }}
for _, m := range {{ $receiver }}.modifiers {
m(selector)
}
@@ -28,7 +28,7 @@ in the LICENSE file in the root directory of this source tree.
{{/* Template for passing the modifiers to the sqlgraph.QuerySpec. */}}
{{ define "dialect/sql/query/spec/modify" }}
{{- if or ($.FeatureEnabled "sql/lock") ($.FeatureEnabled "sql/modifier") }}
{{- $receiver := pascal $.Scope.Builder | receiver }}
{{- $receiver := $.Scope.Receiver }}
if len({{ $receiver }}.modifiers) > 0 {
_spec.Modifiers = {{ $receiver }}.modifiers
}
@@ -39,7 +39,7 @@ in the LICENSE file in the root directory of this source tree.
{{ define "dialect/sql/query/additional/modify" }}
{{ if $.FeatureEnabled "sql/modifier" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $receiver := $.Scope.Receiver }}
{{ $selectBuilder := pascal $.Name | printf "%sSelect" }}
// Modify adds a query modifier for attaching custom logic to queries.
func ({{ $receiver }} *{{ $builder }}) Modify(modifiers ...func(s *sql.Selector)) *{{ $selectBuilder }} {
@@ -53,7 +53,7 @@ in the LICENSE file in the root directory of this source tree.
{{ define "dialect/sql/select/additional/modify" }}
{{ if $.FeatureEnabled "sql/modifier" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $receiver := $.Scope.Receiver }}
// Modify adds a query modifier for attaching custom logic to queries.
func ({{ $receiver }} *{{ $builder }}) Modify(modifiers ...func(s *sql.Selector)) *{{ $builder }} {
{{ $receiver }}.modifiers = append({{ $receiver }}.modifiers, modifiers...)
@@ -74,7 +74,7 @@ in the LICENSE file in the root directory of this source tree.
{{ if $.FeatureEnabled "sql/modifier" }}
{{ $pkg := $.Scope.Package }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $receiver := $.Scope.Receiver }}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func ({{ $receiver }} *{{ $builder }}) Modify(modifiers ...func(u *sql.UpdateBuilder)) *{{ $builder }} {
{{ $receiver }}.modifiers = append({{ $receiver }}.modifiers, modifiers...)
@@ -86,7 +86,7 @@ in the LICENSE file in the root directory of this source tree.
{{/* Template for passing the modifiers to the sqlgraph.UpdateSpec. */}}
{{ define "dialect/sql/update/spec/modify" }}
{{- if $.FeatureEnabled "sql/modifier" }}
{{- $receiver := pascal $.Scope.Builder | receiver }}
{{- $receiver := $.Scope.Receiver }}
_spec.AddModifiers({{ $receiver }}.modifiers...)
{{- end }}
{{- end -}}

View File

@@ -66,7 +66,7 @@ in the LICENSE file in the root directory of this source tree.
{{ define "dialect/sql/query/additional/namedges" }}
{{- if $.FeatureEnabled "namedges" }}
{{ $builder := $.QueryName }}
{{ $receiver := receiver $builder }}
{{ $receiver := $.QueryReceiver }}
{{- range $e := $.Edges }}
{{- if not $e.Unique }}
{{ $ebuilder := $e.Type.QueryName }}
@@ -93,7 +93,7 @@ in the LICENSE file in the root directory of this source tree.
{{- define "dialect/sql/query/all/nodes/namedges" }}
{{- if $.FeatureEnabled "namedges" }}
{{- $builder := pascal $.Scope.Builder }}
{{- $receiver := receiver $builder }}
{{- $receiver := $.Scope.Receiver }}
{{- range $e := $.Edges }}
{{- if not $e.Unique }}
for name, query := range {{ $receiver }}.{{ $e.EagerLoadNamedField }} {

View File

@@ -100,7 +100,7 @@ func NewSchemaConfigContext(parent context.Context, config SchemaConfig) context
{{- define "dialect/sql/defedge/spec/schemaconfig" }}
{{- $e := $.Scope.Edge }}
{{- $builder := pascal $.Scope.Builder }}
{{- $receiver := receiver $builder }}
{{- $receiver := $.Scope.Receiver }}
{{- $ident := "edge" }}{{ with $.Scope.Ident }}{{ $ident = . }}{{ end }}
{{- if $.FeatureEnabled "sql/schemaconfig" }}
{{- $schema := $e.Type.Name }}
@@ -126,7 +126,7 @@ func NewSchemaConfigContext(parent context.Context, config SchemaConfig) context
{{/* A template for injecting the SchemaConfig to the context. Should be executed before other templates. */}}
{{- define "dialect/sql/spec/ctxschemaconfig" -}}
{{- $builder := pascal $.Scope.Builder }}
{{- $receiver := receiver $builder }}
{{- $receiver := $.Scope.Receiver }}
{{- $ident := "_spec.Node" }}{{ with $.Scope.Ident }}{{ $ident = . }}{{ end }}
{{- if $.FeatureEnabled "sql/schemaconfig" }}
{{ $ident }}.Schema = {{ $receiver }}.schemaConfig.{{ $.Name }}
@@ -138,7 +138,7 @@ func NewSchemaConfigContext(parent context.Context, config SchemaConfig) context
{{- define "dialect/sql/query/selector/ctxschemaconfig" -}}
{{- $builder := pascal $.Scope.Builder }}
{{- $receiver := receiver $builder }}
{{- $receiver := $.Scope.Receiver }}
{{- if $.FeatureEnabled "sql/schemaconfig" }}
t1.Schema({{ $receiver }}.schemaConfig.{{ $.Name }})
ctx = internal.NewSchemaConfigContext(ctx, {{ $receiver }}.schemaConfig)