How can we go about customizing sqlparse?

167 views
Skip to first unread message

Matthew Moisen

unread,
Aug 28, 2017, 3:25:24 AM8/28/17
to sqlparse
There are many different preferences for how SQL queries should be written. How can I go about customizing the output of sqlparse?

Here are two examples:

sqlparse.format('CASE WHEN 1 THEN 2 WHEN 3 THEN 4 ELSE 5 END', reindent=True)

sqlparse's output

CASE
    WHEN 1 THEN 2
    WHEN 3 THEN 4
    ELSE 5
END

My preference:

CASE
    WHEN 1 THEN
        2
    WHEN 3 THEN
        5
    ELSE
        5
END

sqlparse.format('SELECT 1, 2 FROM foo WHERE 1=1 AND ((2=2 or 3=3) AND 4=4)', reindent=True)

sqlplarse's output

SELECT 1,
       2
FROM foo
WHERE 1=1
  AND ((2=2
        or 3=3)
       AND 4=4)

My preference:

SELECT 1,
       2
FROM foo
WHERE 1=1
  AND (
    (
      2=2
      or 3=3
   )
   AND 4=4
  )

Thanks and best regards,

Matthew

Andi Albrecht

unread,
Aug 28, 2017, 4:41:07 AM8/28/17
to sqlparse
Hi Matthew,

there are already some formatting options. Run "sqlformat --help" to see what's currently implemented. AFAIK the documentation lacks some options - shame on me, I'll fix it ;)

However, I don't think that some options are missing that'd be required to format the statements as shown in your examples. Please open a ticket on the issue tracker at https://github.com/andialbrecht/sqlparse/ for any missing option.

Best regards,

Andi

--
You received this message because you are subscribed to the Google Groups "sqlparse" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlparse+u...@googlegroups.com.
To post to this group, send email to sqlp...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlparse.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages