You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
5.3KB

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