mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: allow group-by by relations
This commit is contained in:
committed by
Ariel Mashraki
parent
26b8f3b6b2
commit
a51c50f6a3
@@ -753,12 +753,23 @@ func (cgb *CardGroupBy) sqlScan(ctx context.Context, v interface{}) error {
|
||||
|
||||
func (cgb *CardGroupBy) sqlQuery() *sql.Selector {
|
||||
selector := cgb.sql
|
||||
columns := make([]string, 0, len(cgb.fields)+len(cgb.fns))
|
||||
columns = append(columns, cgb.fields...)
|
||||
aggregation := make([]string, 0, len(cgb.fns))
|
||||
for _, fn := range cgb.fns {
|
||||
columns = append(columns, fn(selector))
|
||||
aggregation = append(aggregation, fn(selector))
|
||||
}
|
||||
return selector.Select(columns...).GroupBy(cgb.fields...)
|
||||
// If no columns were selected in a custom aggregation function, the default
|
||||
// selection is the fields used for "group-by", and the aggregation functions.
|
||||
if len(selector.Columns()) == 0 {
|
||||
columns := make([]string, 0, len(cgb.fields)+len(cgb.fns))
|
||||
for _, f := range cgb.fields {
|
||||
columns = append(columns, selector.C(f))
|
||||
}
|
||||
for _, c := range aggregation {
|
||||
columns = append(columns, c)
|
||||
}
|
||||
selector.Select(columns...)
|
||||
}
|
||||
return selector.GroupBy(selector.Columns(cgb.fields...)...)
|
||||
}
|
||||
|
||||
// CardSelect is the builder for selecting fields of Card entities.
|
||||
|
||||
@@ -745,12 +745,23 @@ func (ugb *UserGroupBy) sqlScan(ctx context.Context, v interface{}) error {
|
||||
|
||||
func (ugb *UserGroupBy) sqlQuery() *sql.Selector {
|
||||
selector := ugb.sql
|
||||
columns := make([]string, 0, len(ugb.fields)+len(ugb.fns))
|
||||
columns = append(columns, ugb.fields...)
|
||||
aggregation := make([]string, 0, len(ugb.fns))
|
||||
for _, fn := range ugb.fns {
|
||||
columns = append(columns, fn(selector))
|
||||
aggregation = append(aggregation, fn(selector))
|
||||
}
|
||||
return selector.Select(columns...).GroupBy(ugb.fields...)
|
||||
// If no columns were selected in a custom aggregation function, the default
|
||||
// selection is the fields used for "group-by", and the aggregation functions.
|
||||
if len(selector.Columns()) == 0 {
|
||||
columns := make([]string, 0, len(ugb.fields)+len(ugb.fns))
|
||||
for _, f := range ugb.fields {
|
||||
columns = append(columns, selector.C(f))
|
||||
}
|
||||
for _, c := range aggregation {
|
||||
columns = append(columns, c)
|
||||
}
|
||||
selector.Select(columns...)
|
||||
}
|
||||
return selector.GroupBy(selector.Columns(ugb.fields...)...)
|
||||
}
|
||||
|
||||
// UserSelect is the builder for selecting fields of User entities.
|
||||
|
||||
Reference in New Issue
Block a user