compiler.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. # ext/compiler.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. r"""Provides an API for creation of custom ClauseElements and compilers.
  8. Synopsis
  9. ========
  10. Usage involves the creation of one or more
  11. :class:`~sqlalchemy.sql.expression.ClauseElement` subclasses and one or
  12. more callables defining its compilation::
  13. from sqlalchemy.ext.compiler import compiles
  14. from sqlalchemy.sql.expression import ColumnClause
  15. class MyColumn(ColumnClause):
  16. inherit_cache = True
  17. @compiles(MyColumn)
  18. def compile_mycolumn(element, compiler, **kw):
  19. return "[%s]" % element.name
  20. Above, ``MyColumn`` extends :class:`~sqlalchemy.sql.expression.ColumnClause`,
  21. the base expression element for named column objects. The ``compiles``
  22. decorator registers itself with the ``MyColumn`` class so that it is invoked
  23. when the object is compiled to a string::
  24. from sqlalchemy import select
  25. s = select(MyColumn('x'), MyColumn('y'))
  26. print(str(s))
  27. Produces::
  28. SELECT [x], [y]
  29. Dialect-specific compilation rules
  30. ==================================
  31. Compilers can also be made dialect-specific. The appropriate compiler will be
  32. invoked for the dialect in use::
  33. from sqlalchemy.schema import DDLElement
  34. class AlterColumn(DDLElement):
  35. inherit_cache = False
  36. def __init__(self, column, cmd):
  37. self.column = column
  38. self.cmd = cmd
  39. @compiles(AlterColumn)
  40. def visit_alter_column(element, compiler, **kw):
  41. return "ALTER COLUMN %s ..." % element.column.name
  42. @compiles(AlterColumn, 'postgresql')
  43. def visit_alter_column(element, compiler, **kw):
  44. return "ALTER TABLE %s ALTER COLUMN %s ..." % (element.table.name,
  45. element.column.name)
  46. The second ``visit_alter_table`` will be invoked when any ``postgresql``
  47. dialect is used.
  48. .. _compilerext_compiling_subelements:
  49. Compiling sub-elements of a custom expression construct
  50. =======================================================
  51. The ``compiler`` argument is the
  52. :class:`~sqlalchemy.engine.interfaces.Compiled` object in use. This object
  53. can be inspected for any information about the in-progress compilation,
  54. including ``compiler.dialect``, ``compiler.statement`` etc. The
  55. :class:`~sqlalchemy.sql.compiler.SQLCompiler` and
  56. :class:`~sqlalchemy.sql.compiler.DDLCompiler` both include a ``process()``
  57. method which can be used for compilation of embedded attributes::
  58. from sqlalchemy.sql.expression import Executable, ClauseElement
  59. class InsertFromSelect(Executable, ClauseElement):
  60. inherit_cache = False
  61. def __init__(self, table, select):
  62. self.table = table
  63. self.select = select
  64. @compiles(InsertFromSelect)
  65. def visit_insert_from_select(element, compiler, **kw):
  66. return "INSERT INTO %s (%s)" % (
  67. compiler.process(element.table, asfrom=True, **kw),
  68. compiler.process(element.select, **kw)
  69. )
  70. insert = InsertFromSelect(t1, select(t1).where(t1.c.x>5))
  71. print(insert)
  72. Produces::
  73. "INSERT INTO mytable (SELECT mytable.x, mytable.y, mytable.z
  74. FROM mytable WHERE mytable.x > :x_1)"
  75. .. note::
  76. The above ``InsertFromSelect`` construct is only an example, this actual
  77. functionality is already available using the
  78. :meth:`_expression.Insert.from_select` method.
  79. .. note::
  80. The above ``InsertFromSelect`` construct probably wants to have "autocommit"
  81. enabled. See :ref:`enabling_compiled_autocommit` for this step.
  82. Cross Compiling between SQL and DDL compilers
  83. ---------------------------------------------
  84. SQL and DDL constructs are each compiled using different base compilers -
  85. ``SQLCompiler`` and ``DDLCompiler``. A common need is to access the
  86. compilation rules of SQL expressions from within a DDL expression. The
  87. ``DDLCompiler`` includes an accessor ``sql_compiler`` for this reason, such as
  88. below where we generate a CHECK constraint that embeds a SQL expression::
  89. @compiles(MyConstraint)
  90. def compile_my_constraint(constraint, ddlcompiler, **kw):
  91. kw['literal_binds'] = True
  92. return "CONSTRAINT %s CHECK (%s)" % (
  93. constraint.name,
  94. ddlcompiler.sql_compiler.process(
  95. constraint.expression, **kw)
  96. )
  97. Above, we add an additional flag to the process step as called by
  98. :meth:`.SQLCompiler.process`, which is the ``literal_binds`` flag. This
  99. indicates that any SQL expression which refers to a :class:`.BindParameter`
  100. object or other "literal" object such as those which refer to strings or
  101. integers should be rendered **in-place**, rather than being referred to as
  102. a bound parameter; when emitting DDL, bound parameters are typically not
  103. supported.
  104. .. _enabling_compiled_autocommit:
  105. Enabling Autocommit on a Construct
  106. ==================================
  107. Recall from the section :ref:`autocommit` that the :class:`_engine.Engine`,
  108. when
  109. asked to execute a construct in the absence of a user-defined transaction,
  110. detects if the given construct represents DML or DDL, that is, a data
  111. modification or data definition statement, which requires (or may require,
  112. in the case of DDL) that the transaction generated by the DBAPI be committed
  113. (recall that DBAPI always has a transaction going on regardless of what
  114. SQLAlchemy does). Checking for this is actually accomplished by checking for
  115. the "autocommit" execution option on the construct. When building a
  116. construct like an INSERT derivation, a new DDL type, or perhaps a stored
  117. procedure that alters data, the "autocommit" option needs to be set in order
  118. for the statement to function with "connectionless" execution
  119. (as described in :ref:`dbengine_implicit`).
  120. Currently a quick way to do this is to subclass :class:`.Executable`, then
  121. add the "autocommit" flag to the ``_execution_options`` dictionary (note this
  122. is a "frozen" dictionary which supplies a generative ``union()`` method)::
  123. from sqlalchemy.sql.expression import Executable, ClauseElement
  124. class MyInsertThing(Executable, ClauseElement):
  125. _execution_options = \
  126. Executable._execution_options.union({'autocommit': True})
  127. More succinctly, if the construct is truly similar to an INSERT, UPDATE, or
  128. DELETE, :class:`.UpdateBase` can be used, which already is a subclass
  129. of :class:`.Executable`, :class:`_expression.ClauseElement` and includes the
  130. ``autocommit`` flag::
  131. from sqlalchemy.sql.expression import UpdateBase
  132. class MyInsertThing(UpdateBase):
  133. def __init__(self, ...):
  134. ...
  135. DDL elements that subclass :class:`.DDLElement` already have the
  136. "autocommit" flag turned on.
  137. Changing the default compilation of existing constructs
  138. =======================================================
  139. The compiler extension applies just as well to the existing constructs. When
  140. overriding the compilation of a built in SQL construct, the @compiles
  141. decorator is invoked upon the appropriate class (be sure to use the class,
  142. i.e. ``Insert`` or ``Select``, instead of the creation function such
  143. as ``insert()`` or ``select()``).
  144. Within the new compilation function, to get at the "original" compilation
  145. routine, use the appropriate visit_XXX method - this
  146. because compiler.process() will call upon the overriding routine and cause
  147. an endless loop. Such as, to add "prefix" to all insert statements::
  148. from sqlalchemy.sql.expression import Insert
  149. @compiles(Insert)
  150. def prefix_inserts(insert, compiler, **kw):
  151. return compiler.visit_insert(insert.prefix_with("some prefix"), **kw)
  152. The above compiler will prefix all INSERT statements with "some prefix" when
  153. compiled.
  154. .. _type_compilation_extension:
  155. Changing Compilation of Types
  156. =============================
  157. ``compiler`` works for types, too, such as below where we implement the
  158. MS-SQL specific 'max' keyword for ``String``/``VARCHAR``::
  159. @compiles(String, 'mssql')
  160. @compiles(VARCHAR, 'mssql')
  161. def compile_varchar(element, compiler, **kw):
  162. if element.length == 'max':
  163. return "VARCHAR('max')"
  164. else:
  165. return compiler.visit_VARCHAR(element, **kw)
  166. foo = Table('foo', metadata,
  167. Column('data', VARCHAR('max'))
  168. )
  169. Subclassing Guidelines
  170. ======================
  171. A big part of using the compiler extension is subclassing SQLAlchemy
  172. expression constructs. To make this easier, the expression and
  173. schema packages feature a set of "bases" intended for common tasks.
  174. A synopsis is as follows:
  175. * :class:`~sqlalchemy.sql.expression.ClauseElement` - This is the root
  176. expression class. Any SQL expression can be derived from this base, and is
  177. probably the best choice for longer constructs such as specialized INSERT
  178. statements.
  179. * :class:`~sqlalchemy.sql.expression.ColumnElement` - The root of all
  180. "column-like" elements. Anything that you'd place in the "columns" clause of
  181. a SELECT statement (as well as order by and group by) can derive from this -
  182. the object will automatically have Python "comparison" behavior.
  183. :class:`~sqlalchemy.sql.expression.ColumnElement` classes want to have a
  184. ``type`` member which is expression's return type. This can be established
  185. at the instance level in the constructor, or at the class level if its
  186. generally constant::
  187. class timestamp(ColumnElement):
  188. type = TIMESTAMP()
  189. inherit_cache = True
  190. * :class:`~sqlalchemy.sql.functions.FunctionElement` - This is a hybrid of a
  191. ``ColumnElement`` and a "from clause" like object, and represents a SQL
  192. function or stored procedure type of call. Since most databases support
  193. statements along the line of "SELECT FROM <some function>"
  194. ``FunctionElement`` adds in the ability to be used in the FROM clause of a
  195. ``select()`` construct::
  196. from sqlalchemy.sql.expression import FunctionElement
  197. class coalesce(FunctionElement):
  198. name = 'coalesce'
  199. inherit_cache = True
  200. @compiles(coalesce)
  201. def compile(element, compiler, **kw):
  202. return "coalesce(%s)" % compiler.process(element.clauses, **kw)
  203. @compiles(coalesce, 'oracle')
  204. def compile(element, compiler, **kw):
  205. if len(element.clauses) > 2:
  206. raise TypeError("coalesce only supports two arguments on Oracle")
  207. return "nvl(%s)" % compiler.process(element.clauses, **kw)
  208. * :class:`~sqlalchemy.schema.DDLElement` - The root of all DDL expressions,
  209. like CREATE TABLE, ALTER TABLE, etc. Compilation of ``DDLElement``
  210. subclasses is issued by a ``DDLCompiler`` instead of a ``SQLCompiler``.
  211. ``DDLElement`` also features ``Table`` and ``MetaData`` event hooks via the
  212. ``execute_at()`` method, allowing the construct to be invoked during CREATE
  213. TABLE and DROP TABLE sequences.
  214. * :class:`~sqlalchemy.sql.expression.Executable` - This is a mixin which
  215. should be used with any expression class that represents a "standalone"
  216. SQL statement that can be passed directly to an ``execute()`` method. It
  217. is already implicit within ``DDLElement`` and ``FunctionElement``.
  218. Most of the above constructs also respond to SQL statement caching. A
  219. subclassed construct will want to define the caching behavior for the object,
  220. which usually means setting the flag ``inherit_cache`` to the value of
  221. ``False`` or ``True``. See the next section :ref:`compilerext_caching`
  222. for background.
  223. .. _compilerext_caching:
  224. Enabling Caching Support for Custom Constructs
  225. ==============================================
  226. SQLAlchemy as of version 1.4 includes a
  227. :ref:`SQL compilation caching facility <sql_caching>` which will allow
  228. equivalent SQL constructs to cache their stringified form, along with other
  229. structural information used to fetch results from the statement.
  230. For reasons discussed at :ref:`caching_caveats`, the implementation of this
  231. caching system takes a conservative approach towards including custom SQL
  232. constructs and/or subclasses within the caching system. This includes that
  233. any user-defined SQL constructs, including all the examples for this
  234. extension, will not participate in caching by default unless they positively
  235. assert that they are able to do so. The :attr:`.HasCacheKey.inherit_cache`
  236. attribute when set to ``True`` at the class level of a specific subclass
  237. will indicate that instances of this class may be safely cached, using the
  238. cache key generation scheme of the immediate superclass. This applies
  239. for example to the "synopsis" example indicated previously::
  240. class MyColumn(ColumnClause):
  241. inherit_cache = True
  242. @compiles(MyColumn)
  243. def compile_mycolumn(element, compiler, **kw):
  244. return "[%s]" % element.name
  245. Above, the ``MyColumn`` class does not include any new state that
  246. affects its SQL compilation; the cache key of ``MyColumn`` instances will
  247. make use of that of the ``ColumnClause`` superclass, meaning it will take
  248. into account the class of the object (``MyColumn``), the string name and
  249. datatype of the object::
  250. >>> MyColumn("some_name", String())._generate_cache_key()
  251. CacheKey(
  252. key=('0', <class '__main__.MyColumn'>,
  253. 'name', 'some_name',
  254. 'type', (<class 'sqlalchemy.sql.sqltypes.String'>,
  255. ('length', None), ('collation', None))
  256. ), bindparams=[])
  257. For objects that are likely to be **used liberally as components within many
  258. larger statements**, such as :class:`_schema.Column` subclasses and custom SQL
  259. datatypes, it's important that **caching be enabled as much as possible**, as
  260. this may otherwise negatively affect performance.
  261. An example of an object that **does** contain state which affects its SQL
  262. compilation is the one illustrated at :ref:`compilerext_compiling_subelements`;
  263. this is an "INSERT FROM SELECT" construct that combines together a
  264. :class:`_schema.Table` as well as a :class:`_sql.Select` construct, each of
  265. which independently affect the SQL string generation of the construct. For
  266. this class, the example illustrates that it simply does not participate in
  267. caching::
  268. class InsertFromSelect(Executable, ClauseElement):
  269. inherit_cache = False
  270. def __init__(self, table, select):
  271. self.table = table
  272. self.select = select
  273. @compiles(InsertFromSelect)
  274. def visit_insert_from_select(element, compiler, **kw):
  275. return "INSERT INTO %s (%s)" % (
  276. compiler.process(element.table, asfrom=True, **kw),
  277. compiler.process(element.select, **kw)
  278. )
  279. While it is also possible that the above ``InsertFromSelect`` could be made to
  280. produce a cache key that is composed of that of the :class:`_schema.Table` and
  281. :class:`_sql.Select` components together, the API for this is not at the moment
  282. fully public. However, for an "INSERT FROM SELECT" construct, which is only
  283. used by itself for specific operations, caching is not as critical as in the
  284. previous example.
  285. For objects that are **used in relative isolation and are generally
  286. standalone**, such as custom :term:`DML` constructs like an "INSERT FROM
  287. SELECT", **caching is generally less critical** as the lack of caching for such
  288. a construct will have only localized implications for that specific operation.
  289. Further Examples
  290. ================
  291. "UTC timestamp" function
  292. -------------------------
  293. A function that works like "CURRENT_TIMESTAMP" except applies the
  294. appropriate conversions so that the time is in UTC time. Timestamps are best
  295. stored in relational databases as UTC, without time zones. UTC so that your
  296. database doesn't think time has gone backwards in the hour when daylight
  297. savings ends, without timezones because timezones are like character
  298. encodings - they're best applied only at the endpoints of an application
  299. (i.e. convert to UTC upon user input, re-apply desired timezone upon display).
  300. For PostgreSQL and Microsoft SQL Server::
  301. from sqlalchemy.sql import expression
  302. from sqlalchemy.ext.compiler import compiles
  303. from sqlalchemy.types import DateTime
  304. class utcnow(expression.FunctionElement):
  305. type = DateTime()
  306. inherit_cache = True
  307. @compiles(utcnow, 'postgresql')
  308. def pg_utcnow(element, compiler, **kw):
  309. return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
  310. @compiles(utcnow, 'mssql')
  311. def ms_utcnow(element, compiler, **kw):
  312. return "GETUTCDATE()"
  313. Example usage::
  314. from sqlalchemy import (
  315. Table, Column, Integer, String, DateTime, MetaData
  316. )
  317. metadata = MetaData()
  318. event = Table("event", metadata,
  319. Column("id", Integer, primary_key=True),
  320. Column("description", String(50), nullable=False),
  321. Column("timestamp", DateTime, server_default=utcnow())
  322. )
  323. "GREATEST" function
  324. -------------------
  325. The "GREATEST" function is given any number of arguments and returns the one
  326. that is of the highest value - its equivalent to Python's ``max``
  327. function. A SQL standard version versus a CASE based version which only
  328. accommodates two arguments::
  329. from sqlalchemy.sql import expression, case
  330. from sqlalchemy.ext.compiler import compiles
  331. from sqlalchemy.types import Numeric
  332. class greatest(expression.FunctionElement):
  333. type = Numeric()
  334. name = 'greatest'
  335. inherit_cache = True
  336. @compiles(greatest)
  337. def default_greatest(element, compiler, **kw):
  338. return compiler.visit_function(element)
  339. @compiles(greatest, 'sqlite')
  340. @compiles(greatest, 'mssql')
  341. @compiles(greatest, 'oracle')
  342. def case_greatest(element, compiler, **kw):
  343. arg1, arg2 = list(element.clauses)
  344. return compiler.process(case([(arg1 > arg2, arg1)], else_=arg2), **kw)
  345. Example usage::
  346. Session.query(Account).\
  347. filter(
  348. greatest(
  349. Account.checking_balance,
  350. Account.savings_balance) > 10000
  351. )
  352. "false" expression
  353. ------------------
  354. Render a "false" constant expression, rendering as "0" on platforms that
  355. don't have a "false" constant::
  356. from sqlalchemy.sql import expression
  357. from sqlalchemy.ext.compiler import compiles
  358. class sql_false(expression.ColumnElement):
  359. inherit_cache = True
  360. @compiles(sql_false)
  361. def default_false(element, compiler, **kw):
  362. return "false"
  363. @compiles(sql_false, 'mssql')
  364. @compiles(sql_false, 'mysql')
  365. @compiles(sql_false, 'oracle')
  366. def int_false(element, compiler, **kw):
  367. return "0"
  368. Example usage::
  369. from sqlalchemy import select, union_all
  370. exp = union_all(
  371. select(users.c.name, sql_false().label("enrolled")),
  372. select(customers.c.name, customers.c.enrolled)
  373. )
  374. """
  375. from .. import exc
  376. from .. import util
  377. from ..sql import sqltypes
  378. def compiles(class_, *specs):
  379. """Register a function as a compiler for a
  380. given :class:`_expression.ClauseElement` type."""
  381. def decorate(fn):
  382. # get an existing @compiles handler
  383. existing = class_.__dict__.get("_compiler_dispatcher", None)
  384. # get the original handler. All ClauseElement classes have one
  385. # of these, but some TypeEngine classes will not.
  386. existing_dispatch = getattr(class_, "_compiler_dispatch", None)
  387. if not existing:
  388. existing = _dispatcher()
  389. if existing_dispatch:
  390. def _wrap_existing_dispatch(element, compiler, **kw):
  391. try:
  392. return existing_dispatch(element, compiler, **kw)
  393. except exc.UnsupportedCompilationError as uce:
  394. util.raise_(
  395. exc.UnsupportedCompilationError(
  396. compiler,
  397. type(element),
  398. message="%s construct has no default "
  399. "compilation handler." % type(element),
  400. ),
  401. from_=uce,
  402. )
  403. existing.specs["default"] = _wrap_existing_dispatch
  404. # TODO: why is the lambda needed ?
  405. setattr(
  406. class_,
  407. "_compiler_dispatch",
  408. lambda *arg, **kw: existing(*arg, **kw),
  409. )
  410. setattr(class_, "_compiler_dispatcher", existing)
  411. if specs:
  412. for s in specs:
  413. existing.specs[s] = fn
  414. else:
  415. existing.specs["default"] = fn
  416. return fn
  417. return decorate
  418. def deregister(class_):
  419. """Remove all custom compilers associated with a given
  420. :class:`_expression.ClauseElement` type.
  421. """
  422. if hasattr(class_, "_compiler_dispatcher"):
  423. class_._compiler_dispatch = class_._original_compiler_dispatch
  424. del class_._compiler_dispatcher
  425. class _dispatcher(object):
  426. def __init__(self):
  427. self.specs = {}
  428. def __call__(self, element, compiler, **kw):
  429. # TODO: yes, this could also switch off of DBAPI in use.
  430. fn = self.specs.get(compiler.dialect.name, None)
  431. if not fn:
  432. try:
  433. fn = self.specs["default"]
  434. except KeyError as ke:
  435. util.raise_(
  436. exc.UnsupportedCompilationError(
  437. compiler,
  438. type(element),
  439. message="%s construct has no default "
  440. "compilation handler." % type(element),
  441. ),
  442. replace_context=ke,
  443. )
  444. # if compilation includes add_to_result_map, collect add_to_result_map
  445. # arguments from the user-defined callable, which are probably none
  446. # because this is not public API. if it wasn't called, then call it
  447. # ourselves.
  448. arm = kw.get("add_to_result_map", None)
  449. if arm:
  450. arm_collection = []
  451. kw["add_to_result_map"] = lambda *args: arm_collection.append(args)
  452. expr = fn(element, compiler, **kw)
  453. if arm:
  454. if not arm_collection:
  455. arm_collection.append(
  456. (None, None, (element,), sqltypes.NULLTYPE)
  457. )
  458. for tup in arm_collection:
  459. arm(*tup)
  460. return expr