使用TransactSql.ScriptDom,可以通过扩展TSqlFragmentVisitor类来查看SQL语句的对象模型。对于每种语句类型,都有一个可以覆盖的单独的Visit方法。但我希望在每种类型的每次访问中都使用完全相同的代码。对于各种陈述,我需要像通用访问这样的东西。我怎么能这样做?
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 :(得分:0)
我确实为各种语句找到了一个通用方法:Visit(TSqlStatement node)。代码现在看起来像这样:
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);
}
}