Architect Shack

Navigation



Quick Search
»
Advanced Search »

PoweredBy

Page History: Poor Man's T-SQL Formatter

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: 2011-05-02 02:59


This is a homebrew SQL (T-SQL) formatter, mainly created because I could not find any free and open-source T-SQL formatters that handle T-SQL exactly the way I want, including full stored procedure formatting.

It's written in C#, and designed to work in three phases, each using a pluggable class:
  • Tokenization - text-level parsing, identifying operators vs keywords vs whitespace vs comments, etc
  • Parsing - keyword-level parsing, identifying hierarchical relationships between components of the T-SQL code, statements, clauses, blocks, flow control, etc.
    • Please note, this is not a fully-fledged SQL parser. It does not distinguish between different types of DML, it does not parse full expression trees, etc - there's a lot it doesn't do, it just does the bare minimum to enable the formatting I was aiming for.
  • Formatting - turning the SQL parse tree back into T-SQL code, with customizable formatting preferences including HTML colorizing.

The library (and winforms demo and test projects) is released under the Affero GPL, and may continue to be subject to significant changes as I add the major features that I see are missing. I'll update this again when it reaches a more stable state.

The code can be found on GitHub, I'd be very grateful for any feedback on the code or the demo functionality here: https://github.com/TaoK/PoorMansTSqlFormatter

Demo

Paste some T-SQL here (then leave the textbox):

And it will be formatted here: