MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / visit_label

Method visit_label

lib/sqlalchemy/sql/compiler.py:2614–2668  ·  view source on GitHub ↗
(
        self,
        label,
        add_to_result_map=None,
        within_label_clause=False,
        within_columns_clause=False,
        render_label_as_label=None,
        result_map_targets=(),
        within_tstring=False,
        **kw,
    )

Source from the content-addressed store, hash-verified

2612 )
2613
2614 def visit_label(
2615 self,
2616 label,
2617 add_to_result_map=None,
2618 within_label_clause=False,
2619 within_columns_clause=False,
2620 render_label_as_label=None,
2621 result_map_targets=(),
2622 within_tstring=False,
2623 **kw,
2624 ):
2625 if within_tstring:
2626 raise exc.CompileError(
2627 "Using label() directly inside tstring is not supported "
2628 "as it is ambiguous how the label expression should be "
2629 "rendered without knowledge of how it's being used in SQL"
2630 )
2631 # only render labels within the columns clause
2632 # or ORDER BY clause of a select. dialect-specific compilers
2633 # can modify this behavior.
2634 render_label_with_as = (
2635 within_columns_clause and not within_label_clause
2636 )
2637 render_label_only = render_label_as_label is label
2638
2639 if render_label_only or render_label_with_as:
2640 if isinstance(label.name, elements._truncated_label):
2641 labelname = self._truncated_identifier("colident", label.name)
2642 else:
2643 labelname = label.name
2644
2645 if render_label_with_as:
2646 if add_to_result_map is not None:
2647 add_to_result_map(
2648 labelname,
2649 label.name,
2650 (label, labelname) + label._alt_names + result_map_targets,
2651 label.type,
2652 )
2653 return (
2654 label.element._compiler_dispatch(
2655 self,
2656 within_columns_clause=True,
2657 within_label_clause=True,
2658 **kw,
2659 )
2660 + OPERATORS[operators.as_]
2661 + self.preparer.format_label(label, labelname)
2662 )
2663 elif render_label_only:
2664 return self.preparer.format_label(label, labelname)
2665 else:
2666 return label.element._compiler_dispatch(
2667 self, within_columns_clause=False, **kw
2668 )
2669
2670 def _fallback_column_name(self, column):
2671 raise exc.CompileError(

Callers

nothing calls this directly

Calls 3

_truncated_identifierMethod · 0.95
format_labelMethod · 0.80
_compiler_dispatchMethod · 0.45

Tested by

no test coverage detected