Skip to content

Commit 24e5c91

Browse files
ComputerdoresCyanVoxel
authored andcommitted
fix: search now uses TagEntry (#656)
1 parent af30301 commit 24e5c91

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

tagstudio/src/core/library/alchemy/library.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
TextField,
5050
_FieldID,
5151
)
52-
from .joins import TagSubtag
52+
from .joins import TagEntry, TagSubtag
5353
from .models import Entry, Folder, Preferences, Tag, TagAlias, ValueType
5454
from .visitors import SQLBoolExpressionBuilder
5555

@@ -559,8 +559,7 @@ def search_library(
559559

560560
if search.ast:
561561
start_time = time.time()
562-
563-
statement = statement.outerjoin(Entry.tag_box_fields).where(
562+
statement = statement.outerjoin(TagEntry).where(
564563
SQLBoolExpressionBuilder(self).visit(search.ast)
565564
)
566565

tagstudio/src/core/library/alchemy/visitors.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from src.core.query_lang import BaseVisitor
99
from src.core.query_lang.ast import AST, ANDList, Constraint, ConstraintType, Not, ORList, Property
1010

11-
from .joins import TagField
12-
from .models import Entry, Tag, TagAlias, TagBoxField
11+
from .joins import TagEntry
12+
from .models import Entry, Tag, TagAlias
1313

1414
# workaround to have autocompletion in the Editor
1515
if TYPE_CHECKING:
@@ -72,19 +72,20 @@ def visit_and_list(self, node: ANDList) -> ColumnExpressionArgument:
7272
# If there is just one tag id, check the normal way
7373
elif len(tag_ids) == 1:
7474
bool_expressions.append(
75-
self.__entry_satisfies_expression(TagField.tag_id == tag_ids[0])
75+
self.__entry_satisfies_expression(TagEntry.tag_id == tag_ids[0])
7676
)
7777

7878
return and_(*bool_expressions)
7979

8080
def visit_constraint(self, node: Constraint) -> ColumnExpressionArgument:
81+
"""Returns a Boolean Expression that is true, if the Entry satisfies the constraint."""
8182
if len(node.properties) != 0:
8283
raise NotImplementedError("Properties are not implemented yet") # TODO TSQLANG
8384

8485
if node.type == ConstraintType.Tag:
85-
return TagBoxField.tags.any(Tag.id.in_(self.__get_tag_ids(node.value)))
86+
return Entry.tags.any(Tag.id.in_(self.__get_tag_ids(node.value)))
8687
elif node.type == ConstraintType.TagID:
87-
return TagBoxField.tags.any(Tag.id == int(node.value))
88+
return Entry.tags.any(Tag.id == int(node.value))
8889
elif node.type == ConstraintType.Path:
8990
return Entry.path.op("GLOB")(node.value)
9091
elif node.type == ConstraintType.MediaType:
@@ -100,9 +101,7 @@ def visit_constraint(self, node: Constraint) -> ColumnExpressionArgument:
100101
)
101102
elif node.type == ConstraintType.Special: # noqa: SIM102 unnecessary once there is a second special constraint
102103
if node.value.lower() == "untagged":
103-
return ~Entry.id.in_(
104-
select(Entry.id).join(Entry.tag_box_fields).join(TagBoxField.tags)
105-
)
104+
return ~Entry.id.in_(select(Entry.id).join(TagEntry))
106105

107106
# raise exception if Constraint stays unhandled
108107
raise NotImplementedError("This type of constraint is not implemented yet")
@@ -141,11 +140,10 @@ def __entry_has_all_tags(self, tag_ids: list[int]) -> BinaryExpression[bool]:
141140
# Relational Division Query
142141
return Entry.id.in_(
143142
select(Entry.id)
144-
.outerjoin(TagBoxField)
145-
.outerjoin(TagField)
146-
.where(TagField.tag_id.in_(tag_ids))
143+
.outerjoin(TagEntry)
144+
.where(TagEntry.tag_id.in_(tag_ids))
147145
.group_by(Entry.id)
148-
.having(func.count(distinct(TagField.tag_id)) == len(tag_ids))
146+
.having(func.count(distinct(TagEntry.tag_id)) == len(tag_ids))
149147
)
150148

151149
def __entry_satisfies_ast(self, partial_query: AST) -> BinaryExpression[bool]:
@@ -155,7 +153,8 @@ def __entry_satisfies_ast(self, partial_query: AST) -> BinaryExpression[bool]:
155153
def __entry_satisfies_expression(
156154
self, expr: ColumnExpressionArgument
157155
) -> BinaryExpression[bool]:
158-
"""Returns Binary Expression that is true if the Entry satisfies the column expression."""
159-
return Entry.id.in_(
160-
select(Entry.id).outerjoin(Entry.tag_box_fields).outerjoin(TagField).where(expr)
161-
)
156+
"""Returns Binary Expression that is true if the Entry satisfies the column expression.
157+
158+
Executed on: Entry ⟕ TagEntry (Entry LEFT OUTER JOIN TagEntry).
159+
"""
160+
return Entry.id.in_(select(Entry.id).outerjoin(TagEntry).where(expr))

0 commit comments

Comments
 (0)