Azure Data Studio – Poor SQL Extension

When I write T-SQL, I attempt to make it as readable as possible. In order to do this, I must follow best practices for formatting.  There are a number of online formatters that can help you out, which you would depend on if using SQL Server Management Studio.  However, with Azure Data Studio that is no longer that case.  There is an extension named Poor SQL Formatter that we can use.  Like most extensions for Azure Data Studio, this extension is currently in preview.  Despite that, it can still be very useful.

Installing the Extension

To install the extension, the first thing we need to do if find it.  This can be done by clicking the Extension item under the View menu.  Once the Extension window appears, simply type the work “poor” in the search window.

When you click install, you will be taken to the GitHub page for the extension.  You will need to download the vsix file.  You will need to place it in a location that will not be overwritten or deleted.
Once you have downloaded the file, you will now need to install it.  This can be done by going to the File menu and clicking “Install extension from VSIX package”.  Then you will need to navigate to the location you placed the file.  When you locate the file, Azure Data Studio will then install it.  You will see a popup in the lower right corner that the install is complete.
Once installed, this extension does not require a restart of Azure Data Studio.
What does this Extension Do?
According to the documentation in ADS the extension provides the following help with formatting TSQL.  These are copied directly from the documentation.
      • poorSql.tabIndent: Use tabs for indentation?
      • poorSql.numIndentSpaces: Number of spaces to use when indenting. Ignored if tabIndent is true.
      • poorSql.maxLineWidth: Max characters per line.
      • poorSql.statementBreaks: Number of line breaks between statements.
      • poorSql.clauseBreaks: Number of line breaks between clauses within a statement.
      • poorSql.expandCommaLists: Expand comma-delimited lists onto new lines?
      • poorSql.trailingCommas: Should commas be at the end of lines?
      • poorSql.spaceAfterExpandedComma: Should a space be added after commas?
      • poorSql.expandBooleanExpressions: Should boolean operators cause a linebreak?
      • poorSql.expandCaseStatements: Should WHEN and THEN expressions in a CASE statement cause a linebreak?
      • poorSql.expandBetweenConditions: Should arguments of BETWEEN expressions cause linebreaks?
      • poorSql.expandInLists: Should IN lists be split by linebreaks?
      • poorSql.breakJoinOnSections: Should the ON section of a JOIN clause cause a linebreak?
      • poorSql.uppercaseKeywords: Should keywords be automatically uppercased?
      • poorSql.keywordStandardization: Should less-common keywords be replaced with common alternatives? (Caution: only safe for T-SQL)
      • poorSql.obfuscate.randomizeKeywordCase: Obfuscation: should the case of keywords be randomized?
      • poorSql.obfuscate.randomizeLineLengths: Obfuscation: should line lengths be randomized?
      • poorSql.obfuscate.preserveComments: Obfuscation: should comments be preserved?
      • poorSql.obfuscate.enableKeywordSubstitution: Obfuscation: allow common keywords to be replaced with less common alternatives? (Caution: only safe for T-SQL)

Now that the extension is installed, you will see the above items in Settings. You can get to settings under the File menu, File –> Preferences –> Settings.  The best way to find all the settings related to this extension is to do a search on the word “poor”. Most of these are Boolean settings, True or False.  However there are some that are not.  The settings below are the ones that are not Boolean and the default value is also listed.

      • poorSql.numIndentSpaces is set to 4
      • poorSql.maxLineWidth is set to 999
      • poorSql.clauseBreaks is set to 1
      • poorSql.statementBreaks is set to 2

There are a few settings that are not enabled by default.  These are listed below.  Something to keep in mind, the extension is still in preview and could change with the final release.

      • poorSql.trailingCommas
      • poorSql.tabIndent
      • poorSql.obfuscate.enableKeywordSubstitution
      • poorSql.keywordStandardization
      • poorSql.expandInLists
      • poorSql.expandBetweenConditions
Now that it is installed, how do I use it?
If you look at the code below, you may see a few coding issues. these are issues I believe make it more difficult to read the code.
      • Not all key words a capitalized
      • The columns in the SELECT are kind of sloppy.  There are three columns in one line with the other two are the only column in the line
      • The CASE is more difficult to read because it is on a single line
      • The joins are hard to read because they are not organized in a readable manner
Below is the unformatted code.
select p.Class
    , p.Color, p.ProductID, p.ProductNumber,
    , CASE WHEN p.color = ‘Black’THEN ‘BL’ END
from Production.Product p INNER JOIN Sales.SalesOrderDetail o ON p.ProductID = o.ProductID
inner JOIN sales.SalesOrderHeader h ON o.SalesOrderID = h.SalesOrderID
WHERE color = ‘black’
In order to use the PoorSQL extension, you will need to use the Command Pallet.  The Command Pallet can be found under the View menu. Once you click it you will see something similar to the image below.  The Command Pallet is a location that lists out many commands that we will use.  As more extensions are added, many of them place commands here to make it easier to use.  The Command Pallet if used can make our lives just a bit easier.
To find the PoorSQL commands, just type the word Poor into the search box and you will see the commands in the screenshot below.
If you pick the bottom one, SQL: Obfuscate, this will apply all the settings that have Obfuscate in the name.  If you choose the top option, SQL: Format, it will apply all the other settings, ones that don’t have Obfuscate in the title.
Once you have made a choice and clicked on it, all the appropriate settings will be allied. In the case of the code above, I chose SQL:Format.
SELECT p.Class
    , p.Color
    , p.ProductID
    , p.ProductNumber
    , p.Size
    , CASE
        WHEN p.color = ‘Black’
            THEN ‘BL’
FROM Production.Product p
INNER JOIN Sales.SalesOrderDetail o
    ON p.ProductID = o.ProductID
INNER JOIN sales.SalesOrderHeader h
    ON o.SalesOrderID = h.SalesOrderID
WHERE color = ‘black’
After utilizing SQL:Format in the Poor SQL Formatter, the code is much easier to read and did not take very long to apply the formatting rules.  Of course being easier to read is my opinion, you will have to make that determination for yourself.
Overall, I do like this extension and would recommend to install it.  However, please keep in mind that this in still in Preview and may change before the official release.
Thanks for visiting my blog!!!