Only group by if using having clauses (#1750)

* Only group by if using having clauses
* Change tag sorting SQL
* Add sorting unit tests
This commit is contained in:
WithoutPants
2021-10-01 16:24:58 +10:00
committed by GitHub
parent e3480531a7
commit ca0a8b00ec
4 changed files with 42 additions and 11 deletions

View File

@@ -238,8 +238,8 @@ func (r *repository) buildQueryBody(body string, whereClauses []string, havingCl
if len(whereClauses) > 0 {
body = body + " WHERE " + strings.Join(whereClauses, " AND ") // TODO handle AND or OR
}
body = body + " GROUP BY " + r.tableName + ".id "
if len(havingClauses) > 0 {
body = body + " GROUP BY " + r.tableName + ".id "
body = body + " HAVING " + strings.Join(havingClauses, " AND ") // TODO handle AND or OR
}

View File

@@ -543,20 +543,15 @@ func (qb *tagQueryBuilder) getTagSort(query *queryBuilder, findFilter *models.Fi
if findFilter.Sort != nil {
switch *findFilter.Sort {
case "scenes_count":
query.join("scenes_tags", "", "scenes_tags.tag_id = tags.id")
return " ORDER BY COUNT(distinct scenes_tags.scene_id) " + direction
return getCountSort(tagTable, scenesTagsTable, tagIDColumn, direction)
case "scene_markers_count":
query.join("scene_markers_tags", "", "scene_markers_tags.tag_id = tags.id")
return " ORDER BY COUNT(distinct scene_markers_tags.scene_marker_id) " + direction
return getCountSort(tagTable, "scene_markers_tags", tagIDColumn, direction)
case "images_count":
query.join("images_tags", "", "images_tags.tag_id = tags.id")
return " ORDER BY COUNT(distinct images_tags.image_id) " + direction
return getCountSort(tagTable, imagesTagsTable, tagIDColumn, direction)
case "galleries_count":
query.join("galleries_tags", "", "galleries_tags.tag_id = tags.id")
return " ORDER BY COUNT(distinct galleries_tags.gallery_id) " + direction
return getCountSort(tagTable, galleriesTagsTable, tagIDColumn, direction)
case "performers_count":
query.join("performers_tags", "", "performers_tags.tag_id = tags.id")
return " ORDER BY COUNT(distinct performers_tags.performer_id) " + direction
return getCountSort(tagTable, performersTagsTable, tagIDColumn, direction)
}
}

View File

@@ -151,6 +151,41 @@ func TestTagFindByNames(t *testing.T) {
})
}
func TestTagQuerySort(t *testing.T) {
withTxn(func(r models.Repository) error {
sqb := r.Tag()
sortBy := "scenes_count"
dir := models.SortDirectionEnumDesc
findFilter := &models.FindFilterType{
Sort: &sortBy,
Direction: &dir,
}
tags := queryTags(t, sqb, nil, findFilter)
assert := assert.New(t)
assert.Equal(tagIDs[tagIdxWithScene], tags[0].ID)
sortBy = "scene_markers_count"
tags = queryTags(t, sqb, nil, findFilter)
assert.Equal(tagIDs[tagIdxWithMarker], tags[0].ID)
sortBy = "images_count"
tags = queryTags(t, sqb, nil, findFilter)
assert.Equal(tagIDs[tagIdxWithImage], tags[0].ID)
sortBy = "galleries_count"
tags = queryTags(t, sqb, nil, findFilter)
assert.Equal(tagIDs[tagIdxWithGallery], tags[0].ID)
sortBy = "performers_count"
tags = queryTags(t, sqb, nil, findFilter)
assert.Equal(tagIDs[tagIdxWithPerformer], tags[0].ID)
return nil
})
}
func TestTagQueryName(t *testing.T) {
const tagIdx = 1
tagName := getSceneStringValue(tagIdx, "Name")

View File

@@ -14,6 +14,7 @@
* Support filtering Movies by Performers. ([#1675](https://github.com/stashapp/stash/pull/1675))
### 🎨 Improvements
* Improved image query performance. ([#1750](https://github.com/stashapp/stash/pull/1750))
* Added movie count to performer and studio cards. ([#1760](https://github.com/stashapp/stash/pull/1760))
* Added date and details to Movie card, and move scene count to icon. ([#1758](https://github.com/stashapp/stash/pull/1758))
* Added date and details to Gallery card, and move image count to icon. ([#1763](https://github.com/stashapp/stash/pull/1763))