Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for neatly indenting SQL statements (Python implementation would be nice)

Tags:

I'd like to reformat some SQL statements that are a single string with newlines in to something that's much easier to read.

I don't personally know of a good coding style for indenting SQL - how should nested queries / where clauses / left joins / etc by represented to maximise readability?

Has anyone seen a pretty-printing algorithm that does this already? In Python would be even better.

like image 262
Simon Willison Avatar asked Apr 28 '09 14:04

Simon Willison


1 Answers

You can try sqlparse. It's a Python module that provides simple SQL formatting. A online demo is available here.

like image 101
Andi Albrecht Avatar answered Oct 21 '22 08:10

Andi Albrecht