Skip to content

Commit

Permalink
better handling of redefine and lazy tables thanks Paolo Pastori
Browse files Browse the repository at this point in the history
  • Loading branch information
mdipierro committed Apr 3, 2018
1 parent cd9c357 commit dfc507e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
37 changes: 20 additions & 17 deletions pydal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@

class MetaDAL(type):
def __call__(cls, *args, **kwargs):
#: intercept arguments for DAL costumisation on call
#: intercept arguments for DAL customisation on call
intercepts = [
'logger', 'representers', 'serializers', 'uuid', 'validators',
'validators_method', 'Table', 'Row']
Expand Down Expand Up @@ -484,8 +484,7 @@ def __init__(self, uri='sqlite://dummy.db',
self.validators = None
adapter = self._adapter
self._uri_hash = table_hash or hashlib_md5(adapter.uri).hexdigest()
self.check_reserved = check_reserved
if self.check_reserved:
if check_reserved:
from .contrib.reserved_sql_keywords import ADAPTERS as RSK
self.RSK = RSK
self._migrate = migrate
Expand Down Expand Up @@ -542,9 +541,9 @@ def import_table_definitions(self, path, migrate=False,
def check_reserved_keyword(self, name):
"""
Validates `name` against SQL keywords
Uses self.check_reserve which is a list of operators to use.
Uses self._check_reserved which is a list of operators to use.
"""
for backend in self.check_reserved:
for backend in self._check_reserved:
if name.upper() in self.RSK[backend]:
raise SyntaxError(
'invalid table/column name "%s" is a "%s" reserved SQL/NOSQL keyword' % (name, backend.upper()))
Expand All @@ -555,6 +554,10 @@ def parse_as_rest(self, patterns, args, vars, queries=None,
patterns, args, vars, queries, nested_select)

def define_table(self, tablename, *fields, **kwargs):
invalid_kwargs = set(kwargs) - TABLE_ARGS
if invalid_kwargs:
raise SyntaxError('invalid table "%s" attributes: %s' %
(tablename, invalid_kwargs))
if not fields and 'fields' in kwargs:
fields = kwargs.get('fields',())
if not isinstance(tablename, str):
Expand All @@ -565,23 +568,23 @@ def define_table(self, tablename, *fields, **kwargs):
raise SyntaxError("invalid unicode table name")
else:
raise SyntaxError("missing table name")
elif hasattr(self, tablename) or tablename in self.tables:
if kwargs.get('redefine', False):
delattr(self, tablename)
redefine = kwargs.get('redefine', False)
if tablename in self.tables:
if redefine:
try:
delattr(self, tablename)
except:
pass
else:
raise SyntaxError('table already defined: %s' % tablename)
elif tablename.startswith('_') or hasattr(self, tablename) or \
elif tablename.startswith('_') or tablename in dir(self) or \
REGEX_PYTHON_KEYWORDS.match(tablename):
raise SyntaxError('invalid table name: %s' % tablename)
elif self.check_reserved:
elif self._check_reserved:
self.check_reserved_keyword(tablename)
else:
invalid_kwargs = set(kwargs) - TABLE_ARGS
if invalid_kwargs:
raise SyntaxError('invalid table "%s" attributes: %s' %
(tablename, invalid_kwargs))
if self._lazy_tables and tablename not in self._LAZY_TABLES:
self._LAZY_TABLES[tablename] = (tablename, fields, kwargs)
if self._lazy_tables:
if tablename not in self._LAZY_TABLES or redefine:
self._LAZY_TABLES[tablename] = (tablename, fields, kwargs)
table = None
else:
table = self.lazy_define_table(tablename, *fields, **kwargs)
Expand Down
8 changes: 4 additions & 4 deletions pydal/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,15 @@ def include_new(field):

fieldnames_set = set()
reserved = dir(Table) + ['fields']
if (db and db.check_reserved):
check_reserved = db.check_reserved_keyword
if (db and db._check_reserved):
check_reserved_keyword = db.check_reserved_keyword
else:
def check_reserved(field_name):
def check_reserved_keyword(field_name):
if field_name in reserved:
raise SyntaxError("field name %s not allowed" % field_name)
for field in fields:
field_name = field.name
check_reserved(field_name)
check_reserved_keyword(field_name)
if db and db._ignore_field_case:
fname_item = field_name.lower()
else:
Expand Down

0 comments on commit dfc507e

Please sign in to comment.