From: Konstantin Tretyakov Date: Mon, 17 Dec 2018 22:21:03 +0000 (-0500) Subject: Fix the "greatest" example. X-Git-Tag: rel_1_3_0b2~61^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8722fb84a67905aaf9ac6be79255f58e422fa6ea;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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 --- diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py index b56943b2b4..6a0909d361 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::