Revert "Replace `COUNT(DISTINCT primary_key)` with `COUNT(*)`"

Genres are required as part of the count queries, so filter by genres work
This commit is contained in:
Deluan 2023-11-25 23:08:20 -05:00
parent 8c8e1ea701
commit 28dc98dec4
4 changed files with 5 additions and 1 deletions

View File

@ -76,6 +76,7 @@ func artistFilter(field string, value interface{}) Sqlizer {
func (r *albumRepository) CountAll(options ...model.QueryOptions) (int64, error) {
sql := r.newSelectWithAnnotation("album.id")
sql = r.withGenres(sql) // Required for filtering by genre
return r.count(sql, options...)
}

View File

@ -52,6 +52,7 @@ func (r *artistRepository) selectArtist(options ...model.QueryOptions) SelectBui
func (r *artistRepository) CountAll(options ...model.QueryOptions) (int64, error) {
sql := r.newSelectWithAnnotation("artist.id")
sql = r.withGenres(sql) // Required for filtering by genre
return r.count(sql, options...)
}

View File

@ -40,6 +40,7 @@ func NewMediaFileRepository(ctx context.Context, o orm.QueryExecutor) *mediaFile
func (r *mediaFileRepository) CountAll(options ...model.QueryOptions) (int64, error) {
sql := r.newSelectWithAnnotation("media_file.id")
sql = r.withGenres(sql) // Required for filtering by genre
return r.count(sql, options...)
}

View File

@ -181,7 +181,8 @@ func (r sqlRepository) exists(existsQuery SelectBuilder) (bool, error) {
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
countQuery = countQuery.
RemoveColumns().Columns("count(*) as count").From(r.tableName)
RemoveColumns().Columns("count(distinct " + r.tableName + ".id) as count").
From(r.tableName)
countQuery = r.applyFilters(countQuery, options...)
var res struct{ Count int64 }
err := r.queryOne(countQuery, &res)