From 1ab44eb467de58f2a7213b1b0459e8b9a82331f0 Mon Sep 17 00:00:00 2001 From: Nodude <75137537+NodudeWasTaken@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:05:20 +0200 Subject: [PATCH] pgsql missing some groupby's (some like title are unnecessary, but it works for now) --- pkg/sqlite/file.go | 4 ++-- pkg/sqlite/gallery.go | 8 ++++---- pkg/sqlite/group.go | 8 ++++---- pkg/sqlite/image.go | 12 +++++++----- pkg/sqlite/performer.go | 2 +- pkg/sqlite/query.go | 2 +- pkg/sqlite/scene.go | 28 +++++++++++++++------------- pkg/sqlite/scene_marker.go | 4 ++-- pkg/sqlite/sql.go | 2 +- pkg/sqlite/studio.go | 2 +- pkg/sqlite/tag.go | 2 +- 11 files changed, 39 insertions(+), 35 deletions(-) diff --git a/pkg/sqlite/file.go b/pkg/sqlite/file.go index 3711bf7da61..90c0ac04f95 100644 --- a/pkg/sqlite/file.go +++ b/pkg/sqlite/file.go @@ -941,11 +941,11 @@ func (qb *FileStore) setQuerySort(query *queryBuilder, findFilter *models.FindFi case "path": // special handling for path query.sortAndPagination += fmt.Sprintf(" ORDER BY folders.path %s, files.basename %[1]s", direction) - query.addGroupBy([]string{"folders.path", "files.basename"}) + query.addGroupBy("folders.path", "files.basename") default: add, agg := getSort(sort, direction, "files") query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) } return nil diff --git a/pkg/sqlite/gallery.go b/pkg/sqlite/gallery.go index ee08fdd17ab..1e030bcdb8a 100644 --- a/pkg/sqlite/gallery.go +++ b/pkg/sqlite/gallery.go @@ -838,13 +838,13 @@ func (qb *GalleryStore) setGallerySort(query *queryBuilder, findFilter *models.F addFileTable() addFolderTable() query.sortAndPagination += fmt.Sprintf(" ORDER BY COALESCE(folders.path, '') || COALESCE(file_folder.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI %s", direction) - query.addGroupBy([]string{"folders.path", "file_folder.path", "files.basename"}) + query.addGroupBy("folders.path", "file_folder.path", "files.basename") case "file_mod_time": sort = "mod_time" addFileTable() add, agg := getSort(sort, direction, fileTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) case "title": addFileTable() addFolderTable() @@ -852,12 +852,12 @@ func (qb *GalleryStore) setGallerySort(query *queryBuilder, findFilter *models.F default: add, agg := getSort(sort, direction, "galleries") query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) } // Whatever the sorting, always use title/id as a final sort query.sortAndPagination += ", COALESCE(galleries.title, CAST(galleries.id as text)) COLLATE NATURAL_CI ASC" - query.addGroupBy([]string{"galleries.title", "galleries.id"}) + query.addGroupBy("galleries.title", "galleries.id") return nil } diff --git a/pkg/sqlite/group.go b/pkg/sqlite/group.go index 86212356ecd..4d4e54a001c 100644 --- a/pkg/sqlite/group.go +++ b/pkg/sqlite/group.go @@ -515,14 +515,14 @@ func (qb *GroupStore) setGroupSort(query *queryBuilder, findFilter *models.FindF if query.hasJoin("groups_parents") { add, agg := getSort("order_index", direction, "groups_parents") query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) } else { // this will give unexpected results if the query is not filtered by a parent group and // the group has multiple parents and order indexes query.join(groupRelationsTable, "", "groups.id = groups_relations.sub_id") add, agg := getSort("order_index", direction, groupRelationsTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) } case "tag_count": query.sortAndPagination += getCountSort(groupTable, groupsTagsTable, groupIDColumn, direction) @@ -531,12 +531,12 @@ func (qb *GroupStore) setGroupSort(query *queryBuilder, findFilter *models.FindF default: add, agg := getSort(sort, direction, "groups") query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) } // Whatever the sorting, always use name/id as a final sort query.sortAndPagination += ", COALESCE(groups.name, CAST(groups.id as text)) COLLATE NATURAL_CI ASC" - query.addGroupBy([]string{"groups.name", "groups.id"}) + query.addGroupBy("groups.name", "groups.id") return nil } diff --git a/pkg/sqlite/image.go b/pkg/sqlite/image.go index dd946a536b9..71a8e05843e 100644 --- a/pkg/sqlite/image.go +++ b/pkg/sqlite/image.go @@ -865,6 +865,7 @@ func (qb *ImageStore) queryGroupedFields(ctx context.Context, options models.Ima ) query.addColumn("COALESCE(image_files.width, 0) * COALESCE(image_files.height, 0) as megapixels") aggregateQuery.addColumn("COALESCE(SUM(temp.megapixels), 0) / 1000000 as megapixels") + query.addGroupBy("image_files.width", "image_files.height") } if options.TotalSize { @@ -880,6 +881,7 @@ func (qb *ImageStore) queryGroupedFields(ctx context.Context, options models.Ima ) query.addColumn("COALESCE(files.size, 0) as size") aggregateQuery.addColumn("SUM(temp.size) as size") + query.addGroupBy("files.size") } const includeSortPagination = false @@ -969,7 +971,7 @@ func (qb *ImageStore) setImageSortAndPagination(q *queryBuilder, findFilter *mod addFilesJoin() addFolderJoin() sortClause = " ORDER BY COALESCE(folders.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI " + direction - q.addGroupBy([]string{"folders.path", "files.basename"}) + q.addGroupBy("folders.path", "files.basename") case "file_count": sortClause = getCountSort(imageTable, imagesFilesTable, imageIDColumn, direction) case "tag_count": @@ -980,21 +982,21 @@ func (qb *ImageStore) setImageSortAndPagination(q *queryBuilder, findFilter *mod addFilesJoin() add, agg := getSort(sort, direction, "files") sortClause = add - q.addGroupBy(agg) + q.addGroupBy(agg...) case "title": addFilesJoin() addFolderJoin() sortClause = " ORDER BY COALESCE(images.title, files.basename) COLLATE NATURAL_CI " + direction + ", folders.path COLLATE NATURAL_CI " + direction - q.addGroupBy([]string{"images.title", "files.basename", "folders.path"}) + q.addGroupBy("images.title", "files.basename", "folders.path") default: add, agg := getSort(sort, direction, "images") sortClause = add - q.addGroupBy(agg) + q.addGroupBy(agg...) } // Whatever the sorting, always use title/id as a final sort sortClause += ", COALESCE(images.title, CAST(images.id as text)) COLLATE NATURAL_CI ASC" - q.addGroupBy([]string{"images.title", "images.id"}) + q.addGroupBy("images.title", "images.id") } q.sortAndPagination = sortClause + getPagination(findFilter) diff --git a/pkg/sqlite/performer.go b/pkg/sqlite/performer.go index 1951b79859b..6fb604aedf1 100644 --- a/pkg/sqlite/performer.go +++ b/pkg/sqlite/performer.go @@ -619,7 +619,7 @@ func (qb *PerformerStore) makeQuery(ctx context.Context, performerFilter *models return nil, err } query.sortAndPagination += getPagination(findFilter) - query.addGroupBy(agg) + query.addGroupBy(agg...) return &query, nil } diff --git a/pkg/sqlite/query.go b/pkg/sqlite/query.go index 77ab3bc44e5..eba5b67f4c7 100644 --- a/pkg/sqlite/query.go +++ b/pkg/sqlite/query.go @@ -34,7 +34,7 @@ func (qb *queryBuilder) addColumn(column string) { qb.columns = append(qb.columns, column) } -func (qb *queryBuilder) addGroupBy(columns []string) { +func (qb *queryBuilder) addGroupBy(columns ...string) { if len(columns) > 0 { qb.groupByClauses = sliceutil.AppendUniques(qb.groupByClauses, columns) } diff --git a/pkg/sqlite/scene.go b/pkg/sqlite/scene.go index 55d85431fc6..f927b301fa0 100644 --- a/pkg/sqlite/scene.go +++ b/pkg/sqlite/scene.go @@ -1007,6 +1007,7 @@ func (qb *SceneStore) queryGroupedFields(ctx context.Context, options models.Sce ) query.addColumn("COALESCE(video_files.duration, 0) as duration") aggregateQuery.addColumn("SUM(temp.duration) as duration") + query.addGroupBy("video_files.duration") } if options.TotalSize { @@ -1022,6 +1023,7 @@ func (qb *SceneStore) queryGroupedFields(ctx context.Context, options models.Sce ) query.addColumn("COALESCE(files.size, 0) as size") aggregateQuery.addColumn("SUM(temp.size) as size") + query.addGroupBy("files.size") } const includeSortPagination = false @@ -1132,12 +1134,12 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF query.join(groupsScenesTable, "", "scenes.id = groups_scenes.scene_id") add, group := getSort("scene_index", direction, groupsScenesTable) query.sortAndPagination += add - query.addGroupBy(group) + query.addGroupBy(group...) case "group_scene_number": query.join(groupsScenesTable, "scene_group", "scenes.id = scene_group.scene_id") add, group := getSort("scene_index", direction, "scene_group") query.sortAndPagination += add - query.addGroupBy(group) + query.addGroupBy(group...) case "tag_count": query.sortAndPagination += getCountSort(sceneTable, scenesTagsTable, sceneIDColumn, direction) case "performer_count": @@ -1149,7 +1151,7 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF addFileTable() addFolderTable() query.sortAndPagination += fmt.Sprintf(" ORDER BY COALESCE(folders.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI %s", direction) - query.addGroupBy([]string{"folders.path", "files.basename"}) + query.addGroupBy("folders.path", "files.basename") case "perceptual_similarity": // special handling for phash addFileTable() @@ -1162,45 +1164,45 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF ) query.sortAndPagination += " ORDER BY fingerprints_phash.fingerprint " + direction + ", files.size DESC" - query.addGroupBy([]string{"fingerprints_phash.fingerprint", "files.size"}) + query.addGroupBy("fingerprints_phash.fingerprint", "files.size") case "bitrate": sort = "bit_rate" addVideoFileTable() add, group := getSort(sort, direction, videoFileTable) query.sortAndPagination += add - query.addGroupBy(group) + query.addGroupBy(group...) case "file_mod_time": sort = "mod_time" addFileTable() add, agg := getSort(sort, direction, fileTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) case "framerate": sort = "frame_rate" addVideoFileTable() add, agg := getSort(sort, direction, videoFileTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) case "filesize": addFileTable() add, agg := getSort(sort, direction, fileTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) case "duration": addVideoFileTable() add, agg := getSort(sort, direction, videoFileTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) case "interactive", "interactive_speed": addVideoFileTable() add, agg := getSort(sort, direction, videoFileTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) case "title": addFileTable() addFolderTable() query.sortAndPagination += " ORDER BY COALESCE(scenes.title, files.basename) COLLATE NATURAL_CI " + direction + ", folders.path COLLATE NATURAL_CI " + direction - query.addGroupBy([]string{"scenes.title", "files.basename", "folders.path"}) + query.addGroupBy("scenes.title", "files.basename", "folders.path") case "play_count": query.sortAndPagination += getCountSort(sceneTable, scenesViewDatesTable, sceneIDColumn, direction) case "last_played_at": @@ -1212,12 +1214,12 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF default: add, agg := getSort(sort, direction, "scenes") query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) } // Whatever the sorting, always use title/id as a final sort query.sortAndPagination += ", COALESCE(scenes.title, CAST(scenes.id as text)) COLLATE NATURAL_CI ASC" - query.addGroupBy([]string{"scenes.title", "scenes.id"}) + query.addGroupBy("scenes.title", "scenes.id") return nil } diff --git a/pkg/sqlite/scene_marker.go b/pkg/sqlite/scene_marker.go index 3a7f5badd59..1070d02c05f 100644 --- a/pkg/sqlite/scene_marker.go +++ b/pkg/sqlite/scene_marker.go @@ -377,14 +377,14 @@ func (qb *SceneMarkerStore) setSceneMarkerSort(query *queryBuilder, findFilter * query.join(sceneTable, "", "scenes.id = scene_markers.scene_id") add, agg := getSort(sort, direction, sceneTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) case "title": query.join(tagTable, "", "scene_markers.primary_tag_id = tags.id") query.sortAndPagination += " ORDER BY COALESCE(NULLIF(scene_markers.title,''), tags.name) COLLATE NATURAL_CI " + direction default: add, agg := getSort(sort, direction, sceneMarkerTable) query.sortAndPagination += add - query.addGroupBy(agg) + query.addGroupBy(agg...) } query.sortAndPagination += ", scene_markers.scene_id ASC, scene_markers.seconds ASC" diff --git a/pkg/sqlite/sql.go b/pkg/sqlite/sql.go index 87eb4c1e9f8..88df4846bc7 100644 --- a/pkg/sqlite/sql.go +++ b/pkg/sqlite/sql.go @@ -19,7 +19,7 @@ func selectAll(tableName string) string { func distinctIDs(qb *queryBuilder, tableName string) { columnId := getColumn(tableName, "id") qb.addColumn(columnId) - qb.addGroupBy([]string{columnId}) + qb.addGroupBy(columnId) qb.from = tableName } diff --git a/pkg/sqlite/studio.go b/pkg/sqlite/studio.go index 0b2a09d4015..b5ba2624a00 100644 --- a/pkg/sqlite/studio.go +++ b/pkg/sqlite/studio.go @@ -545,7 +545,7 @@ func (qb *StudioStore) makeQuery(ctx context.Context, studioFilter *models.Studi return nil, err } query.sortAndPagination += getPagination(findFilter) - query.addGroupBy(group) + query.addGroupBy(group...) return &query, nil } diff --git a/pkg/sqlite/tag.go b/pkg/sqlite/tag.go index eb93b0503ba..4d58538ceea 100644 --- a/pkg/sqlite/tag.go +++ b/pkg/sqlite/tag.go @@ -631,7 +631,7 @@ func (qb *TagStore) Query(ctx context.Context, tagFilter *models.TagFilterType, return nil, 0, err } query.sortAndPagination += getPagination(findFilter) - query.addGroupBy(group) + query.addGroupBy(group...) idsResult, countResult, err := query.executeFind(ctx) if err != nil { return nil, 0, err