entc/gen: add Aggregate to <T>Select and <T>Query

This commit is contained in:
Ariel Mashraki
2022-10-22 22:39:13 +03:00
committed by Ariel Mashraki
parent 1bc4d48a51
commit 765ec09d31
165 changed files with 2629 additions and 226 deletions

View File

@@ -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 {

View File

@@ -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 {