0

With TransactSql.ScriptDom, it is possible to view object model of an SQL statement by extending the TSqlFragmentVisitor class. For each statement type, there is a separate Visit method which can be overridden. But I want to put exactly the same code into each Visit, of each type. I need something like a generic Visit, for all kinds of Statements. How can I do that?

using Microsoft.SqlServer.TransactSql.ScriptDom;
using System.Reflection;
public class CustomVisitor: TSqlFragmentVisitor
{
    private void DoSomething(dynamic obj)
    {
        foreach (var property in obj.GetType().
            GetProperties(BindingFlags.Public | BindingFlags.Instance))
        {
            // Recursively analyse object model to find specific objects
        }
    }

    // Create table
    public override void Visit(CreateTableStatement node)
    {
        DoSomething(node);
        base.Visit(node);
    }

    // Create view
    public override void Visit(CreateViewStatement node)
    {
        DoSomething(node);
        base.Visit(node);
    }

    // ...
    // Huge number of Visit for different types of statement
}
Pavel Sinkevich
  • 619
  • 5
  • 12

1 Answers1

0

I did found a generic method for all kinds of Statements: Visit(TSqlStatement node). The code now looks like this:

    using Microsoft.SqlServer.TransactSql.ScriptDom;
    using System.Reflection;
    public class CustomVisitor : TSqlFragmentVisitor
    {
        private void DoSomething(dynamic obj)
        {
            foreach (var property in obj.GetType().
                GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                // Recursively analyse object model to find specific objects
            }
        }

        // Generic statement
        public override void Visit(TSqlStatement node)
        {
            DoSomething(node);
            base.Visit(node);
        }
    }
// Generic statement
public override void Visit(TSqlStatement node)
{
    DoSomething(node);
    base.Visit(node);
}

}

Pavel Sinkevich
  • 619
  • 5
  • 12
  • `void Visit(TSqlFragment node)` is even more generic, also `base.Visit(node)` is not required for this because the abstract class implementation in `TSqlFragmentVisitor` is always empty – Thymine Jul 16 '18 at 19:54