mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: add Aggregate to <T>Select and <T>Query
This commit is contained in:
committed by
Ariel Mashraki
parent
1bc4d48a51
commit
765ec09d31
@@ -36,8 +36,7 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery() *sql.Selector {
|
||||
for _, fn := range {{ $receiver }}.fns {
|
||||
aggregation = append(aggregation, fn(selector))
|
||||
}
|
||||
// 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 no columns were selected, the default selection is the fields used for "group-by", and the aggregation functions.*/}}
|
||||
if len(selector.SelectedColumns()) == 0 {
|
||||
columns := make([]string, 0, len({{ $receiver }}.fields) + len({{ $receiver}}.fns))
|
||||
for _, f := range {{ $receiver }}.fields {
|
||||
|
||||
@@ -11,6 +11,17 @@ in the LICENSE file in the root directory of this source tree.
|
||||
{{ $receiver := receiver $builder }}
|
||||
|
||||
func ({{ $receiver }} *{{ $builder }}) sqlScan(ctx context.Context, v any) error {
|
||||
aggregation := make([]string, 0, len({{ $receiver}}.fns))
|
||||
for _, fn := range {{ $receiver }}.fns {
|
||||
aggregation = append(aggregation, fn({{ $receiver }}.sql))
|
||||
}
|
||||
switch n := len(*{{ $receiver }}.selector.flds); {
|
||||
{{- /* If no columns were selected, the default selection is the aggregation.*/}}
|
||||
case n == 0 && len(aggregation) > 0:
|
||||
{{ $receiver }}.sql.Select(aggregation...)
|
||||
case n != 0 && len(aggregation) > 0:
|
||||
{{ $receiver }}.sql.AppendSelect(aggregation...)
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := {{ $receiver }}.sql.Query()
|
||||
if err := {{ $receiver }}.driver.Query(ctx, query, args, rows); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user