mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
ent/entc: configure storage driver in codegen
Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1229 Reviewed By: alexsn Differential Revision: D16539934 fbshipit-source-id: b3a8bf1f1be6f65ad3f649cd921ea20fc24182bf
This commit is contained in:
committed by
Facebook Github Bot
parent
c6b178de20
commit
56656dfcb6
145
entc/gen/template/dialect/sql/predicate.tmpl
Normal file
145
entc/gen/template/dialect/sql/predicate.tmpl
Normal file
@@ -0,0 +1,145 @@
|
||||
{{ define "dialect/sql/predicate/id" -}}
|
||||
func(s *sql.Selector) {
|
||||
{{- if $.ID.IsString }}id, _ := strconv.Atoi(id){{- end }}
|
||||
s.Where(sql.EQ(s.C({{ $.ID.Constant }}), id))
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/predicate/id/ops" -}}
|
||||
{{- $op := $.Scope.Op -}}
|
||||
{{- $arg := $.Scope.Arg -}}
|
||||
func(s *sql.Selector) {
|
||||
{{- if $op.Variadic }}
|
||||
// if not arguments were provided, append the FALSE constants,
|
||||
// since we can't apply "IN ()". This will make this predicate falsy.
|
||||
if len({{ $arg }}) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
v := make([]interface{}, len({{ $arg }}))
|
||||
for i := range v {
|
||||
{{ if $.ID.IsString }}v[i], _ = strconv.Atoi({{ $arg }}[i]){{ else }}v[i] = {{ $arg }}[i]{{ end }}
|
||||
}
|
||||
{{- else if $.ID.IsString }}
|
||||
v, _ := strconv.Atoi({{ $arg }})
|
||||
{{- end }}
|
||||
s.Where(sql.{{ $op.Name }}(s.C({{ $.ID.Constant }}), v{{ if $op.Variadic }}...{{ end }}))
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/predicate/field" -}}
|
||||
{{- $f := $.Scope.Field -}}
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EQ(s.C({{ $f.Constant }}), v))
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/predicate/field/ops" -}}
|
||||
{{- $f := $.Scope.Field -}}
|
||||
{{- $op := $.Scope.Op -}}
|
||||
{{- $arg := $.Scope.Arg -}}
|
||||
func(s *sql.Selector) {
|
||||
{{- if $op.Variadic }}
|
||||
// if not arguments were provided, append the FALSE constants,
|
||||
// since we can't apply "IN ()". This will make this predicate falsy.
|
||||
if len({{ $arg }}) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
{{- end }}
|
||||
s.Where(sql.{{ $op.Name }}(s.C({{ $f.Constant }}), v{{ if $op.Variadic }}...{{ end }}))
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/predicate/edge/has" -}}
|
||||
{{- $e := $.Scope.Edge -}}
|
||||
func(s *sql.Selector) {
|
||||
{{- if $e.M2M }}
|
||||
t1 := s.Table()
|
||||
s.Where(
|
||||
sql.In(
|
||||
t1.C({{ $.ID.Constant }}),
|
||||
sql.Select({{ $e.PKConstant }}[{{ if $e.IsInverse }}1{{ else }}0{{ end }}]).From(sql.Table({{ $e.TableConstant }})),
|
||||
),
|
||||
)
|
||||
{{- else if or $e.M2O (and $e.O2O $e.IsInverse) }}{{/* M2O || (O2O with inverse edge) */}}
|
||||
t1 := s.Table()
|
||||
s.Where(sql.NotNull(t1.C({{ $e.ColumnConstant }})))
|
||||
{{- else }}{{/* O2M || (O2O with assoc edge) */}}
|
||||
t1 := s.Table()
|
||||
s.Where(
|
||||
sql.In(
|
||||
t1.C({{ $.ID.Constant }}),
|
||||
sql.Select({{ $e.ColumnConstant }}).
|
||||
From(sql.Table({{ $e.TableConstant }})).
|
||||
Where(sql.NotNull({{ $e.ColumnConstant }})),
|
||||
),
|
||||
)
|
||||
{{- end }}
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/predicate/edge/haswith" -}}
|
||||
{{- $e := $.Scope.Edge -}}
|
||||
func(s *sql.Selector) {
|
||||
{{- if $e.M2M }}
|
||||
{{ $i := 1 }}{{ $j := 0 }}{{- if $e.IsInverse }}{{ $i = 0 }}{{ $j = 1 }}{{ end -}}
|
||||
t1 := s.Table()
|
||||
t2 := sql.Table(
|
||||
{{- if ne $.Table $e.Type.Table -}}
|
||||
{{ $e.InverseTableConstant }}
|
||||
{{- else -}}
|
||||
Table
|
||||
{{- end -}}
|
||||
)
|
||||
t3 := sql.Table({{ $e.TableConstant }})
|
||||
t4 := sql.Select(t3.C({{ $e.PKConstant }}[{{ $j }}])).
|
||||
From(t3).
|
||||
Join(t2).
|
||||
On(t3.C({{ $e.PKConstant }}[{{ $i }}]), t2.C({{ $e.Type.ID.Constant }}))
|
||||
t5 := sql.Select().From(t2)
|
||||
for _, p := range preds {
|
||||
p(t5)
|
||||
}
|
||||
t4.FromSelect(t5)
|
||||
s.Where(sql.In(t1.C({{ $.ID.Constant }}), t4))
|
||||
{{- else if or $e.M2O (and $e.O2O $e.IsInverse) }}{{/* M2O || (O2O with inverse edge) */}}
|
||||
t1 := s.Table()
|
||||
t2 := sql.Select({{ $e.Type.ID.Constant }}).From(sql.Table(
|
||||
{{- if ne $.Table $e.Type.Table -}}
|
||||
{{ $e.InverseTableConstant }}
|
||||
{{- else -}}
|
||||
{{ $e.TableConstant }}
|
||||
{{- end -}}
|
||||
))
|
||||
for _, p := range preds {
|
||||
p(t2)
|
||||
}
|
||||
s.Where(sql.In(t1.C({{ $e.ColumnConstant }}), t2))
|
||||
{{- else }}{{/* O2M || (O2O with assoc edge) */}}
|
||||
t1 := s.Table()
|
||||
t2 := sql.Select({{ $e.ColumnConstant }}).From(sql.Table({{ $e.TableConstant }}))
|
||||
for _, p := range preds {
|
||||
p(t2)
|
||||
}
|
||||
s.Where(sql.In(t1.C({{ $.ID.Constant }}), t2))
|
||||
{{- end }}
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/predicate/or" -}}
|
||||
func(s *sql.Selector) {
|
||||
for i, p := range predicates {
|
||||
if i > 0 {
|
||||
s.Or()
|
||||
}
|
||||
p(s)
|
||||
}
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/predicate/not" -}}
|
||||
func(s *sql.Selector) {
|
||||
p(s.Not())
|
||||
}
|
||||
{{- end }}
|
||||
Reference in New Issue
Block a user