dialect/sql: add hasneigbors for predicates (#184)

Summary:
Pull Request resolved: https://github.com/facebookincubator/ent/pull/184

This diff is part of the ongoing effort for moving the sql logic out from codegen.

Reviewed By: alexsn

Differential Revision: D18656817

fbshipit-source-id: 71001d37eb78032414c2f83f200d3011b50c114b
This commit is contained in:
Ariel Mashraki
2019-11-24 03:26:03 -08:00
committed by Facebook Github Bot
parent c355ed49be
commit 2543d90167
2 changed files with 92 additions and 0 deletions

View File

@@ -142,3 +142,37 @@ func SetNeighbors(dialect string, s *Step) (q *Selector) {
}
return q
}
// HasNeighbors applies on the given Selector a neighbors check.
func HasNeighbors(q *Selector, s *Step) {
builder := Dialect(q.dialect)
switch r := s.Edge.Rel; {
case r == M2M:
pk1 := s.Edge.Columns[1]
if s.Edge.Inverse {
pk1 = s.Edge.Columns[0]
}
from := q.Table()
join := builder.Table(s.Edge.Table)
q.Where(
In(
from.C(s.From.Column),
builder.Select(join.C(pk1)).From(join),
),
)
case r == M2O || (r == O2O && s.Edge.Inverse):
from := q.Table()
q.Where(NotNull(from.C(s.Edge.Columns[0])))
case r == O2M || (r == O2O && !s.Edge.Inverse):
from := q.Table()
to := builder.Table(s.Edge.Table)
q.Where(
In(
from.C(s.From.Column),
builder.Select(to.C(s.Edge.Columns[0])).
From(to).
Where(NotNull(to.C(s.Edge.Columns[0]))),
),
)
}
}