Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/sqlalchemy/sql/roles.py: 92%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# sql/roles.py
2# Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
3# <see AUTHORS file>
4#
5# This module is part of SQLAlchemy and is released under
6# the MIT License: https://www.opensource.org/licenses/mit-license.php
7from __future__ import annotations
9from typing import Any
10from typing import Generic
11from typing import Literal
12from typing import Optional
13from typing import TYPE_CHECKING
14from typing import TypeVar
16from .. import util
18if TYPE_CHECKING:
19 from ._typing import _PropagateAttrsType
20 from .elements import Label
21 from .selectable import _SelectIterable
22 from .selectable import FromClause
23 from .selectable import Subquery
25_T = TypeVar("_T", bound=Any)
26_T_co = TypeVar("_T_co", bound=Any, covariant=True)
29class SQLRole:
30 """Define a "role" within a SQL statement structure.
32 Classes within SQL Core participate within SQLRole hierarchies in order
33 to more accurately indicate where they may be used within SQL statements
34 of all types.
36 .. versionadded:: 1.4
38 """
40 __slots__ = ()
41 allows_lambda = False
42 uses_inspection = False
45class SyntaxExtensionRole(SQLRole):
46 __slots__ = ()
47 _role_name = "Syntax extension construct"
50class UsesInspection:
51 __slots__ = ()
52 _post_inspect: Literal[None] = None
53 uses_inspection = True
56class AllowsLambdaRole:
57 __slots__ = ()
58 allows_lambda = True
61class HasCacheKeyRole(SQLRole):
62 __slots__ = ()
63 _role_name = "Cacheable Core or ORM object"
66class ExecutableOptionRole(SQLRole):
67 __slots__ = ()
68 _role_name = "ExecutionOption Core or ORM object"
71class LiteralValueRole(SQLRole):
72 __slots__ = ()
73 _role_name = "Literal Python value"
76class ColumnArgumentRole(SQLRole):
77 __slots__ = ()
78 _role_name = "Column expression"
81class ColumnArgumentOrKeyRole(ColumnArgumentRole):
82 __slots__ = ()
83 _role_name = "Column expression or string key"
86class StrAsPlainColumnRole(ColumnArgumentRole):
87 __slots__ = ()
88 _role_name = "Column expression or string key"
91class ColumnListRole(SQLRole):
92 """Elements suitable for forming comma separated lists of expressions."""
94 __slots__ = ()
97class StringRole(SQLRole):
98 """mixin indicating a role that results in strings"""
100 __slots__ = ()
103class TruncatedLabelRole(StringRole, SQLRole):
104 __slots__ = ()
105 _role_name = "String SQL identifier"
108class ColumnsClauseRole(AllowsLambdaRole, UsesInspection, ColumnListRole):
109 __slots__ = ()
110 _role_name = (
111 "Column expression, FROM clause, or other columns clause element"
112 )
114 @property
115 def _select_iterable(self) -> _SelectIterable:
116 raise NotImplementedError()
119class TypedColumnsClauseRole(Generic[_T_co], SQLRole):
120 """element-typed form of ColumnsClauseRole"""
122 __slots__ = ()
125class LimitOffsetRole(SQLRole):
126 __slots__ = ()
127 _role_name = "LIMIT / OFFSET expression"
130class ByOfRole(ColumnListRole):
131 __slots__ = ()
132 _role_name = "GROUP BY / OF / etc. expression"
135class GroupByRole(AllowsLambdaRole, UsesInspection, ByOfRole):
136 __slots__ = ()
137 # note there's a special case right now where you can pass a whole
138 # ORM entity to group_by() and it splits out. we may not want to keep
139 # this around
141 _role_name = "GROUP BY expression"
144class OrderByRole(AllowsLambdaRole, ByOfRole):
145 __slots__ = ()
146 _role_name = "ORDER BY expression"
149class StructuralRole(SQLRole):
150 __slots__ = ()
153class StatementOptionRole(StructuralRole):
154 __slots__ = ()
155 _role_name = "statement sub-expression element"
158class OnClauseRole(AllowsLambdaRole, StructuralRole):
159 __slots__ = ()
160 _role_name = (
161 "ON clause, typically a SQL expression or "
162 "ORM relationship attribute"
163 )
166class WhereHavingRole(OnClauseRole):
167 __slots__ = ()
168 _role_name = "SQL expression for WHERE/HAVING role"
171class ExpressionElementRole(TypedColumnsClauseRole[_T_co]):
172 # note when using generics for ExpressionElementRole,
173 # the generic type needs to be in
174 # sqlalchemy.sql.coercions._impl_lookup mapping also.
175 # these are set up for basic types like int, bool, str, float
176 # right now
178 __slots__ = ()
179 _role_name = "SQL expression element"
181 def label(self, name: Optional[str]) -> Label[_T]:
182 raise NotImplementedError()
185class ConstExprRole(ExpressionElementRole[_T]):
186 __slots__ = ()
187 _role_name = "Constant True/False/None expression"
190class LabeledColumnExprRole(ExpressionElementRole[_T]):
191 __slots__ = ()
194class BinaryElementRole(ExpressionElementRole[_T]):
195 __slots__ = ()
196 _role_name = "SQL expression element or literal value"
199class InElementRole(SQLRole):
200 __slots__ = ()
201 _role_name = (
202 "IN expression list, SELECT construct, or bound parameter object"
203 )
206class JoinTargetRole(AllowsLambdaRole, UsesInspection, StructuralRole):
207 __slots__ = ()
208 _role_name = (
209 "Join target, typically a FROM expression, or ORM "
210 "relationship attribute"
211 )
214class FromClauseRole(ColumnsClauseRole, JoinTargetRole):
215 __slots__ = ()
216 _role_name = "FROM expression, such as a Table or alias() object"
218 _is_subquery = False
220 named_with_column: bool
223class AnonymizedFromClauseRole(FromClauseRole):
224 __slots__ = ()
226 if TYPE_CHECKING:
228 def _anonymous_fromclause(
229 self, *, name: Optional[str] = None, flat: bool = False
230 ) -> FromClause: ...
233class ReturnsRowsRole(SQLRole):
234 __slots__ = ()
235 _role_name = (
236 "Row returning expression such as a SELECT, a FROM clause, or an "
237 "INSERT/UPDATE/DELETE with RETURNING"
238 )
241class StatementRole(SQLRole):
242 __slots__ = ()
243 _role_name = "Executable SQL or text() construct"
245 if TYPE_CHECKING:
247 @util.memoized_property
248 def _propagate_attrs(self) -> _PropagateAttrsType: ...
250 else:
251 _propagate_attrs = util.EMPTY_DICT
254class SelectStatementRole(StatementRole, ReturnsRowsRole):
255 __slots__ = ()
256 _role_name = "SELECT construct or equivalent text() construct"
258 def subquery(self) -> Subquery:
259 raise NotImplementedError(
260 "All SelectStatementRole objects should implement a "
261 ".subquery() method."
262 )
265class HasCTERole(ReturnsRowsRole):
266 __slots__ = ()
269class IsCTERole(SQLRole):
270 __slots__ = ()
271 _role_name = "CTE object"
274class CompoundElementRole(AllowsLambdaRole, SQLRole):
275 """SELECT statements inside a CompoundSelect, e.g. UNION, EXTRACT, etc."""
277 __slots__ = ()
278 _role_name = (
279 "SELECT construct for inclusion in a UNION or other set construct"
280 )
283# TODO: are we using this?
284class DMLRole(StatementRole):
285 __slots__ = ()
288class DMLTableRole(FromClauseRole):
289 __slots__ = ()
290 _role_name = "subject table for an INSERT, UPDATE or DELETE"
293class DMLColumnRole(SQLRole):
294 __slots__ = ()
295 _role_name = "SET/VALUES column expression or string key"
298class DMLSelectRole(SQLRole):
299 """A SELECT statement embedded in DML, typically INSERT from SELECT"""
301 __slots__ = ()
302 _role_name = "SELECT statement or equivalent textual object"
305class DDLRole(StatementRole):
306 __slots__ = ()
309class DDLExpressionRole(StructuralRole):
310 __slots__ = ()
311 _role_name = "SQL expression element for DDL constraint"
314class DDLConstraintColumnRole(SQLRole):
315 __slots__ = ()
316 _role_name = "String column name or column expression for DDL constraint"
319class DDLReferredColumnRole(DDLConstraintColumnRole):
320 __slots__ = ()
321 _role_name = (
322 "String column name or Column object for DDL foreign key constraint"
323 )