]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix the "greatest" example.
authorKonstantin Tretyakov <kt@ut.ee>
Mon, 10 Dec 2018 02:20:02 +0000 (03:20 +0100)
committerGitHub <noreply@github.com>
Mon, 10 Dec 2018 02:20:02 +0000 (03:20 +0100)
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?

lib/sqlalchemy/ext/compiler.py

index b56943b2b41bbf8293b921696fb3d636d5e38e23..28fe45964d995ce0b1b1ec093227f99601e8ab52 100644 (file)
@@ -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::