From 8b25d546b6113bd8db7e3627939d107ebd834fff Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 7 Mar 2018 20:04:17 +0100 Subject: [PATCH] Simplified Contains, StartsWith, and EndsWith lookups. --- django/db/models/lookups.py | 43 ++++++++++--------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index 7b01d068634..e5f620eb2ca 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -376,6 +376,7 @@ class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup): class PatternLookup(BuiltinLookup): + param_pattern = '%%%s%%' def get_rhs_op(self, connection, rhs): # Assume we are in startswith. We need to produce SQL like: @@ -393,18 +394,18 @@ class PatternLookup(BuiltinLookup): else: return super().get_rhs_op(connection, rhs) + def process_rhs(self, qn, connection): + rhs, params = super().process_rhs(qn, connection) + if params and not self.bilateral_transforms: + params[0] = self.param_pattern % connection.ops.prep_for_like_query(params[0]) + return rhs, params + @Field.register_lookup class Contains(PatternLookup): lookup_name = 'contains' prepare_rhs = False - def process_rhs(self, qn, connection): - rhs, params = super().process_rhs(qn, connection) - if params and not self.bilateral_transforms: - params[0] = "%%%s%%" % connection.ops.prep_for_like_query(params[0]) - return rhs, params - @Field.register_lookup class IContains(Contains): @@ -415,50 +416,28 @@ class IContains(Contains): @Field.register_lookup class StartsWith(PatternLookup): lookup_name = 'startswith' + param_pattern = '%s%%' prepare_rhs = False - def process_rhs(self, qn, connection): - rhs, params = super().process_rhs(qn, connection) - if params and not self.bilateral_transforms: - params[0] = "%s%%" % connection.ops.prep_for_like_query(params[0]) - return rhs, params - @Field.register_lookup -class IStartsWith(PatternLookup): +class IStartsWith(StartsWith): lookup_name = 'istartswith' prepare_rhs = False - def process_rhs(self, qn, connection): - rhs, params = super().process_rhs(qn, connection) - if params and not self.bilateral_transforms: - params[0] = "%s%%" % connection.ops.prep_for_like_query(params[0]) - return rhs, params - @Field.register_lookup class EndsWith(PatternLookup): lookup_name = 'endswith' + param_pattern = '%%%s' prepare_rhs = False - def process_rhs(self, qn, connection): - rhs, params = super().process_rhs(qn, connection) - if params and not self.bilateral_transforms: - params[0] = "%%%s" % connection.ops.prep_for_like_query(params[0]) - return rhs, params - @Field.register_lookup -class IEndsWith(PatternLookup): +class IEndsWith(EndsWith): lookup_name = 'iendswith' prepare_rhs = False - def process_rhs(self, qn, connection): - rhs, params = super().process_rhs(qn, connection) - if params and not self.bilateral_transforms: - params[0] = "%%%s" % connection.ops.prep_for_like_query(params[0]) - return rhs, params - @Field.register_lookup class Range(FieldGetDbPrepValueIterableMixin, BuiltinLookup):