如何从过程中获取 SelectStatement?
How to get SelectStatement from Procedure?
这是 . I'm trying to use DACExtensions 的后续问题,用于从 Procedure
检索 SelectStatement
对象,以便我可以使用 T4 模板从我的 SSDT 项目生成包装函数。问题是我的访客对象的节点 属性 中没有任何节点。我错过了什么?
这是我的访客:
public class SelectVisitor : TSqlFragmentVisitor
{
public SelectVisitor() { this.Nodes = new List<SelectStatement>(); }
public List<SelectStatement> Nodes { get; private set; }
public override void Visit(SelectStatement node)
{
base.Visit(node);
this.Nodes.Add(node);
}
}
下面是我尝试使用它的方式:
// Create the model
var procFiles = Directory.GetFiles(sqlPath, "*.sql", SearchOption.AllDirectories);
var model = new TSqlTypedModel(SqlServerVersion.Sql100, new TSqlModelOptions());
foreach(var procFile in procFiles)
{
model.AddObjects(File.ReadAllText(procFile));
}
// Loop through the procs
var procs = model.GetObjects<TSqlProcedure>(DacQueryScopes.UserDefined);
foreach(var proc in procs){
var selectVisitor = new SelectVisitor();
var ast = proc.GetAst();
ast.Accept(selectVisitor);
foreach(var node in selectVisitor.Nodes){
// Nodes has Count=0 :(
}
}
使用 TSqlModelUtils.TryGetFragmentForAnalysis should ensure you get the original AST inside the model - hopefully that will have what you need. You may want to debug in and view your AST and what actually gets created - that's how we do things internally and often you may be surprised about what actually gets generated. Finally note that the DacpacExplorer 工具可能会使这更容易可视化 - 它现在应该支持显示对象(例如过程)背后的 AST。
这是 Procedure
检索 SelectStatement
对象,以便我可以使用 T4 模板从我的 SSDT 项目生成包装函数。问题是我的访客对象的节点 属性 中没有任何节点。我错过了什么?
这是我的访客:
public class SelectVisitor : TSqlFragmentVisitor
{
public SelectVisitor() { this.Nodes = new List<SelectStatement>(); }
public List<SelectStatement> Nodes { get; private set; }
public override void Visit(SelectStatement node)
{
base.Visit(node);
this.Nodes.Add(node);
}
}
下面是我尝试使用它的方式:
// Create the model
var procFiles = Directory.GetFiles(sqlPath, "*.sql", SearchOption.AllDirectories);
var model = new TSqlTypedModel(SqlServerVersion.Sql100, new TSqlModelOptions());
foreach(var procFile in procFiles)
{
model.AddObjects(File.ReadAllText(procFile));
}
// Loop through the procs
var procs = model.GetObjects<TSqlProcedure>(DacQueryScopes.UserDefined);
foreach(var proc in procs){
var selectVisitor = new SelectVisitor();
var ast = proc.GetAst();
ast.Accept(selectVisitor);
foreach(var node in selectVisitor.Nodes){
// Nodes has Count=0 :(
}
}
使用 TSqlModelUtils.TryGetFragmentForAnalysis should ensure you get the original AST inside the model - hopefully that will have what you need. You may want to debug in and view your AST and what actually gets created - that's how we do things internally and often you may be surprised about what actually gets generated. Finally note that the DacpacExplorer 工具可能会使这更容易可视化 - 它现在应该支持显示对象(例如过程)背后的 AST。