MCPcopy Index your code
hub / github.com/ent/ent / OrderByNeighborsCount

Function OrderByNeighborsCount

dialect/sql/sqlgraph/graph.go:380–436  ·  view source on GitHub ↗

OrderByNeighborsCount appends ordering based on the number of neighbors. For example, order users by their number of posts.

(q *sql.Selector, s *Step, opts ...sql.OrderTermOption)

Source from the content-addressed store, hash-verified

378// OrderByNeighborsCount appends ordering based on the number of neighbors.
379// For example, order users by their number of posts.
380func OrderByNeighborsCount(q *sql.Selector, s *Step, opts ...sql.OrderTermOption) {
381 var (
382 join *sql.Selector
383 opt = sql.NewOrderTermOptions(opts...)
384 build = sql.Dialect(q.Dialect())
385 )
386 switch {
387 case s.FromEdgeOwner():
388 // For M2O and O2O inverse, the FK resides in the same table.
389 // Hence, the order by is on the nullability of the column.
390 x := func(b *sql.Builder) {
391 b.Ident(s.From.Column)
392 if opt.Desc {
393 b.WriteOp(sql.OpNotNull)
394 } else {
395 b.WriteOp(sql.OpIsNull)
396 }
397 }
398 q.OrderExpr(build.Expr(x))
399 case s.ThroughEdgeTable():
400 countAs := countAlias(q, s, opt)
401 terms := []sql.OrderTerm{
402 sql.OrderByCount("*", append([]sql.OrderTermOption{sql.OrderAs(countAs)}, opts...)...),
403 }
404 pk1 := s.Edge.Columns[0]
405 if s.Edge.Inverse {
406 pk1 = s.Edge.Columns[1]
407 }
408 joinT := build.Table(s.Edge.Table).Schema(s.Edge.Schema)
409 join = build.Select(
410 joinT.C(pk1),
411 ).From(joinT).GroupBy(joinT.C(pk1))
412 selectTerms(join, terms)
413 q.LeftJoin(join).
414 On(
415 q.C(s.From.Column),
416 join.C(pk1),
417 )
418 orderTerms(q, join, terms)
419 case s.ToEdgeOwner():
420 countAs := countAlias(q, s, opt)
421 terms := []sql.OrderTerm{
422 sql.OrderByCount("*", append([]sql.OrderTermOption{sql.OrderAs(countAs)}, opts...)...),
423 }
424 edgeT := build.Table(s.Edge.Table).Schema(s.Edge.Schema)
425 join = build.Select(
426 edgeT.C(s.Edge.Columns[0]),
427 ).From(edgeT).GroupBy(edgeT.C(s.Edge.Columns[0]))
428 selectTerms(join, terms)
429 q.LeftJoin(join).
430 On(
431 q.C(s.From.Column),
432 join.C(s.Edge.Columns[0]),
433 )
434 orderTerms(q, join, terms)
435 }
436}
437

Callers 15

OrderByEdgeCountFunction · 0.92
ByAdminsCountFunction · 0.92
ByCarCountFunction · 0.92
ByFriendsCountFunction · 0.92
ByChildrenCountFunction · 0.92
ByPetsCountFunction · 0.92
ByFilesCountFunction · 0.92
ByGroupsCountFunction · 0.92
ByFriendsCountFunction · 0.92
ByFollowersCountFunction · 0.92
ByFollowingCountFunction · 0.92
ByChildrenCountFunction · 0.92

Calls 15

NewOrderTermOptionsFunction · 0.92
DialectFunction · 0.92
OrderByCountFunction · 0.92
OrderAsFunction · 0.92
countAliasFunction · 0.85
selectTermsFunction · 0.85
orderTermsFunction · 0.85
FromEdgeOwnerMethod · 0.80
IdentMethod · 0.80
WriteOpMethod · 0.80
ExprMethod · 0.80
ThroughEdgeTableMethod · 0.80

Tested by 2

OrderByEdgeCountFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…