Files
ent/entc/gen/template/dialect/sql/feature/modifier.tmpl
2025-03-17 15:42:41 +07:00

86 lines
3.3 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.Type */}}
{{/* Templates used by the "sql/modifier" feature-flag to add custom modifiers to the builders. */}}
{{/* Template for adding the "modifiers" field to the query builder. */}}
{{ define "dialect/sql/query/fields/additional/modify" -}}
{{- if or ($.FeatureEnabled "sql/lock") ($.FeatureEnabled "sql/modifier") }}
modifiers []func(*sql.Selector)
{{- end }}
{{- end -}}
{{/* 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") }}
for _, m := range q.modifiers {
m(selector)
}
{{- end }}
{{- end -}}
{{/* Template for passing the modifiers to the sqlgraph.QuerySpec. */}}
{{ define "dialect/sql/query/spec/modify" }}
{{- if or ($.FeatureEnabled "sql/lock") ($.FeatureEnabled "sql/modifier") }}
if len(q.modifiers) > 0 {
_spec.Modifiers = q.modifiers
}
{{- end }}
{{- end -}}
{{/* A template for adding the Modify method to the query-builder. */}}
{{ define "dialect/sql/query/additional/modify" }}
{{ if $.FeatureEnabled "sql/modifier" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $selectBuilder := pascal $.Name | printf "%sSelect" }}
// Modify adds a query modifier for attaching custom logic to queries.
func (q *{{ $builder }}) Modify(modifiers ...func(s *sql.Selector)) *{{ $selectBuilder }} {
q.modifiers = append(q.modifiers, modifiers...)
return q.Select()
}
{{ end }}
{{ end }}
{{/* A template for adding the Modify method to the select-builder. */}}
{{ define "dialect/sql/select/additional/modify" }}
{{ if $.FeatureEnabled "sql/modifier" }}
{{ $builder := pascal $.Scope.Builder }}
// Modify adds a query modifier for attaching custom logic to queries.
func (q *{{ $builder }}) Modify(modifiers ...func(s *sql.Selector)) *{{ $builder }} {
q.modifiers = append(q.modifiers, modifiers...)
return q
}
{{ end }}
{{ end }}
{{/* Template for adding the "modifiers" field to the update builder. */}}
{{ define "dialect/sql/update/fields/additional/modify" -}}
{{- if $.FeatureEnabled "sql/modifier" }}
modifiers []func(*sql.UpdateBuilder)
{{- end }}
{{- end -}}
{{/* A template for adding the Modify method to the update and updateone builders. */}}
{{ define "dialect/sql/update/additional/modify" }}
{{ if $.FeatureEnabled "sql/modifier" }}
{{ $pkg := $.Scope.Package }}
{{ $builder := pascal $.Scope.Builder }}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (u *{{ $builder }}) Modify(modifiers ...func(*sql.UpdateBuilder)) *{{ $builder }} {
u.modifiers = append(u.modifiers, modifiers...)
return u
}
{{ end }}
{{ end }}
{{/* Template for passing the modifiers to the sqlgraph.UpdateSpec. */}}
{{ define "dialect/sql/update/spec/modify" }}
{{- if $.FeatureEnabled "sql/modifier" }}
_spec.AddModifiers(u.modifiers...)
{{- end }}
{{- end -}}