]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- cyclomatic complexity; break up visit_select, goes from F to D
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 27 Sep 2014 22:13:50 +0000 (18:13 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 27 Sep 2014 22:15:21 +0000 (18:15 -0400)
lib/sqlalchemy/sql/compiler.py

index 0bdc60b8c173bdae9aab72506a789a82a0177674..18b4d4cfcc476b30ba3fcfce5d2bf205693b6a90 100644 (file)
@@ -1491,29 +1491,7 @@ class SQLCompiler(Compiled):
                     select, transformed_select)
             return text
 
-        correlate_froms = entry['correlate_froms']
-        asfrom_froms = entry['asfrom_froms']
-
-        if asfrom:
-            froms = select._get_display_froms(
-                explicit_correlate_froms=correlate_froms.difference(
-                    asfrom_froms),
-                implicit_correlate_froms=())
-        else:
-            froms = select._get_display_froms(
-                explicit_correlate_froms=correlate_froms,
-                implicit_correlate_froms=asfrom_froms)
-
-        new_correlate_froms = set(selectable._from_objects(*froms))
-        all_correlate_froms = new_correlate_froms.union(correlate_froms)
-
-        new_entry = {
-            'asfrom_froms': new_correlate_froms,
-            'iswrapper': iswrapper,
-            'correlate_froms': all_correlate_froms,
-            'selectable': select,
-        }
-        self.stack.append(new_entry)
+        froms = self._setup_select_stack(select, entry, asfrom, iswrapper)
 
         column_clause_args = kwargs.copy()
         column_clause_args.update({
@@ -1524,18 +1502,11 @@ class SQLCompiler(Compiled):
         text = "SELECT "  # we're off to a good start !
 
         if select._hints:
-            byfrom = dict([
-                (from_, hinttext % {
-                    'name': from_._compiler_dispatch(
-                        self, ashint=True)
-                })
-                for (from_, dialect), hinttext in
-                select._hints.items()
-                if dialect in ('*', self.dialect.name)
-            ])
-            hint_text = self.get_select_hint_text(byfrom)
+            hint_text, byfrom = self._setup_select_hints(select)
             if hint_text:
                 text += hint_text + " "
+        else:
+            byfrom = None
 
         if select._prefixes:
             text += self._generate_prefixes(
@@ -1556,6 +1527,70 @@ class SQLCompiler(Compiled):
             if c is not None
         ]
 
+        text = self._compose_select_body(
+            text, select, inner_columns, froms, byfrom, kwargs)
+
+        if select._statement_hints:
+            per_dialect = [
+                ht for (dialect_name, ht)
+                in select._statement_hints
+                if dialect_name in ('*', self.dialect.name)
+            ]
+            if per_dialect:
+                text += " " + self.get_statement_hint_text(per_dialect)
+
+        if self.ctes and \
+                compound_index == 0 and toplevel:
+            text = self._render_cte_clause() + text
+
+        self.stack.pop(-1)
+
+        if asfrom and parens:
+            return "(" + text + ")"
+        else:
+            return text
+
+    def _setup_select_hints(self, select):
+        byfrom = dict([
+            (from_, hinttext % {
+                'name': from_._compiler_dispatch(
+                    self, ashint=True)
+            })
+            for (from_, dialect), hinttext in
+            select._hints.items()
+            if dialect in ('*', self.dialect.name)
+        ])
+        hint_text = self.get_select_hint_text(byfrom)
+        return hint_text, byfrom
+
+    def _setup_select_stack(self, select, entry, asfrom, iswrapper):
+        correlate_froms = entry['correlate_froms']
+        asfrom_froms = entry['asfrom_froms']
+
+        if asfrom:
+            froms = select._get_display_froms(
+                explicit_correlate_froms=correlate_froms.difference(
+                    asfrom_froms),
+                implicit_correlate_froms=())
+        else:
+            froms = select._get_display_froms(
+                explicit_correlate_froms=correlate_froms,
+                implicit_correlate_froms=asfrom_froms)
+
+        new_correlate_froms = set(selectable._from_objects(*froms))
+        all_correlate_froms = new_correlate_froms.union(correlate_froms)
+
+        new_entry = {
+            'asfrom_froms': new_correlate_froms,
+            'iswrapper': iswrapper,
+            'correlate_froms': all_correlate_froms,
+            'selectable': select,
+        }
+        self.stack.append(new_entry)
+        return froms
+
+    def _compose_select_body(
+            self, text, select, inner_columns, froms, byfrom, kwargs):
         text += ', '.join(inner_columns)
 
         if froms:
@@ -1599,25 +1634,7 @@ class SQLCompiler(Compiled):
         if select._for_update_arg is not None:
             text += self.for_update_clause(select, **kwargs)
 
-        if select._statement_hints:
-            per_dialect = [
-                ht for (dialect_name, ht)
-                in select._statement_hints
-                if dialect_name in ('*', self.dialect.name)
-            ]
-            if per_dialect:
-                text += " " + self.get_statement_hint_text(per_dialect)
-
-        if self.ctes and \
-                compound_index == 0 and toplevel:
-            text = self._render_cte_clause() + text
-
-        self.stack.pop(-1)
-
-        if asfrom and parens:
-            return "(" + text + ")"
-        else:
-            return text
+        return text
 
     def _generate_prefixes(self, stmt, prefixes, **kw):
         clause = " ".join(