PoshCode Logo PowerShell Code Repository

SQLParser.ps1 by Chad Miller 27 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1445"></script>download | new post

Uses Visual Studio Database Edition classes Microsoft.Data.Schema.ScriptDom and Microsoft.Data.Schema.Script.Sql to parse T-SQL

  1. #requires -version 2
  2.  
  3. #Uses Visual Studio Database Edition classes Microsoft.Data.Schema.ScriptDom and Microsoft.Data.Schema.Script.Sql to parse T-SQL
  4. #Chad Miller
  5. #http://chadwickmiller.spaces.live.com/
  6.  
  7. $PSScriptRoot = (Split-Path $MyInvocation.MyCommand.Path -Parent)
  8.  
  9.  
  10. Add-Type -Path "$PSScriptRoot\Microsoft.Data.Schema.ScriptDom.dll"
  11. Add-Type -Path "$PSScriptRoot\Microsoft.Data.Schema.ScriptDom.Sql.dll"
  12.  
  13. $Source = @"
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using Microsoft.Data.Schema.ScriptDom;
  19. using Microsoft.Data.Schema.ScriptDom.Sql;
  20. using System.IO;
  21.  
  22.    public class SQLParser
  23.    {
  24.        private IScriptFragment fragment;
  25.        
  26.        public SQLParser(SqlVersion sqlVersion, bool quotedIdentifier, string inputScript)
  27.        {
  28.            switch (sqlVersion)
  29.            {
  30.                case SqlVersion.Sql80:
  31.                    SQLParser80 (quotedIdentifier, inputScript);
  32.                    break;
  33.                case SqlVersion.Sql90:
  34.                    SQLParser90 (quotedIdentifier, inputScript);
  35.                    break;
  36.                case SqlVersion.Sql100:
  37.                    SQLParser100 (quotedIdentifier, inputScript);
  38.                    break;
  39.            }
  40.        }
  41.        
  42.        private void SQLParser100 (bool quotedIdentifier, string inputScript)
  43.        {
  44.            TSql100Parser parser = new TSql100Parser(quotedIdentifier);
  45.            Parse(parser, inputScript);
  46.        }
  47.  
  48.        private void SQLParser90 (bool quotedIdentifier, string inputScript)
  49.        {
  50.            TSql90Parser parser90 = new TSql90Parser(quotedIdentifier);
  51.            Parse(parser90, inputScript);
  52.        }
  53.  
  54.        private void SQLParser80 (bool quotedIdentifier, string inputScript)
  55.        {
  56.            TSql80Parser parser80 = new TSql80Parser(quotedIdentifier);
  57.            Parse(parser80, inputScript);
  58.        }
  59.  
  60.        private void Parse(TSql100Parser parser, string inputScript)
  61.        {
  62.            IList<ParseError> errors;
  63.  
  64.            using (StringReader sr = new StringReader(inputScript))
  65.            {
  66.                fragment = parser.Parse(sr, out errors);
  67.            }
  68.            
  69.            if (errors != null && errors.Count > 0)
  70.            {
  71.                StringBuilder sb = new StringBuilder();
  72.                foreach (var error in errors)
  73.                {
  74.                    sb.AppendLine(error.Message);
  75.                    sb.AppendLine("offset " + error.Offset.ToString());
  76.                }
  77.                throw new ArgumentException("InvalidSQLScript", sb.ToString());
  78.            }
  79.        }
  80.  
  81.        private void Parse(TSql90Parser parser, string inputScript)
  82.        {
  83.            IList<ParseError> errors;
  84.  
  85.            using (StringReader sr = new StringReader(inputScript))
  86.            {
  87.                fragment = parser.Parse(sr, out errors);
  88.            }
  89.            
  90.            if (errors != null && errors.Count > 0)
  91.            {
  92.                StringBuilder sb = new StringBuilder();
  93.                foreach (var error in errors)
  94.                {
  95.                    sb.AppendLine(error.Message);
  96.                    sb.AppendLine("offset " + error.Offset.ToString());
  97.                }
  98.                throw new ArgumentException("InvalidSQLScript", sb.ToString());
  99.            }
  100.        }
  101.  
  102.        private void Parse(TSql80Parser parser, string inputScript)
  103.        {
  104.            IList<ParseError> errors;
  105.  
  106.            using (StringReader sr = new StringReader(inputScript))
  107.            {
  108.                fragment = parser.Parse(sr, out errors);
  109.            }
  110.  
  111.            if (errors != null && errors.Count > 0)
  112.            {
  113.                StringBuilder sb = new StringBuilder();
  114.                foreach (var error in errors)
  115.                {
  116.                    sb.AppendLine(error.Message);
  117.                    sb.AppendLine("offset " + error.Offset.ToString());
  118.                }
  119.                throw new ArgumentException("InvalidSQLScript", sb.ToString());
  120.            }
  121.        }
  122.  
  123.        public IScriptFragment Fragment
  124.        {
  125.            get { return fragment; }
  126.        }
  127.  
  128.        
  129.    }
  130. "@
  131.  
  132. $refs = @("$PSScriptRoot\Microsoft.Data.Schema.ScriptDom.dll","$PSScriptRoot\Microsoft.Data.Schema.ScriptDom.Sql.dll")
  133. add-type -ReferencedAssemblies $refs -TypeDefinition $Source -Language CSharpVersion3 -passThru

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me