MCPcopy
hub / github.com/ent/ent / HasNeighborsWith

Function HasNeighborsWith

dialect/sql/sqlgraph/graph.go:281–353  ·  view source on GitHub ↗

HasNeighborsWith applies on the given Selector a neighbors check. The given predicate applies its filtering on the selector.

(q *sql.Selector, s *Step, pred func(*sql.Selector))

Source from the content-addressed store, hash-verified

279// HasNeighborsWith applies on the given Selector a neighbors check.
280// The given predicate applies its filtering on the selector.
281func HasNeighborsWith(q *sql.Selector, s *Step, pred func(*sql.Selector)) {
282 builder := sql.Dialect(q.Dialect())
283 switch {
284 case s.ThroughEdgeTable():
285 pk1, pk2 := s.Edge.Columns[1], s.Edge.Columns[0]
286 if s.Edge.Inverse {
287 pk1, pk2 = pk2, pk1
288 }
289 to := builder.Table(s.To.Table).Schema(s.To.Schema)
290 edge := builder.Table(s.Edge.Table).Schema(s.Edge.Schema)
291 join := builder.Select(edge.C(pk2)).
292 From(edge).
293 Join(to).
294 On(edge.C(pk1), to.C(s.To.Column))
295 matches := builder.Select().From(to)
296 matches.WithContext(q.Context())
297 pred(matches)
298 join.FromSelect(matches)
299 q.Where(sql.In(q.C(s.From.Column), join))
300 case s.FromEdgeOwner():
301 to := builder.Table(s.To.Table).Schema(s.To.Schema)
302 // Avoid ambiguity in case both source
303 // and edge tables are the same.
304 if s.To.Table == q.TableName() {
305 to.As(fmt.Sprintf("%s_edge", s.To.Table))
306 // Choose the alias name until we do not
307 // have a collision. Limit to 5 iterations.
308 for i := 1; i <= 5; i++ {
309 if to.C("c") != q.C("c") {
310 break
311 }
312 to.As(fmt.Sprintf("%s_edge_%d", s.To.Table, i))
313 }
314 }
315 matches := builder.Select(to.C(s.To.Column)).
316 From(to)
317 matches.WithContext(q.Context())
318 matches.Where(
319 sql.ColumnsEQ(
320 q.C(s.Edge.Columns[0]),
321 to.C(s.To.Column),
322 ),
323 )
324 pred(matches)
325 q.Where(sql.Exists(matches))
326 case s.ToEdgeOwner():
327 to := builder.Table(s.Edge.Table).Schema(s.Edge.Schema)
328 // Avoid ambiguity in case both source
329 // and edge tables are the same.
330 if s.Edge.Table == q.TableName() {
331 to.As(fmt.Sprintf("%s_edge", s.Edge.Table))
332 // Choose the alias name until we do not
333 // have a collision. Limit to 5 iterations.
334 for i := 1; i <= 5; i++ {
335 if to.C("c") != q.C("c") {
336 break
337 }
338 to.As(fmt.Sprintf("%s_edge_%d", s.Edge.Table, i))

Callers 15

HasAdminsWithFunction · 0.92
HasCarWithFunction · 0.92
HasPetsWithFunction · 0.92
HasFriendsWithFunction · 0.92
HasOwnerWithFunction · 0.92
HasOwnerWithFunction · 0.92
HasParentWithFunction · 0.92
HasChildrenWithFunction · 0.92
HasSpouseWithFunction · 0.92
HasCarWithFunction · 0.92
HasOwnerWithFunction · 0.92
HasCardWithFunction · 0.92

Calls 15

DialectFunction · 0.92
InFunction · 0.92
ColumnsEQFunction · 0.92
ExistsFunction · 0.92
ThroughEdgeTableMethod · 0.80
OnMethod · 0.80
WithContextMethod · 0.80
ContextMethod · 0.80
FromEdgeOwnerMethod · 0.80
TableNameMethod · 0.80
ToEdgeOwnerMethod · 0.80
DialectMethod · 0.65

Tested by 2

TestHasNeighborsWithFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…