Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion dialect/sql/sqlgraph/entql.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,22 @@ func (e *state) evalBinary(expr *entql.BinaryExpr) *sql.Predicate {
func (e *state) evalEdge(name string, exprs ...entql.Expr) *sql.Predicate {
edge, ok := e.context.Edges[name]
expect(ok, "edge %q was not found for node %q", name, e.context.Type)
var toC string
switch {
case edge.To.ID != nil:
toC = edge.To.ID.Column
// Edge-owner points to its edge schema.
case edge.To.CompositeID != nil && !edge.Spec.Inverse:
toC = edge.To.CompositeID[0].Column
// Edge-backref points to its edge schema.
case edge.To.CompositeID != nil && edge.Spec.Inverse:
toC = edge.To.CompositeID[1].Column
default:
panic(evalError{fmt.Sprintf("expect id definition for edge %q", name)})
}
step := NewStep(
From(e.context.Table, e.context.ID.Column),
To(edge.To.Table, edge.To.ID.Column),
To(edge.To.Table, toC),
Edge(edge.Spec.Rel, edge.Spec.Inverse, edge.Spec.Table, edge.Spec.Columns...),
)
selector := e.selector.Clone().SetP(nil)
Expand Down
9 changes: 9 additions & 0 deletions entc/gen/template/dialect/sql/entql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ var schemaGraph = func() *sqlgraph.Schema {
Type: field.{{ $n.ID.Type.ConstName }},
Column: {{ $n.Package }}.{{ $n.ID.Constant }},
},
{{- else }}
CompositeID: []*sqlgraph.FieldSpec{
{{- range $id := $n.EdgeSchema.ID }}
{
Type: field.{{ $id.Type.ConstName }},
Column: {{ $n.Package }}.{{ $id.Constant }},
},
{{- end }}
},
{{- end }}
},
Type: "{{ $n.Name }}",
Expand Down
6 changes: 4 additions & 2 deletions entc/gen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ type (
// For O2O and O2M, it's the table name of the type we're this edge point to.
// For M2O, this is the owner's type, and for M2M this is the join table.
Table string
// Columns holds the relation column in the relation table above.
// In O2M, M2O and O2O, this the first element.
// Columns holds the relation column(s) in the relation table above.
// For O2M, M2O and O2O, it contains one element with the column name.
// For M2M edges, it contains two columns defined in the join table with
// the same order as defined in the schema: (owner_id, reference_id).
Columns []string
// foreign-key information for non-M2M edges.
fk *ForeignKey
Expand Down
10 changes: 10 additions & 0 deletions entc/integration/customid/ent/entql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions entc/integration/edgeschema/edgeschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
_ "entgo.io/ent/entc/integration/edgeschema/ent/runtime"
"entgo.io/ent/entc/integration/edgeschema/ent/tweetlike"
"entgo.io/ent/entc/integration/edgeschema/ent/user"
"entgo.io/ent/entql"

"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -249,3 +250,45 @@ func TestEdgeSchemaForO2M(t *testing.T) {
).SaveX(ctx)
nat.Update().AddTweets(tweets...).ExecX(ctx)
}

func TestEdgeSchemaEntQL(t *testing.T) {
client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1")
require.NoError(t, err)
defer client.Close()
ctx := context.Background()
require.NoError(t, client.Schema.Create(ctx, migrate.WithGlobalUniqueID(true)))

tweets := client.Tweet.CreateBulk(
client.Tweet.Create().SetText("t1"),
client.Tweet.Create().SetText("t2"),
).SaveX(ctx)
nat := client.User.Create().SetName("nati").SaveX(ctx)
a8m := client.User.Create().SetName("a8m").AddLikedTweets(tweets...).SaveX(ctx)

// Using the regular fluent API.
require.Equal(t, a8m.ID, client.User.Query().Where(user.HasLikes()).OnlyIDX(ctx))
require.Equal(t, nat.ID, client.User.Query().Where(user.Not(user.HasLikes())).OnlyIDX(ctx))

// Using EntQL.
q1, q2 := client.User.Query(), client.User.Query()
q1.Filter().WhereHasLikes()
q2.Filter().Where(entql.Not(entql.HasEdge("likes")))
require.Equal(t, a8m.ID, q1.OnlyIDX(ctx))
require.Equal(t, nat.ID, q2.OnlyIDX(ctx))

nat.Update().AddLikedTweets(tweets[0]).ExecX(ctx)
// Using the regular fluent API.
require.Equal(t, 2, client.User.Query().Where(user.HasLikesWith(tweetlike.TweetID(tweets[0].ID))).CountX(ctx))
require.Equal(t, 1, client.User.Query().Where(user.HasLikesWith(tweetlike.TweetID(tweets[1].ID))).CountX(ctx))
// Using EntQL.
q1, q2 = client.User.Query(), client.User.Query()
q1.Filter().WhereHasLikesWith(tweetlike.TweetID(tweets[0].ID))
q2.Filter().Where(entql.HasEdgeWith("likes", entql.FieldEQ(tweetlike.FieldTweetID, tweets[0].ID)))
require.Equal(t, 2, q1.CountX(ctx))
require.Equal(t, 2, q2.CountX(ctx))
q1, q2 = client.User.Query(), client.User.Query()
q1.Filter().WhereHasLikesWith(tweetlike.TweetID(tweets[1].ID))
q2.Filter().Where(entql.HasEdgeWith("likes", entql.FieldEQ(tweetlike.FieldTweetID, tweets[1].ID)))
require.Equal(t, 1, q1.CountX(ctx))
require.Equal(t, 1, q2.CountX(ctx))
}
1 change: 1 addition & 0 deletions entc/integration/edgeschema/ent/entc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func main() {
// Code generated by ent, DO NOT EDIT.
`,
Features: []gen.Feature{
gen.FeatureEntQL,
gen.FeatureUpsert,
gen.FeaturePrivacy,
gen.FeatureSnapshot,
Expand Down
Loading