From cfc3912dcdcd46d42463b3d8e7d3e414afef5b85 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Fri, 23 Jun 2023 11:04:12 +1000 Subject: [PATCH] Handle missing studio ids in getHierarchicalValues (#3845) --- pkg/sqlite/filter.go | 13 +++++++++++-- pkg/sqlite/scene_test.go | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pkg/sqlite/filter.go b/pkg/sqlite/filter.go index 5934b2c99..3bb05ba0c 100644 --- a/pkg/sqlite/filter.go +++ b/pkg/sqlite/filter.go @@ -2,6 +2,7 @@ package sqlite import ( "context" + "database/sql" "errors" "fmt" "path/filepath" @@ -950,13 +951,21 @@ WHERE id in {inBinding} query := fmt.Sprintf("WITH RECURSIVE %s SELECT 'VALUES' || GROUP_CONCAT('(' || root_id || ', ' || item_id || ')') AS val FROM items", withClause) - var valuesClause string + var valuesClause sql.NullString err := tx.Get(ctx, &valuesClause, query, args...) if err != nil { return "", fmt.Errorf("failed to get hierarchical values: %w", err) } - return valuesClause, nil + // if no values are found, just return a values string with the values only + if !valuesClause.Valid { + for i, value := range values { + values[i] = fmt.Sprintf("(%s, %s)", value, value) + } + valuesClause.String = "VALUES" + strings.Join(values, ",") + } + + return valuesClause.String, nil } func addHierarchicalConditionClauses(f *filterBuilder, criterion models.HierarchicalMultiCriterionInput, table, idColumn string) { diff --git a/pkg/sqlite/scene_test.go b/pkg/sqlite/scene_test.go index db0bbfbc8..50691437d 100644 --- a/pkg/sqlite/scene_test.go +++ b/pkg/sqlite/scene_test.go @@ -2116,6 +2116,8 @@ func TestSceneQuery(t *testing.T) { var ( endpoint = sceneStashID(sceneIdxWithGallery).Endpoint stashID = sceneStashID(sceneIdxWithGallery).StashID + + depth = -1 ) tests := []struct { @@ -2219,6 +2221,20 @@ func TestSceneQuery(t *testing.T) { nil, false, }, + { + "with studio id 0 including child studios", + nil, + &models.SceneFilterType{ + Studios: &models.HierarchicalMultiCriterionInput{ + Value: []string{"0"}, + Modifier: models.CriterionModifierIncludes, + Depth: &depth, + }, + }, + nil, + nil, + false, + }, } for _, tt := range tests {