Skip to content

Commit

Permalink
Update regex to be compatible with django 2 to Django 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dzen committed Apr 13, 2021
1 parent 36f287e commit df2a9e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
16 changes: 9 additions & 7 deletions ldapdb/backends/ldap/compiler.py
Expand Up @@ -15,7 +15,8 @@
from ldapdb.models.fields import ListField

_ORDER_BY_LIMIT_OFFSET_RE = re.compile(
r'(?:\bORDER BY\b\s+(.+?))?\s*(?:\bLIMIT\b\s+(-?\d+))?\s*(?:\bOFFSET\b\s+(\d+))?$')
r"(?:\bORDER BY\b\s+([\w\.]+)\s(?P<order>\bASC\b)|(\bDESC\b))\s{1,2}(?:\bLIMIT\b\s+(?P<limit>-?\d+))?[\)\s]?(?:\bOFFSET\b\s+(?P<offset>(\d+)))?" # noqa: E501
)


class LdapDBError(Exception):
Expand Down Expand Up @@ -146,12 +147,13 @@ def execute_sql(self, result_type=compiler.SINGLE, chunked_fetch=False,
if hasattr(self.query, 'subquery') and self.query.subquery:
sql = self.query.subquery
m = _ORDER_BY_LIMIT_OFFSET_RE.search(sql)
limit = m.group(2)
offset = m.group(3)
if limit and int(limit) >= 0:
output.append(int(limit))
elif offset:
output.append(len(vals) - int(offset))
if m:
limit = m.group('limit')
offset = m.group('offset')
if limit and int(limit) >= 0:
output.append(int(limit))
elif offset:
output.append(len(vals) - int(offset))
else:
output.append(len(vals))
else:
Expand Down
29 changes: 29 additions & 0 deletions ldapdb/tests.py
Expand Up @@ -246,3 +246,32 @@ def test_or(self):
where.add(self._build_lookup("cn", 'exact', "foo", field=fields.CharField), AND)
where.add(self._build_lookup("givenName", 'exact', "bar", field=fields.CharField), OR)
self.assertEqual(self._where_as_ldap(where), "(|(cn=foo)(givenName=bar))")


class CompilerRegexTestCase(TestCase):

def _run_regex(self, sql):
match = ldapdb_compiler._ORDER_BY_LIMIT_OFFSET_RE.search(sql)
self.assertIsNotNone(match)
return match

def test_regex(self):
sql = "SELECT examples_ldapgroup.cn AS Col1 FROM examples_ldapgroup ORDER BY examples_ldapgroup.gidNumber ASC LIMIT 2" # noqa: E501
match = self._run_regex(sql)
self.assertEqual(match.group('limit'), '2')
self.assertEqual(match.group('offset'), None)

sql = "SELECT COUNT(*) FROM (SELECT examples_ldapgroup.cn AS Col1 FROM examples_ldapgroup ORDER BY examples_ldapgroup.gidNumber ASC LIMIT 2) subquery" # noqa: E501
match = self._run_regex(sql)
self.assertEqual(match.group('limit'), '2')

sql = "SELECT COUNT(*) FROM (SELECT examples_ldapgroup.cn AS Col1 FROM examples_ldapgroup ORDER BY examples_ldapgroup.gidNumber ASC LIMIT -1 OFFSET 1) subquery" # noqa: E501
match = self._run_regex(sql)
self.assertEqual(match.group('limit'), '-1')
self.assertEqual(match.group('offset'), '1')

def test_regex_django22(self):
sql = "SELECT examples_ldapgroup.cn AS Col1 FROM examples_ldapgroup ORDER BY examples_ldapgroup.gidNumber ASC LIMIT 2" # noqa: E501
match = self._run_regex(sql)
self.assertEqual(match.group('limit'), '2')
self.assertEqual(match.group('offset'), None)

0 comments on commit df2a9e7

Please sign in to comment.