mirror of
https://github.com/stashapp/stash.git
synced 2025-12-18 12:54:38 +03:00
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:
@@ -238,8 +238,8 @@ func (r *repository) buildQueryBody(body string, whereClauses []string, havingCl
|
|||||||
if len(whereClauses) > 0 {
|
if len(whereClauses) > 0 {
|
||||||
body = body + " WHERE " + strings.Join(whereClauses, " AND ") // TODO handle AND or OR
|
body = body + " WHERE " + strings.Join(whereClauses, " AND ") // TODO handle AND or OR
|
||||||
}
|
}
|
||||||
body = body + " GROUP BY " + r.tableName + ".id "
|
|
||||||
if len(havingClauses) > 0 {
|
if len(havingClauses) > 0 {
|
||||||
|
body = body + " GROUP BY " + r.tableName + ".id "
|
||||||
body = body + " HAVING " + strings.Join(havingClauses, " AND ") // TODO handle AND or OR
|
body = body + " HAVING " + strings.Join(havingClauses, " AND ") // TODO handle AND or OR
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -543,20 +543,15 @@ func (qb *tagQueryBuilder) getTagSort(query *queryBuilder, findFilter *models.Fi
|
|||||||
if findFilter.Sort != nil {
|
if findFilter.Sort != nil {
|
||||||
switch *findFilter.Sort {
|
switch *findFilter.Sort {
|
||||||
case "scenes_count":
|
case "scenes_count":
|
||||||
query.join("scenes_tags", "", "scenes_tags.tag_id = tags.id")
|
return getCountSort(tagTable, scenesTagsTable, tagIDColumn, direction)
|
||||||
return " ORDER BY COUNT(distinct scenes_tags.scene_id) " + direction
|
|
||||||
case "scene_markers_count":
|
case "scene_markers_count":
|
||||||
query.join("scene_markers_tags", "", "scene_markers_tags.tag_id = tags.id")
|
return getCountSort(tagTable, "scene_markers_tags", tagIDColumn, direction)
|
||||||
return " ORDER BY COUNT(distinct scene_markers_tags.scene_marker_id) " + direction
|
|
||||||
case "images_count":
|
case "images_count":
|
||||||
query.join("images_tags", "", "images_tags.tag_id = tags.id")
|
return getCountSort(tagTable, imagesTagsTable, tagIDColumn, direction)
|
||||||
return " ORDER BY COUNT(distinct images_tags.image_id) " + direction
|
|
||||||
case "galleries_count":
|
case "galleries_count":
|
||||||
query.join("galleries_tags", "", "galleries_tags.tag_id = tags.id")
|
return getCountSort(tagTable, galleriesTagsTable, tagIDColumn, direction)
|
||||||
return " ORDER BY COUNT(distinct galleries_tags.gallery_id) " + direction
|
|
||||||
case "performers_count":
|
case "performers_count":
|
||||||
query.join("performers_tags", "", "performers_tags.tag_id = tags.id")
|
return getCountSort(tagTable, performersTagsTable, tagIDColumn, direction)
|
||||||
return " ORDER BY COUNT(distinct performers_tags.performer_id) " + direction
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
func TestTagQueryName(t *testing.T) {
|
||||||
const tagIdx = 1
|
const tagIdx = 1
|
||||||
tagName := getSceneStringValue(tagIdx, "Name")
|
tagName := getSceneStringValue(tagIdx, "Name")
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
* Support filtering Movies by Performers. ([#1675](https://github.com/stashapp/stash/pull/1675))
|
* Support filtering Movies by Performers. ([#1675](https://github.com/stashapp/stash/pull/1675))
|
||||||
|
|
||||||
### 🎨 Improvements
|
### 🎨 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 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 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))
|
* Added date and details to Gallery card, and move image count to icon. ([#1763](https://github.com/stashapp/stash/pull/1763))
|
||||||
|
|||||||
Reference in New Issue
Block a user