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:
Ariel Mashraki
2019-11-05 13:07:38 -08:00
committed by Facebook Github Bot
parent a5d6a9e8b8
commit 930a17b767
17 changed files with 758 additions and 443 deletions

View File

@@ -170,12 +170,17 @@ func (c *CardClient) GetX(ctx context.Context, id int) *Card {
func (c *CardClient) QueryOwner(ca *Card) *UserQuery {
query := &UserQuery{config: c.config}
id := ca.ID
builder := sql.Dialect(ca.driver.Dialect())
t1 := builder.Table(user.Table)
t2 := builder.Select(card.OwnerColumn).
From(builder.Table(card.OwnerTable)).
Where(sql.EQ(card.FieldID, id))
query.sql = builder.Select().From(t1).Join(t2).On(t1.C(user.FieldID), t2.C(card.OwnerColumn))
step := &sql.Step{}
step.From.V = id
step.From.Table = card.Table
step.From.Column = card.FieldID
step.To.Table = user.Table
step.To.Column = user.FieldID
step.Edge.Rel = sql.O2O
step.Edge.Inverse = true
step.Edge.Table = card.OwnerTable
step.Edge.Columns = append(step.Edge.Columns, card.OwnerColumn)
query.sql = sql.Neighbors(ca.driver.Dialect(), step)
return query
}
@@ -248,9 +253,17 @@ func (c *UserClient) GetX(ctx context.Context, id int) *User {
func (c *UserClient) QueryCard(u *User) *CardQuery {
query := &CardQuery{config: c.config}
id := u.ID
builder := sql.Dialect(u.driver.Dialect())
query.sql = builder.Select().From(builder.Table(card.Table)).
Where(sql.EQ(user.CardColumn, id))
step := &sql.Step{}
step.From.V = id
step.From.Table = user.Table
step.From.Column = user.FieldID
step.To.Table = card.Table
step.To.Column = card.FieldID
step.Edge.Rel = sql.O2O
step.Edge.Inverse = false
step.Edge.Table = user.CardTable
step.Edge.Columns = append(step.Edge.Columns, user.CardColumn)
query.sql = sql.Neighbors(u.driver.Dialect(), step)
return query
}