SQL ScriptDOM

repository·main·Indexed 42 days ago

https://github.com/microsoft/sqlscriptdom

A .NET library for parsing and formatting SQL scripts, converting SQL code into a structured Abstract Syntax Tree (AST) for programmatic analysis. Distributed via the Microsoft.SqlServer.TransactSql.ScriptDom NuGet package, it is compatible with C# and PowerShell applications and includes the AstGen tool for generating AST classes and visitors.

Tokens
1.3K
Snippets
2
Records
5
Agent score
55%

What's inside SQL ScriptDOM

  1. Overview of SQL ScriptDOM capabilities

    main

    SQL ScriptDOM is a .NET library designed for analyzing SQL scripts. It provides two primary capabilities:

    1. Parsing: Converting SQL script text into a structured Abstract Syntax Tree (AST) that can be programmatically analyzed.
    2. Formatting: Reformatting SQL scripts according to specific rules.

    It is used internally by DacFX and can be used as a standalone library in C# and PowerShell applications. The library is built using ANTLR.

  2. Install SQL ScriptDOM via NuGet

    main

    SQL ScriptDOM is available as a .NET library distributed via NuGet. You can use it in C# or PowerShell applications to parse and format SQL scripts. To add it to your project, install the Microsoft.SqlServer.TransactSql.ScriptDom package.

    # Example using dotnet CLI
    dotnet add package Microsoft.SqlServer.TransactSql.ScriptDom
  3. How to file issues and get help with SQL ScriptDOM

    main

    To report bugs or request new features for SQL ScriptDOM, use GitHub Issues. Before creating a new issue, search the existing issue tracker to ensure your topic has not already been addressed.

    For general questions, guidance, and community help regarding the use of this project, join the DacFx Discussions on GitHub.

  4. Use the AstGen tool to generate AST classes

    main

    AstGen is a tool used to generate Abstract Syntax Tree (AST) classes and a visitor (following the Visitor design pattern) to walk through the generated classes.

    Depending on the command line switch used, it can generate either spec classes or implementation classes:

    • Implementation classes: Written to the <output code file>.
    • Spec classes: Written to the <output spec file>.

    The tool processes an input XML file defining the types, classes, interfaces, and members to be generated.

  5. Define AST structures using the AstGen XML format

    main

    To generate classes, provide an XML file with a <Types> root element. You can define <Class>, <Interface>, and <Member> elements.

    Summary comments can be provided either as an attribute (Summary="...") or as a sub-node (<Summary>...</Summary>) for both classes and members.

    XML Schema Reference

    Class Attributes

    • Name (Mandatory): The name of the class.
    • Summary (Optional): Default is an empty string.
    • Base (Optional): The base type for the class. Default is TSqlFragment.
    • Abstract (Optional): Default is false. If true, adds the abstract keyword to the class definition.
    • IsSql80 (Optional): Default is true. Indicates support for SQL 2000 constructs.
    • IsSql90 (Optional): Default is false. Indicates support for SQL 2005 constructs.

    Interface Attributes

    • Name (Mandatory): The name of the interface.
    • Summary (Optional): Default is an empty string.
    • Base (Optional): The base type for the interface. Default is an empty string.

    Member Attributes

    • Name (Mandatory): The name of the member.
    • Type (Mandatory): The type of the member.
    • Summary (Optional): Default is an empty string.
    • Collection (Optional): Default is false. If true, generates a collection property instead of a regular one.
    • GenerateAdder (Optional): Default is true. Controls generation of Add<memberName> for collection members (only applies to collections).
    • GenerateUpdatePositionInfoCall (Optional): Default is true. Controls whether the UpdatePositionInfo call is generated in properties (not applicable to bool, int, or enum).
    • IsSql80 (Optional): Default is true. Indicates support for SQL 2000 constructs.
    • IsSql90 (Optional): Default is false. Indicates support for SQL 2005 constructs.

    ModelBuilderSubstitution Attributes

    Used within a member to define sub-visitor logic:

    • Type (Mandatory): The name of the sub-visitor (which will be generated).
    • Handler (Mandatory): The name of the handler for the sub-visitor (this method is called but NOT generated).
    • VisitChildren (Optional): Default is true. Specifies whether the ExplicitVisit method is overridden in the ModelBuilder.
    <?xml version="1.0" ?> 
    <Types>
       <Class Name = "NullableConstraint"> 
    	<Member Name="Nullable" Type = "bool" GenerateUpdatePositionInfoCall="false"/>
       </Class> 
       <Class Name = "Column" Summary = "Represents a column that is used inside an expression.">
    	<Member Name="Identifiers" Type="Identifier" Collection = "true"/>
    	<Member Name="ColumnType" Type="ColumnType" Summary="Type of the column."/>
       </Class>
       <Class Name = "SimpleTableSource" BaseType = "SelectTableSource" Abstract="true"> 
    	<Member Name="Alias" Type="Identifier" Summary = "As identifier. May be null."/>
       </Class> 
    </Types>