I am going to add the more explicative exceptions. Patterns fields will go in the new DAL but probably with a slightly different notation. Thanks Hans.
On May 31, 3:43 am, HansD <hans.don...@pobox.com> wrote: > allowing for stuff like: > > def SQLPatternFields(value_list, pattern, type): > fields = [] > if not pattern or pattern == '': > pattern = '%s' > for value in value_list: > fields.append( > SQLField(pattern % value, type) > ) > return fields > > periods_fields = SQLPatternFields(range(1,12), 'period_%02d', ' > integer') > # generates SQLField('period_01', 'integer') upto SQLField > ('period_12', 'integer') > > db.define_table( > 'period', > periods_fields > ) > > (on gae the db design needs to be a bit more denormalized, this keeps > you DRY) > while building this I ran into exception msgs not being verbose enough > > PS: already send to Massimo (before the new DAL announcement) > > === modified file 'web2py/gluon/contrib/gql.py' > --- web2py/gluon/contrib/gql.py 2009-05-25 06:12:10 +0000 > +++ web2py/gluon/contrib/gql.py 2009-05-28 21:27:50 +0000 > @@ -55,7 +55,8 @@ > def cleanup(text): > if re.compile('[^0-9a-zA-Z_]').findall(text): > raise SyntaxError, \ > - 'only [0-9a-zA-Z_] allowed in table and field names' > + 'only [0-9a-zA-Z_] allowed in table and field names, > received %s' \ > + % text > return text > > @@ -113,7 +114,8 @@ > > def __setattr__(self, key, value): > if key in self: > - raise SyntaxError, 'Object exists and cannot be > redefined' > + raise SyntaxError, 'Object exists and cannot be > redefined: %s' \ > + % key > self[key] = value > > def __repr__(self): > @@ -155,11 +157,11 @@ > > tablename = cleanup(tablename) > if tablename in dir(self) or tablename[0] == '_': > - raise SyntaxError, 'invalid table name' > + raise SyntaxError, 'invalid table name: %s' % tablename > if not tablename in self.tables: > self.tables.append(tablename) > else: > - raise SyntaxError, 'table already defined' > + raise SyntaxError, 'table already defined: %s' % > tablename > t = self[tablename] = SQLTable(self, tablename, *fields) > t._create() > return t > @@ -200,15 +202,25 @@ > *fields > ): > new_fields = [] > + > + def check_field(field, new_fields): > + > + if isinstance(field, SQLField): > + new_fields.append(field) > + elif isinstance(field, SQLTable): > + new_fields += [copy.copy(field[f]) for f in > + field.fields if f != 'id'] > + elif isinstance(field, list) or isinstance(field, > tuple): > + for field2 in field: > + check_field(field2, new_fields) > + else: > + raise SyntaxError, \ > + 'define_table field argument "%s" is not a > SQLField' \ > + % field > + > for field in fields: > - if isinstance(field, SQLField): > - new_fields.append(field) > - elif isinstance(field, SQLTable): > - new_fields += [copy.copy(field[f]) for f in > - field.fields if f != 'id'] > - else: > - raise SyntaxError, \ > - 'define_table argument is not a SQLField' > + check_field(field, new_fields) > + > fields = new_fields > self._db = db > self._tablename = tablename > @@ -248,18 +260,20 @@ > if not referenced: > raise SyntaxError, 'SQLTable: reference to > nothing!' > if not referenced in self._db: > - raise SyntaxError, 'SQLTable: table does not > exist' > + raise SyntaxError, \ > + 'SQLTable: table does not exist: %s' % > referenced > referee = self._db[referenced] > ftype = \ > self._db._translator[field.type[:9]](self._db > [referenced]) > if self._tablename in referee.fields: # ## THIS IS > OK > raise SyntaxError, \ > - 'SQLField: table name has same name as a > field in referenced table' > + 'SQLField: table name has same name as a > field in referenced table: %s' \ > + % self._tablename > self._db[referenced]._referenced_by.append > ((self._tablename, > field.name)) > elif not field.type in self._db._translator\ > or not self._db._translator[field.type]: > - raise SyntaxError, 'SQLField: unkown field type' > + raise SyntaxError, 'SQLField: unknown field type: %s' > % field.type > else: > ftype = self._db._translator[field.type](**attr) > myfields[field.name] = ftype > @@ -401,7 +415,7 @@ > > self.name = fieldname = cleanup(fieldname) > if fieldname in dir(SQLTable) or fieldname[0] == '_': > - raise SyntaxError, 'SQLField: invalid field name' > + raise SyntaxError, 'SQLField: invalid field name: %s' % > fieldname > if isinstance(type, SQLTable): > type = 'reference ' + type._tablename > if not length and type == 'string': > @@ -543,14 +557,15 @@ > return > if isinstance(right, (SQLField, SQLXorable)): > raise SyntaxError, \ > - 'SQLQuery: right side of filter must be a value or > entity' > + 'SQLQuery: right side of filter must be a value or > entity: %s' \ > + % right > if isinstance(left, SQLField) and left.name == 'id': > if not right: > right = 0 > try: > value = long(right) > except: > - raise SyntaxError, 'id value must be integer' > + raise SyntaxError, 'id value must be integer: %s' % > id > if op == '=': > self.get_one = \ > QueryException(tablename=left._tablename, > id=value) > @@ -654,7 +669,7 @@ > ] > if [key for key in attributes.keys() if not key > in valid_attributes]: > - raise SyntaxError, 'invalid select attribute' > + raise SyntaxError, 'invalid select attribute: %s' % key > if fields and isinstance(fields[0], SQLALL): > self._tables.insert(0, fields[0].table._tablename) > table = self._get_table_or_raise() > @@ -821,7 +836,7 @@ > > def __getitem__(self, i): > if i >= len(self.response) or i < 0: > - raise SyntaxError, 'SQLRows: no such row' > + raise SyntaxError, 'SQLRows: no such row: %s' % i > if len(self.response[0]) != len(self.colnames): > raise SyntaxError, 'SQLRows: internal error' > row = SQLStorage() > > === modified file 'web2py/gluon/sql.py' > --- web2py/gluon/sql.py 2009-05-25 06:12:10 +0000 > +++ web2py/gluon/sql.py 2009-05-28 21:27:51 +0000 > @@ -404,7 +404,8 @@ > def cleanup(text): > if re.compile('[^0-9a-zA-Z_]').findall(text): > raise SyntaxError, \ > - 'only [0-9a-zA-Z_] allowed in table and field names' > + 'only [0-9a-zA-Z_] allowed in table and field names, > received %s' \ > + % text > return text > > @@ -492,7 +493,8 @@ > > def __setattr__(self, key, value): > if key in self: > - raise SyntaxError, 'Object exists and cannot be > redefined' > + raise SyntaxError, \ > + 'Object %s exists and cannot be redefined' % key > self[key] = value > > def __repr__(self): > @@ -581,7 +583,8 @@ > for (i, db) in instances: > keys.append('%s.%i' % (thread_key, i)) > if not db._dbname == 'postgres': > - raise SyntaxError, 'only supported by postgresql' > + raise SyntaxError, \ > + 'distributed transactyion only supported by > postgresql' > try: > for (i, db) in instances: > db._execute("PREPARE TRANSACTION '%s';" % keys[i]) > @@ -664,7 +667,8 @@ > m = re.compile('^(?P<user>[^:@]+)(\:(?P<passwd>[...@]*))?@(? > P<host>[^\:/]+)(\:(?P<port>[0-9]+))?/(?P<db>[^?]+)(\?set_encoding=(? > P<charset>\w+))?$' > ).match(self._uri[8:]) > if not m: > - raise SyntaxError, "Invalid URI string in SQLDB" > + raise SyntaxError, \ > + "Invalid URI string in SQLDB: %s" % self._uri > user = m.group('user') > if not user: > raise SyntaxError, 'User required' > @@ -766,7 +770,8 @@ > re.compile('^(?P<user>[^:@]+)(\:(?P<passwd>[...@] > *))?@(?P<host>[^\:/]+)(\:(?P<port>[0-9]+))?/(?P<db>.+)$' > ).match(self._uri[skip:]) > if not m: > - raise SyntaxError, "Invalid URI string in SQLDB" > + raise SyntaxError, \ > + "Invalid URI string in SQLDB: %s" % self._uri > user = m.group('user') > if not user: > raise SyntaxError, 'User required' > @@ -801,7 +806,8 @@ > re.compile('^(?P<user>[^:@]+)(\:(?P<passwd>[...@]*))?@(? > P<host>[^\:/]+)(\:(?P<port>[0-9]+))?/(?P<db>.+)(\?set_encoding=(? > P<charset>\w+))?$' > ).match(self._uri[11:]) > if not m: > - raise SyntaxError, "Invalid URI string in SQLDB" > + raise SyntaxError, \ > + "Invalid URI string in SQLDB: %s" % self._uri > user = m.group('user') > if not user: > raise SyntaxError, 'User required' > @@ -834,7 +840,8 @@ > re.compile('^(?P<user>[^:@]+)(\:(?P<passwd>[...@]*))?@(? > P<host>[^\:/]+)(\:(?P<port>[0-9]+))?/(?P<db>.+)$' > ).match(self._uri[11:]) > if not m: > - raise SyntaxError, "Invalid URI string in SQLDB" > + raise SyntaxError, \ > + "Invalid URI string in SQLDB: %s" % self._uri > user = m.group('user') > if not user: > raise SyntaxError, 'User required' > @@ -890,7 +897,8 @@ > self._cursor = Dummy() > self._execute = lambda a: [] > else: > - raise SyntaxError, 'database type not supported' > + raise SyntaxError, \ > + 'database type not supported: %s' % self._uri > self._translator = SQL_DIALECTS[self._dbname] > > # ## register this instance of SQLDB > @@ -913,12 +921,13 @@ > if not 'migrate' in args: > args['migrate'] = True > if args.keys() != ['migrate']: > - raise SyntaxError, 'invalid table attribute' > + raise SyntaxError, \ > + 'invalid table attribute: %s' % arg.keys() > tablename = cleanup(tablename) > if tablename in dir(self) or tablename[0] == '_': > - raise SyntaxError, 'invalid table name' > + raise SyntaxError, 'invalid table name: %s' % tablename > if tablename in self.tables: > - raise SyntaxError, 'table already defined' > + raise SyntaxError, 'table already defined: %s' % > tablename > t = self[tablename] = SQLTable(self, tablename, *fields) > if self._uri == 'None': > args['migrate'] = False > @@ -1066,15 +1075,23 @@ > *fields > ): > new_fields = [] > + > + def check_field(field, new_fields): > + > + if isinstance(field, SQLField): > + new_fields.append(field) > + elif isinstance(field, SQLTable): > + new_fields += [copy.copy(field[f]) for f in > + field.fields if f != 'id'] > + elif isinstance(field, list) or isinstance(field, > tuple): > + for field2 in field: > + check_field(field2, new_fields) > + else: > + raise SyntaxError, \ > + 'define_table field argument "%s" is not a > SQLField' % field > + > for field in fields: > - if isinstance(field, SQLField): > - new_fields.append(field) > - elif isinstance(field, SQLTable): > - new_fields += [copy.copy(field[f]) for f in > - field.fields if f != 'id'] > - else: > - raise SyntaxError, \ > - 'define_table argument is not a SQLField' > + check_field(field, new_fields) > fields = new_fields > self._db = db > self._tablename = tablename > @@ -1109,20 +1126,21 @@ > self.insert(**self._filter_fields(value)) > elif not self._db(self.id > == key).update(**self._filter_fields > (value)): > - raise SyntaxError, 'No such record' > + raise SyntaxError, 'No such record: %s' % key > else: > dict.__setitem__(self, str(key), value) > > def __delitem__(self, key): > if not is_integer(key) or not self._db(self.id == key).delete > (): > - raise SyntaxError, 'No such record' > + raise SyntaxError, 'No such record: %s' % key > > def __getattr__(self, key): > return self[key] > > def __setattr__(self, key, value): > if key in self: > - raise SyntaxError, 'Object exists and cannot be > redefined' > + raise SyntaxError, \ > + 'Object exists and cannot be redefined: %s' % key > self[key] = value > > def __repr__(self): > @@ -1155,7 +1173,8 @@ > if not referenced: > raise SyntaxError, 'SQLTable: reference to > nothing!' > if not referenced in self._db: > - raise SyntaxError, 'SQLTable: table does not > exist' > + raise SyntaxError, \ > + 'SQLTable: table does not exist: ' % > referenced > referee = self._db[referenced] > ftype = self._db._translator[field.type[:9]]\ > % dict(table_name=self._tablename, > @@ -1164,11 +1183,13 @@ > on_delete_action=field.ondelete) > if self._tablename in referee.fields: # ## THIS IS > OK > raise SyntaxError, \ > - 'SQLField: table name has same name as a > field in referenced table' > + 'SQLField: table name has same name as a > field in referenced table: %s' \ > + % self._tablename > self._db[referenced]._referenced_by.append > ((self._tablename, > field.name)) > elif not field.type in self._db._translator: > - raise SyntaxError, 'SQLField: unkown field type' > + raise SyntaxError, \ > + 'SQLField: unkown field type %s' % field.type > else: > ftype = self._db._translator[field.type]\ > % dict(length=field.length) > @@ -1348,7 +1369,7 @@ > def _insert(self, **fields): > (fs, vs) = ([], []) > if [key for key in fields if not key in self.fields]: > - raise SyntaxError, 'invalid field name' > + raise SyntaxError, 'invalid field name: %s' % key > for fieldname in self.fields: > if fieldname == 'id': > continue > @@ -1365,7 +1386,7 @@ > fs.append(fieldname) > vs.append(sql_represent(field.default, ft, fd)) > elif field.required is True: > - raise SyntaxError,'SQLTable: missing required field: > %s'%field > + raise SyntaxError,'SQLTable: missing required field: > %s' % field > sql_f = ', '.join(fs) > sql_v = ', '.join(vs) > sql_t = self._tablename > @@ -1601,7 +1622,7 @@ > > self.name = fieldname = cleanup(fieldname) > if fieldname in dir(SQLTable) or fieldname[0] == '_': > - raise SyntaxError, 'SQLField: invalid field name' > + raise SyntaxError, 'SQLField: invalid field name: %s' % > fieldname > if isinstance(type, SQLTable): > type = 'reference ' + type._tablename > if not length and type == 'string': > @@ -1716,13 +1737,16 @@ > > def __getslice__(self, start, stop): > if start < 0 or stop < start: > - raise SyntaxError, 'not supported' > + raise SyntaxError, 'not supported: %s - %s' % (start, > stop) > d = dict(field=str(self), pos=start + 1, length=stop - start) > s = self._db._translator['substring'] % d > return SQLXorable(s, 'string', self._db) > > def __str__(self): > - return '%s.%s' % (self._tablename, self.name) > + try: > + return '%s.%s' % (self._tablename, self.name) > + except: > + return '<no table>.%s' % self.name > > SQLDB.Field = SQLField # necessary in gluon/globals.py > session.connect > @@ -1873,7 +1897,7 @@ > ] > if [key for key in attributes if not key > in valid_attributes]: > - raise SyntaxError, 'invalid select attribute' > + raise SyntaxError, 'invalid select attribute: %s' % key > > # ## if not fields specified take them all from the requested > tables > > @@ -2101,7 +2125,7 @@ > > def __getitem__(self, i): > if i >= len(self.response) or i < 0: > - raise SyntaxError, 'SQLRows: no such row' > + raise SyntaxError, 'SQLRows: no such row: %s' % i > if len(self.response[0]) != len(self.colnames): > raise SyntaxError, 'SQLRows: internal error' > row = SQLStorage() --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py Web Framework" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---