Skip to content

Commit

Permalink
fix(bun): Replace GroupBy clause with select distinct (#645)
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de>
  • Loading branch information
fjogeleit authored Dec 20, 2024
1 parent b262a67 commit 4411c29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
12 changes: 12 additions & 0 deletions pkg/database/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func (q *QueryBuilder) Columns(columns ...string) *QueryBuilder {
return q
}

func (q *QueryBuilder) ColumnExpr(query string, args ...any) *QueryBuilder {
q.query.ColumnExpr(query, args...)

return q
}

func (q *QueryBuilder) Distinct() *QueryBuilder {
q.query.Distinct()

return q
}

func (q *QueryBuilder) Group(columns ...string) *QueryBuilder {
q.query.Group(columns...)

Expand Down
18 changes: 8 additions & 10 deletions pkg/database/bun.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,9 @@ func (s *Store) FetchNamespaceResourceResults(ctx context.Context, filter Filter
}

func (s *Store) CountNamespaceResourceResults(ctx context.Context, filter Filter) (int, error) {
query := FromQuery(s.db.NewSelect().Model((*ResourceResult)(nil))).
return FromQuery(s.db.NewSelect().Model((*ResourceResult)(nil))).
Columns("res.id").
Distinct().
FilterMap(map[string][]string{
"source": filter.Sources,
"category": filter.Categories,
Expand All @@ -399,10 +400,8 @@ func (s *Store) CountNamespaceResourceResults(ctx context.Context, filter Filter
FilterReportLabels(filter.ReportLabel).
Exclude(filter, "res").
NamespaceScope().
Group("res.id").
GetQuery()

return query.Count(ctx)
GetQuery().
Count(ctx)
}

func (s *Store) FetchClusterResourceResults(ctx context.Context, filter Filter, pagination Pagination) ([]ResourceResult, error) {
Expand Down Expand Up @@ -430,8 +429,9 @@ func (s *Store) FetchClusterResourceResults(ctx context.Context, filter Filter,
}

func (s *Store) CountClusterResourceResults(ctx context.Context, filter Filter) (int, error) {
query := FromQuery(s.db.NewSelect().Model((*ResourceResult)(nil))).
return FromQuery(s.db.NewSelect().Model((*ResourceResult)(nil))).
Columns("res.id").
Distinct().
FilterMap(map[string][]string{
"source": filter.Sources,
"category": filter.Categories,
Expand All @@ -442,10 +442,8 @@ func (s *Store) CountClusterResourceResults(ctx context.Context, filter Filter)
FilterReportLabels(filter.ReportLabel).
Exclude(filter, "res").
ClusterScope().
Group("res.id").
GetQuery()

return query.Count(ctx)
GetQuery().
Count(ctx)
}

func (s *Store) FetchResourceResults(ctx context.Context, id string, filter Filter) ([]ResourceResult, error) {
Expand Down

0 comments on commit 4411c29

Please sign in to comment.