~ubuntu-branches/debian/sid/sqlalchemy/sid

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/schema.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2012-03-15 21:05:49 UTC
  • mfrom: (1.4.19)
  • Revision ID: package-import@ubuntu.com-20120315210549-fiuynu6jue9keqlh
Tags: 0.7.6-1
* New upstream release
* debhelper's compatibility bumped to 7
* Standards-Version bumped to 3.9.3 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
    else:
81
81
        return schema + "." + name
82
82
 
 
83
def _validate_dialect_kwargs(kwargs, name):
 
84
    # validate remaining kwargs that they all specify DB prefixes
 
85
    if len([k for k in kwargs
 
86
            if not re.match(
 
87
                        r'^(?:%s)_' % 
 
88
                        '|'.join(dialects.__all__), k
 
89
                    )
 
90
            ]):
 
91
        raise TypeError(
 
92
            "Invalid argument(s) for %s: %r" % (name, kwargs.keys()))
 
93
 
83
94
 
84
95
class Table(SchemaItem, expression.TableClause):
85
96
    """Represent a table in a database.
369
380
        # allow user-overrides
370
381
        self._init_items(*args)
371
382
 
372
 
    def _autoload(self, metadata, autoload_with, include_columns, exclude_columns=None):
 
383
    def _autoload(self, metadata, autoload_with, include_columns, exclude_columns=()):
373
384
        if self.primary_key.columns:
374
 
            PrimaryKeyConstraint()._set_parent_with_dispatch(self)
 
385
            PrimaryKeyConstraint(*[
 
386
                c for c in self.primary_key.columns 
 
387
                if c.key in exclude_columns
 
388
            ])._set_parent_with_dispatch(self)
375
389
 
376
390
        if autoload_with:
377
391
            autoload_with.run_callable(
424
438
            if not autoload_replace:
425
439
                exclude_columns = [c.name for c in self.c]
426
440
            else:
427
 
                exclude_columns = None
 
441
                exclude_columns = ()
428
442
            self._autoload(self.metadata, autoload_with, include_columns, exclude_columns)
429
443
 
430
444
        self._extra_kwargs(**kwargs)
432
446
 
433
447
    def _extra_kwargs(self, **kwargs):
434
448
        # validate remaining kwargs that they all specify DB prefixes
435
 
        if len([k for k in kwargs
436
 
                if not re.match(
437
 
                            r'^(?:%s)_' % 
438
 
                            '|'.join(dialects.__all__), k
439
 
                        )
440
 
                ]):
441
 
            raise TypeError(
442
 
                "Invalid argument(s) for Table: %r" % kwargs.keys())
 
449
        _validate_dialect_kwargs(kwargs, "Table")
443
450
        self.kwargs.update(kwargs)
444
451
 
445
452
    def _init_collections(self):
1028
1035
                    "The 'index' keyword argument on Column is boolean only. "
1029
1036
                    "To create indexes with a specific name, create an "
1030
1037
                    "explicit Index object external to the Table.")
1031
 
            Index(expression._generated_label('ix_%s' % self._label), self, unique=self.unique)
 
1038
            Index(expression._truncated_label('ix_%s' % self._label), self, unique=self.unique)
1032
1039
        elif self.unique:
1033
1040
            if isinstance(self.unique, basestring):
1034
1041
                raise exc.ArgumentError(
1093
1100
                    "been assigned.")
1094
1101
        try:
1095
1102
            c = self._constructor(
1096
 
                name or self.name, 
 
1103
                expression._as_truncated(name or self.name), 
1097
1104
                self.type, 
1098
1105
                key = name or self.key, 
1099
1106
                primary_key = self.primary_key, 
1119
1126
 
1120
1127
        c.table = selectable
1121
1128
        selectable._columns.add(c)
 
1129
        if selectable._is_clone_of is not None:
 
1130
            c._is_clone_of = selectable._is_clone_of.columns[c.name]
1122
1131
        if self.primary_key:
1123
1132
            selectable.primary_key.add(c)
1124
1133
        c.dispatch.after_parent_attach(c, selectable)
1809
1818
    __visit_name__ = 'constraint'
1810
1819
 
1811
1820
    def __init__(self, name=None, deferrable=None, initially=None, 
1812
 
                            _create_rule=None):
 
1821
                            _create_rule=None, 
 
1822
                            **kw):
1813
1823
        """Create a SQL constraint.
1814
1824
 
1815
1825
        :param name:
1839
1849
 
1840
1850
          _create_rule is used by some types to create constraints.
1841
1851
          Currently, its call signature is subject to change at any time.
 
1852
        
 
1853
        :param \**kwargs: 
 
1854
          Dialect-specific keyword parameters, see the documentation
 
1855
          for various dialects and constraints regarding options here.
1842
1856
 
1843
1857
        """
1844
1858
 
1847
1861
        self.initially = initially
1848
1862
        self._create_rule = _create_rule
1849
1863
        util.set_creation_order(self)
 
1864
        _validate_dialect_kwargs(kw, self.__class__.__name__)
 
1865
        self.kwargs = kw
1850
1866
 
1851
1867
    @property
1852
1868
    def table(self):
2192
2208
        self.table = None
2193
2209
        # will call _set_parent() if table-bound column
2194
2210
        # objects are present
 
2211
        if not columns:
 
2212
            util.warn("No column names or expressions given for Index.")
2195
2213
        ColumnCollectionMixin.__init__(self, *columns)
2196
2214
        self.name = name
2197
2215
        self.unique = kw.pop('unique', False)
3004
3022
   return element
3005
3023
 
3006
3024
def _to_schema_column_or_string(element):
3007
 
  if hasattr(element, '__clause_element__'):
3008
 
      element = element.__clause_element__()
3009
 
  return element
 
3025
    if hasattr(element, '__clause_element__'):
 
3026
        element = element.__clause_element__()
 
3027
    if not isinstance(element, (basestring, expression.ColumnElement)):
 
3028
        raise exc.ArgumentError("Element %r is not a string name or column element" % element)
 
3029
    return element
3010
3030
 
3011
3031
class _CreateDropBase(DDLElement):
3012
3032
    """Base class for DDL constucts that represent CREATE and DROP or