@@ -148,19 +148,6 @@ def formfield(self, **kwargs):
148148 )
149149
150150
151- def compile_json_path (key_transforms , include_root = True ):
152- path = ["$" ] if include_root else []
153- for key_transform in key_transforms :
154- try :
155- num = int (key_transform )
156- except ValueError : # non-integer
157- path .append ("." )
158- path .append (json .dumps (key_transform ))
159- else :
160- path .append ("[%s]" % num )
161- return "" .join (path )
162-
163-
164151class DataContains (FieldGetDbPrepValueMixin , PostgresOperatorLookup ):
165152 lookup_name = "contains"
166153 postgres_operator = "@>"
@@ -194,7 +181,7 @@ def as_sql(self, compiler, connection):
194181class HasKeyLookup (PostgresOperatorLookup ):
195182 logical_operator = None
196183
197- def compile_json_path_final_key (self , key_transform ):
184+ def compile_json_path_final_key (self , connection , key_transform ):
198185 # Compile the final key without interpreting ints as array elements.
199186 return ".%s" % json .dumps (key_transform )
200187
@@ -204,7 +191,7 @@ def _as_sql_parts(self, compiler, connection):
204191 lhs_sql , lhs_params , lhs_key_transforms = self .lhs .preprocess_lhs (
205192 compiler , connection
206193 )
207- lhs_json_path = compile_json_path (lhs_key_transforms )
194+ lhs_json_path = connection . ops . compile_json_path (lhs_key_transforms )
208195 else :
209196 lhs_sql , lhs_params = self .process_lhs (compiler , connection )
210197 lhs_json_path = "$"
@@ -218,8 +205,10 @@ def _as_sql_parts(self, compiler, connection):
218205 else :
219206 rhs_key_transforms = [key ]
220207 * rhs_key_transforms , final_key = rhs_key_transforms
221- rhs_json_path = compile_json_path (rhs_key_transforms , include_root = False )
222- rhs_json_path += self .compile_json_path_final_key (final_key )
208+ rhs_json_path = connection .ops .compile_json_path (
209+ rhs_key_transforms , include_root = False
210+ )
211+ rhs_json_path += self .compile_json_path_final_key (connection , final_key )
223212 yield lhs_sql , lhs_params , lhs_json_path + rhs_json_path
224213
225214 def _combine_sql_parts (self , parts ):
@@ -296,8 +285,8 @@ class HasAnyKeys(HasKeys):
296285
297286
298287class HasKeyOrArrayIndex (HasKey ):
299- def compile_json_path_final_key (self , key_transform ):
300- return compile_json_path ([key_transform ], include_root = False )
288+ def compile_json_path_final_key (self , connection , key_transform ):
289+ return connection . ops . compile_json_path ([key_transform ], include_root = False )
301290
302291
303292class CaseInsensitiveMixin :
@@ -378,12 +367,12 @@ def preprocess_lhs(self, compiler, connection):
378367
379368 def as_mysql (self , compiler , connection ):
380369 lhs , params , key_transforms = self .preprocess_lhs (compiler , connection )
381- json_path = compile_json_path (key_transforms )
370+ json_path = connection . ops . compile_json_path (key_transforms )
382371 return "JSON_EXTRACT(%s, %%s)" % lhs , (* params , json_path )
383372
384373 def as_oracle (self , compiler , connection ):
385374 lhs , params , key_transforms = self .preprocess_lhs (compiler , connection )
386- json_path = compile_json_path (key_transforms )
375+ json_path = connection . ops . compile_json_path (key_transforms )
387376 if connection .features .supports_primitives_in_json_field :
388377 sql = (
389378 "COALESCE("
@@ -419,7 +408,7 @@ def as_postgresql(self, compiler, connection):
419408
420409 def as_sqlite (self , compiler , connection ):
421410 lhs , params , key_transforms = self .preprocess_lhs (compiler , connection )
422- json_path = compile_json_path (key_transforms )
411+ json_path = connection . ops . compile_json_path (key_transforms )
423412 datatype_values = "," .join (
424413 [repr (datatype ) for datatype in connection .ops .jsonfield_datatype_values ]
425414 )
@@ -441,7 +430,7 @@ def as_mysql(self, compiler, connection):
441430 return "JSON_UNQUOTE(%s)" % sql , params
442431 else :
443432 lhs , params , key_transforms = self .preprocess_lhs (compiler , connection )
444- json_path = compile_json_path (key_transforms )
433+ json_path = connection . ops . compile_json_path (key_transforms )
445434 return "(%s ->> %%s)" % lhs , (* params , json_path )
446435
447436 @classmethod
0 commit comments