From 77bf8f0713ad0d37956f72ddc50a41ac62619c1e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 30 Dec 2009 04:49:11 +0000 Subject: [PATCH] some compile docs --- lib/sqlalchemy/ext/compiler.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py index 0e3db00e02..f893538e8e 100644 --- a/lib/sqlalchemy/ext/compiler.py +++ b/lib/sqlalchemy/ext/compiler.py @@ -30,6 +30,9 @@ Produces:: SELECT [x], [y] +Dialect-specific compilation rules +================================== + Compilers can also be made dialect-specific. The appropriate compiler will be invoked for the dialect in use:: @@ -51,6 +54,9 @@ for the dialect in use:: The second ``visit_alter_table`` will be invoked when any ``postgres`` dialect is used. +Compiling sub-elements of a custom expression construct +======================================================= + The ``compiler`` argument is the :class:`~sqlalchemy.engine.base.Compiled` object in use. This object can be inspected for any information about the in-progress compilation, including ``compiler.dialect``, ``compiler.statement`` etc. @@ -76,6 +82,24 @@ Produces:: "INSERT INTO mytable (SELECT mytable.x, mytable.y, mytable.z FROM mytable WHERE mytable.x > :x_1)" +Changing the default compilation of existing constructs +======================================================= + +The compiler extension applies just as well to the existing constructs. When overriding +the compilation of a built in SQL construct, the @compiles decorator is invoked upon +the appropriate class (be sure to use the class, i.e. ``Insert`` or ``Select``, instead of the creation function such as ``insert()`` or ``select()``). + +Within the new compilation function, to get at the "original" compilation routine, +use the appropriate visit_XXX method - this because compiler.process() will call upon the +overriding routine and cause an endless loop. Such as, to add "prefix" to all insert statements:: + +from sqlalchemy.sql.expression import Insert + +@compiles(Insert) +def prefix_inserts(insert, compiler, **kw) + return compiler.visit_insert(insert.prefix_with("some prefix"), **kw) + +The above compiler will prefix all INSERT statements with "some prefix" when compiled. """ -- 2.47.2