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-04-25 13:49


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.

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

I also plan to make a simple demo available here, hopefully over the next couple of days.

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

© 2007-2012 Tao Klerks | Home | Contact Me