mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: move schemaconfig template to feature-flag
This commit is contained in:
committed by
Ariel Mashraki
parent
0e7823383e
commit
aeb1ccc571
179
entc/gen/template/dialect/sql/feature/schemaconfig.tmpl
Normal file
179
entc/gen/template/dialect/sql/feature/schemaconfig.tmpl
Normal file
@@ -0,0 +1,179 @@
|
||||
{{/*
|
||||
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.
|
||||
*/}}
|
||||
|
||||
{{- define "dialect/sql/internal/schemaconfig" -}}
|
||||
{{ with extend $ "Package" "internal" -}}
|
||||
{{ template "header" . }}
|
||||
{{ end }}
|
||||
import "context"
|
||||
|
||||
// SchemaConfig represents alternative schema names for all tables
|
||||
// that can be passed at runtime.
|
||||
type SchemaConfig struct {
|
||||
{{- range $n := $.Nodes }}
|
||||
{{ $n.Name }} string // {{ $n.Name }} table.
|
||||
{{- range $e := $n.Edges }}
|
||||
{{- if and $e.M2M (not $e.Inverse) }}
|
||||
{{ $n.Name }}{{ $e.StructField }} string // {{ $n.Name }}-{{ $e.Name }}->{{ $e.Type.Name }} table.
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
type schemaCtxKey struct{}
|
||||
|
||||
// SchemaConfigFromContext returns a SchemaConfig stored inside a context, or empty if there isn't one.
|
||||
func SchemaConfigFromContext(ctx context.Context) SchemaConfig {
|
||||
config, _ := ctx.Value(schemaCtxKey{}).(SchemaConfig)
|
||||
return config
|
||||
}
|
||||
|
||||
// NewSchemaConfigContext returns a new context with the given SchemaConfig attached.
|
||||
func NewSchemaConfigContext(parent context.Context, config SchemaConfig) context.Context {
|
||||
return context.WithValue(parent, schemaCtxKey{}, config)
|
||||
}
|
||||
|
||||
{{- end }}
|
||||
|
||||
{{/* Addtional imports by the schemaconfig feature. */}}
|
||||
{{- define "dialect/sql/import/additional/schemaconfig" -}}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
"{{ $.Config.Package }}/internal"
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{/* Addtional fields to the config struct. */}}
|
||||
{{- define "dialect/sql/config/fields/schemaconfig" -}}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
schemaConfig SchemaConfig
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
|
||||
{{/* Addtional top-level code for the generated config.go file. */}}
|
||||
{{- define "dialect/sql/config/options/schemaconfig" }}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
// SchemaConfig represents alternative schema names for all tables
|
||||
// that can be passed at runtime.
|
||||
type SchemaConfig = internal.SchemaConfig
|
||||
|
||||
// AlternateSchemas allows alternate schema names to be
|
||||
// passed into ent operations.
|
||||
func AlternateSchema(schemaConfig SchemaConfig) Option {
|
||||
return func(c *config) {
|
||||
c.schemaConfig = schemaConfig
|
||||
}
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "dialect/sql/delete/spec/schemaconfig" }}
|
||||
{{- template "dialect/sql/spec/schemaconfig" $ }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "dialect/sql/update/spec/schemaconfig" }}
|
||||
{{- template "dialect/sql/spec/schemaconfig" $ }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "dialect/sql/create/spec/schemaconfig" }}
|
||||
{{- with extend $ "Ident" "_spec" "SkipContext" true }}
|
||||
{{- template "dialect/sql/spec/schemaconfig" . }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "dialect/sql/query/spec/schemaconfig" }}
|
||||
{{- template "dialect/sql/spec/schemaconfig" . }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "dialect/sql/query/eagerloading/spec/schemaconfig" }}
|
||||
{{- with extend $ "Ident" "_spec.Edge" }}
|
||||
{{- template "dialect/sql/defedge/spec/schemaconfig" . }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "dialect/sql/defedge/spec/schemaconfig" }}
|
||||
{{- $e := $.Scope.Edge }}
|
||||
{{- $builder := pascal $.Scope.Builder }}
|
||||
{{- $receiver := receiver $builder }}
|
||||
{{- $ident := "edge" }}{{ with $.Scope.Ident }}{{ $ident = . }}{{ end }}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
{{- $schema := $e.Type.Name }}
|
||||
{{- if $e.OwnFK }}
|
||||
{{- $schema = $.Name }}
|
||||
{{- else if $e.M2M }}
|
||||
{{- if $e.Inverse }}
|
||||
{{- $schema = print $e.Type.Name (pascal $e.Inverse) }}
|
||||
{{- else }}
|
||||
{{- $schema = print $.Name $e.StructField }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{ $ident }}.Schema = {{ $receiver }}.schemaConfig.{{ $schema }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- define "dialect/sql/spec/schemaconfig" -}}
|
||||
{{- $builder := pascal $.Scope.Builder }}
|
||||
{{- $receiver := receiver $builder }}
|
||||
{{- $ident := "_spec.Node" }}{{ with $.Scope.Ident }}{{ $ident = . }}{{ end }}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
{{ $ident }}.Schema = {{ $receiver }}.schemaConfig.{{ $.Name }}
|
||||
{{- if not $.Scope.SkipContext }}
|
||||
ctx = internal.NewSchemaConfigContext(ctx, {{ $receiver }}.schemaConfig)
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "dialect/sql/query/selector/schemaconfig" -}}
|
||||
{{- $builder := pascal $.Scope.Builder }}
|
||||
{{- $receiver := receiver $builder }}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
t1.Schema({{ $receiver }}.schemaConfig.{{ $.Name }})
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "dialect/sql/query/path/schemaconfig" }}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
schemaConfig := {{ $.Scope.Receiver }}.schemaConfig
|
||||
{{- template "dialect/sql/query/step/schemaconfig" . }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "dialect/sql/query/from/schemaconfig" }}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
schemaConfig := {{ $.Scope.Receiver }}.schemaConfig
|
||||
{{- template "dialect/sql/query/step/schemaconfig" . }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "dialect/sql/predicate/edge/has/schemaconfig" -}}
|
||||
{{- template "dialect/sql/predicate/edge/schemaconfig" . }}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "dialect/sql/predicate/edge/haswith/schemaconfig" -}}
|
||||
{{- template "dialect/sql/predicate/edge/schemaconfig" . }}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "dialect/sql/predicate/edge/schemaconfig" -}}
|
||||
{{- if $.FeatureEnabled "sql/schemaconfig" }}
|
||||
schemaConfig := internal.SchemaConfigFromContext(s.Context())
|
||||
{{- template "dialect/sql/query/step/schemaconfig" . }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- define "dialect/sql/query/step/schemaconfig" -}}
|
||||
{{- $e := $.Scope.Edge }}
|
||||
step.To.Schema = schemaConfig.{{ $e.Type.Name }}
|
||||
{{- $schema := $e.Type.Name }}
|
||||
{{- if $e.OwnFK }}
|
||||
{{- $schema = $.Name }}
|
||||
{{- else if $e.M2M }}
|
||||
{{- if $e.Inverse }}
|
||||
{{- $schema = print $e.Type.Name (pascal $e.Inverse) }}
|
||||
{{- else }}
|
||||
{{- $schema = print $.Name $e.StructField }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
step.Edge.Schema = schemaConfig.{{ $schema }}
|
||||
{{- end -}}
|
||||
@@ -72,6 +72,12 @@ in the LICENSE file in the root directory of this source tree.
|
||||
{{- end -}}
|
||||
),
|
||||
)
|
||||
{{- /* Allow mutating the sqlgraph.Step by ent extensions or user templates.*/}}
|
||||
{{- with $tmpls := matchTemplate "dialect/sql/predicate/edge/has/*" }}
|
||||
{{- range $tmpl := $tmpls }}
|
||||
{{- xtemplate $tmpl $ }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
sqlgraph.HasNeighbors(s, step)
|
||||
}
|
||||
{{- end }}
|
||||
@@ -91,6 +97,12 @@ in the LICENSE file in the root directory of this source tree.
|
||||
{{- end -}}
|
||||
),
|
||||
)
|
||||
{{- /* Allow mutating the sqlgraph.Step by ent extensions or user templates.*/}}
|
||||
{{- with $tmpls := matchTemplate "dialect/sql/predicate/edge/haswith/*" }}
|
||||
{{- range $tmpl := $tmpls }}
|
||||
{{- xtemplate $tmpl $ }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
|
||||
for _, p := range preds {
|
||||
p(s)
|
||||
|
||||
@@ -134,6 +134,14 @@ func ({{ $receiver }} *{{ $builder }}) querySpec() *sqlgraph.QuerySpec {
|
||||
return _spec
|
||||
}
|
||||
|
||||
{{ template "dialect/sql/query/selector" $ }}
|
||||
|
||||
{{ end }}
|
||||
|
||||
{{ define "dialect/sql/query/selector" }}
|
||||
{{ $builder := pascal $.Scope.Builder }}
|
||||
{{ $receiver := receiver $builder }}
|
||||
|
||||
func ({{ $receiver }} *{{ $builder }}) sqlQuery(ctx context.Context) *sql.Selector {
|
||||
builder := sql.Dialect({{ $receiver }}.driver.Dialect())
|
||||
t1 := builder.Table({{ $.Package }}.Table)
|
||||
@@ -142,6 +150,12 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery(ctx context.Context) *sql.Select
|
||||
selector = {{ $receiver }}.sql
|
||||
selector.Select(selector.Columns({{ $.Package }}.Columns...)...)
|
||||
}
|
||||
{{- /* Allow mutating the sql.Selector by ent extensions or user templates.*/}}
|
||||
{{- with $tmpls := matchTemplate "dialect/sql/query/selector/*" }}
|
||||
{{- range $tmpl := $tmpls }}
|
||||
{{- xtemplate $tmpl $ }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
for _, p := range {{ $receiver }}.predicates {
|
||||
p(selector)
|
||||
}
|
||||
@@ -181,6 +195,12 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery(ctx context.Context) *sql.Select
|
||||
{{- end -}}
|
||||
),
|
||||
)
|
||||
{{- /* Allow mutating the sqlgraph.Step by ent extensions or user templates.*/}}
|
||||
{{- with $tmpls := matchTemplate "dialect/sql/query/path/*" }}
|
||||
{{- range $tmpl := $tmpls }}
|
||||
{{- xtemplate $tmpl $ }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{ $ident }} = sqlgraph.SetNeighbors({{ $receiver }}.driver.Dialect(), step)
|
||||
{{ end }}
|
||||
|
||||
@@ -202,6 +222,12 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery(ctx context.Context) *sql.Select
|
||||
{{- end -}}
|
||||
),
|
||||
)
|
||||
{{- /* Allow mutating the sqlgraph.Step by ent extensions or user templates.*/}}
|
||||
{{- with $tmpls := matchTemplate "dialect/sql/query/from/*" }}
|
||||
{{- range $tmpl := $tmpls }}
|
||||
{{- xtemplate $tmpl $ }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{ $ident }} = sqlgraph.Neighbors({{ $receiver }}.driver.Dialect(), step)
|
||||
{{ end }}
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ent: starting a transaction: %v", err)
|
||||
}
|
||||
cfg := config{driver: &txDriver{tx: tx, drv: c.driver}, log: c.log, debug: c.debug, hooks: c.hooks}
|
||||
cfg := c.config
|
||||
cfg.driver = &txDriver{tx: tx, drv: c.driver}
|
||||
return &Tx{
|
||||
config: cfg,
|
||||
{{ range $_, $n := $.Nodes -}}
|
||||
|
||||
Reference in New Issue
Block a user