This topic describes how to create a new SQL Enlight analysis template that extends the default template.

  1. 1. Create a new Class Library C# project named MyCompany.SqlEnlight.Analysis.Rules.

    Note

    The project have to be created targeting .NET Framework 2.0.

  2. Change the Default namespace from MyCompany.SqlEnlight.Analysis.Rules to MyCompany.SqlEnlight.Analysis.

    This should be done to ensure correct resolution of the rules’ embedded resources names.

    tutorial-1-1
     

  3. Add the following assembly references:

    • Ubitsoft.SqlEnlight.Model
    • Ubitsoft.SqlEnlight.Analysis.Rules

    The assemblies are located in the SQL Enlight installation folder.

  4. Rename the Class1.cs to CustomAnalysisTemplate.cs.

  5. Modify the class and add replace the content of the CustomAnalysisTemplate.cs file with the following code:

    CopyC#
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Ubitsoft.SqlEnlight.Analysis;
    using Ubitsoft.SqlEnlight.Model.Analysis;
    
    namespace MyCompany.SqlEnlight.Analysis.Rules
    {
        public class CustomAnalysisTemplate : BasicAnalysisTemplate
        {
            public CustomAnalysisTemplate()
                : base()
            {
            }
    
            /// <summary>
            /// The initialization is done in the base class constructor.
            /// </summary>
            protected override void InitializeTemplate()
            {
                base.InitializeTemplate();
            }
        }
    }
  6. Add new folders to the project Rules and Rules\TestScripts.

  7. Add new Batch Analysis Rule to the Rules folder.

    tutorial-1-2
     

    Note

    Make sure to set the desired name of the rule at this step in order to avoid later difficulties renaming the rule files.

  8. Add new Text file to the Rules\TestScripts folder and rename it to Rule1.sql (the name must be same as the rule name) and change its Build Action to Embedded Resource.

    tutorial-1-3
     

The structure of the project after the modifications should look like this:

tutorial-1-4
 

Modify the CustomAnalysisTemplate’s InitializeTemplate method and add initialization code for the new rule:

CopyC#
/// <summary>
/// The initialization is done in the base class constructor.
/// </summary>
protected override void InitializeTemplate()
{
    base.InitializeTemplate();

    Rule1 rule1 = new Rule1();

    // Add the new rule to the collection of existing rules.
    this.Rules.Add(rule1.Name, rule1);

    // Create a new group and add it to the groups collection.
    this.Groups.Add("CustomRules", new AnalysisGroup("CustomRules", "My Rules", "My of SQL analysis rules.", new string[] { rule1.Name }, true));
}

From here you can continue and implement the rule that we created for the template.

How to do this, you can see in the next tutorial: How To: Create Analysis Rule with Visual Studio.

See Also