ScriptDOM .NET library for T-SQL parsing is now open source
Published Apr 25 2023 02:00 PM 9,918 Views

ScriptDOM is a powerful .NET library for code parsing, generating an abstract syntax tree (AST) that can be leveraged to apply code formatting, detect antipatterns, and more. We are thrilled to announce that the source code for ScriptDOM has been released into open source under the MIT license and is available on GitHub.  In addition, ScriptDOM is now distributed by Microsoft as a standalone NuGet package.

 

ScriptDOM capabilities 

Several use cases for ScriptDOM are mentioned in Arvind Shyamsundar’s excellent review article, “Programmatically parsing Transact SQL (T-SQL) with the ScriptDom parser”. Each use of ScriptDOM is rooted in the construction of an abstract syntax tree that can be accessed by subsequent interactions. The following example code reads text from a file and checks it for T-SQL syntax errors: 

 

 

using Microsoft.SqlServer.TransactSql.ScriptDom;

namespace Azure.Samples
{
    class Program
    {
        static int Main(string[] args)
        {

            // get file path from user
            Console.WriteLine("Enter SQL file path:");
            string? filePath = Console.ReadLine();
            while (filePath == null)
            {
                filePath = Console.ReadLine();
            }

            using (StreamReader rdr = new StreamReader(filePath))
            {
                // parse file with scriptdom
                IList<ParseError> errors = new List<ParseError>();
                var parser = new TSql160Parser(true, SqlEngineType.All);
                var tree = parser.Parse(rdr, out errors);

                if (errors.Count == 0)
                {
                    Console.WriteLine("No errors found.");
                }
                foreach (ParseError err in errors)
                {
                    // print errors
                    Console.Error.WriteLine(err.Message);
                    return -1;
                }
            }
            return 0;
        }
    }
}

 

 

The many classes within the ScriptDOM namespace are constructed around the TSqlParser class. As new syntax is added to Azure SQL and SQL Server, it is aligned with the appropriate derived class, such as TSql160Parser.

 

Community involvement 

We believe that ScriptDOM has the potential to grow beyond the valuable library it is today with a nearly limitless future powered by developers. By open sourcing ScriptDOM, we hope to enable more people to build innovative tools and projects on top of this library. You are encouraged to check out the source code on the GitHub repository, contribute to the project, and share your feedback with us. 

 

Here are some community projects and blog posts to inspire you: 

https://github.com/arvindshmicrosoft/SQLScriptDomSamples 

https://github.com/davebally/TSQL-Smells 

https://www.sqlservercentral.com/stairways/stairway-to-scriptdom  

https://the.agilesql.club/2015/11/how-to-get-started-with-the-scriptdom/

 

Speaking of community - we'd also like to thank Duncan Smart, who graciously transferred the NuGet.org package name Microsoft.SqlServer.TransactSql.ScriptDom to the team.

 

We look forward to engaging with you both in the ScriptDOM repository and in the DacFx discussions.

Version history
Last update:
‎Apr 25 2023 02:03 PM
Updated by: