title: Specifying sorting direction in psycopg2 by parameter
body: <p>I want to specify in a query the sorting direction (ASC/DESC) by parameter, but psycopg2 throws an error as I'm sending a string as a replacement for a reserved word:</p>
  
  <pre><code>ProgrammingError: syntax error at or near "'ASC'"
  </code></pre>
  
  <p>this is my code:</p>
  
  <pre><code>cursor.execute("""
      SELECT * FROM foo
          ORDER BY
              CASE %(order_by)s
                  WHEN 'a' THEN textfield_a %(sort_direction)s
                  WHEN 'b' THEN textfield_b %(sort_direction)s
                  ELSE 'c' THEN textfield_c
              END""", dict(order_by='a', sort_direction='ASC'))
  </code></pre>
  
  <p>Is there a way in psycopg2 to do this?</p>
  
  <p>Of course a possible solution would be to concatenate both parameters and duplicate the condtions like:</p>
  
  <pre><code>ORDER BY
  CASE %(order_by_concatenation)s
      WHEN 'a:ASC' THEN textfield_a ASC
      WHEN 'a:DESC' THEN textfield_a DESC
      WHEN 'b:ASC' THEN textfield_b ASC
      WHEN 'b:DESC' THEN textfield_b DESC
      ELSE 'c' THEN textfield_c
  END
  </code></pre>
  
  <p>but I don't like this idea, as in the future I might have many more conditions...</p>