简体   繁体   English

使用SSDT作为T4模板的源

[英]Using SSDT as source for T4 templates

I have a SQL Server Data Tools (SSDT) project that has a number of stored procedures for which I would like to generate the C# code to call them using T4. 我有一个SQL Server数据工具(SSDT)项目,它有许多存储过程,我想生成C#代码以使用T4调用它们。 Are there any existing examples for doing this? 这样做有没有现成的例子?

So far, I can create a function per proc, but I'd really like to be able to tap into the meta data SSDT creates so I can get the parameters, data types, and return values from it rather than doing string parsing. 到目前为止,我可以创建一个每个proc的函数,但我真的希望能够利用SSDT创建的元数据,这样我就可以从中获取参数,数据类型和返回值,而不是进行字符串解析。

COOL WITH A CAPITAL C! COOL与资本C! (but don't tell anyone who uses an ORM!) (但不要告诉任何使用ORM的人!)

To get the datatypes etc make sure you grab the latest DacExtensions from the MS DacFx team: 要获取数据类型等,请确保从MS DacFx团队获取最新的DacExtensions:

https://github.com/Microsoft/DACExtensions https://github.com/Microsoft/DACExtensions

The new api (which incidentally is written using T4 templates) makes finding the info you need many many times simpler. 新的api(偶然使用T4模板编写)使得您需要的信息简单多了许多倍。

There should be enough information you need in this blog to get you going: 您应该在此博客中提供足够的信息来帮助您:

https://the.agilesql.club/Blogs/Ed-Elliott/DacFx-Create-tSQLt-Tests-From-A-Dacpac https://the.agilesql.club/Blogs/Ed-Elliott/DacFx-Create-tSQLt-Tests-From-A-Dacpac

The only difference is that you are creating C# and not T-SQL so you won't have to deal with the ScriptDom. 唯一的区别是您正在创建C#而不是T-SQL,因此您不必处理ScriptDom。

When you do this, please dump it on github it sounds like a really useful project. 当你这样做时,请把它转储到github上听起来像一个非常有用的项目。

To answer this question in the comments: 要在评论中回答这个问题

I can create methods with the correct parameters, but I'm struggling to find where the objects are in the model are that represent the content of a stored procedure. 我可以用正确的参数创建方法,但我很难找到模型中对象的位置,它们代表了存储过程的内容。 I need to know the columns returned by a SELECT statement in order to generate the return objects. 我需要知道SELECT语句返回的列,以便生成返回对象。 Any ideas? 有任何想法吗?

The referenced objects are provided by the TSqlProcedure.BodyDependencies relationship. 引用的对象由TSqlProcedure.BodyDependencies关系提供。 That will return objects referenced in the stored proc body, but won't tell you how they are used. 这将返回存储过程体中引用的对象,但不会告诉您如何使用它们。 The relational model doesn't try to embed this info as it doesn't help in deployment, but you can get it by querying the SQLDOM AST for the procedure. 关系模型不会尝试嵌入此信息,因为它在部署中没有帮助,但您可以通过查询SQLDOM AST来获取该过程。

The AST is a syntax tree defining the actual structure of the Procedure statement, including the structure of the procedur body. AST是定义Procedure语句的实际结构的语法树,包括程序体的结构。 What you need to do is: 你需要做的是:

  • Create a Visitor that visits SelectStatement nodes (or their children) 创建访问SelectStatement节点(或其子节点)的访问者
  • Find the column names used in the select 找到select中使用的列名称
  • Map these names to names of objects returned by TSqlProcedure.BodyDependencies. 将这些名称映射到TSqlProcedure.BodyDependencies返回的对象的名称。 Now you have a rich object that can state the table the column is contained in, the column's data type, etc. 现在您有一个富对象,可以声明包含列的表,列的数据类型等。
  • Do whatever you need to based on this (for example define a return type with the correct properties matching the column data types?) 基于此做任何你需要的东西(例如,使用与列数据类型匹配的正确属性定义返回类型?)

A few notes / resources: 一些笔记/资源:

  • Ed's DacpacExplorer will help you view and understand the code. Ed的DacpacExplorer将帮助您查看和理解代码。
  • Dave Ballantyne just added SQLDOM support to DacpacExplorer. Dave Ballantyne刚刚为DacpacExplorer添加了SQLDOM支持。 Not only will this help you see what statements you need to match in the visitor, you should also look at how they use loadAsScriptBackedModel (see this commit ) to ensure you have the full AST for the procedure body. 这不仅可以帮助您查看访问者需要匹配的语句,还应该查看他们如何使用loadAsScriptBackedModel(请参阅此提交 )以确保您拥有过程正文的完整AST。 Without this you would just get the body as one SqlScript object which isn't much use to you. 如果没有这个,你只需将主体作为一个SqlScript对象获取,这对你没有多大用处。
  • Further examples of the visitor pattern are Dave's TSqlSmells and the DacExtensions project 访问者模式的更多示例是Dave的TSqlSmellsDacExtensions项目
  • Twitter is an easy way to contact Ed, Dave and me if you are blocked :-) 如果你被阻止,Twitter是一个简单的联系Ed,Dave和我的方式:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM