Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/roles.py: 93%
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-2024 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 Optional
12from typing import TYPE_CHECKING
13from typing import TypeVar
15from .. import util
16from ..util.typing import Literal
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 UsesInspection:
46 __slots__ = ()
47 _post_inspect: Literal[None] = None
48 uses_inspection = True
51class AllowsLambdaRole:
52 __slots__ = ()
53 allows_lambda = True
56class HasCacheKeyRole(SQLRole):
57 __slots__ = ()
58 _role_name = "Cacheable Core or ORM object"
61class ExecutableOptionRole(SQLRole):
62 __slots__ = ()
63 _role_name = "ExecutionOption Core or ORM object"
66class LiteralValueRole(SQLRole):
67 __slots__ = ()
68 _role_name = "Literal Python value"
71class ColumnArgumentRole(SQLRole):
72 __slots__ = ()
73 _role_name = "Column expression"
76class ColumnArgumentOrKeyRole(ColumnArgumentRole):
77 __slots__ = ()
78 _role_name = "Column expression or string key"
81class StrAsPlainColumnRole(ColumnArgumentRole):
82 __slots__ = ()
83 _role_name = "Column expression or string key"
86class ColumnListRole(SQLRole):
87 """Elements suitable for forming comma separated lists of expressions."""
89 __slots__ = ()
92class StringRole(SQLRole):
93 """mixin indicating a role that results in strings"""
95 __slots__ = ()
98class TruncatedLabelRole(StringRole, SQLRole):
99 __slots__ = ()
100 _role_name = "String SQL identifier"
103class ColumnsClauseRole(AllowsLambdaRole, UsesInspection, ColumnListRole):
104 __slots__ = ()
105 _role_name = (
106 "Column expression, FROM clause, or other columns clause element"
107 )
109 @property
110 def _select_iterable(self) -> _SelectIterable:
111 raise NotImplementedError()
114class TypedColumnsClauseRole(Generic[_T_co], SQLRole):
115 """element-typed form of ColumnsClauseRole"""
117 __slots__ = ()
120class LimitOffsetRole(SQLRole):
121 __slots__ = ()
122 _role_name = "LIMIT / OFFSET expression"
125class ByOfRole(ColumnListRole):
126 __slots__ = ()
127 _role_name = "GROUP BY / OF / etc. expression"
130class GroupByRole(AllowsLambdaRole, UsesInspection, ByOfRole):
131 __slots__ = ()
132 # note there's a special case right now where you can pass a whole
133 # ORM entity to group_by() and it splits out. we may not want to keep
134 # this around
136 _role_name = "GROUP BY expression"
139class OrderByRole(AllowsLambdaRole, ByOfRole):
140 __slots__ = ()
141 _role_name = "ORDER BY expression"
144class StructuralRole(SQLRole):
145 __slots__ = ()
148class StatementOptionRole(StructuralRole):
149 __slots__ = ()
150 _role_name = "statement sub-expression element"
153class OnClauseRole(AllowsLambdaRole, StructuralRole):
154 __slots__ = ()
155 _role_name = (
156 "ON clause, typically a SQL expression or "
157 "ORM relationship attribute"
158 )
161class WhereHavingRole(OnClauseRole):
162 __slots__ = ()
163 _role_name = "SQL expression for WHERE/HAVING role"
166class ExpressionElementRole(TypedColumnsClauseRole[_T_co]):
167 # note when using generics for ExpressionElementRole,
168 # the generic type needs to be in
169 # sqlalchemy.sql.coercions._impl_lookup mapping also.
170 # these are set up for basic types like int, bool, str, float
171 # right now
173 __slots__ = ()
174 _role_name = "SQL expression element"
176 def label(self, name: Optional[str]) -> Label[_T]:
177 raise NotImplementedError()
180class ConstExprRole(ExpressionElementRole[_T]):
181 __slots__ = ()
182 _role_name = "Constant True/False/None expression"
185class LabeledColumnExprRole(ExpressionElementRole[_T]):
186 __slots__ = ()
189class BinaryElementRole(ExpressionElementRole[_T]):
190 __slots__ = ()
191 _role_name = "SQL expression element or literal value"
194class InElementRole(SQLRole):
195 __slots__ = ()
196 _role_name = (
197 "IN expression list, SELECT construct, or bound parameter object"
198 )
201class JoinTargetRole(AllowsLambdaRole, UsesInspection, StructuralRole):
202 __slots__ = ()
203 _role_name = (
204 "Join target, typically a FROM expression, or ORM "
205 "relationship attribute"
206 )
209class FromClauseRole(ColumnsClauseRole, JoinTargetRole):
210 __slots__ = ()
211 _role_name = "FROM expression, such as a Table or alias() object"
213 _is_subquery = False
215 named_with_column: bool
218class StrictFromClauseRole(FromClauseRole):
219 __slots__ = ()
220 # does not allow text() or select() objects
223class AnonymizedFromClauseRole(StrictFromClauseRole):
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 )