Skip to content

Commit 514104c

Browse files
charettesfelixxm
authored andcommitted
Refs #29396, #30494 -- Reduced code duplication in year lookups.
1 parent 2b582a7 commit 514104c

2 files changed

Lines changed: 27 additions & 38 deletions

File tree

django/db/models/lookups.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,6 @@ def year_lookup_bounds(self, connection, year):
483483
bounds = connection.ops.year_lookup_bounds_for_date_field(year)
484484
return bounds
485485

486-
487-
class YearComparisonLookup(YearLookup):
488486
def as_sql(self, compiler, connection):
489487
# Avoid the extract operation if the rhs is a direct value to allow
490488
# indexes to be used.
@@ -493,53 +491,44 @@ def as_sql(self, compiler, connection):
493491
# that is self.lhs.lhs.
494492
lhs_sql, params = self.process_lhs(compiler, connection, self.lhs.lhs)
495493
rhs_sql, _ = self.process_rhs(compiler, connection)
496-
rhs_sql = self.get_rhs_op(connection, rhs_sql)
494+
rhs_sql = self.get_direct_rhs_sql(connection, rhs_sql)
497495
start, finish = self.year_lookup_bounds(connection, self.rhs)
498-
params.append(self.get_bound(start, finish))
496+
params.extend(self.get_bound_params(start, finish))
499497
return '%s %s' % (lhs_sql, rhs_sql), params
500498
return super().as_sql(compiler, connection)
501499

502-
def get_rhs_op(self, connection, rhs):
500+
def get_direct_rhs_sql(self, connection, rhs):
503501
return connection.operators[self.lookup_name] % rhs
504502

505-
def get_bound(self, start, finish):
503+
def get_bound_params(self, start, finish):
506504
raise NotImplementedError(
507-
'subclasses of YearComparisonLookup must provide a get_bound() method'
505+
'subclasses of YearLookup must provide a get_bound_params() method'
508506
)
509507

510508

511509
class YearExact(YearLookup, Exact):
512-
lookup_name = 'exact'
513-
514-
def as_sql(self, compiler, connection):
515-
# Avoid the extract operation if the rhs is a direct value to allow
516-
# indexes to be used.
517-
if self.rhs_is_direct_value():
518-
# Skip the extract part by directly using the originating field,
519-
# that is self.lhs.lhs.
520-
lhs_sql, params = self.process_lhs(compiler, connection, self.lhs.lhs)
521-
bounds = self.year_lookup_bounds(connection, self.rhs)
522-
params.extend(bounds)
523-
return '%s BETWEEN %%s AND %%s' % lhs_sql, params
524-
return super().as_sql(compiler, connection)
510+
def get_direct_rhs_sql(self, connection, rhs):
511+
return 'BETWEEN %s AND %s'
525512

513+
def get_bound_params(self, start, finish):
514+
return (start, finish)
526515

527516

528-
class YearGt(YearComparisonLookup, GreaterThan):
529-
def get_bound(self, start, finish):
530-
return finish
517+
class YearGt(YearLookup, GreaterThan):
518+
def get_bound_params(self, start, finish):
519+
return (finish,)
531520

532521

533-
class YearGte(YearComparisonLookup, GreaterThanOrEqual):
534-
def get_bound(self, start, finish):
535-
return start
522+
class YearGte(YearLookup, GreaterThanOrEqual):
523+
def get_bound_params(self, start, finish):
524+
return (start,)
536525

537526

538-
class YearLt(YearComparisonLookup, LessThan):
539-
def get_bound(self, start, finish):
540-
return start
527+
class YearLt(YearLookup, LessThan):
528+
def get_bound_params(self, start, finish):
529+
return (start,)
541530

542531

543-
class YearLte(YearComparisonLookup, LessThanOrEqual):
544-
def get_bound(self, start, finish):
545-
return finish
532+
class YearLte(YearLookup, LessThanOrEqual):
533+
def get_bound_params(self, start, finish):
534+
return (finish,)

tests/lookup/test_lookups.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
from django.db.models import Value
44
from django.db.models.fields import DateTimeField
5-
from django.db.models.lookups import YearComparisonLookup
5+
from django.db.models.lookups import YearLookup
66
from django.test import SimpleTestCase
77

88

9-
class YearComparisonLookupTests(SimpleTestCase):
10-
def test_get_bound(self):
11-
look_up = YearComparisonLookup(
9+
class YearLookupTests(SimpleTestCase):
10+
def test_get_bound_params(self):
11+
look_up = YearLookup(
1212
lhs=Value(datetime(2010, 1, 1, 0, 0, 0), output_field=DateTimeField()),
1313
rhs=Value(datetime(2010, 1, 1, 23, 59, 59), output_field=DateTimeField()),
1414
)
15-
msg = 'subclasses of YearComparisonLookup must provide a get_bound() method'
15+
msg = 'subclasses of YearLookup must provide a get_bound_params() method'
1616
with self.assertRaisesMessage(NotImplementedError, msg):
17-
look_up.get_bound(datetime(2010, 1, 1, 0, 0, 0), datetime(2010, 1, 1, 23, 59, 59))
17+
look_up.get_bound_params(datetime(2010, 1, 1, 0, 0, 0), datetime(2010, 1, 1, 23, 59, 59))

0 commit comments

Comments
 (0)