Use '\' as escape char for wildcard

issue10626
review349031002
(grafted from c25c1b385cb9b7cf7e27e035a482485f3324f184)

--HG--
branch : 6.0
This commit is contained in:
Cédric Krier committed 2021-08-12 18:39:50 +02:00
1 parent 3ad6a236b1
commit 2d188923cd
2 files changed
+22 -7

No files matched your search

+21 -6
View File
@@ -109,6 +109,23 @@ def likify(value, escape='\\'):
return '%' + value + '%'
def is_full_text(value, escape='\\'):
escaped = value.strip('%')
escaped = escaped.replace(escape + '%', '').replace(escape + '_', '')
if '%' in escaped or '_' in escaped:
return False
return value.startswith('%') and value.endswith('%')
def is_like(value, escape='\\'):
escaped = value.replace(escape + '%', '').replace(escape + '_', '')
return '%' in escaped or '_' in escaped
def unescape(value, escape='\\'):
return value.replace(escape + '%', '%').replace(escape + '_', '_')
def quote(value):
"Quote string if needed"
if not isinstance(value, str):
@@ -594,8 +611,7 @@ class DomainParser(object):
if name.endswith('.rec_name'):
name = name[:-9]
if name not in self.fields:
escaped = value.replace('%%', '__')
if escaped.startswith('%') and escaped.endswith('%'):
if is_full_text(value):
value = value[1:-1]
return quote(value)
field = self.fields[name]
@@ -606,15 +622,14 @@ class DomainParser(object):
target = None
if 'ilike' in operator:
escaped = value.replace('%%', '__')
if escaped.startswith('%') and escaped.endswith('%'):
if is_full_text(value):
value = value[1:-1]
elif '%' not in escaped:
elif not is_like(value):
if operator == 'ilike':
operator = '='
else:
operator = '!'
value = value.replace('%%', '%')
value = unescape(value)
def_operator = default_operator(field)
if def_operator == operator.strip():
operator = ''
+1 -1
View File
@@ -674,7 +674,7 @@ class DomainParserTestCase(TestCase):
self.assertEqual(dom.string([('name', 'ilike', 'Doe')]), 'Name: =Doe')
self.assertEqual(dom.string([('name', 'ilike', 'Doe%')]), 'Name: Doe%')
self.assertEqual(
dom.string([('name', 'ilike', 'Doe%%')]), 'Name: =Doe%')
dom.string([('name', 'ilike', 'Doe\\%')]), 'Name: =Doe%')
self.assertEqual(
dom.string([('name', 'not ilike', '%Doe%')]), 'Name: !Doe')
self.assertEqual(