diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2007-06-27 18:58:10 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2007-06-27 18:58:10 +0000 |
| commit | 24512a74befc6282a1d299cab452ee9463cc2baa (patch) | |
| tree | 7654c2ed72d28b7a0b9f47c0bfbebb9b75949ae6 /django/db/backends/sqlite3 | |
| parent | 7dc8b1a1a8a8e2771f37568a125fd51a3283b043 (diff) | |
Fixed #1465: added support for regex lookups. Thanks, Tom Tobin.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5555 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/backends/sqlite3')
| -rw-r--r-- | django/db/backends/sqlite3/base.py | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index b753879d7a..c4ecf5f578 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -64,9 +64,10 @@ class DatabaseWrapper(local): } kwargs.update(self.options) self.connection = Database.connect(**kwargs) - # Register extract and date_trunc functions. + # Register extract, date_trunc, and regexp functions. self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) + self.connection.create_function("regexp", 2, _sqlite_regexp) cursor = self.connection.cursor(factory=SQLiteCursorWrapper) cursor.row_factory = utf8rowFactory if settings.DEBUG: @@ -214,6 +215,13 @@ def _sqlite_date_trunc(lookup_type, dt): elif lookup_type == 'day': return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day) +def _sqlite_regexp(re_pattern, re_string): + import re + try: + return bool(re.search(re_pattern, re_string)) + except: + return False + # SQLite requires LIKE statements to include an ESCAPE clause if the value # being escaped has a percent or underscore in it. # See http://www.sqlite.org/lang_expr.html for an explanation. @@ -222,6 +230,8 @@ OPERATOR_MAPPING = { 'iexact': "LIKE %s ESCAPE '\\'", 'contains': "LIKE %s ESCAPE '\\'", 'icontains': "LIKE %s ESCAPE '\\'", + 'regex': 'REGEXP %s', + 'iregex': "REGEXP '(?i)' || %s", 'gt': '> %s', 'gte': '>= %s', 'lt': '< %s', |
