]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- cyclomatic complexity; break up reflecttable(), goes from E to B
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 27 Sep 2014 20:12:04 +0000 (16:12 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 27 Sep 2014 22:14:59 +0000 (18:14 -0400)
lib/sqlalchemy/engine/reflection.py

index c0a3240a53aea39c850cb95fff609ef9095f57ea..838a5bdd2b2484cf9ae5fbbe4069df14c93c579b 100644 (file)
@@ -489,55 +489,83 @@ class Inspector(object):
         for col_d in self.get_columns(
                 table_name, schema, **table.dialect_kwargs):
             found_table = True
-            orig_name = col_d['name']
 
-            table.dispatch.column_reflect(self, table, col_d)
+            self._reflect_column(
+                table, col_d, include_columns,
+                exclude_columns, cols_by_orig_name)
 
-            name = col_d['name']
-            if include_columns and name not in include_columns:
-                continue
-            if exclude_columns and name in exclude_columns:
-                continue
+        if not found_table:
+            raise exc.NoSuchTableError(table.name)
 
-            coltype = col_d['type']
+        self._reflect_pk(
+            table_name, schema, table, cols_by_orig_name, exclude_columns)
 
-            col_kw = dict(
-                (k, col_d[k])
-                for k in ['nullable', 'autoincrement', 'quote', 'info', 'key']
-                if k in col_d
-            )
+        self._reflect_fk(
+            table_name, schema, table, cols_by_orig_name,
+            exclude_columns, reflection_options)
 
-            colargs = []
-            if col_d.get('default') is not None:
-                # the "default" value is assumed to be a literal SQL
-                # expression, so is wrapped in text() so that no quoting
-                # occurs on re-issuance.
-                colargs.append(
-                    sa_schema.DefaultClause(
-                        sql.text(col_d['default']), _reflected=True
-                    )
-                )
+        self._reflect_indexes(
+            table_name, schema, table, cols_by_orig_name,
+            include_columns, exclude_columns, reflection_options)
 
-            if 'sequence' in col_d:
-                # TODO: mssql and sybase are using this.
-                seq = col_d['sequence']
-                sequence = sa_schema.Sequence(seq['name'], 1, 1)
-                if 'start' in seq:
-                    sequence.start = seq['start']
-                if 'increment' in seq:
-                    sequence.increment = seq['increment']
-                colargs.append(sequence)
+    def _reflect_column(
+        self, table, col_d, include_columns,
+            exclude_columns, cols_by_orig_name):
 
-            cols_by_orig_name[orig_name] = col = \
-                sa_schema.Column(name, coltype, *colargs, **col_kw)
+        orig_name = col_d['name']
 
-            if col.key in table.primary_key:
-                col.primary_key = True
-            table.append_column(col)
+        table.dispatch.column_reflect(self, table, col_d)
 
-        if not found_table:
-            raise exc.NoSuchTableError(table.name)
+        # fetch name again as column_reflect is allowed to
+        # change it
+        name = col_d['name']
+        if (include_columns and name not in include_columns) \
+                or (exclude_columns and name in exclude_columns):
+            return
 
+        coltype = col_d['type']
+
+        col_kw = dict(
+            (k, col_d[k])
+            for k in ['nullable', 'autoincrement', 'quote', 'info', 'key']
+            if k in col_d
+        )
+
+        colargs = []
+        if col_d.get('default') is not None:
+            # the "default" value is assumed to be a literal SQL
+            # expression, so is wrapped in text() so that no quoting
+            # occurs on re-issuance.
+            colargs.append(
+                sa_schema.DefaultClause(
+                    sql.text(col_d['default']), _reflected=True
+                )
+            )
+
+        if 'sequence' in col_d:
+            self._reflect_col_sequence(col_d, colargs)
+
+        cols_by_orig_name[orig_name] = col = \
+            sa_schema.Column(name, coltype, *colargs, **col_kw)
+
+        if col.key in table.primary_key:
+            col.primary_key = True
+        table.append_column(col)
+
+    def _reflect_col_sequence(self, col_d, colargs):
+        if 'sequence' in col_d:
+            # TODO: mssql and sybase are using this.
+            seq = col_d['sequence']
+            sequence = sa_schema.Sequence(seq['name'], 1, 1)
+            if 'start' in seq:
+                sequence.start = seq['start']
+            if 'increment' in seq:
+                sequence.increment = seq['increment']
+            colargs.append(sequence)
+
+    def _reflect_pk(
+            self, table_name, schema, table,
+            cols_by_orig_name, exclude_columns):
         pk_cons = self.get_pk_constraint(
             table_name, schema, **table.dialect_kwargs)
         if pk_cons:
@@ -554,6 +582,9 @@ class Inspector(object):
             # its column collection
             table.primary_key._reload(pk_cols)
 
+    def _reflect_fk(
+            self, table_name, schema, table, cols_by_orig_name,
+            exclude_columns, reflection_options):
         fkeys = self.get_foreign_keys(
             table_name, schema, **table.dialect_kwargs)
         for fkey_d in fkeys:
@@ -596,6 +627,10 @@ class Inspector(object):
                 sa_schema.ForeignKeyConstraint(constrained_columns, refspec,
                                                conname, link_to_name=True,
                                                **options))
+
+    def _reflect_indexes(
+        self, table_name, schema, table, cols_by_orig_name,
+            include_columns, exclude_columns, reflection_options):
         # Indexes
         indexes = self.get_indexes(table_name, schema)
         for index_d in indexes: