我怎样才能解析T-SQL中Microsoft.SqlServer.Management.SqlParser使用类AST [英] How can I parse T-SQL to AST using the classes in Microsoft.SqlServer.Management.SqlParser

查看:278
本文介绍了我怎样才能解析T-SQL中Microsoft.SqlServer.Management.SqlParser使用类AST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类的解析器只有方法的解析返回一个 ParseResult ,然后好像我可以做什么用的 ParseResult
我怎样才能得到我的sql语句,或者仅仅是元数据标记的抽象语法树可以从SQL被解析的罚款。

The class Parser only has the method Parse that returns a ParseResult, and seems I can do nothing with the ParseResult. How can I get the abstract syntax tree of my sql statement, or just the metadata tokens can be parsed from sql is fine.

推荐答案

我做了一些研究,发现我可以使用反射使用生成分析的信息xml文件的 SqlScript.WriteXml
和这里的是示例代码,我不知道是否有更好的办法。

I did some research, and found that I can use reflection to generate the parsed information xml file using SqlScript.WriteXml. And here's is the sample code, I don't know if there is any better way.

var rst = Parser.Parse(File.ReadAllText(@"*.sql"));
var fieldInfo = rst.GetType().GetField("sqlScript", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
var script = fieldInfo.GetValue(rst);
var writer = XmlWriter.Create("*.xml");
script.GetType().InvokeMember("WriteXml", BindingFlags.NonPublic| BindingFlags.Instance | BindingFlags.InvokeMethod
    , null, script, new object[] { writer });
writer.Close();

这篇关于我怎样才能解析T-SQL中Microsoft.SqlServer.Management.SqlParser使用类AST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆