Merge pull request #259 from facebookincubator/sqlgraph/query

entc/gen: add edge-fields + minor changes
This commit is contained in:
Ariel Mashraki
2019-12-23 14:44:27 +02:00
committed by GitHub
17 changed files with 100 additions and 36 deletions

View File

@@ -262,13 +262,13 @@ func (g *Graph) resolve(t *Type) error {
switch {
case !e.Unique && e.Type == t:
e.Rel.Type = M2M
e.SelfRef = true
e.Bidi = true
e.Rel.Table = t.Label() + "_" + e.Name
c1, c2 := e.Owner.Label()+"_id", rules.Singularize(e.Name)+"_id"
e.Rel.Columns = append(e.Rel.Columns, c1, c2)
case e.Unique && e.Type == t:
e.Rel.Type = O2O
e.SelfRef = true
e.Bidi = true
e.Rel.Table = t.Table()
case e.Unique:
e.Rel.Type = M2O

File diff suppressed because one or more lines are too long

View File

@@ -56,7 +56,7 @@ in the LICENSE file in the root directory of this source tree.
{{- end -}}
func(t *dsl.Traversal) {
{{- /* if it's an edge with self-reference, take the two vertices */}}
{{- if $e.SelfRef }}
{{- if $e.Bidi }}
t.Both({{ $label }})
{{- else }}
t.{{ $direction }}E({{ $label }}).{{ $direction }}V()
@@ -76,7 +76,7 @@ in the LICENSE file in the root directory of this source tree.
{{- $label = $e.InverseConstant -}}
{{- end -}}
func(t *dsl.Traversal) {
{{- if $e.SelfRef }}{{/* selfref means it should be true in one of the directions */}}
{{- if $e.Bidi }}{{/* selfref means it should be true in one of the directions */}}
in, out := __.InV(), __.OutV()
for _, p := range preds {
p(in)

View File

@@ -74,7 +74,7 @@ func ({{ $receiver }} *{{ $builder }}) gremlinQuery() *dsl.Traversal {
{{- $e := $.Scope.Edge }} {{/* the edge we need to genegrate the path to. */}}
{{- $receiver := $.Scope.Receiver }}
gremlin := {{ $receiver }}.gremlinQuery()
{{- if $e.SelfRef }}
{{- if $e.Bidi }}
query.gremlin = gremlin.Both({{ $.Package }}.{{ $e.Constant }})
{{- else if $e.IsInverse }}
query.gremlin = gremlin.InE({{ $e.Type.Package }}.{{ $e.Constant }}).OutV()
@@ -90,7 +90,7 @@ func ({{ $receiver }} *{{ $builder }}) gremlinQuery() *dsl.Traversal {
{{- $e := $.Scope.Edge }} {{/* the edge we need to genegrate the path to. */}}
{{- $receiver := $.Scope.Receiver }}
{{- if $e.SelfRef }}
{{- if $e.Bidi }}
query.gremlin = g.V({{ $receiver }}.ID).Both({{ $n.Package }}.{{ $e.Constant }})
{{- else if $e.IsInverse }}
query.gremlin = g.V({{ $receiver }}.ID).InE({{ $e.Type.Package }}.{{ $e.Constant }}).OutV()

View File

@@ -108,7 +108,7 @@ func ({{ $receiver }} *{{ $builder }}) gremlin({{ if $one }}id {{ $.ID.Type }}{{
{{- else }}
for id := range {{ $receiver }}.removed{{ $e.StructField }} {
{{- end }}
{{- if $e.SelfRef }}
{{- if $e.Bidi }}
tr := rv.Clone().BothE({{ $name }}){{ if not $e.Unique }}.Where(__.Or(__.InV().HasID(id), __.OutV().HasID(id))){{ end }}.Drop().Iterate()
{{- else if $e.IsInverse }}
tr := rv.Clone().InE({{ $name }}){{ if not $e.Unique }}.Where(__.OtherV().HasID(id)){{ end }}.Drop().Iterate()
@@ -125,7 +125,7 @@ func ({{ $receiver }} *{{ $builder }}) gremlin({{ if $one }}id {{ $.ID.Type }}{{
v.AddE({{ $name }}).To(g.V(id)).OutV()
{{- end }}
{{- if $e.HasConstraint }}
{{- if $e.SelfRef }}
{{- if $e.Bidi }}
constraints = append(constraints, &constraint{
pred: rv.Clone().Both({{ $name }}).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge({{ $.Package }}.Label, {{ $name }}, id)),

View File

@@ -112,7 +112,7 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }}
Inverse: {{ $e.IsInverse }},
Table: {{ $.Package }}.{{ $e.TableConstant }},
Columns: {{ if $e.M2M }}{{ $.Package }}.{{ $e.PKConstant }}{{ else }}[]string{ {{ $.Package }}.{{ $e.ColumnConstant }} }{{ end }},
Bidi: {{ $e.SelfRef }},
Bidi: {{ $e.Bidi }},
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.{{ $e.Type.ID.Type.ConstName }},

View File

@@ -89,14 +89,14 @@ type (
StructTag string
// Relation holds the relation info of an edge.
Rel Relation
// SelfRef indicates if this edge is a self-reference to the same
// type with the same name (symmetric relation). For example, a User
// type have one of following edges:
// Bidi indicates if this edge is a bidirectional edge. A self-reference
// to the same type with the same name (symmetric relation). For example,
// a User type have one of following edges:
//
// edge.To("friends", User.Type) // many 2 many.
// edge.To("spouse", User.Type).Unique() // one 2 one.
//
SelfRef bool
Bidi bool
}
// Relation holds the relational database information for edges.
@@ -384,6 +384,21 @@ func (t *Type) AddIndex(idx *load.Index) error {
return nil
}
// EdgeFields returns the edges that resides in the type's
// table in SQL as foreign-keys (columns).
func (t Type) EdgeFields() []*Edge {
var fks []*Edge
for _, e := range t.Edges {
switch {
case e.M2O():
fks = append(fks, e)
case e.O2O() && (e.IsInverse() || e.Bidi):
fks = append(fks, e)
}
}
return fks
}
// Constant returns the constant name of the field.
func (f Field) Constant() string { return "Field" + pascal(f.Name) }
@@ -596,6 +611,7 @@ func (e Edge) PKConstant() string { return pascal(e.Name) + "PrimaryKey" }
// HasConstraint indicates if this edge has a unique constraint check.
// We check uniqueness when both-directions are unique or one of them.
// Used by the Gremlin storage-layer.
func (e Edge) HasConstraint() bool {
return e.Rel.Type == O2O || e.Rel.Type == O2M
}

View File

@@ -0,0 +1,7 @@
// 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.
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema

View File

@@ -0,0 +1,7 @@
// 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.
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema

View File

@@ -0,0 +1,7 @@
// 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.
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --idtype string --template ./template --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema

View File

@@ -1,15 +0,0 @@
// 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.
package integration
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --idtype string --template ./ent/template --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./ent/schema
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --target ./gremlin/ent --storage=gremlin --idtype string --template ./ent/template --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./ent/schema
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./migrate/entv1/schema
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./migrate/entv2/schema
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./template/ent/schema --template=template/ent/template
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./json/ent/schema
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./config/ent/schema
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." --idtype uint64 ./idtype/ent/schema
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./customid/ent/schema

View File

@@ -0,0 +1,7 @@
// 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.
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --target . --storage=gremlin --idtype string --template ../../ent/template --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ../../ent/schema

View File

@@ -0,0 +1,7 @@
// 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.
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." --idtype uint64 ./schema

View File

@@ -0,0 +1,7 @@
// 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.
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema

View File

@@ -0,0 +1,7 @@
// 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.
package entv1
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema

View File

@@ -0,0 +1,7 @@
// 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.
package entv2
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema

View File

@@ -0,0 +1,7 @@
// 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.
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate --template=./template --header "// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema