Sublime Text 2 – Format SQL

I’ve posted before about formatting SQL in my IDE. I always inherit code where the previous developer formats their SQL different than I like. Currently I’m suffering from ALL CAPS SQL.

SELECT INT_SUB_ID FROM INT_SUBCATEGORY WHERE INT_CAT_ID = 123

My eyes! My eyes!

I immediately hit Package Control in Sublime and sure enough there is a SQL package: Format SQL.

After installing (and restarting Sublime) you have a new option under Selection. Format > Format SQL Statement.

This got the indention correct but I was looking for some case changes:

SELECT INT_SUB_ID
FROM INT_SUBCATEGORY
WHERE INT_CAT_ID = 123

Looking at the python-sqlparse library documentation there are some statements to change case for keywords and identifiers. So I cracked open \Sublime Text 2\Packages\Format SQL\FormatSQL.py and found this line:

return sqlparse.format(s, keyword_case="upper", reindent=True, indent_width=indent_size)

And modified it like so:

return sqlparse.format(s, keyword_case="upper", identifier_case="lower", reindent=True, indent_width=indent_size)

And now I get:

SELECT int_sub_id
FROM int_subcategory
WHERE int_cat_id = 123

Done!

Recent Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>