mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql: add neighbors function (#140)
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/140 This part of the effort of moving logic from Go templates to shared packages. Next diffs will migrate the neighbors-set and predicates as well. Dedicated tests for `sql/dialect/graph.go` will be added in a follow-up diff. Reviewed By: alexsn Differential Revision: D18304531 fbshipit-source-id: 93e9cf2636b5b4525eb27305bd2708122cfd6e40
This commit is contained in:
committed by
Facebook Github Bot
parent
a5d6a9e8b8
commit
930a17b767
@@ -170,19 +170,17 @@ func (c *GroupClient) GetX(ctx context.Context, id int) *Group {
|
||||
func (c *GroupClient) QueryUsers(gr *Group) *UserQuery {
|
||||
query := &UserQuery{config: c.config}
|
||||
id := gr.ID
|
||||
builder := sql.Dialect(gr.driver.Dialect())
|
||||
t1 := builder.Table(user.Table)
|
||||
t2 := builder.Table(group.Table)
|
||||
t3 := builder.Table(group.UsersTable)
|
||||
t4 := builder.Select(t3.C(group.UsersPrimaryKey[1])).
|
||||
From(t3).
|
||||
Join(t2).
|
||||
On(t3.C(group.UsersPrimaryKey[0]), t2.C(group.FieldID)).
|
||||
Where(sql.EQ(t2.C(group.FieldID), id))
|
||||
query.sql = builder.Select().
|
||||
From(t1).
|
||||
Join(t4).
|
||||
On(t1.C(user.FieldID), t4.C(group.UsersPrimaryKey[1]))
|
||||
step := &sql.Step{}
|
||||
step.From.V = id
|
||||
step.From.Table = group.Table
|
||||
step.From.Column = group.FieldID
|
||||
step.To.Table = user.Table
|
||||
step.To.Column = user.FieldID
|
||||
step.Edge.Rel = sql.M2M
|
||||
step.Edge.Inverse = false
|
||||
step.Edge.Table = group.UsersTable
|
||||
step.Edge.Columns = append(step.Edge.Columns, group.UsersPrimaryKey...)
|
||||
query.sql = sql.Neighbors(gr.driver.Dialect(), step)
|
||||
|
||||
return query
|
||||
}
|
||||
@@ -255,19 +253,17 @@ func (c *UserClient) GetX(ctx context.Context, id int) *User {
|
||||
func (c *UserClient) QueryGroups(u *User) *GroupQuery {
|
||||
query := &GroupQuery{config: c.config}
|
||||
id := u.ID
|
||||
builder := sql.Dialect(u.driver.Dialect())
|
||||
t1 := builder.Table(group.Table)
|
||||
t2 := builder.Table(user.Table)
|
||||
t3 := builder.Table(user.GroupsTable)
|
||||
t4 := builder.Select(t3.C(user.GroupsPrimaryKey[0])).
|
||||
From(t3).
|
||||
Join(t2).
|
||||
On(t3.C(user.GroupsPrimaryKey[1]), t2.C(user.FieldID)).
|
||||
Where(sql.EQ(t2.C(user.FieldID), id))
|
||||
query.sql = builder.Select().
|
||||
From(t1).
|
||||
Join(t4).
|
||||
On(t1.C(group.FieldID), t4.C(user.GroupsPrimaryKey[0]))
|
||||
step := &sql.Step{}
|
||||
step.From.V = id
|
||||
step.From.Table = user.Table
|
||||
step.From.Column = user.FieldID
|
||||
step.To.Table = group.Table
|
||||
step.To.Column = group.FieldID
|
||||
step.Edge.Rel = sql.M2M
|
||||
step.Edge.Inverse = true
|
||||
step.Edge.Table = user.GroupsTable
|
||||
step.Edge.Columns = append(step.Edge.Columns, user.GroupsPrimaryKey...)
|
||||
query.sql = sql.Neighbors(u.driver.Dialect(), step)
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user