countAlias returns the alias to use for the count column.
(q *sql.Selector, s *Step, opt *sql.OrderTermOptions)
| 354 | |
| 355 | // countAlias returns the alias to use for the count column. |
| 356 | func countAlias(q *sql.Selector, s *Step, opt *sql.OrderTermOptions) string { |
| 357 | if opt.As != "" { |
| 358 | return opt.As |
| 359 | } |
| 360 | selected := make(map[string]struct{}) |
| 361 | for _, c := range q.SelectedColumns() { |
| 362 | selected[c] = struct{}{} |
| 363 | } |
| 364 | column := fmt.Sprintf("count_%s", s.To.Table) |
| 365 | // If the column was already selected, |
| 366 | // try to find a free alias. |
| 367 | if _, ok := selected[column]; ok { |
| 368 | for i := 1; i <= 5; i++ { |
| 369 | ci := fmt.Sprintf("%s_%d", column, i) |
| 370 | if _, ok := selected[ci]; !ok { |
| 371 | return ci |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | return column |
| 376 | } |
| 377 | |
| 378 | // OrderByNeighborsCount appends ordering based on the number of neighbors. |
| 379 | // For example, order users by their number of posts. |
no test coverage detected
searching dependent graphs…