summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/compiler.py
diff options
context:
space:
mode:
authorKonstantin Tretyakov <kt@ut.ee>2018-12-17 17:21:03 -0500
committersqla-tester <sqla-tester@sqlalchemy.org>2018-12-17 17:21:03 -0500
commit8722fb84a67905aaf9ac6be79255f58e422fa6ea (patch)
tree5cee6268152463f1b5cdaf8e3f1437016a475b78 /lib/sqlalchemy/ext/compiler.py
parent3b6ff1b9f9d740f9a5b11b1774ee77c563926f84 (diff)
downloadsqlalchemy-8722fb84a67905aaf9ac6be79255f58e422fa6ea.tar.gz
Fix the "greatest" example.
The current example code does not pass `**kw` down to the `compiler.process` calls, thus the example does not work when invoked with, `literal_binds=True`. Besides, the calls to `process` each argument twice are wasteful, and reusing the built-in `case` expression instead of hard-coding the SQL statements is slightly nicer overall, isn't it? Closes: #4402 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4402 Pull-request-sha: 24ee93f63e21fccae6cbc1cc1c154dd56f1e1963 Change-Id: I02424d9eb2b35abd5cdec5c2cd5d464a56e7fae6
Diffstat (limited to 'lib/sqlalchemy/ext/compiler.py')
-rw-r--r--lib/sqlalchemy/ext/compiler.py20
1 files changed, 8 insertions, 12 deletions
diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py
index b56943b2b..6a0909d36 100644
--- a/lib/sqlalchemy/ext/compiler.py
+++ b/lib/sqlalchemy/ext/compiler.py
@@ -85,8 +85,8 @@ method which can be used for compilation of embedded attributes::
@compiles(InsertFromSelect)
def visit_insert_from_select(element, compiler, **kw):
return "INSERT INTO %s (%s)" % (
- compiler.process(element.table, asfrom=True),
- compiler.process(element.select)
+ compiler.process(element.table, asfrom=True, **kw),
+ compiler.process(element.select, **kw)
)
insert = InsertFromSelect(t1, select([t1]).where(t1.c.x>5))
@@ -119,10 +119,11 @@ below where we generate a CHECK constraint that embeds a SQL expression::
@compiles(MyConstraint)
def compile_my_constraint(constraint, ddlcompiler, **kw):
+ kw['literal_binds'] = True
return "CONSTRAINT %s CHECK (%s)" % (
constraint.name,
ddlcompiler.sql_compiler.process(
- constraint.expression, literal_binds=True)
+ constraint.expression, **kw)
)
Above, we add an additional flag to the process step as called by
@@ -265,13 +266,13 @@ A synopsis is as follows:
@compiles(coalesce)
def compile(element, compiler, **kw):
- return "coalesce(%s)" % compiler.process(element.clauses)
+ return "coalesce(%s)" % compiler.process(element.clauses, **kw)
@compiles(coalesce, 'oracle')
def compile(element, compiler, **kw):
if len(element.clauses) > 2:
raise TypeError("coalesce only supports two arguments on Oracle")
- return "nvl(%s)" % compiler.process(element.clauses)
+ return "nvl(%s)" % compiler.process(element.clauses, **kw)
* :class:`~sqlalchemy.schema.DDLElement` - The root of all DDL expressions,
like CREATE TABLE, ALTER TABLE, etc. Compilation of ``DDLElement``
@@ -336,7 +337,7 @@ that is of the highest value - its equivalent to Python's ``max``
function. A SQL standard version versus a CASE based version which only
accommodates two arguments::
- from sqlalchemy.sql import expression
+ from sqlalchemy.sql import expression, case
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.types import Numeric
@@ -353,12 +354,7 @@ accommodates two arguments::
@compiles(greatest, 'oracle')
def case_greatest(element, compiler, **kw):
arg1, arg2 = list(element.clauses)
- return "CASE WHEN %s > %s THEN %s ELSE %s END" % (
- compiler.process(arg1),
- compiler.process(arg2),
- compiler.process(arg1),
- compiler.process(arg2),
- )
+ return compiler.process(case([(arg1 > arg2, arg1)], else_=arg2), **kw)
Example usage::