8
8
from src .core .query_lang import BaseVisitor
9
9
from src .core .query_lang .ast import AST , ANDList , Constraint , ConstraintType , Not , ORList , Property
10
10
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
13
13
14
14
# workaround to have autocompletion in the Editor
15
15
if TYPE_CHECKING :
@@ -72,19 +72,20 @@ def visit_and_list(self, node: ANDList) -> ColumnExpressionArgument:
72
72
# If there is just one tag id, check the normal way
73
73
elif len (tag_ids ) == 1 :
74
74
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 ])
76
76
)
77
77
78
78
return and_ (* bool_expressions )
79
79
80
80
def visit_constraint (self , node : Constraint ) -> ColumnExpressionArgument :
81
+ """Returns a Boolean Expression that is true, if the Entry satisfies the constraint."""
81
82
if len (node .properties ) != 0 :
82
83
raise NotImplementedError ("Properties are not implemented yet" ) # TODO TSQLANG
83
84
84
85
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 )))
86
87
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 ))
88
89
elif node .type == ConstraintType .Path :
89
90
return Entry .path .op ("GLOB" )(node .value )
90
91
elif node .type == ConstraintType .MediaType :
@@ -100,9 +101,7 @@ def visit_constraint(self, node: Constraint) -> ColumnExpressionArgument:
100
101
)
101
102
elif node .type == ConstraintType .Special : # noqa: SIM102 unnecessary once there is a second special constraint
102
103
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 ))
106
105
107
106
# raise exception if Constraint stays unhandled
108
107
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]:
141
140
# Relational Division Query
142
141
return Entry .id .in_ (
143
142
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 ))
147
145
.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 ))
149
147
)
150
148
151
149
def __entry_satisfies_ast (self , partial_query : AST ) -> BinaryExpression [bool ]:
@@ -155,7 +153,8 @@ def __entry_satisfies_ast(self, partial_query: AST) -> BinaryExpression[bool]:
155
153
def __entry_satisfies_expression (
156
154
self , expr : ColumnExpressionArgument
157
155
) -> 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