~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/engine/ddl.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# engine/ddl.py
2
 
# Copyright (C) 2009-2011 the SQLAlchemy authors and contributors <see AUTHORS file>
 
2
# Copyright (C) 2009-2013 the SQLAlchemy authors and contributors <see AUTHORS file>
3
3
#
4
4
# This module is part of SQLAlchemy and is released under
5
5
# the MIT License: http://www.opensource.org/licenses/mit-license.php
6
6
 
7
7
"""Routines to handle CREATE/DROP workflow."""
8
8
 
9
 
from sqlalchemy import engine, schema
10
 
from sqlalchemy.sql import util as sql_util
 
9
from .. import schema
 
10
from ..sql import util as sql_util
11
11
 
12
12
 
13
13
class DDLBase(schema.SchemaVisitor):
14
14
    def __init__(self, connection):
15
15
        self.connection = connection
16
16
 
 
17
 
17
18
class SchemaGenerator(DDLBase):
18
 
    def __init__(self, dialect, connection, checkfirst=False, tables=None, **kwargs):
 
19
 
 
20
    def __init__(self, dialect, connection, checkfirst=False,
 
21
                 tables=None, **kwargs):
19
22
        super(SchemaGenerator, self).__init__(connection, **kwargs)
20
23
        self.checkfirst = checkfirst
21
 
        self.tables = tables and set(tables) or None
 
24
        self.tables = tables
22
25
        self.preparer = dialect.identifier_preparer
23
26
        self.dialect = dialect
24
27
        self.memo = {}
36
39
            (
37
40
                (not self.dialect.sequences_optional or
38
41
                 not sequence.optional) and
39
 
                 (
40
 
                 not self.checkfirst or
41
 
                 not self.dialect.has_sequence(
42
 
                            self.connection,
43
 
                            sequence.name,
44
 
                            schema=sequence.schema)
45
 
                 )
 
42
                    (
 
43
                        not self.checkfirst or
 
44
                        not self.dialect.has_sequence(
 
45
                                self.connection,
 
46
                                sequence.name,
 
47
                                schema=sequence.schema)
 
48
                     )
46
49
            )
47
50
 
48
51
    def visit_metadata(self, metadata):
49
 
        if self.tables:
 
52
        if self.tables is not None:
50
53
            tables = self.tables
51
54
        else:
52
55
            tables = metadata.tables.values()
103
106
 
104
107
 
105
108
class SchemaDropper(DDLBase):
106
 
    def __init__(self, dialect, connection, checkfirst=False, tables=None, **kwargs):
 
109
 
 
110
    def __init__(self, dialect, connection, checkfirst=False,
 
111
                 tables=None, **kwargs):
107
112
        super(SchemaDropper, self).__init__(connection, **kwargs)
108
113
        self.checkfirst = checkfirst
109
114
        self.tables = tables
112
117
        self.memo = {}
113
118
 
114
119
    def visit_metadata(self, metadata):
115
 
        if self.tables:
 
120
        if self.tables is not None:
116
121
            tables = self.tables
117
122
        else:
118
123
            tables = metadata.tables.values()
119
 
        collection = [t for t in reversed(sql_util.sort_tables(tables))
120
 
                                if self._can_drop_table(t)]
121
 
        seq_coll = [s for s in metadata._sequences.values()
122
 
                                if s.column is None and self._can_drop_sequence(s)]
123
 
 
124
 
        metadata.dispatch.before_drop(metadata, self.connection,
125
 
                                            tables=collection,
126
 
                                            checkfirst=self.checkfirst,
127
 
                                            _ddl_runner=self)
 
124
 
 
125
        collection = [
 
126
            t
 
127
            for t in reversed(sql_util.sort_tables(tables))
 
128
            if self._can_drop_table(t)
 
129
        ]
 
130
 
 
131
        seq_coll = [
 
132
            s
 
133
            for s in metadata._sequences.values()
 
134
            if s.column is None and self._can_drop_sequence(s)
 
135
        ]
 
136
 
 
137
        metadata.dispatch.before_drop(
 
138
            metadata, self.connection, tables=collection,
 
139
            checkfirst=self.checkfirst, _ddl_runner=self)
128
140
 
129
141
        for table in collection:
130
142
            self.traverse_single(table, drop_ok=True)
132
144
        for seq in seq_coll:
133
145
            self.traverse_single(seq, drop_ok=True)
134
146
 
135
 
        metadata.dispatch.after_drop(metadata, self.connection,
136
 
                                            tables=collection,
137
 
                                            checkfirst=self.checkfirst,
138
 
                                            _ddl_runner=self)
 
147
        metadata.dispatch.after_drop(
 
148
            metadata, self.connection, tables=collection,
 
149
            checkfirst=self.checkfirst, _ddl_runner=self)
139
150
 
140
151
    def _can_drop_table(self, table):
141
152
        self.dialect.validate_identifier(table.name)
149
160
            ((not self.dialect.sequences_optional or
150
161
                 not sequence.optional) and
151
162
                (not self.checkfirst or
152
 
                 self.dialect.has_sequence(
 
163
                self.dialect.has_sequence(
153
164
                                self.connection,
154
165
                                sequence.name,
155
166
                                schema=sequence.schema))