Represent a SQL template string using Python 3.14+ t-strings. E.g.:: from sqlalchemy import tstring, column a = 5 b = 10 stmt = tstring(t"select {a}, {b}") result = connection.execute(stmt) The :class:`_expression.TString` construct is produced usi
| 2735 | |
| 2736 | |
| 2737 | class TString(AbstractTextClause, inspection.Inspectable["TString"]): |
| 2738 | """Represent a SQL template string using Python 3.14+ t-strings. |
| 2739 | |
| 2740 | E.g.:: |
| 2741 | |
| 2742 | from sqlalchemy import tstring, column |
| 2743 | |
| 2744 | a = 5 |
| 2745 | b = 10 |
| 2746 | stmt = tstring(t"select {a}, {b}") |
| 2747 | result = connection.execute(stmt) |
| 2748 | |
| 2749 | The :class:`_expression.TString` construct is produced using the |
| 2750 | :func:`_expression.tstring` function; see that function for full |
| 2751 | documentation. |
| 2752 | |
| 2753 | .. versionadded:: 2.1 |
| 2754 | |
| 2755 | .. seealso:: |
| 2756 | |
| 2757 | :func:`_expression.tstring` |
| 2758 | |
| 2759 | """ |
| 2760 | |
| 2761 | __visit_name__ = "tstring" |
| 2762 | |
| 2763 | _traverse_internals: _TraverseInternalsType = [ |
| 2764 | ("parts", InternalTraversal.dp_clauseelement_list) |
| 2765 | ] + ExecutableStatement._executable_traverse_internals |
| 2766 | |
| 2767 | @property |
| 2768 | def _is_star(self) -> bool: # type: ignore[override] |
| 2769 | return ( |
| 2770 | len(self.parts) == 1 |
| 2771 | and isinstance(self.parts[0], TextClause) |
| 2772 | and self.parts[0]._is_star |
| 2773 | ) |
| 2774 | |
| 2775 | def __init__(self, template: Template): |
| 2776 | """Construct a :class:`_expression.TString` from a Python 3.14+ |
| 2777 | template string. |
| 2778 | |
| 2779 | :param template: a Python 3.14+ template string (t-string) that |
| 2780 | contains SQL fragments and Python expressions to be interpolated. |
| 2781 | |
| 2782 | """ |
| 2783 | self.parts: List[ClauseElement] = [] |
| 2784 | |
| 2785 | if not isinstance(template, Template): |
| 2786 | raise exc.ArgumentError("pep-750 Tstring (e.g. t'...') expected") |
| 2787 | |
| 2788 | for part in template: |
| 2789 | if isinstance(part, str): |
| 2790 | self.parts.append(TextClause(part)) |
| 2791 | else: |
| 2792 | assert hasattr(part, "value") |
| 2793 | self.parts.append( |
| 2794 | coercions.expect(roles.TStringElementRole, part.value) |