Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions readonly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
VERSION = tuple(map(int, pkg_resources.get_distribution('django-db-readonly').version.split('.')))
__version__ = VERSION

import re
from time import time

from django.conf import settings
Expand Down Expand Up @@ -40,12 +41,24 @@ class ReadOnlyCursorWrapper(object):
Raises a DatabaseWriteDenied exception if writes are disabled.
"""

SQL_WRITE_BLACKLIST = (
_SQL_WRITE_BLACKLIST = (
# Data Definition
'CREATE', 'ALTER', 'RENAME', 'DROP', 'TRUNCATE',
'CREATE',
'ALTER',
'RENAME',
'DROP',
'TRUNCATE',
# Data Manipulation
'INSERT INTO', 'UPDATE', 'REPLACE', 'DELETE FROM',
'INSERT INTO',
'UPDATE',
'REPLACE',
'DELETE FROM',
)
SQL_WRITE_BLACKLIST_RE = re.compile(
# Generate a regular expression: r'^\s*(CREATE|ALTER|INSERT\s+INTO|...)\b'
r'^\s*(?:%s)\b' % '|'.join([re.sub(r'\s+', '\\s+', sql)
for sql in _SQL_WRITE_BLACKLIST]),
re.IGNORECASE)

def __init__(self, cursor):
self.cursor = cursor
Expand All @@ -70,7 +83,7 @@ def __iter__(self):
return iter(self.cursor)

def _write_sql(self, sql):
return sql.startswith(self.SQL_WRITE_BLACKLIST)
return bool(self.SQL_WRITE_BLACKLIST_RE.match(sql))


class CursorWrapper(util.CursorWrapper):
Expand Down