compiler.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. # ext/compiler.py
  2. # Copyright (C) 2005-2017 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. 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. pass
  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. def __init__(self, column, cmd):
  36. self.column = column
  37. self.cmd = cmd
  38. @compiles(AlterColumn)
  39. def visit_alter_column(element, compiler, **kw):
  40. return "ALTER COLUMN %s ..." % element.column.name
  41. @compiles(AlterColumn, 'postgresql')
  42. def visit_alter_column(element, compiler, **kw):
  43. return "ALTER TABLE %s ALTER COLUMN %s ..." % (element.table.name,
  44. element.column.name)
  45. The second ``visit_alter_table`` will be invoked when any ``postgresql``
  46. dialect is used.
  47. Compiling sub-elements of a custom expression construct
  48. =======================================================
  49. The ``compiler`` argument is the
  50. :class:`~sqlalchemy.engine.interfaces.Compiled` object in use. This object
  51. can be inspected for any information about the in-progress compilation,
  52. including ``compiler.dialect``, ``compiler.statement`` etc. The
  53. :class:`~sqlalchemy.sql.compiler.SQLCompiler` and
  54. :class:`~sqlalchemy.sql.compiler.DDLCompiler` both include a ``process()``
  55. method which can be used for compilation of embedded attributes::
  56. from sqlalchemy.sql.expression import Executable, ClauseElement
  57. class InsertFromSelect(Executable, ClauseElement):
  58. def __init__(self, table, select):
  59. self.table = table
  60. self.select = select
  61. @compiles(InsertFromSelect)
  62. def visit_insert_from_select(element, compiler, **kw):
  63. return "INSERT INTO %s (%s)" % (
  64. compiler.process(element.table, asfrom=True),
  65. compiler.process(element.select)
  66. )
  67. insert = InsertFromSelect(t1, select([t1]).where(t1.c.x>5))
  68. print insert
  69. Produces::
  70. "INSERT INTO mytable (SELECT mytable.x, mytable.y, mytable.z
  71. FROM mytable WHERE mytable.x > :x_1)"
  72. .. note::
  73. The above ``InsertFromSelect`` construct is only an example, this actual
  74. functionality is already available using the
  75. :meth:`.Insert.from_select` method.
  76. .. note::
  77. The above ``InsertFromSelect`` construct probably wants to have "autocommit"
  78. enabled. See :ref:`enabling_compiled_autocommit` for this step.
  79. Cross Compiling between SQL and DDL compilers
  80. ---------------------------------------------
  81. SQL and DDL constructs are each compiled using different base compilers -
  82. ``SQLCompiler`` and ``DDLCompiler``. A common need is to access the
  83. compilation rules of SQL expressions from within a DDL expression. The
  84. ``DDLCompiler`` includes an accessor ``sql_compiler`` for this reason, such as
  85. below where we generate a CHECK constraint that embeds a SQL expression::
  86. @compiles(MyConstraint)
  87. def compile_my_constraint(constraint, ddlcompiler, **kw):
  88. return "CONSTRAINT %s CHECK (%s)" % (
  89. constraint.name,
  90. ddlcompiler.sql_compiler.process(
  91. constraint.expression, literal_binds=True)
  92. )
  93. Above, we add an additional flag to the process step as called by
  94. :meth:`.SQLCompiler.process`, which is the ``literal_binds`` flag. This
  95. indicates that any SQL expression which refers to a :class:`.BindParameter`
  96. object or other "literal" object such as those which refer to strings or
  97. integers should be rendered **in-place**, rather than being referred to as
  98. a bound parameter; when emitting DDL, bound parameters are typically not
  99. supported.
  100. .. _enabling_compiled_autocommit:
  101. Enabling Autocommit on a Construct
  102. ==================================
  103. Recall from the section :ref:`autocommit` that the :class:`.Engine`, when
  104. asked to execute a construct in the absence of a user-defined transaction,
  105. detects if the given construct represents DML or DDL, that is, a data
  106. modification or data definition statement, which requires (or may require,
  107. in the case of DDL) that the transaction generated by the DBAPI be committed
  108. (recall that DBAPI always has a transaction going on regardless of what
  109. SQLAlchemy does). Checking for this is actually accomplished by checking for
  110. the "autocommit" execution option on the construct. When building a
  111. construct like an INSERT derivation, a new DDL type, or perhaps a stored
  112. procedure that alters data, the "autocommit" option needs to be set in order
  113. for the statement to function with "connectionless" execution
  114. (as described in :ref:`dbengine_implicit`).
  115. Currently a quick way to do this is to subclass :class:`.Executable`, then
  116. add the "autocommit" flag to the ``_execution_options`` dictionary (note this
  117. is a "frozen" dictionary which supplies a generative ``union()`` method)::
  118. from sqlalchemy.sql.expression import Executable, ClauseElement
  119. class MyInsertThing(Executable, ClauseElement):
  120. _execution_options = \
  121. Executable._execution_options.union({'autocommit': True})
  122. More succinctly, if the construct is truly similar to an INSERT, UPDATE, or
  123. DELETE, :class:`.UpdateBase` can be used, which already is a subclass
  124. of :class:`.Executable`, :class:`.ClauseElement` and includes the
  125. ``autocommit`` flag::
  126. from sqlalchemy.sql.expression import UpdateBase
  127. class MyInsertThing(UpdateBase):
  128. def __init__(self, ...):
  129. ...
  130. DDL elements that subclass :class:`.DDLElement` already have the
  131. "autocommit" flag turned on.
  132. Changing the default compilation of existing constructs
  133. =======================================================
  134. The compiler extension applies just as well to the existing constructs. When
  135. overriding the compilation of a built in SQL construct, the @compiles
  136. decorator is invoked upon the appropriate class (be sure to use the class,
  137. i.e. ``Insert`` or ``Select``, instead of the creation function such
  138. as ``insert()`` or ``select()``).
  139. Within the new compilation function, to get at the "original" compilation
  140. routine, use the appropriate visit_XXX method - this
  141. because compiler.process() will call upon the overriding routine and cause
  142. an endless loop. Such as, to add "prefix" to all insert statements::
  143. from sqlalchemy.sql.expression import Insert
  144. @compiles(Insert)
  145. def prefix_inserts(insert, compiler, **kw):
  146. return compiler.visit_insert(insert.prefix_with("some prefix"), **kw)
  147. The above compiler will prefix all INSERT statements with "some prefix" when
  148. compiled.
  149. .. _type_compilation_extension:
  150. Changing Compilation of Types
  151. =============================
  152. ``compiler`` works for types, too, such as below where we implement the
  153. MS-SQL specific 'max' keyword for ``String``/``VARCHAR``::
  154. @compiles(String, 'mssql')
  155. @compiles(VARCHAR, 'mssql')
  156. def compile_varchar(element, compiler, **kw):
  157. if element.length == 'max':
  158. return "VARCHAR('max')"
  159. else:
  160. return compiler.visit_VARCHAR(element, **kw)
  161. foo = Table('foo', metadata,
  162. Column('data', VARCHAR('max'))
  163. )
  164. Subclassing Guidelines
  165. ======================
  166. A big part of using the compiler extension is subclassing SQLAlchemy
  167. expression constructs. To make this easier, the expression and
  168. schema packages feature a set of "bases" intended for common tasks.
  169. A synopsis is as follows:
  170. * :class:`~sqlalchemy.sql.expression.ClauseElement` - This is the root
  171. expression class. Any SQL expression can be derived from this base, and is
  172. probably the best choice for longer constructs such as specialized INSERT
  173. statements.
  174. * :class:`~sqlalchemy.sql.expression.ColumnElement` - The root of all
  175. "column-like" elements. Anything that you'd place in the "columns" clause of
  176. a SELECT statement (as well as order by and group by) can derive from this -
  177. the object will automatically have Python "comparison" behavior.
  178. :class:`~sqlalchemy.sql.expression.ColumnElement` classes want to have a
  179. ``type`` member which is expression's return type. This can be established
  180. at the instance level in the constructor, or at the class level if its
  181. generally constant::
  182. class timestamp(ColumnElement):
  183. type = TIMESTAMP()
  184. * :class:`~sqlalchemy.sql.functions.FunctionElement` - This is a hybrid of a
  185. ``ColumnElement`` and a "from clause" like object, and represents a SQL
  186. function or stored procedure type of call. Since most databases support
  187. statements along the line of "SELECT FROM <some function>"
  188. ``FunctionElement`` adds in the ability to be used in the FROM clause of a
  189. ``select()`` construct::
  190. from sqlalchemy.sql.expression import FunctionElement
  191. class coalesce(FunctionElement):
  192. name = 'coalesce'
  193. @compiles(coalesce)
  194. def compile(element, compiler, **kw):
  195. return "coalesce(%s)" % compiler.process(element.clauses)
  196. @compiles(coalesce, 'oracle')
  197. def compile(element, compiler, **kw):
  198. if len(element.clauses) > 2:
  199. raise TypeError("coalesce only supports two arguments on Oracle")
  200. return "nvl(%s)" % compiler.process(element.clauses)
  201. * :class:`~sqlalchemy.schema.DDLElement` - The root of all DDL expressions,
  202. like CREATE TABLE, ALTER TABLE, etc. Compilation of ``DDLElement``
  203. subclasses is issued by a ``DDLCompiler`` instead of a ``SQLCompiler``.
  204. ``DDLElement`` also features ``Table`` and ``MetaData`` event hooks via the
  205. ``execute_at()`` method, allowing the construct to be invoked during CREATE
  206. TABLE and DROP TABLE sequences.
  207. * :class:`~sqlalchemy.sql.expression.Executable` - This is a mixin which
  208. should be used with any expression class that represents a "standalone"
  209. SQL statement that can be passed directly to an ``execute()`` method. It
  210. is already implicit within ``DDLElement`` and ``FunctionElement``.
  211. Further Examples
  212. ================
  213. "UTC timestamp" function
  214. -------------------------
  215. A function that works like "CURRENT_TIMESTAMP" except applies the
  216. appropriate conversions so that the time is in UTC time. Timestamps are best
  217. stored in relational databases as UTC, without time zones. UTC so that your
  218. database doesn't think time has gone backwards in the hour when daylight
  219. savings ends, without timezones because timezones are like character
  220. encodings - they're best applied only at the endpoints of an application
  221. (i.e. convert to UTC upon user input, re-apply desired timezone upon display).
  222. For PostgreSQL and Microsoft SQL Server::
  223. from sqlalchemy.sql import expression
  224. from sqlalchemy.ext.compiler import compiles
  225. from sqlalchemy.types import DateTime
  226. class utcnow(expression.FunctionElement):
  227. type = DateTime()
  228. @compiles(utcnow, 'postgresql')
  229. def pg_utcnow(element, compiler, **kw):
  230. return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
  231. @compiles(utcnow, 'mssql')
  232. def ms_utcnow(element, compiler, **kw):
  233. return "GETUTCDATE()"
  234. Example usage::
  235. from sqlalchemy import (
  236. Table, Column, Integer, String, DateTime, MetaData
  237. )
  238. metadata = MetaData()
  239. event = Table("event", metadata,
  240. Column("id", Integer, primary_key=True),
  241. Column("description", String(50), nullable=False),
  242. Column("timestamp", DateTime, server_default=utcnow())
  243. )
  244. "GREATEST" function
  245. -------------------
  246. The "GREATEST" function is given any number of arguments and returns the one
  247. that is of the highest value - its equivalent to Python's ``max``
  248. function. A SQL standard version versus a CASE based version which only
  249. accommodates two arguments::
  250. from sqlalchemy.sql import expression
  251. from sqlalchemy.ext.compiler import compiles
  252. from sqlalchemy.types import Numeric
  253. class greatest(expression.FunctionElement):
  254. type = Numeric()
  255. name = 'greatest'
  256. @compiles(greatest)
  257. def default_greatest(element, compiler, **kw):
  258. return compiler.visit_function(element)
  259. @compiles(greatest, 'sqlite')
  260. @compiles(greatest, 'mssql')
  261. @compiles(greatest, 'oracle')
  262. def case_greatest(element, compiler, **kw):
  263. arg1, arg2 = list(element.clauses)
  264. return "CASE WHEN %s > %s THEN %s ELSE %s END" % (
  265. compiler.process(arg1),
  266. compiler.process(arg2),
  267. compiler.process(arg1),
  268. compiler.process(arg2),
  269. )
  270. Example usage::
  271. Session.query(Account).\
  272. filter(
  273. greatest(
  274. Account.checking_balance,
  275. Account.savings_balance) > 10000
  276. )
  277. "false" expression
  278. ------------------
  279. Render a "false" constant expression, rendering as "0" on platforms that
  280. don't have a "false" constant::
  281. from sqlalchemy.sql import expression
  282. from sqlalchemy.ext.compiler import compiles
  283. class sql_false(expression.ColumnElement):
  284. pass
  285. @compiles(sql_false)
  286. def default_false(element, compiler, **kw):
  287. return "false"
  288. @compiles(sql_false, 'mssql')
  289. @compiles(sql_false, 'mysql')
  290. @compiles(sql_false, 'oracle')
  291. def int_false(element, compiler, **kw):
  292. return "0"
  293. Example usage::
  294. from sqlalchemy import select, union_all
  295. exp = union_all(
  296. select([users.c.name, sql_false().label("enrolled")]),
  297. select([customers.c.name, customers.c.enrolled])
  298. )
  299. """
  300. from .. import exc
  301. from ..sql import visitors
  302. def compiles(class_, *specs):
  303. """Register a function as a compiler for a
  304. given :class:`.ClauseElement` type."""
  305. def decorate(fn):
  306. # get an existing @compiles handler
  307. existing = class_.__dict__.get('_compiler_dispatcher', None)
  308. # get the original handler. All ClauseElement classes have one
  309. # of these, but some TypeEngine classes will not.
  310. existing_dispatch = getattr(class_, '_compiler_dispatch', None)
  311. if not existing:
  312. existing = _dispatcher()
  313. if existing_dispatch:
  314. def _wrap_existing_dispatch(element, compiler, **kw):
  315. try:
  316. return existing_dispatch(element, compiler, **kw)
  317. except exc.UnsupportedCompilationError:
  318. raise exc.CompileError(
  319. "%s construct has no default "
  320. "compilation handler." % type(element))
  321. existing.specs['default'] = _wrap_existing_dispatch
  322. # TODO: why is the lambda needed ?
  323. setattr(class_, '_compiler_dispatch',
  324. lambda *arg, **kw: existing(*arg, **kw))
  325. setattr(class_, '_compiler_dispatcher', existing)
  326. if specs:
  327. for s in specs:
  328. existing.specs[s] = fn
  329. else:
  330. existing.specs['default'] = fn
  331. return fn
  332. return decorate
  333. def deregister(class_):
  334. """Remove all custom compilers associated with a given
  335. :class:`.ClauseElement` type."""
  336. if hasattr(class_, '_compiler_dispatcher'):
  337. # regenerate default _compiler_dispatch
  338. visitors._generate_dispatch(class_)
  339. # remove custom directive
  340. del class_._compiler_dispatcher
  341. class _dispatcher(object):
  342. def __init__(self):
  343. self.specs = {}
  344. def __call__(self, element, compiler, **kw):
  345. # TODO: yes, this could also switch off of DBAPI in use.
  346. fn = self.specs.get(compiler.dialect.name, None)
  347. if not fn:
  348. try:
  349. fn = self.specs['default']
  350. except KeyError:
  351. raise exc.CompileError(
  352. "%s construct has no default "
  353. "compilation handler." % type(element))
  354. return fn(element, compiler, **kw)