MCPcopy
hub / github.com/django/django / YearLookup

Class YearLookup

django/db/models/lookups.py:704–742  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

702
703
704class YearLookup(Lookup):
705 def year_lookup_bounds(self, connection, year):
706 from django.db.models.functions import ExtractIsoYear
707
708 iso_year = isinstance(self.lhs, ExtractIsoYear)
709 output_field = self.lhs.lhs.output_field
710 if isinstance(output_field, DateTimeField):
711 bounds = connection.ops.year_lookup_bounds_for_datetime_field(
712 year,
713 iso_year=iso_year,
714 )
715 else:
716 bounds = connection.ops.year_lookup_bounds_for_date_field(
717 year,
718 iso_year=iso_year,
719 )
720 return bounds
721
722 def as_sql(self, compiler, connection):
723 # Avoid the extract operation if the rhs is a direct value to allow
724 # indexes to be used.
725 if self.rhs_is_direct_value():
726 # Skip the extract part by directly using the originating field,
727 # that is self.lhs.lhs.
728 lhs_sql, params = self.process_lhs(compiler, connection, self.lhs.lhs)
729 rhs_sql, _ = self.process_rhs(compiler, connection)
730 rhs_sql = self.get_direct_rhs_sql(connection, rhs_sql)
731 start, finish = self.year_lookup_bounds(connection, self.rhs)
732 params = (*params, *self.get_bound_params(start, finish))
733 return "%s %s" % (lhs_sql, rhs_sql), params
734 return super().as_sql(compiler, connection)
735
736 def get_direct_rhs_sql(self, connection, rhs):
737 return connection.operators[self.lookup_name] % rhs
738
739 def get_bound_params(self, start, finish):
740 raise NotImplementedError(
741 "subclasses of YearLookup must provide a get_bound_params() method"
742 )
743
744
745class YearExact(YearLookup, Exact):

Callers 2

test_reprMethod · 0.90
test_get_bound_paramsMethod · 0.90

Calls

no outgoing calls

Tested by 2

test_reprMethod · 0.72
test_get_bound_paramsMethod · 0.72