diff options
Diffstat (limited to 'lib/sqlalchemy/schema.py')
-rw-r--r-- | lib/sqlalchemy/schema.py | 88 |
1 files changed, 38 insertions, 50 deletions
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index f894e6c31..3a74cbd59 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -27,7 +27,7 @@ Since these objects are part of the SQL expression language, they are usable as components in SQL expressions. """ -from __future__ import with_statement + import re import inspect from . import exc, util, dialects, event, events, inspection @@ -679,7 +679,7 @@ class Table(SchemaItem, expression.TableClause): # skip indexes that would be generated # by the 'index' flag on Column if len(index.columns) == 1 and \ - list(index.columns)[0].index: + list(index.columns)[0].index: continue Index(index.name, unique=index.unique, @@ -898,7 +898,7 @@ class Column(SchemaItem, expression.ColumnClause): type_ = kwargs.pop('type_', None) args = list(args) if args: - if isinstance(args[0], basestring): + if isinstance(args[0], util.string_types): if name is not None: raise exc.ArgumentError( "May not pass name positionally and as a keyword.") @@ -944,11 +944,7 @@ class Column(SchemaItem, expression.ColumnClause): args.append(self.default) else: if getattr(self.type, '_warn_on_bytestring', False): - # Py3K - #if isinstance(self.default, bytes): - # Py2K - if isinstance(self.default, str): - # end Py2K + if isinstance(self.default, util.binary_type): util.warn("Unicode column received non-unicode " "default value.") args.append(ColumnDefault(self.default)) @@ -983,7 +979,7 @@ class Column(SchemaItem, expression.ColumnClause): if kwargs: raise exc.ArgumentError( - "Unknown arguments passed to Column: " + repr(kwargs.keys())) + "Unknown arguments passed to Column: " + repr(list(kwargs))) def __str__(self): if self.name is None: @@ -1048,7 +1044,7 @@ class Column(SchemaItem, expression.ColumnClause): if self.key in table._columns: col = table._columns.get(self.key) if col is not self: - for fk in list(col.foreign_keys): + for fk in col.foreign_keys: table.foreign_keys.remove(fk) if fk.constraint in table.constraints: # this might have been removed @@ -1069,7 +1065,7 @@ class Column(SchemaItem, expression.ColumnClause): self.table = table if self.index: - if isinstance(self.index, basestring): + if isinstance(self.index, util.string_types): raise exc.ArgumentError( "The 'index' keyword argument on Column is boolean only. " "To create indexes with a specific name, create an " @@ -1077,7 +1073,7 @@ class Column(SchemaItem, expression.ColumnClause): Index(expression._truncated_label('ix_%s' % self._label), self, unique=self.unique) elif self.unique: - if isinstance(self.unique, basestring): + if isinstance(self.unique, util.string_types): raise exc.ArgumentError( "The 'unique' keyword argument on Column is boolean " "only. To create unique constraints or indexes with a " @@ -1153,23 +1149,15 @@ class Column(SchemaItem, expression.ColumnClause): nullable=self.nullable, quote=self.quote, _proxies=[self], *fk) - except TypeError, e: - # Py3K - #raise TypeError( - # "Could not create a copy of this %r object. " - # "Ensure the class includes a _constructor() " - # "attribute or method which accepts the " - # "standard Column constructor arguments, or " - # "references the Column class itself." % self.__class__) from e - # Py2K - raise TypeError( - "Could not create a copy of this %r object. " - "Ensure the class includes a _constructor() " - "attribute or method which accepts the " - "standard Column constructor arguments, or " - "references the Column class itself. " - "Original error: %s" % (self.__class__, e)) - # end Py2K + except TypeError: + util.raise_from_cause( + TypeError( + "Could not create a copy of this %r object. " + "Ensure the class includes a _constructor() " + "attribute or method which accepts the " + "standard Column constructor arguments, or " + "references the Column class itself." % self.__class__) + ) c.table = selectable selectable._columns.add(c) @@ -1345,7 +1333,7 @@ class ForeignKey(SchemaItem): if schema: return schema + "." + self.column.table.name + \ "." + self.column.key - elif isinstance(self._colspec, basestring): + elif isinstance(self._colspec, util.string_types): return self._colspec elif hasattr(self._colspec, '__clause_element__'): _column = self._colspec.__clause_element__() @@ -1390,7 +1378,7 @@ class ForeignKey(SchemaItem): """ # ForeignKey inits its remote column as late as possible, so tables # can be defined without dependencies - if isinstance(self._colspec, basestring): + if isinstance(self._colspec, util.string_types): # locate the parent table this foreign key is attached to. we # use the "original" column which our parent column represents # (its a list of columns/other ColumnElements if the parent @@ -1919,7 +1907,7 @@ class DefaultClause(FetchedValue): has_argument = True def __init__(self, arg, for_update=False, _reflected=False): - util.assert_arg_type(arg, (basestring, + util.assert_arg_type(arg, (util.string_types[0], expression.ClauseElement, expression.TextClause), 'arg') super(DefaultClause, self).__init__(for_update) @@ -2029,7 +2017,7 @@ class ColumnCollectionMixin(object): def _set_parent(self, table): for col in self._pending_colargs: - if isinstance(col, basestring): + if isinstance(col, util.string_types): col = table.c[col] self.columns.add(col) @@ -2247,7 +2235,7 @@ class ForeignKeyConstraint(Constraint): self._set_parent_with_dispatch(table) elif columns and \ isinstance(columns[0], Column) and \ - columns[0].table is not None: + columns[0].table is not None: self._set_parent_with_dispatch(columns[0].table) @property @@ -2256,19 +2244,19 @@ class ForeignKeyConstraint(Constraint): @property def columns(self): - return self._elements.keys() + return list(self._elements) @property def elements(self): - return self._elements.values() + return list(self._elements.values()) def _set_parent(self, table): super(ForeignKeyConstraint, self)._set_parent(table) - for col, fk in self._elements.iteritems(): + for col, fk in self._elements.items(): # string-specified column names now get # resolved to Column objects - if isinstance(col, basestring): + if isinstance(col, util.string_types): try: col = table.c[col] except KeyError: @@ -2278,7 +2266,7 @@ class ForeignKeyConstraint(Constraint): "named '%s' is present." % (table.description, col)) if not hasattr(fk, 'parent') or \ - fk.parent is not col: + fk.parent is not col: fk._set_parent_with_dispatch(col) if self.use_alter: @@ -2569,7 +2557,7 @@ class MetaData(SchemaItem): return 'MetaData(bind=%r)' % self.bind def __contains__(self, table_or_key): - if not isinstance(table_or_key, basestring): + if not isinstance(table_or_key, util.string_types): table_or_key = table_or_key.key return table_or_key in self.tables @@ -2629,7 +2617,7 @@ class MetaData(SchemaItem): def _bind_to(self, bind): """Bind this MetaData to an Engine, Connection, string or URL.""" - if isinstance(bind, (basestring, url.URL)): + if isinstance(bind, util.string_types + (url.URL, )): from sqlalchemy import create_engine self._bind = create_engine(bind) else: @@ -2662,7 +2650,7 @@ class MetaData(SchemaItem): :meth:`.Inspector.sorted_tables` """ - return sqlutil.sort_tables(self.tables.itervalues()) + return sqlutil.sort_tables(self.tables.values()) def reflect(self, bind=None, schema=None, views=False, only=None): """Load all available table definitions from the database. @@ -2723,7 +2711,7 @@ class MetaData(SchemaItem): bind.dialect.get_view_names(conn, schema) ) - current = set(self.tables.iterkeys()) + current = set(self.tables) if only is None: load = [name for name in available if name not in current] @@ -2845,7 +2833,7 @@ class ThreadLocalMetaData(MetaData): def _bind_to(self, bind): """Bind to a Connectable in the caller's thread.""" - if isinstance(bind, (basestring, url.URL)): + if isinstance(bind, util.string_types + (url.URL, )): try: self.context._engine = self.__engines[bind] except KeyError: @@ -2870,7 +2858,7 @@ class ThreadLocalMetaData(MetaData): def dispose(self): """Dispose all bound engines, in all thread contexts.""" - for e in self.__engines.itervalues(): + for e in self.__engines.values(): if hasattr(e, 'dispose'): e.dispose() @@ -3075,7 +3063,7 @@ class DDLElement(expression.Executable, _DDLCompiles): not self._should_execute_deprecated(None, target, bind, **kw): return False - if isinstance(self.dialect, basestring): + if isinstance(self.dialect, util.string_types): if self.dialect != bind.engine.name: return False elif isinstance(self.dialect, (tuple, list, set)): @@ -3090,7 +3078,7 @@ class DDLElement(expression.Executable, _DDLCompiles): def _should_execute_deprecated(self, event, target, bind, **kw): if self.on is None: return True - elif isinstance(self.on, basestring): + elif isinstance(self.on, util.string_types): return self.on == bind.engine.name elif isinstance(self.on, (tuple, list, set)): return bind.engine.name in self.on @@ -3105,7 +3093,7 @@ class DDLElement(expression.Executable, _DDLCompiles): def _check_ddl_on(self, on): if (on is not None and - (not isinstance(on, (basestring, tuple, list, set)) and + (not isinstance(on, util.string_types + (tuple, list, set)) and not util.callable(on))): raise exc.ArgumentError( "Expected the name of a database dialect, a tuple " @@ -3230,7 +3218,7 @@ class DDL(DDLElement): """ - if not isinstance(statement, basestring): + if not isinstance(statement, util.string_types): raise exc.ArgumentError( "Expected a string or unicode SQL statement, got '%r'" % statement) @@ -3262,7 +3250,7 @@ def _to_schema_column(element): def _to_schema_column_or_string(element): if hasattr(element, '__clause_element__'): element = element.__clause_element__() - if not isinstance(element, (basestring, expression.ColumnElement)): + if not isinstance(element, util.string_types + (expression.ColumnElement, )): msg = "Element %r is not a string name or column element" raise exc.ArgumentError(msg % element) return element |