expression.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # sql/expression.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. """Defines the public namespace for SQL expression constructs.
  8. Prior to version 0.9, this module contained all of "elements", "dml",
  9. "default_comparator" and "selectable". The module was broken up
  10. and most "factory" functions were moved to be grouped with their associated
  11. class.
  12. """
  13. __all__ = [
  14. "Alias",
  15. "AliasedReturnsRows",
  16. "any_",
  17. "all_",
  18. "CacheKey",
  19. "ClauseElement",
  20. "ColumnCollection",
  21. "ColumnElement",
  22. "CompoundSelect",
  23. "Delete",
  24. "FromClause",
  25. "Insert",
  26. "Join",
  27. "Lateral",
  28. "LambdaElement",
  29. "StatementLambdaElement",
  30. "Select",
  31. "Selectable",
  32. "TableClause",
  33. "TableValuedAlias",
  34. "Update",
  35. "Values",
  36. "alias",
  37. "and_",
  38. "asc",
  39. "between",
  40. "bindparam",
  41. "case",
  42. "cast",
  43. "column",
  44. "custom_op",
  45. "cte",
  46. "delete",
  47. "desc",
  48. "distinct",
  49. "except_",
  50. "except_all",
  51. "exists",
  52. "extract",
  53. "func",
  54. "modifier",
  55. "collate",
  56. "insert",
  57. "intersect",
  58. "intersect_all",
  59. "join",
  60. "label",
  61. "lateral",
  62. "lambda_stmt",
  63. "literal",
  64. "literal_column",
  65. "not_",
  66. "null",
  67. "nulls_first",
  68. "nulls_last",
  69. "or_",
  70. "outparam",
  71. "outerjoin",
  72. "over",
  73. "select",
  74. "table",
  75. "text",
  76. "tuple_",
  77. "type_coerce",
  78. "quoted_name",
  79. "union",
  80. "union_all",
  81. "update",
  82. "quoted_name",
  83. "within_group",
  84. "Subquery",
  85. "TableSample",
  86. "tablesample",
  87. "values",
  88. ]
  89. from .base import _from_objects
  90. from .base import _select_iterables
  91. from .base import ColumnCollection
  92. from .base import Executable
  93. from .base import PARSE_AUTOCOMMIT
  94. from .dml import Delete
  95. from .dml import Insert
  96. from .dml import Update
  97. from .dml import UpdateBase
  98. from .dml import ValuesBase
  99. from .elements import _truncated_label
  100. from .elements import between
  101. from .elements import BinaryExpression
  102. from .elements import BindParameter
  103. from .elements import BooleanClauseList
  104. from .elements import Case
  105. from .elements import Cast
  106. from .elements import ClauseElement
  107. from .elements import ClauseList
  108. from .elements import collate
  109. from .elements import CollectionAggregate
  110. from .elements import ColumnClause
  111. from .elements import ColumnElement
  112. from .elements import Extract
  113. from .elements import False_
  114. from .elements import FunctionFilter
  115. from .elements import Grouping
  116. from .elements import Label
  117. from .elements import literal
  118. from .elements import literal_column
  119. from .elements import not_
  120. from .elements import Null
  121. from .elements import outparam
  122. from .elements import Over
  123. from .elements import quoted_name
  124. from .elements import ReleaseSavepointClause
  125. from .elements import RollbackToSavepointClause
  126. from .elements import SavepointClause
  127. from .elements import TextClause
  128. from .elements import True_
  129. from .elements import Tuple
  130. from .elements import TypeClause
  131. from .elements import TypeCoerce
  132. from .elements import UnaryExpression
  133. from .elements import WithinGroup
  134. from .functions import func
  135. from .functions import Function
  136. from .functions import FunctionElement
  137. from .functions import modifier
  138. from .lambdas import lambda_stmt
  139. from .lambdas import LambdaElement
  140. from .lambdas import StatementLambdaElement
  141. from .operators import ColumnOperators
  142. from .operators import custom_op
  143. from .operators import Operators
  144. from .selectable import Alias
  145. from .selectable import AliasedReturnsRows
  146. from .selectable import CompoundSelect
  147. from .selectable import CTE
  148. from .selectable import Exists
  149. from .selectable import FromClause
  150. from .selectable import FromGrouping
  151. from .selectable import GenerativeSelect
  152. from .selectable import HasCTE
  153. from .selectable import HasPrefixes
  154. from .selectable import HasSuffixes
  155. from .selectable import Join
  156. from .selectable import LABEL_STYLE_DEFAULT
  157. from .selectable import LABEL_STYLE_DISAMBIGUATE_ONLY
  158. from .selectable import LABEL_STYLE_NONE
  159. from .selectable import LABEL_STYLE_TABLENAME_PLUS_COL
  160. from .selectable import Lateral
  161. from .selectable import ReturnsRows
  162. from .selectable import ScalarSelect
  163. from .selectable import Select
  164. from .selectable import Selectable
  165. from .selectable import SelectBase
  166. from .selectable import Subquery
  167. from .selectable import subquery
  168. from .selectable import TableClause
  169. from .selectable import TableSample
  170. from .selectable import TableValuedAlias
  171. from .selectable import TextAsFrom
  172. from .selectable import TextualSelect
  173. from .selectable import Values
  174. from .traversals import CacheKey
  175. from .visitors import Visitable
  176. from ..util.langhelpers import public_factory
  177. # factory functions - these pull class-bound constructors and classmethods
  178. # from SQL elements and selectables into public functions. This allows
  179. # the functions to be available in the sqlalchemy.sql.* namespace and
  180. # to be auto-cross-documenting from the function to the class itself.
  181. all_ = public_factory(CollectionAggregate._create_all, ".sql.expression.all_")
  182. any_ = public_factory(CollectionAggregate._create_any, ".sql.expression.any_")
  183. and_ = public_factory(BooleanClauseList.and_, ".sql.expression.and_")
  184. alias = public_factory(Alias._factory, ".sql.expression.alias")
  185. tablesample = public_factory(
  186. TableSample._factory, ".sql.expression.tablesample"
  187. )
  188. lateral = public_factory(Lateral._factory, ".sql.expression.lateral")
  189. or_ = public_factory(BooleanClauseList.or_, ".sql.expression.or_")
  190. bindparam = public_factory(BindParameter, ".sql.expression.bindparam")
  191. select = public_factory(Select._create, ".sql.expression.select")
  192. text = public_factory(TextClause._create_text, ".sql.expression.text")
  193. table = public_factory(TableClause, ".sql.expression.table")
  194. column = public_factory(ColumnClause, ".sql.expression.column")
  195. over = public_factory(Over, ".sql.expression.over")
  196. within_group = public_factory(WithinGroup, ".sql.expression.within_group")
  197. label = public_factory(Label, ".sql.expression.label")
  198. case = public_factory(Case, ".sql.expression.case")
  199. cast = public_factory(Cast, ".sql.expression.cast")
  200. cte = public_factory(CTE._factory, ".sql.expression.cte")
  201. values = public_factory(Values, ".sql.expression.values")
  202. extract = public_factory(Extract, ".sql.expression.extract")
  203. tuple_ = public_factory(Tuple, ".sql.expression.tuple_")
  204. except_ = public_factory(
  205. CompoundSelect._create_except, ".sql.expression.except_"
  206. )
  207. except_all = public_factory(
  208. CompoundSelect._create_except_all, ".sql.expression.except_all"
  209. )
  210. intersect = public_factory(
  211. CompoundSelect._create_intersect, ".sql.expression.intersect"
  212. )
  213. intersect_all = public_factory(
  214. CompoundSelect._create_intersect_all, ".sql.expression.intersect_all"
  215. )
  216. union = public_factory(CompoundSelect._create_union, ".sql.expression.union")
  217. union_all = public_factory(
  218. CompoundSelect._create_union_all, ".sql.expression.union_all"
  219. )
  220. exists = public_factory(Exists, ".sql.expression.exists")
  221. nulls_first = public_factory(
  222. UnaryExpression._create_nulls_first, ".sql.expression.nulls_first"
  223. )
  224. nullsfirst = nulls_first # deprecated 1.4; see #5435
  225. nulls_last = public_factory(
  226. UnaryExpression._create_nulls_last, ".sql.expression.nulls_last"
  227. )
  228. nullslast = nulls_last # deprecated 1.4; see #5435
  229. asc = public_factory(UnaryExpression._create_asc, ".sql.expression.asc")
  230. desc = public_factory(UnaryExpression._create_desc, ".sql.expression.desc")
  231. distinct = public_factory(
  232. UnaryExpression._create_distinct, ".sql.expression.distinct"
  233. )
  234. type_coerce = public_factory(TypeCoerce, ".sql.expression.type_coerce")
  235. true = public_factory(True_._instance, ".sql.expression.true")
  236. false = public_factory(False_._instance, ".sql.expression.false")
  237. null = public_factory(Null._instance, ".sql.expression.null")
  238. join = public_factory(Join._create_join, ".sql.expression.join")
  239. outerjoin = public_factory(Join._create_outerjoin, ".sql.expression.outerjoin")
  240. insert = public_factory(Insert, ".sql.expression.insert")
  241. update = public_factory(Update, ".sql.expression.update")
  242. delete = public_factory(Delete, ".sql.expression.delete")
  243. funcfilter = public_factory(FunctionFilter, ".sql.expression.funcfilter")
  244. # internal functions still being called from tests and the ORM,
  245. # these might be better off in some other namespace
  246. # old names for compatibility
  247. _Executable = Executable
  248. _BindParamClause = BindParameter
  249. _Label = Label
  250. _SelectBase = SelectBase
  251. _BinaryExpression = BinaryExpression
  252. _Cast = Cast
  253. _Null = Null
  254. _False = False_
  255. _True = True_
  256. _TextClause = TextClause
  257. _UnaryExpression = UnaryExpression
  258. _Case = Case
  259. _Tuple = Tuple
  260. _Over = Over
  261. _TypeClause = TypeClause
  262. _Extract = Extract
  263. _Exists = Exists
  264. _Grouping = Grouping
  265. _FromGrouping = FromGrouping
  266. _ScalarSelect = ScalarSelect