Fix marker tag filtering (#3846)

This commit is contained in:
WithoutPants
2023-06-23 11:04:54 +10:00
committed by GitHub
parent cfc3912dcd
commit b4879ef758
3 changed files with 174 additions and 67 deletions

View File

@@ -317,9 +317,11 @@ func sceneMarkerTagIDCriterionHandler(qb *SceneMarkerStore, tagID *string) crite
}
}
func sceneMarkerTagsCriterionHandler(qb *SceneMarkerStore, tags *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
func sceneMarkerTagsCriterionHandler(qb *SceneMarkerStore, criterion *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
return func(ctx context.Context, f *filterBuilder) {
if tags != nil {
if criterion != nil {
tags := criterion.CombineExcludes()
if tags.Modifier == models.CriterionModifierIsNull || tags.Modifier == models.CriterionModifierNotNull {
var notClause string
if tags.Modifier == models.CriterionModifierNotNull {
@@ -332,26 +334,59 @@ func sceneMarkerTagsCriterionHandler(qb *SceneMarkerStore, tags *models.Hierarch
return
}
if len(tags.Value) == 0 {
return
}
valuesClause, err := getHierarchicalValues(ctx, qb.tx, tags.Value, tagTable, "tags_relations", "parent_id", "child_id", tags.Depth)
if err != nil {
f.setError(err)
if tags.Modifier == models.CriterionModifierEquals && tags.Depth != nil && *tags.Depth != 0 {
f.setError(fmt.Errorf("depth is not supported for equals modifier for marker tag filtering"))
return
}
f.addWith(`marker_tags AS (
SELECT mt.scene_marker_id, t.column1 AS root_tag_id FROM scene_markers_tags mt
INNER JOIN (` + valuesClause + `) t ON t.column2 = mt.tag_id
UNION
SELECT m.id, t.column1 FROM scene_markers m
INNER JOIN (` + valuesClause + `) t ON t.column2 = m.primary_tag_id
)`)
if len(tags.Value) == 0 && len(tags.Excludes) == 0 {
return
}
f.addLeftJoin("marker_tags", "", "marker_tags.scene_marker_id = scene_markers.id")
if len(tags.Value) > 0 {
valuesClause, err := getHierarchicalValues(ctx, qb.tx, tags.Value, tagTable, "tags_relations", "parent_id", "child_id", tags.Depth)
if err != nil {
f.setError(err)
return
}
addHierarchicalConditionClauses(f, *tags, "marker_tags", "root_tag_id")
f.addWith(`marker_tags AS (
SELECT mt.scene_marker_id, t.column1 AS root_tag_id FROM scene_markers_tags mt
INNER JOIN (` + valuesClause + `) t ON t.column2 = mt.tag_id
UNION
SELECT m.id, t.column1 FROM scene_markers m
INNER JOIN (` + valuesClause + `) t ON t.column2 = m.primary_tag_id
)`)
f.addLeftJoin("marker_tags", "", "marker_tags.scene_marker_id = scene_markers.id")
switch tags.Modifier {
case models.CriterionModifierEquals:
// includes only the provided ids
f.addWhere("marker_tags.root_tag_id IS NOT NULL")
tagsLen := len(tags.Value)
f.addHaving(fmt.Sprintf("count(distinct marker_tags.root_tag_id) IS %d", tagsLen))
// decrement by one to account for primary tag id
f.addWhere("(SELECT COUNT(*) FROM scene_markers_tags s WHERE s.scene_marker_id = scene_markers.id) = ?", tagsLen-1)
case models.CriterionModifierNotEquals:
f.setError(fmt.Errorf("not equals modifier is not supported for scene marker tags"))
default:
addHierarchicalConditionClauses(f, tags, "marker_tags", "root_tag_id")
}
}
if len(criterion.Excludes) > 0 {
valuesClause, err := getHierarchicalValues(ctx, dbWrapper{}, tags.Excludes, tagTable, "tags_relations", "parent_id", "child_id", tags.Depth)
if err != nil {
f.setError(err)
return
}
clause := "scene_markers.id NOT IN (SELECT scene_markers_tags.scene_marker_id FROM scene_markers_tags WHERE scene_markers_tags.tag_id IN (SELECT column2 FROM (%s)))"
f.addWhere(fmt.Sprintf(clause, valuesClause))
f.addWhere(fmt.Sprintf("scene_markers.primary_tag_id NOT IN (SELECT column2 FROM (%s))", valuesClause))
}
}
}
}