From eb7956a05a937dea2eb11a85505d27cbf4513c3a Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Fri, 9 Dec 2022 08:29:34 +1100 Subject: [PATCH] Correct title ordering for objects without titles (#3244) * Correct scene title ordering * Correct ordering for other objects * Add basename function, add to gallery title sort --- pkg/sqlite/database.go | 1 + pkg/sqlite/functions.go | 5 +++++ pkg/sqlite/gallery.go | 2 +- pkg/sqlite/gallery_test.go | 7 +++++++ pkg/sqlite/image.go | 2 +- pkg/sqlite/scene.go | 2 +- ui/v2.5/src/docs/en/Changelog/v0190.md | 1 + 7 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/sqlite/database.go b/pkg/sqlite/database.go index be142abb7..9c1b46073 100644 --- a/pkg/sqlite/database.go +++ b/pkg/sqlite/database.go @@ -428,6 +428,7 @@ func registerCustomDriver() { funcs := map[string]interface{}{ "regexp": regexFn, "durationToTinyInt": durationToTinyIntFn, + "basename": basenameFn, } for name, fn := range funcs { diff --git a/pkg/sqlite/functions.go b/pkg/sqlite/functions.go index 29e93aa22..f639d0b5b 100644 --- a/pkg/sqlite/functions.go +++ b/pkg/sqlite/functions.go @@ -1,6 +1,7 @@ package sqlite import ( + "path/filepath" "strconv" "strings" ) @@ -30,3 +31,7 @@ func durationToTinyIntFn(str string) (int64, error) { return int64(seconds), nil } + +func basenameFn(str string) (string, error) { + return filepath.Base(str), nil +} diff --git a/pkg/sqlite/gallery.go b/pkg/sqlite/gallery.go index e45d7cb9f..d59f160aa 100644 --- a/pkg/sqlite/gallery.go +++ b/pkg/sqlite/gallery.go @@ -1109,7 +1109,7 @@ func (qb *GalleryStore) setGallerySort(query *queryBuilder, findFilter *models.F case "title": addFileTable() addFolderTable() - query.sortAndPagination += " ORDER BY galleries.title COLLATE NATURAL_CS " + direction + ", folders.path " + direction + ", file_folder.path " + direction + ", files.basename COLLATE NATURAL_CS " + direction + query.sortAndPagination += " ORDER BY COALESCE(galleries.title, files.basename, basename(COALESCE(folders.path, ''))) COLLATE NATURAL_CS " + direction + ", file_folder.path " + direction default: query.sortAndPagination += getSort(sort, direction, "galleries") } diff --git a/pkg/sqlite/gallery_test.go b/pkg/sqlite/gallery_test.go index 80e25b6d1..291e02e08 100644 --- a/pkg/sqlite/gallery_test.go +++ b/pkg/sqlite/gallery_test.go @@ -2414,6 +2414,13 @@ func TestGalleryQuerySorting(t *testing.T) { -1, -1, }, + { + "title", + "title", + models.SortDirectionEnumDesc, + -1, + -1, + }, } qb := db.Gallery diff --git a/pkg/sqlite/image.go b/pkg/sqlite/image.go index 9cc0e957a..7c3cd6c9d 100644 --- a/pkg/sqlite/image.go +++ b/pkg/sqlite/image.go @@ -1019,7 +1019,7 @@ func (qb *ImageStore) setImageSortAndPagination(q *queryBuilder, findFilter *mod case "title": addFilesJoin() addFolderJoin() - sortClause = " ORDER BY images.title COLLATE NATURAL_CS " + direction + ", folders.path " + direction + ", files.basename COLLATE NATURAL_CS " + direction + sortClause = " ORDER BY COALESCE(images.title, files.basename) COLLATE NATURAL_CS " + direction + ", folders.path " + direction default: sortClause = getSort(sort, direction, "images") } diff --git a/pkg/sqlite/scene.go b/pkg/sqlite/scene.go index 341e9ee26..cb753679c 100644 --- a/pkg/sqlite/scene.go +++ b/pkg/sqlite/scene.go @@ -1446,7 +1446,7 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF case "title": addFileTable() addFolderTable() - query.sortAndPagination += " ORDER BY scenes.title COLLATE NATURAL_CS " + direction + ", folders.path " + direction + ", files.basename COLLATE NATURAL_CS " + direction + query.sortAndPagination += " ORDER BY COALESCE(scenes.title, files.basename) COLLATE NATURAL_CS " + direction + ", folders.path " + direction case "play_count": // handle here since getSort has special handling for _count suffix query.sortAndPagination += " ORDER BY scenes.play_count " + direction diff --git a/ui/v2.5/src/docs/en/Changelog/v0190.md b/ui/v2.5/src/docs/en/Changelog/v0190.md index a5b2530ff..8489697b1 100644 --- a/ui/v2.5/src/docs/en/Changelog/v0190.md +++ b/ui/v2.5/src/docs/en/Changelog/v0190.md @@ -9,6 +9,7 @@ * Changed performer aliases to be a list, rather than a string field. ([#3113](https://github.com/stashapp/stash/pull/3113)) ### 🐛 Bug fixes +* Fixed objects without titles not being sorted correctly with objects with titles. ([#3244](https://github.com/stashapp/stash/pull/3244)) * Fixed incorrect new Performer pill being removed when creating Performer from scrape dialog. ([#3251](https://github.com/stashapp/stash/pull/3251)) * Fixed date fields not being nulled correctly when cleared. ([#3243](https://github.com/stashapp/stash/pull/3243)) * Fixed scene wall items to show file base name where scene has no title set. ([#3242](https://github.com/stashapp/stash/pull/3242))