Architect Shack

Navigation



Quick Search
»
Advanced Search »

PoweredBy

Poor Man's T-SQL Formatter

RSS
Modified on 2017-11-24 04:20 by TaoK Categorized as C#, dotNet 2, IIS, SQL
Try it now on PoorSQL.com, the free
online TSQL formatting service
!

Overview

This is a free and open-source SQL (T-SQL) formatter:
  • Handles complete multi-batch scripts, including object definition scripts such as stored procedures, triggers, etc.
  • Provides formatting options to cater to different common formatting styles/standards
  • Optionally outputs "colorized" html code rather than just the formatted SQL
  • Also provides "minifier" option to strip out comments and whitespace, to obfuscate rather than pretty-print your code
  • Available ready-to-use in a variety of forms
    • SSMS (SQL Server Management Studio) and Visual Studio Addin/Extension that allows you to format the current file or selected text with a single hotkey - supports any version of SSMS or SSMS Express, and any Full (not Express) version of Visual Studio.
    • Notepad++ plugin, for quick single-key formatting in your favorite general-purpose text editor.
    • Command-line utility that lets you format any number of files in bulk, or format from some other arbitrary program - for windows (.Net) or any environment (node/npm)
    • Winforms app for easy offline formatting (also lets you look at the token stream and parse tree)
    • WinMerge plugin, for automatically formatting SQL files before comparison, allowing WinMerge to display content changes only, ignoring formatting differences.
    • JS library that exposes the same functionality in any browser or other Javascript-based context, used in the demo/online formatting site http://poorsql.com
  • Also available as a .Net 2.0/3.5 library, downloadable here or through NuGet
  • Written in C# using a pluggable design that should allow for other SQL dialects to be supported in future

This formatter does implement a high-level SQL tokenizer and parser, but the granularity of the parser is not very high. 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 support the target formatting.

The library is largely complete, but undocumented. For the list of known issues, please see the issue tracker on github. As there is only as much parsing detail as necessary to support the desired formatting, the parse tree format is still not finalized; many formatting enhancements require new elements in the parse tree.

The code is hosted on GitHub at https://github.com/TaoK/PoorMansTSqlFormatter; I'm always very grateful for any feedback on the functionality, the code or any other aspect of the project!

To keep an eye on new releases or other related news,



I also send out occasional announcement emails to people who've contacted me - email me at if you'd like to get these announcements (this might eventually become a real mailing list).



Download

Ready-to-run:



  • Notepad++ plugin: It's recommended to use the Notepad++ plugin manager if available (Notepad++ is an amazing free GPL-licensed text editor, available at notepad-plus-plus.org; it has a very rich plugin community available through the "Plugin Manager" plugin. This is no longer installed by default, but adding it is very easy, and some replacement will presumably be made available shortly).
    • If you need to install the plugin manually, you can get it from here: SqlFormatterNppPlugin.1.6.10.zip (3418 downloads for the latest version, about 400000 for previous versions) and unzip the entire contents of the ZIP file (1 DLL + 1 folder containing other DLLs and stuff) into your Notepad++ install folder's "plugins" subfolder, eg "C:\Program Files\Notepad++\plugins". If you get an error about the plugin not supporting unicode, then the wrong DLL is in the plugins folder, simply delete the incorrect DLLs and start again (please contact me for help if you have any issues with this).
    • 64-bit version : SqlFormatterNppPlugin.x64.1.6.10.zip (1296 downloads for the latest version, about 350 for previous versions)





Compiled Library to use in other GPL/AGPL projects or internal non-distributed projects:
JS Library to use in other GPL/AGPL projects or internal non-distributed projects:
Source:

Latest Changes

FORMATTER ERROR (Malformed List) FORMATTER ERROR (Malformed List)
  • Appveyor build configuration, as per proposal in Pull Request #178 from chcg

FORMATTER ERROR (Malformed List)
  • Fixed newly split SSMS Extension Installer to set correct registry key at install time.
  • Restructured changelog to follow Keep a Changelog(http://keepachangelog.com/)

Full Change Log:
Known / Open Issues:

License / Redistribution

The library and accompanying programs are released under the Affero GPL. This means that you are allowed to make any changes you want, there are restrictions/requirements attached only if you choose to redistribute the original or modified code: If you choose to redistribute/expose the functionality, then you must make the source available to the recipients/users (even of a web service). For private usage, you can make any changes you want and never need to tell anyone; if you distribute the app/functionality or a service that relies on it, then you must also distribute the source. For more details, please see the AGPL article on wikipedia or at the source, the FSF (free software foundation).

At least for the moment (as I'm the only one who has contributed to the project so far), the project could be dual-licensed if you really, really wanted to use it in a commercial program. If so please contact me, I'm sure we could work something out.

Tips & Tricks

  • If you don't want a certain block of code to be reformatted, then just put a comment containing [noformat] before and [/noformat] after, like this:
    SELECT 1,
    2,
    3
    -- [noformat] don't want any formatting here, I like it with spaces before AND after the commas here.
    SELECT 4 , 5 , 6 , 7 , 8 , 9
    --[/noformat]
    SELECT 1,
    2,
    3
  • If you want to minimize the space taken by a chunk of SQL, and don't care about legibility, you can minify it with a comment containing [minify] before and [/minify] after, like this:
    SELECT 1,
    2,
    3
    --[minify]
    SELECT 4AS[Four],5AS Five,6,7,8,9
    --[/minify]
    SELECT 1,
    2,
    3
  • Loren Halvorson pointed out you can use the command-line formatter to pre-format SQL files in Beyond Compare, with a command like this:
    SqlFormatter –o "%t" "%s"
  • Paul Kohler shared a registry change he uses to format files from the Windows Explorer context menu (save as something.reg and run it):
    Windows Registry Editor Version 5.00
    [HKEY_CLASSES_ROOT\*\shell\Reformat SQL]
    [HKEY_CLASSES_ROOT\*\shell\Reformat SQL\command]
    @="C:\\Tools\\SqlFormatter.1.2.1\\SqlFormatter.exe \"%1\" /is:\" \" /tc+"
  • If you're the author of a SQL-related tool and you want to make use of the formatting library, but you don't want to release your project under the AGPL license, then you can always use the command-line formatter as an optional external tool. If you then redistribute the command-line tool you need to publish/provide the source of the formatter tool, but you don't need to relicense your project to match. Even simpler, you can suggest that users download the formatter separately to enable formatting functionality.

Feedback & Known Issues

For general feedback please email me at .

To browse existing / known issues or add new ones to be tracked, please use the GitHub issue tracker.

Instant Demo

The demo has been moved to poorsql.com - check it out and contact me with any issues!

Contributing

So far I many contributions, but contributing is easy with GitHub! Just fork the project, make your changes, and create a Pull Request for me to take a look and I will (if I agree with the changes of course) happily incorporate them!

https://github.com/TaoK/PoorMansTSqlFormatter

I need to formulate a "Contributor Agreement" to be able to accept any significant contributions, but that should be quick enough to do as the need arises, please let me know if you're interested.

Or if you just want to show your support, you can report your use of the project on Ohloh:

Background

Other Products and Projects

There is a comparison of all the SQL Formatting products that I know of, in wiki (editable) form, in the project wiki at https://github.com/TaoK/PoorMansTSqlFormatter/wiki/SQL-Formatter-comparisons.

(Previously there was a comparison table here, but it was years out of date and not editable by anyone but me. That was silly.)

Your Formatting preferences? Who are you?

The only reason my personal preferences are important here, is that I'm the one writing this :).

What I can do, however, is explain the rationale behind my preferences, in the hope that I might sway someone's opinion:
  • I find that too much whitespace in the body/joins distracts from the structure of the query, especially if you end up having to scroll to find parts of the query. This is why I'm not fond of indenting the "ON" portion of a join clause
  • I find that logical expressions are easiest to read (and write) when each portion is on its own line. This applies regardless of whether it's in a join clause's "ON" conditions, or the "WHERE" clause
  • I find it very important to be able to write SQL (in a variety of editors) in the target format, without needing something to clean up after you. This is the main reason I don't like "Aligned" SQL formatting. I understand it can be more legible, but it's too much of a pain to maintain (esp. in embedded/ad-hoc SQL queries). It also breaks when you use tabs (in different editors with different tab stops), and I like tabs! They're easy-to-navigate whitespace!

What if I want my SQL formatted differently?

I am completely open to providing additional formatting options, obviously within the time I have available to devote to this project. Existing open requests are tracked on the GitHub issue tracker, with prefix "Option: ". As the project is open source and hosted on github, it's also trivial to fork the project, download to your local machine and get cracking on any changes you'd like to see!

  Name Size
- PoorMansTSqlFormatterSSMSAddIn.Setup.1.6.1.msi 756.00 KB
- PoorMansTSqlFormatterSSMSAddIn.Setup.1.6.10.msi 696.00 KB
- PoorMansTSqlFormatterSSMSAddIn.Setup.1.6.6.msi 696.00 KB
- PoorMansTSqlFormatterSSMSPackage.Setup.1.6.1.msi 748.00 KB
- PoorMansTSqlFormatterSSMSPackage.Setup.1.6.10.msi 688.00 KB
- PoorMansTSqlFormatterSSMSPackage.Setup.1.6.11.msi 688.00 KB
- PoorMansTSqlFormatterSSMSPackage.Setup.1.6.6.msi 688.00 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.0.9.11.msi 432.00 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.0.9.12.msi 435.00 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.0.9.13.msi 436.00 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.1.0.1.msi 437.50 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.1.1.1.msi 438.00 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.1.2.1.msi 438.00 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.1.3.1.msi 441.50 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.1.4.1.msi 442.00 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.1.5.1.msi 444.50 KB
- PoorMansTSqlFormatterWinMergePlugin.Setup.msi 396.00 KB
- SqlFormatter.0.9.10.zip 69.98 KB
- SqlFormatter.0.9.11.zip 70.50 KB
- SqlFormatter.0.9.12.zip 73.88 KB
- SqlFormatter.0.9.13.zip 75.14 KB
- SqlFormatter.0.9.8.zip 43.47 KB
- SqlFormatter.0.9.9.zip 69.72 KB
- SqlFormatter.1.0.1.zip 76.81 KB
- SqlFormatter.1.1.1.zip 81.31 KB
- SqlFormatter.1.2.1.zip 82.44 KB
- SqlFormatter.1.3.1.zip 86.65 KB
- SqlFormatter.1.4.1.zip 82.46 KB
- SqlFormatter.1.5.1.zip 85.85 KB
- SqlFormatter.1.6.10.zip 93.67 KB
- SqlFormatterLibrary.0.9.10.zip 157.38 KB
- SqlFormatterLibrary.0.9.12.zip 171.88 KB
- SqlFormatterLibrary.0.9.13.zip 177.65 KB
- SqlFormatterLibrary.0.9.8.zip 103.92 KB
- SqlFormatterLibrary.0.9.9.zip 157.06 KB
- SqlFormatterLibrary.1.0.1.zip 183.52 KB
- SqlFormatterLibrary.1.1.1.rar 157.38 KB
- SqlFormatterLibrary.1.2.1.zip 185.58 KB
- SqlFormatterLibrary.1.3.1.zip 204.28 KB
- SqlFormatterLibrary.1.4.1.zip 203.18 KB
- SqlFormatterLibrary.1.4.2.zip 204.98 KB
- SqlFormatterLibrary.1.4.3.zip 205.56 KB
- SqlFormatterLibrary.1.5.1.zip 219.92 KB
- SqlFormatterLibrary_35.1.3.1.zip 215.36 KB
- SqlFormatterLibrary_35.1.4.1.zip 155.19 KB
- SqlFormatterLibrary_35.1.4.2.zip 156.65 KB
- SqlFormatterLibrary_35.1.4.3.zip 157.78 KB
- SqlFormatterLibrary_35.1.5.1.zip 171.80 KB
- SqlFormatterNppPlugin.1.3.1.zip 223.89 KB
- SqlFormatterNppPlugin.1.4.1.zip 222.83 KB
- SqlFormatterNppPlugin.1.5.1.zip 226.68 KB
- SqlFormatterNppPlugin.1.6.10.zip 161.70 KB
- SqlFormatterNppPlugin.1.6.6.zip 162.87 KB
- SqlFormatterNppPlugin.x64.1.6.1.zip 232.24 KB
- SqlFormatterNppPlugin.x64.1.6.10.zip 161.71 KB
- SqlFormatterNppPlugin.x64.1.6.6.zip 162.89 KB
- SqlFormatterNppPlugin1.2.1.zip 208.78 KB
- SqlFormatterSSMSAddIn.Setup.0.9.10.msi 479.50 KB
- SqlFormatterSSMSAddIn.Setup.0.9.12.msi 483.00 KB
- SqlFormatterSSMSAddIn.Setup.0.9.13.msi 485.00 KB
- SqlFormatterSSMSAddIn.Setup.0.9.8.msi 452.00 KB
- SqlFormatterSSMSAddIn.Setup.0.9.9.msi 479.00 KB
- SqlFormatterSSMSAddIn.Setup.1.0.1.msi 486.00 KB
- SqlFormatterSSMSAddIn.Setup.1.1.1.msi 516.50 KB
- SqlFormatterSSMSAddIn.Setup.1.2.1.msi 512.00 KB
- SqlFormatterSSMSAddIn.Setup.1.3.1.msi 575.50 KB
- SqlFormatterSSMSAddIn.Setup.1.4.1.msi 575.00 KB
- SqlFormatterSSMSAddIn.Setup.1.4.4.msi 573.00 KB
- SqlFormatterSSMSAddIn.Setup.1.5.1.msi 578.50 KB
- SqlFormatterSSMSAddIn.Setup.1.5.1_alternative.msi 578.50 KB
- SqlFormatterSSMSAddIn.Setup.1.5.2.msi 579.00 KB
- SqlFormatterSSMSAddIn.Setup.1.5.3.msi 579.00 KB
- SqlFormatterWinForms.0.9.10.zip 108.99 KB
- SqlFormatterWinForms.0.9.12.zip 112.75 KB
- SqlFormatterWinForms.0.9.13.zip 114.16 KB
- SqlFormatterWinForms.0.9.8.zip 82.67 KB
- SqlFormatterWinForms.0.9.9.zip 108.80 KB
- SqlFormatterWinForms.1.0.1.zip 115.47 KB
- SqlFormatterWinForms.1.1.1.zip 147.60 KB
- SqlFormatterWinForms.1.2.1.zip 147.61 KB
- SqlFormatterWinForms.1.3.1.zip 155.83 KB
- SqlFormatterWinForms.1.4.1.zip 150.68 KB
- SqlFormatterWinForms.1.4.3.zip 151.38 KB
- SqlFormatterWinForms.1.5.1.zip 156.14 KB
- SqlFormatterWinforms.1.6.10.zip 148.12 KB
- SqlFormatterWinforms.1.6.6.zip 152.91 KB
- SqlFormatterWinMergePlugin.0.9.11.rar 62.55 KB

© 2007-2012 Tao Klerks | Home | Contact Me