2929from django_mongodb_backend .query_utils import process_lhs
3030
3131
32+ def base_expression (self , compiler , connection , as_path = False , ** extra ):
33+ if as_path and hasattr (self , "as_mql_path" ) and getattr (self , "can_use_path" , False ):
34+ return self .as_mql_path (compiler , connection , ** extra )
35+
36+ expr = self .as_mql_expr (compiler , connection , ** extra )
37+ return {"$expr" : expr } if as_path else expr
38+
39+
3240def case (self , compiler , connection ):
3341 case_parts = []
3442 for case in self .cases :
3543 case_mql = {}
3644 try :
37- case_mql ["case" ] = case .as_mql (compiler , connection , as_path = False )
45+ case_mql ["case" ] = case .as_mql (compiler , connection )
3846 except EmptyResultSet :
3947 continue
4048 except FullResultSet :
@@ -84,20 +92,20 @@ def col_pairs(self, compiler, connection, as_path=False):
8492 return cols [0 ].as_mql (compiler , connection , as_path = as_path )
8593
8694
87- def combined_expression (self , compiler , connection , as_path = False ):
95+ def combined_expression (self , compiler , connection ):
8896 expressions = [
89- self .lhs .as_mql (compiler , connection , as_path = as_path ),
90- self .rhs .as_mql (compiler , connection , as_path = as_path ),
97+ self .lhs .as_mql (compiler , connection ),
98+ self .rhs .as_mql (compiler , connection ),
9199 ]
92100 return connection .ops .combine_expression (self .connector , expressions )
93101
94102
95- def expression_wrapper_expr (self , compiler , connection ):
96- return self .expression .as_mql (compiler , connection , as_path = False )
103+ def expression_wrapper (self , compiler , connection ):
104+ return self .expression .as_mql (compiler , connection )
97105
98106
99- def negated_expression_expr (self , compiler , connection ):
100- return {"$not" : expression_wrapper_expr (self , compiler , connection )}
107+ def negated_expression (self , compiler , connection ):
108+ return {"$not" : expression_wrapper (self , compiler , connection )}
101109
102110
103111def order_by (self , compiler , connection ):
@@ -172,10 +180,10 @@ def ref(self, compiler, connection, as_path=False): # noqa: ARG001
172180
173181@property
174182def ref_is_simple_column (self ):
175- return isinstance ( self .source , Col ) and self . source . alias is not None
183+ return self .source . is_simple_column
176184
177185
178- def star (self , compiler , connection , as_path = False ): # noqa: ARG001
186+ def star (self , compiler , connection ): # noqa: ARG001
179187 return {"$literal" : True }
180188
181189
@@ -190,11 +198,11 @@ def exists(self, compiler, connection, get_wrapping_pipeline=None):
190198 lhs_mql = subquery (self , compiler , connection , get_wrapping_pipeline = get_wrapping_pipeline )
191199 except EmptyResultSet :
192200 return Value (False ).as_mql (compiler , connection )
193- return connection .mongo_operators_expr ["isnull" ](lhs_mql , False )
201+ return connection .mongo_expr_operators ["isnull" ](lhs_mql , False )
194202
195203
196- def when (self , compiler , connection , as_path = False ):
197- return self .condition .as_mql (compiler , connection , as_path = as_path )
204+ def when (self , compiler , connection ):
205+ return self .condition .as_mql (compiler , connection )
198206
199207
200208def value (self , compiler , connection , as_path = False ): # noqa: ARG001
@@ -221,18 +229,6 @@ def value(self, compiler, connection, as_path=False): # noqa: ARG001
221229 return value
222230
223231
224- def base_expression (self , compiler , connection , as_path = False , ** extra ):
225- if (
226- as_path
227- and hasattr (self , "as_mql_path" )
228- and getattr (self , "is_simple_expression" , lambda : False )()
229- ):
230- return self .as_mql_path (compiler , connection , ** extra )
231-
232- expr = self .as_mql_expr (compiler , connection , ** extra )
233- return {"$expr" : expr } if as_path else expr
234-
235-
236232def register_expressions ():
237233 BaseExpression .as_mql = base_expression
238234 BaseExpression .is_simple_column = False
@@ -243,15 +239,15 @@ def register_expressions():
243239 CombinedExpression .as_mql_expr = combined_expression
244240 Exists .as_mql_expr = exists
245241 ExpressionList .as_mql = process_lhs
246- ExpressionWrapper .as_mql_expr = expression_wrapper_expr
247- NegatedExpression .as_mql_expr = negated_expression_expr
242+ ExpressionWrapper .as_mql_expr = expression_wrapper
243+ NegatedExpression .as_mql_expr = negated_expression
248244 OrderBy .as_mql_expr = order_by
249245 Query .as_mql = query
250246 RawSQL .as_mql = raw_sql
251247 Ref .as_mql = ref
252248 Ref .is_simple_column = ref_is_simple_column
253249 ResolvedOuterRef .as_mql = ResolvedOuterRef .as_sql
254- Star .as_mql = star
250+ Star .as_mql_expr = star
255251 Subquery .as_mql_expr = subquery
256- When .as_mql = when
252+ When .as_mql_expr = when
257253 Value .as_mql = value
0 commit comments