coercions.py 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. # sql/coercions.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. import numbers
  8. import re
  9. from . import operators
  10. from . import roles
  11. from . import visitors
  12. from .base import ExecutableOption
  13. from .base import Options
  14. from .traversals import HasCacheKey
  15. from .visitors import Visitable
  16. from .. import exc
  17. from .. import inspection
  18. from .. import util
  19. from ..util import collections_abc
  20. elements = None
  21. lambdas = None
  22. schema = None
  23. selectable = None
  24. sqltypes = None
  25. traversals = None
  26. def _is_literal(element):
  27. """Return whether or not the element is a "literal" in the context
  28. of a SQL expression construct.
  29. """
  30. return (
  31. not isinstance(
  32. element,
  33. (Visitable, schema.SchemaEventTarget),
  34. )
  35. and not hasattr(element, "__clause_element__")
  36. )
  37. def _deep_is_literal(element):
  38. """Return whether or not the element is a "literal" in the context
  39. of a SQL expression construct.
  40. does a deeper more esoteric check than _is_literal. is used
  41. for lambda elements that have to distinguish values that would
  42. be bound vs. not without any context.
  43. """
  44. if isinstance(element, collections_abc.Sequence) and not isinstance(
  45. element, str
  46. ):
  47. for elem in element:
  48. if not _deep_is_literal(elem):
  49. return False
  50. else:
  51. return True
  52. return (
  53. not isinstance(
  54. element,
  55. (
  56. Visitable,
  57. schema.SchemaEventTarget,
  58. HasCacheKey,
  59. Options,
  60. util.langhelpers._symbol,
  61. ),
  62. )
  63. and not hasattr(element, "__clause_element__")
  64. and (
  65. not isinstance(element, type)
  66. or not issubclass(element, HasCacheKey)
  67. )
  68. )
  69. def _document_text_coercion(paramname, meth_rst, param_rst):
  70. return util.add_parameter_text(
  71. paramname,
  72. (
  73. ".. warning:: "
  74. "The %s argument to %s can be passed as a Python string argument, "
  75. "which will be treated "
  76. "as **trusted SQL text** and rendered as given. **DO NOT PASS "
  77. "UNTRUSTED INPUT TO THIS PARAMETER**."
  78. )
  79. % (param_rst, meth_rst),
  80. )
  81. def _expression_collection_was_a_list(attrname, fnname, args):
  82. if args and isinstance(args[0], (list, set, dict)) and len(args) == 1:
  83. if isinstance(args[0], list):
  84. util.warn_deprecated_20(
  85. 'The "%s" argument to %s(), when referring to a sequence '
  86. "of items, is now passed as a series of positional "
  87. "elements, rather than as a list. " % (attrname, fnname)
  88. )
  89. return args[0]
  90. else:
  91. return args
  92. def expect(
  93. role,
  94. element,
  95. apply_propagate_attrs=None,
  96. argname=None,
  97. post_inspect=False,
  98. **kw
  99. ):
  100. if (
  101. role.allows_lambda
  102. # note callable() will not invoke a __getattr__() method, whereas
  103. # hasattr(obj, "__call__") will. by keeping the callable() check here
  104. # we prevent most needless calls to hasattr() and therefore
  105. # __getattr__(), which is present on ColumnElement.
  106. and callable(element)
  107. and hasattr(element, "__code__")
  108. ):
  109. return lambdas.LambdaElement(
  110. element,
  111. role,
  112. lambdas.LambdaOptions(**kw),
  113. apply_propagate_attrs=apply_propagate_attrs,
  114. )
  115. # major case is that we are given a ClauseElement already, skip more
  116. # elaborate logic up front if possible
  117. impl = _impl_lookup[role]
  118. original_element = element
  119. if not isinstance(
  120. element,
  121. (elements.ClauseElement, schema.SchemaItem, schema.FetchedValue),
  122. ):
  123. resolved = None
  124. if impl._resolve_literal_only:
  125. resolved = impl._literal_coercion(element, **kw)
  126. else:
  127. original_element = element
  128. is_clause_element = False
  129. # this is a special performance optimization for ORM
  130. # joins used by JoinTargetImpl that we don't go through the
  131. # work of creating __clause_element__() when we only need the
  132. # original QueryableAttribute, as the former will do clause
  133. # adaption and all that which is just thrown away here.
  134. if (
  135. impl._skip_clauseelement_for_target_match
  136. and isinstance(element, role)
  137. and hasattr(element, "__clause_element__")
  138. ):
  139. is_clause_element = True
  140. else:
  141. while hasattr(element, "__clause_element__"):
  142. is_clause_element = True
  143. if not getattr(element, "is_clause_element", False):
  144. element = element.__clause_element__()
  145. else:
  146. break
  147. if not is_clause_element:
  148. if impl._use_inspection:
  149. insp = inspection.inspect(element, raiseerr=False)
  150. if insp is not None:
  151. if post_inspect:
  152. insp._post_inspect
  153. try:
  154. resolved = insp.__clause_element__()
  155. except AttributeError:
  156. impl._raise_for_expected(original_element, argname)
  157. if resolved is None:
  158. resolved = impl._literal_coercion(
  159. element, argname=argname, **kw
  160. )
  161. else:
  162. resolved = element
  163. else:
  164. resolved = element
  165. if (
  166. apply_propagate_attrs is not None
  167. and not apply_propagate_attrs._propagate_attrs
  168. and resolved._propagate_attrs
  169. ):
  170. apply_propagate_attrs._propagate_attrs = resolved._propagate_attrs
  171. if impl._role_class in resolved.__class__.__mro__:
  172. if impl._post_coercion:
  173. resolved = impl._post_coercion(
  174. resolved,
  175. argname=argname,
  176. original_element=original_element,
  177. **kw
  178. )
  179. return resolved
  180. else:
  181. return impl._implicit_coercions(
  182. original_element, resolved, argname=argname, **kw
  183. )
  184. def expect_as_key(role, element, **kw):
  185. kw["as_key"] = True
  186. return expect(role, element, **kw)
  187. def expect_col_expression_collection(role, expressions):
  188. for expr in expressions:
  189. strname = None
  190. column = None
  191. resolved = expect(role, expr)
  192. if isinstance(resolved, util.string_types):
  193. strname = resolved = expr
  194. else:
  195. cols = []
  196. visitors.traverse(resolved, {}, {"column": cols.append})
  197. if cols:
  198. column = cols[0]
  199. add_element = column if column is not None else strname
  200. yield resolved, column, strname, add_element
  201. class RoleImpl(object):
  202. __slots__ = ("_role_class", "name", "_use_inspection")
  203. def _literal_coercion(self, element, **kw):
  204. raise NotImplementedError()
  205. _post_coercion = None
  206. _resolve_literal_only = False
  207. _skip_clauseelement_for_target_match = False
  208. def __init__(self, role_class):
  209. self._role_class = role_class
  210. self.name = role_class._role_name
  211. self._use_inspection = issubclass(role_class, roles.UsesInspection)
  212. def _implicit_coercions(self, element, resolved, argname=None, **kw):
  213. self._raise_for_expected(element, argname, resolved)
  214. def _raise_for_expected(
  215. self,
  216. element,
  217. argname=None,
  218. resolved=None,
  219. advice=None,
  220. code=None,
  221. err=None,
  222. ):
  223. if resolved is not None and resolved is not element:
  224. got = "%r object resolved from %r object" % (resolved, element)
  225. else:
  226. got = repr(element)
  227. if argname:
  228. msg = "%s expected for argument %r; got %s." % (
  229. self.name,
  230. argname,
  231. got,
  232. )
  233. else:
  234. msg = "%s expected, got %s." % (self.name, got)
  235. if advice:
  236. msg += " " + advice
  237. util.raise_(exc.ArgumentError(msg, code=code), replace_context=err)
  238. class _Deannotate(object):
  239. __slots__ = ()
  240. def _post_coercion(self, resolved, **kw):
  241. from .util import _deep_deannotate
  242. return _deep_deannotate(resolved)
  243. class _StringOnly(object):
  244. __slots__ = ()
  245. _resolve_literal_only = True
  246. class _ReturnsStringKey(object):
  247. __slots__ = ()
  248. def _implicit_coercions(
  249. self, original_element, resolved, argname=None, **kw
  250. ):
  251. if isinstance(original_element, util.string_types):
  252. return original_element
  253. else:
  254. self._raise_for_expected(original_element, argname, resolved)
  255. def _literal_coercion(self, element, **kw):
  256. return element
  257. class _ColumnCoercions(object):
  258. __slots__ = ()
  259. def _warn_for_scalar_subquery_coercion(self):
  260. util.warn(
  261. "implicitly coercing SELECT object to scalar subquery; "
  262. "please use the .scalar_subquery() method to produce a scalar "
  263. "subquery.",
  264. )
  265. def _implicit_coercions(
  266. self, original_element, resolved, argname=None, **kw
  267. ):
  268. if not getattr(resolved, "is_clause_element", False):
  269. self._raise_for_expected(original_element, argname, resolved)
  270. elif resolved._is_select_statement:
  271. self._warn_for_scalar_subquery_coercion()
  272. return resolved.scalar_subquery()
  273. elif resolved._is_from_clause and isinstance(
  274. resolved, selectable.Subquery
  275. ):
  276. self._warn_for_scalar_subquery_coercion()
  277. return resolved.element.scalar_subquery()
  278. elif self._role_class.allows_lambda and resolved._is_lambda_element:
  279. return resolved
  280. else:
  281. self._raise_for_expected(original_element, argname, resolved)
  282. def _no_text_coercion(
  283. element, argname=None, exc_cls=exc.ArgumentError, extra=None, err=None
  284. ):
  285. util.raise_(
  286. exc_cls(
  287. "%(extra)sTextual SQL expression %(expr)r %(argname)sshould be "
  288. "explicitly declared as text(%(expr)r)"
  289. % {
  290. "expr": util.ellipses_string(element),
  291. "argname": "for argument %s" % (argname,) if argname else "",
  292. "extra": "%s " % extra if extra else "",
  293. }
  294. ),
  295. replace_context=err,
  296. )
  297. class _NoTextCoercion(object):
  298. __slots__ = ()
  299. def _literal_coercion(self, element, argname=None, **kw):
  300. if isinstance(element, util.string_types) and issubclass(
  301. elements.TextClause, self._role_class
  302. ):
  303. _no_text_coercion(element, argname)
  304. else:
  305. self._raise_for_expected(element, argname)
  306. class _CoerceLiterals(object):
  307. __slots__ = ()
  308. _coerce_consts = False
  309. _coerce_star = False
  310. _coerce_numerics = False
  311. def _text_coercion(self, element, argname=None):
  312. return _no_text_coercion(element, argname)
  313. def _literal_coercion(self, element, argname=None, **kw):
  314. if isinstance(element, util.string_types):
  315. if self._coerce_star and element == "*":
  316. return elements.ColumnClause("*", is_literal=True)
  317. else:
  318. return self._text_coercion(element, argname, **kw)
  319. if self._coerce_consts:
  320. if element is None:
  321. return elements.Null()
  322. elif element is False:
  323. return elements.False_()
  324. elif element is True:
  325. return elements.True_()
  326. if self._coerce_numerics and isinstance(element, (numbers.Number)):
  327. return elements.ColumnClause(str(element), is_literal=True)
  328. self._raise_for_expected(element, argname)
  329. class LiteralValueImpl(RoleImpl):
  330. _resolve_literal_only = True
  331. def _implicit_coercions(
  332. self, element, resolved, argname, type_=None, **kw
  333. ):
  334. if not _is_literal(resolved):
  335. self._raise_for_expected(
  336. element, resolved=resolved, argname=argname, **kw
  337. )
  338. return elements.BindParameter(None, element, type_=type_, unique=True)
  339. def _literal_coercion(self, element, argname=None, type_=None, **kw):
  340. return element
  341. class _SelectIsNotFrom(object):
  342. __slots__ = ()
  343. def _raise_for_expected(self, element, argname=None, resolved=None, **kw):
  344. if isinstance(element, roles.SelectStatementRole) or isinstance(
  345. resolved, roles.SelectStatementRole
  346. ):
  347. advice = (
  348. "To create a "
  349. "FROM clause from a %s object, use the .subquery() method."
  350. % (resolved.__class__ if resolved is not None else element,)
  351. )
  352. code = "89ve"
  353. else:
  354. advice = code = None
  355. return super(_SelectIsNotFrom, self)._raise_for_expected(
  356. element,
  357. argname=argname,
  358. resolved=resolved,
  359. advice=advice,
  360. code=code,
  361. **kw
  362. )
  363. class HasCacheKeyImpl(RoleImpl):
  364. __slots__ = ()
  365. def _implicit_coercions(
  366. self, original_element, resolved, argname=None, **kw
  367. ):
  368. if isinstance(original_element, traversals.HasCacheKey):
  369. return original_element
  370. else:
  371. self._raise_for_expected(original_element, argname, resolved)
  372. def _literal_coercion(self, element, **kw):
  373. return element
  374. class ExecutableOptionImpl(RoleImpl):
  375. __slots__ = ()
  376. def _implicit_coercions(
  377. self, original_element, resolved, argname=None, **kw
  378. ):
  379. if isinstance(original_element, ExecutableOption):
  380. return original_element
  381. else:
  382. self._raise_for_expected(original_element, argname, resolved)
  383. def _literal_coercion(self, element, **kw):
  384. return element
  385. class ExpressionElementImpl(_ColumnCoercions, RoleImpl):
  386. __slots__ = ()
  387. def _literal_coercion(
  388. self, element, name=None, type_=None, argname=None, is_crud=False, **kw
  389. ):
  390. if (
  391. element is None
  392. and not is_crud
  393. and (type_ is None or not type_.should_evaluate_none)
  394. ):
  395. # TODO: there's no test coverage now for the
  396. # "should_evaluate_none" part of this, as outside of "crud" this
  397. # codepath is not normally used except in some special cases
  398. return elements.Null()
  399. else:
  400. try:
  401. return elements.BindParameter(
  402. name, element, type_, unique=True, _is_crud=is_crud
  403. )
  404. except exc.ArgumentError as err:
  405. self._raise_for_expected(element, err=err)
  406. def _raise_for_expected(self, element, argname=None, resolved=None, **kw):
  407. if isinstance(element, roles.AnonymizedFromClauseRole):
  408. advice = (
  409. "To create a "
  410. "column expression from a FROM clause row "
  411. "as a whole, use the .table_valued() method."
  412. )
  413. else:
  414. advice = None
  415. return super(ExpressionElementImpl, self)._raise_for_expected(
  416. element, argname=argname, resolved=resolved, advice=advice, **kw
  417. )
  418. class BinaryElementImpl(ExpressionElementImpl, RoleImpl):
  419. __slots__ = ()
  420. def _literal_coercion(
  421. self, element, expr, operator, bindparam_type=None, argname=None, **kw
  422. ):
  423. try:
  424. return expr._bind_param(operator, element, type_=bindparam_type)
  425. except exc.ArgumentError as err:
  426. self._raise_for_expected(element, err=err)
  427. def _post_coercion(self, resolved, expr, **kw):
  428. if resolved.type._isnull and not expr.type._isnull:
  429. resolved = resolved._with_binary_element_type(expr.type)
  430. return resolved
  431. class InElementImpl(RoleImpl):
  432. __slots__ = ()
  433. def _implicit_coercions(
  434. self, original_element, resolved, argname=None, **kw
  435. ):
  436. if resolved._is_from_clause:
  437. if (
  438. isinstance(resolved, selectable.Alias)
  439. and resolved.element._is_select_statement
  440. ):
  441. self._warn_for_implicit_coercion(resolved)
  442. return self._post_coercion(resolved.element, **kw)
  443. else:
  444. self._warn_for_implicit_coercion(resolved)
  445. return self._post_coercion(resolved.select(), **kw)
  446. else:
  447. self._raise_for_expected(original_element, argname, resolved)
  448. def _warn_for_implicit_coercion(self, elem):
  449. util.warn(
  450. "Coercing %s object into a select() for use in IN(); "
  451. "please pass a select() construct explicitly"
  452. % (elem.__class__.__name__)
  453. )
  454. def _literal_coercion(self, element, expr, operator, **kw):
  455. if isinstance(element, collections_abc.Iterable) and not isinstance(
  456. element, util.string_types
  457. ):
  458. non_literal_expressions = {}
  459. element = list(element)
  460. for o in element:
  461. if not _is_literal(o):
  462. if not isinstance(o, operators.ColumnOperators):
  463. self._raise_for_expected(element, **kw)
  464. else:
  465. non_literal_expressions[o] = o
  466. elif o is None:
  467. non_literal_expressions[o] = elements.Null()
  468. if non_literal_expressions:
  469. return elements.ClauseList(
  470. *[
  471. non_literal_expressions[o]
  472. if o in non_literal_expressions
  473. else expr._bind_param(operator, o)
  474. for o in element
  475. ]
  476. )
  477. else:
  478. return expr._bind_param(operator, element, expanding=True)
  479. else:
  480. self._raise_for_expected(element, **kw)
  481. def _post_coercion(self, element, expr, operator, **kw):
  482. if element._is_select_statement:
  483. # for IN, we are doing scalar_subquery() coercion without
  484. # a warning
  485. return element.scalar_subquery()
  486. elif isinstance(element, elements.ClauseList):
  487. assert not len(element.clauses) == 0
  488. return element.self_group(against=operator)
  489. elif isinstance(element, elements.BindParameter):
  490. element = element._clone(maintain_key=True)
  491. element.expanding = True
  492. element.expand_op = operator
  493. return element
  494. else:
  495. return element
  496. class OnClauseImpl(_CoerceLiterals, _ColumnCoercions, RoleImpl):
  497. __slots__ = ()
  498. _coerce_consts = True
  499. def _implicit_coercions(
  500. self, original_element, resolved, argname=None, legacy=False, **kw
  501. ):
  502. if legacy and isinstance(resolved, str):
  503. return resolved
  504. else:
  505. return super(OnClauseImpl, self)._implicit_coercions(
  506. original_element,
  507. resolved,
  508. argname=argname,
  509. legacy=legacy,
  510. **kw
  511. )
  512. def _text_coercion(self, element, argname=None, legacy=False):
  513. if legacy and isinstance(element, str):
  514. util.warn_deprecated_20(
  515. "Using strings to indicate relationship names in "
  516. "Query.join() is deprecated and will be removed in "
  517. "SQLAlchemy 2.0. Please use the class-bound attribute "
  518. "directly."
  519. )
  520. return element
  521. return super(OnClauseImpl, self)._text_coercion(element, argname)
  522. def _post_coercion(self, resolved, original_element=None, **kw):
  523. # this is a hack right now as we want to use coercion on an
  524. # ORM InstrumentedAttribute, but we want to return the object
  525. # itself if it is one, not its clause element.
  526. # ORM context _join and _legacy_join() would need to be improved
  527. # to look for annotations in a clause element form.
  528. if isinstance(original_element, roles.JoinTargetRole):
  529. return original_element
  530. return resolved
  531. class WhereHavingImpl(_CoerceLiterals, _ColumnCoercions, RoleImpl):
  532. __slots__ = ()
  533. _coerce_consts = True
  534. def _text_coercion(self, element, argname=None):
  535. return _no_text_coercion(element, argname)
  536. class StatementOptionImpl(_CoerceLiterals, RoleImpl):
  537. __slots__ = ()
  538. _coerce_consts = True
  539. def _text_coercion(self, element, argname=None):
  540. return elements.TextClause(element)
  541. class ColumnArgumentImpl(_NoTextCoercion, RoleImpl):
  542. __slots__ = ()
  543. class ColumnArgumentOrKeyImpl(_ReturnsStringKey, RoleImpl):
  544. __slots__ = ()
  545. class StrAsPlainColumnImpl(_CoerceLiterals, RoleImpl):
  546. __slots__ = ()
  547. def _text_coercion(self, element, argname=None):
  548. return elements.ColumnClause(element)
  549. class ByOfImpl(_CoerceLiterals, _ColumnCoercions, RoleImpl, roles.ByOfRole):
  550. __slots__ = ()
  551. _coerce_consts = True
  552. def _text_coercion(self, element, argname=None):
  553. return elements._textual_label_reference(element)
  554. class OrderByImpl(ByOfImpl, RoleImpl):
  555. __slots__ = ()
  556. def _post_coercion(self, resolved, **kw):
  557. if (
  558. isinstance(resolved, self._role_class)
  559. and resolved._order_by_label_element is not None
  560. ):
  561. return elements._label_reference(resolved)
  562. else:
  563. return resolved
  564. class GroupByImpl(ByOfImpl, RoleImpl):
  565. __slots__ = ()
  566. def _implicit_coercions(
  567. self, original_element, resolved, argname=None, **kw
  568. ):
  569. if isinstance(resolved, roles.StrictFromClauseRole):
  570. return elements.ClauseList(*resolved.c)
  571. else:
  572. return resolved
  573. class DMLColumnImpl(_ReturnsStringKey, RoleImpl):
  574. __slots__ = ()
  575. def _post_coercion(self, element, as_key=False, **kw):
  576. if as_key:
  577. return element.key
  578. else:
  579. return element
  580. class ConstExprImpl(RoleImpl):
  581. __slots__ = ()
  582. def _literal_coercion(self, element, argname=None, **kw):
  583. if element is None:
  584. return elements.Null()
  585. elif element is False:
  586. return elements.False_()
  587. elif element is True:
  588. return elements.True_()
  589. else:
  590. self._raise_for_expected(element, argname)
  591. class TruncatedLabelImpl(_StringOnly, RoleImpl):
  592. __slots__ = ()
  593. def _implicit_coercions(
  594. self, original_element, resolved, argname=None, **kw
  595. ):
  596. if isinstance(original_element, util.string_types):
  597. return resolved
  598. else:
  599. self._raise_for_expected(original_element, argname, resolved)
  600. def _literal_coercion(self, element, argname=None, **kw):
  601. """coerce the given value to :class:`._truncated_label`.
  602. Existing :class:`._truncated_label` and
  603. :class:`._anonymous_label` objects are passed
  604. unchanged.
  605. """
  606. if isinstance(element, elements._truncated_label):
  607. return element
  608. else:
  609. return elements._truncated_label(element)
  610. class DDLExpressionImpl(_Deannotate, _CoerceLiterals, RoleImpl):
  611. __slots__ = ()
  612. _coerce_consts = True
  613. def _text_coercion(self, element, argname=None):
  614. # see #5754 for why we can't easily deprecate this coercion.
  615. # essentially expressions like postgresql_where would have to be
  616. # text() as they come back from reflection and we don't want to
  617. # have text() elements wired into the inspection dictionaries.
  618. return elements.TextClause(element)
  619. class DDLConstraintColumnImpl(_Deannotate, _ReturnsStringKey, RoleImpl):
  620. __slots__ = ()
  621. class DDLReferredColumnImpl(DDLConstraintColumnImpl):
  622. __slots__ = ()
  623. class LimitOffsetImpl(RoleImpl):
  624. __slots__ = ()
  625. def _implicit_coercions(self, element, resolved, argname=None, **kw):
  626. if resolved is None:
  627. return None
  628. else:
  629. self._raise_for_expected(element, argname, resolved)
  630. def _literal_coercion(self, element, name, type_, **kw):
  631. if element is None:
  632. return None
  633. else:
  634. value = util.asint(element)
  635. return selectable._OffsetLimitParam(
  636. name, value, type_=type_, unique=True
  637. )
  638. class LabeledColumnExprImpl(ExpressionElementImpl):
  639. __slots__ = ()
  640. def _implicit_coercions(
  641. self, original_element, resolved, argname=None, **kw
  642. ):
  643. if isinstance(resolved, roles.ExpressionElementRole):
  644. return resolved.label(None)
  645. else:
  646. new = super(LabeledColumnExprImpl, self)._implicit_coercions(
  647. original_element, resolved, argname=argname, **kw
  648. )
  649. if isinstance(new, roles.ExpressionElementRole):
  650. return new.label(None)
  651. else:
  652. self._raise_for_expected(original_element, argname, resolved)
  653. class ColumnsClauseImpl(_SelectIsNotFrom, _CoerceLiterals, RoleImpl):
  654. __slots__ = ()
  655. _coerce_consts = True
  656. _coerce_numerics = True
  657. _coerce_star = True
  658. _guess_straight_column = re.compile(r"^\w\S*$", re.I)
  659. def _text_coercion(self, element, argname=None):
  660. element = str(element)
  661. guess_is_literal = not self._guess_straight_column.match(element)
  662. raise exc.ArgumentError(
  663. "Textual column expression %(column)r %(argname)sshould be "
  664. "explicitly declared with text(%(column)r), "
  665. "or use %(literal_column)s(%(column)r) "
  666. "for more specificity"
  667. % {
  668. "column": util.ellipses_string(element),
  669. "argname": "for argument %s" % (argname,) if argname else "",
  670. "literal_column": "literal_column"
  671. if guess_is_literal
  672. else "column",
  673. }
  674. )
  675. class ReturnsRowsImpl(RoleImpl):
  676. __slots__ = ()
  677. class StatementImpl(_CoerceLiterals, RoleImpl):
  678. __slots__ = ()
  679. def _post_coercion(self, resolved, original_element, argname=None, **kw):
  680. if resolved is not original_element and not isinstance(
  681. original_element, util.string_types
  682. ):
  683. # use same method as Connection uses; this will later raise
  684. # ObjectNotExecutableError
  685. try:
  686. original_element._execute_on_connection
  687. except AttributeError:
  688. util.warn_deprecated(
  689. "Object %r should not be used directly in a SQL statement "
  690. "context, such as passing to methods such as "
  691. "session.execute(). This usage will be disallowed in a "
  692. "future release. "
  693. "Please use Core select() / update() / delete() etc. "
  694. "with Session.execute() and other statement execution "
  695. "methods." % original_element,
  696. "1.4",
  697. )
  698. return resolved
  699. def _implicit_coercions(
  700. self, original_element, resolved, argname=None, **kw
  701. ):
  702. if resolved._is_lambda_element:
  703. return resolved
  704. else:
  705. return super(StatementImpl, self)._implicit_coercions(
  706. original_element, resolved, argname=argname, **kw
  707. )
  708. def _text_coercion(self, element, argname=None):
  709. util.warn_deprecated_20(
  710. "Using plain strings to indicate SQL statements without using "
  711. "the text() construct is "
  712. "deprecated and will be removed in version 2.0. Ensure plain "
  713. "SQL statements are passed using the text() construct."
  714. )
  715. return elements.TextClause(element)
  716. class SelectStatementImpl(_NoTextCoercion, RoleImpl):
  717. __slots__ = ()
  718. def _implicit_coercions(
  719. self, original_element, resolved, argname=None, **kw
  720. ):
  721. if resolved._is_text_clause:
  722. return resolved.columns()
  723. else:
  724. self._raise_for_expected(original_element, argname, resolved)
  725. class HasCTEImpl(ReturnsRowsImpl):
  726. __slots__ = ()
  727. class IsCTEImpl(RoleImpl):
  728. __slots__ = ()
  729. class JoinTargetImpl(RoleImpl):
  730. __slots__ = ()
  731. _skip_clauseelement_for_target_match = True
  732. def _literal_coercion(self, element, legacy=False, **kw):
  733. if isinstance(element, str):
  734. return element
  735. def _implicit_coercions(
  736. self, original_element, resolved, argname=None, legacy=False, **kw
  737. ):
  738. if isinstance(original_element, roles.JoinTargetRole):
  739. # note that this codepath no longer occurs as of
  740. # #6550, unless JoinTargetImpl._skip_clauseelement_for_target_match
  741. # were set to False.
  742. return original_element
  743. elif legacy and isinstance(resolved, str):
  744. util.warn_deprecated_20(
  745. "Using strings to indicate relationship names in "
  746. "Query.join() is deprecated and will be removed in "
  747. "SQLAlchemy 2.0. Please use the class-bound attribute "
  748. "directly."
  749. )
  750. return resolved
  751. elif legacy and isinstance(resolved, roles.WhereHavingRole):
  752. return resolved
  753. elif legacy and resolved._is_select_statement:
  754. util.warn_deprecated(
  755. "Implicit coercion of SELECT and textual SELECT "
  756. "constructs into FROM clauses is deprecated; please call "
  757. ".subquery() on any Core select or ORM Query object in "
  758. "order to produce a subquery object.",
  759. version="1.4",
  760. )
  761. # TODO: doing _implicit_subquery here causes tests to fail,
  762. # how was this working before? probably that ORM
  763. # join logic treated it as a select and subquery would happen
  764. # in _ORMJoin->Join
  765. return resolved
  766. else:
  767. self._raise_for_expected(original_element, argname, resolved)
  768. class FromClauseImpl(_SelectIsNotFrom, _NoTextCoercion, RoleImpl):
  769. __slots__ = ()
  770. def _implicit_coercions(
  771. self,
  772. original_element,
  773. resolved,
  774. argname=None,
  775. explicit_subquery=False,
  776. allow_select=True,
  777. **kw
  778. ):
  779. if resolved._is_select_statement:
  780. if explicit_subquery:
  781. return resolved.subquery()
  782. elif allow_select:
  783. util.warn_deprecated(
  784. "Implicit coercion of SELECT and textual SELECT "
  785. "constructs into FROM clauses is deprecated; please call "
  786. ".subquery() on any Core select or ORM Query object in "
  787. "order to produce a subquery object.",
  788. version="1.4",
  789. )
  790. return resolved._implicit_subquery
  791. elif resolved._is_text_clause:
  792. return resolved
  793. else:
  794. self._raise_for_expected(original_element, argname, resolved)
  795. def _post_coercion(self, element, deannotate=False, **kw):
  796. if deannotate:
  797. return element._deannotate()
  798. else:
  799. return element
  800. class StrictFromClauseImpl(FromClauseImpl):
  801. __slots__ = ()
  802. def _implicit_coercions(
  803. self,
  804. original_element,
  805. resolved,
  806. argname=None,
  807. allow_select=False,
  808. **kw
  809. ):
  810. if resolved._is_select_statement and allow_select:
  811. util.warn_deprecated(
  812. "Implicit coercion of SELECT and textual SELECT constructs "
  813. "into FROM clauses is deprecated; please call .subquery() "
  814. "on any Core select or ORM Query object in order to produce a "
  815. "subquery object.",
  816. version="1.4",
  817. )
  818. return resolved._implicit_subquery
  819. else:
  820. self._raise_for_expected(original_element, argname, resolved)
  821. class AnonymizedFromClauseImpl(StrictFromClauseImpl):
  822. __slots__ = ()
  823. def _post_coercion(self, element, flat=False, name=None, **kw):
  824. assert name is None
  825. return element._anonymous_fromclause(flat=flat)
  826. class DMLTableImpl(_SelectIsNotFrom, _NoTextCoercion, RoleImpl):
  827. __slots__ = ()
  828. def _post_coercion(self, element, **kw):
  829. if "dml_table" in element._annotations:
  830. return element._annotations["dml_table"]
  831. else:
  832. return element
  833. class DMLSelectImpl(_NoTextCoercion, RoleImpl):
  834. __slots__ = ()
  835. def _implicit_coercions(
  836. self, original_element, resolved, argname=None, **kw
  837. ):
  838. if resolved._is_from_clause:
  839. if (
  840. isinstance(resolved, selectable.Alias)
  841. and resolved.element._is_select_statement
  842. ):
  843. return resolved.element
  844. else:
  845. return resolved.select()
  846. else:
  847. self._raise_for_expected(original_element, argname, resolved)
  848. class CompoundElementImpl(_NoTextCoercion, RoleImpl):
  849. __slots__ = ()
  850. def _raise_for_expected(self, element, argname=None, resolved=None, **kw):
  851. if isinstance(element, roles.FromClauseRole):
  852. if element._is_subquery:
  853. advice = (
  854. "Use the plain select() object without "
  855. "calling .subquery() or .alias()."
  856. )
  857. else:
  858. advice = (
  859. "To SELECT from any FROM clause, use the .select() method."
  860. )
  861. else:
  862. advice = None
  863. return super(CompoundElementImpl, self)._raise_for_expected(
  864. element, argname=argname, resolved=resolved, advice=advice, **kw
  865. )
  866. _impl_lookup = {}
  867. for name in dir(roles):
  868. cls = getattr(roles, name)
  869. if name.endswith("Role"):
  870. name = name.replace("Role", "Impl")
  871. if name in globals():
  872. impl = globals()[name](cls)
  873. _impl_lookup[cls] = impl