From: Konstantin Tretyakov Date: Mon, 10 Dec 2018 02:20:02 +0000 (+0100) Subject: Fix the "greatest" example. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03ea0b3a0837a60afb21ac85fd4bbf13be0ac0bd;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix the "greatest" example. The current code does not pass `**kw` down to further 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 seems slightly nicer, doesn't it? --- diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py index b56943b2b4..28fe45964d 100644 --- a/lib/sqlalchemy/ext/compiler.py +++ b/lib/sqlalchemy/ext/compiler.py @@ -336,7 +336,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 +353,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::