2016-02-19 61 views
0

使用TransactSql.ScriptDom,可以通過擴展TSqlFragmentVisitor類來查看SQL語句的對象模型。對於每種語句類型,都有一個可以被覆蓋的單獨的訪問方法。但是我想爲每種類型的訪問都使用完全相同的代碼。我需要一個類似於訪問的東西,用於各種語句。我怎樣才能做到這一點?在TransactSql.ScriptDom中創建通用語句的訪問者

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 
} 

回答

0

我確實發現了各種語句的通用方法:訪問(TSqlStatement節點)。現在,該代碼如下所示:

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); 
} 

}