• 使用MS的ScriptDom来拆解TSQL脚本


    此处提供9.1.40413.0版本的DLL一共4个:Microsoft.Data.Schema.dll、Microsoft.Data.Schema.ScriptDom.dll、Microsoft.Data.Schema.ScriptDom.Sql.dll、Microsoft.Data.Schema.Sql.dll。传送:https://pan.baidu.com/s/1geOH7qz

    废话不多说,直接上代码:

    public static IEnumerable<string> ParserInternal(TextReader reader)
            {
                var parser = new TSql100Parser(false);
                var errors = new List<ParseError>();
                var tokens = parser.GetTokenStream(reader, errors);
    
                if (errors.Count > 0)
                {
                    var sbError = new StringBuilder();
                    sbError.AppendLine("SQL Script Parse Failed!");
                    foreach (var error in errors)
                    {
                        sbError.AppendLine($"	Line:{error.Line} Column:{error.Column} Error:{error.Message}");
                    }
                    throw new InvalidDataException(sbError.ToString());
                }
    
                var isSkip = true;
                var sbScript = new StringBuilder();
                foreach (var token in tokens)
                {
                    switch (token.TokenType)
                    {
                        case TSqlTokenType.SingleLineComment:
                        case TSqlTokenType.MultilineComment:
                        case TSqlTokenType.WhiteSpace:
                            sbScript.Append(token.Text);
                            break;
                        case TSqlTokenType.Go:
                            if (!isSkip)
                            {
                                isSkip = true;
                                yield return sbScript.ToString();
                            }
                            sbScript = new StringBuilder();
                            break;
                        default:
                            isSkip = false;
                            sbScript.Append(token.Text);
                            break;
                    }
                }
                if (sbScript.Length > 0)
                    yield return sbScript.ToString();
            }
  • 相关阅读:
    Windows10 搭建 ElasticSearch 集群服务
    python开发目录
    nohup命令使用
    shell脚本概述
    关于ELK
    keepalived+DR
    shell脚本中awk的用法
    shell脚本中sed的用法
    shell脚本中grep的用法
    shell检测入侵与邮件报警
  • 原文地址:https://www.cnblogs.com/xuhaibiao/p/9251502.html
Copyright © 2020-2023  润新知