PoshCode Logo PowerShell Code Repository

MacroScopeParser by Chad Miller 22 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1733"></script>download | new post

Uses MacroScope/Antlr to parse SQL query for tables and table aliases

  1. #requires -version 2
  2.  
  3. #Chad Miller
  4. #http://www.sev17.com/
  5. #Uses MacroScope/Antlr to parse SQL query for tables and table aliases
  6. #Download MacroScope from http://macroscope.sourceforge.net/ and compile from source
  7. #Or grab compiled assemblies from http://cid-ea42395138308430.skydrive.live.com/embedicon.aspx/Public/Blog/macroscopeParser.zip
  8.  
  9. param ($commandText)
  10.  
  11. #Assumes MacroScope and Antlr3 assemblies are in same directory
  12. add-type -Path $(Resolve-Path .\Antlr3.Runtime.dll | Select-Object -ExpandProperty Path)
  13. add-type -Path $(Resolve-Path .\MacroScope.dll | Select-Object -ExpandProperty Path)
  14.  
  15. #######################
  16. function Get-Table
  17. {
  18.     param($table)
  19.  
  20.     $table
  21.  
  22.     if ($table.HasNext)
  23.     { Get-Table $table.Next }
  24.    
  25. }
  26.  
  27. $sqlparser =[MacroScope.Factory]::CreateParser($commandText)
  28. $expression = $sqlparser.queryExpression()
  29. Get-Table $expression.From.Item | Select @{n='Name';e={$_.Source.Identifier}}, @{n='Alias';e={$_.Alias}}

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