roles.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # sql/roles.py
  2. # Copyright (C) 2005-2022 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
  7. from .. import util
  8. class SQLRole(object):
  9. """Define a "role" within a SQL statement structure.
  10. Classes within SQL Core participate within SQLRole hierarchies in order
  11. to more accurately indicate where they may be used within SQL statements
  12. of all types.
  13. .. versionadded:: 1.4
  14. """
  15. allows_lambda = False
  16. uses_inspection = False
  17. class UsesInspection(object):
  18. _post_inspect = None
  19. uses_inspection = True
  20. class AllowsLambdaRole(object):
  21. allows_lambda = True
  22. class HasCacheKeyRole(SQLRole):
  23. _role_name = "Cacheable Core or ORM object"
  24. class ExecutableOptionRole(SQLRole):
  25. __slots__ = ()
  26. _role_name = "ExecutionOption Core or ORM object"
  27. class LiteralValueRole(SQLRole):
  28. _role_name = "Literal Python value"
  29. class ColumnArgumentRole(SQLRole):
  30. _role_name = "Column expression"
  31. class ColumnArgumentOrKeyRole(ColumnArgumentRole):
  32. _role_name = "Column expression or string key"
  33. class StrAsPlainColumnRole(ColumnArgumentRole):
  34. _role_name = "Column expression or string key"
  35. class ColumnListRole(SQLRole):
  36. """Elements suitable for forming comma separated lists of expressions."""
  37. class TruncatedLabelRole(SQLRole):
  38. _role_name = "String SQL identifier"
  39. class ColumnsClauseRole(AllowsLambdaRole, UsesInspection, ColumnListRole):
  40. _role_name = "Column expression or FROM clause"
  41. @property
  42. def _select_iterable(self):
  43. raise NotImplementedError()
  44. class LimitOffsetRole(SQLRole):
  45. _role_name = "LIMIT / OFFSET expression"
  46. class ByOfRole(ColumnListRole):
  47. _role_name = "GROUP BY / OF / etc. expression"
  48. class GroupByRole(AllowsLambdaRole, UsesInspection, ByOfRole):
  49. # note there's a special case right now where you can pass a whole
  50. # ORM entity to group_by() and it splits out. we may not want to keep
  51. # this around
  52. _role_name = "GROUP BY expression"
  53. class OrderByRole(AllowsLambdaRole, ByOfRole):
  54. _role_name = "ORDER BY expression"
  55. class StructuralRole(SQLRole):
  56. pass
  57. class StatementOptionRole(StructuralRole):
  58. _role_name = "statement sub-expression element"
  59. class OnClauseRole(AllowsLambdaRole, StructuralRole):
  60. _role_name = "SQL expression for ON clause"
  61. class WhereHavingRole(OnClauseRole):
  62. _role_name = "SQL expression for WHERE/HAVING role"
  63. class ExpressionElementRole(SQLRole):
  64. _role_name = "SQL expression element"
  65. class ConstExprRole(ExpressionElementRole):
  66. _role_name = "Constant True/False/None expression"
  67. class LabeledColumnExprRole(ExpressionElementRole):
  68. pass
  69. class BinaryElementRole(ExpressionElementRole):
  70. _role_name = "SQL expression element or literal value"
  71. class InElementRole(SQLRole):
  72. _role_name = (
  73. "IN expression list, SELECT construct, or bound parameter object"
  74. )
  75. class JoinTargetRole(AllowsLambdaRole, UsesInspection, StructuralRole):
  76. _role_name = (
  77. "Join target, typically a FROM expression, or ORM "
  78. "relationship attribute"
  79. )
  80. class FromClauseRole(ColumnsClauseRole, JoinTargetRole):
  81. _role_name = "FROM expression, such as a Table or alias() object"
  82. _is_subquery = False
  83. @property
  84. def _hide_froms(self):
  85. raise NotImplementedError()
  86. class StrictFromClauseRole(FromClauseRole):
  87. # does not allow text() or select() objects
  88. @property
  89. def description(self):
  90. raise NotImplementedError()
  91. class AnonymizedFromClauseRole(StrictFromClauseRole):
  92. # calls .alias() as a post processor
  93. def _anonymous_fromclause(self, name=None, flat=False):
  94. raise NotImplementedError()
  95. class ReturnsRowsRole(SQLRole):
  96. _role_name = (
  97. "Row returning expression such as a SELECT, a FROM clause, or an "
  98. "INSERT/UPDATE/DELETE with RETURNING"
  99. )
  100. class StatementRole(SQLRole):
  101. _role_name = "Executable SQL or text() construct"
  102. _propagate_attrs = util.immutabledict()
  103. class SelectStatementRole(StatementRole, ReturnsRowsRole):
  104. _role_name = "SELECT construct or equivalent text() construct"
  105. def subquery(self):
  106. raise NotImplementedError(
  107. "All SelectStatementRole objects should implement a "
  108. ".subquery() method."
  109. )
  110. class HasCTERole(ReturnsRowsRole):
  111. pass
  112. class IsCTERole(SQLRole):
  113. _role_name = "CTE object"
  114. class CompoundElementRole(AllowsLambdaRole, SQLRole):
  115. """SELECT statements inside a CompoundSelect, e.g. UNION, EXTRACT, etc."""
  116. _role_name = (
  117. "SELECT construct for inclusion in a UNION or other set construct"
  118. )
  119. # TODO: are we using this?
  120. class DMLRole(StatementRole):
  121. pass
  122. class DMLTableRole(FromClauseRole):
  123. _role_name = "subject table for an INSERT, UPDATE or DELETE"
  124. class DMLColumnRole(SQLRole):
  125. _role_name = "SET/VALUES column expression or string key"
  126. class DMLSelectRole(SQLRole):
  127. """A SELECT statement embedded in DML, typically INSERT from SELECT"""
  128. _role_name = "SELECT statement or equivalent textual object"
  129. class DDLRole(StatementRole):
  130. pass
  131. class DDLExpressionRole(StructuralRole):
  132. _role_name = "SQL expression element for DDL constraint"
  133. class DDLConstraintColumnRole(SQLRole):
  134. _role_name = "String column name or column expression for DDL constraint"
  135. class DDLReferredColumnRole(DDLConstraintColumnRole):
  136. _role_name = (
  137. "String column name or Column object for DDL foreign key constraint"
  138. )