PoshCode Archive  Artifact [47805ce8a3]

Artifact 47805ce8a30a9bc5e5d46f94581314355c64e1b3169b10f087b834eecfd0fcb4:

  • File MacroScopeParser.ps1 — part of check-in [3a0f3ef4b1] at 2018-06-10 13:00:37 on branch trunk — Uses MacroScope/Antlr to parse SQL query for tables and table aliases (user: unknown size: 1268)

# encoding: ascii
# api: powershell
# title: MacroScopeParser
# description: Uses MacroScope/Antlr to parse SQL query for tables and table aliases
# version: 0.1
# type: function
# license: CC0
# function: Get-Table
# x-poshcode-id: 1733
# x-archived: 2010-04-09T13:48:34
#
#
#requires -version 2

#Chad Miller
#http://www.sev17.com/
#Uses MacroScope/Antlr to parse SQL query for tables and table aliases
#Download MacroScope from http://macroscope.sourceforge.net/ and compile from source
#Or grab compiled assemblies from http://cid-ea42395138308430.skydrive.live.com/embedicon.aspx/Public/Blog/macroscopeParser.zip

param ($commandText)

#Assumes MacroScope and Antlr3 assemblies are in same directory
add-type -Path $(Resolve-Path .\Antlr3.Runtime.dll | Select-Object -ExpandProperty Path)
add-type -Path $(Resolve-Path .\MacroScope.dll | Select-Object -ExpandProperty Path)

#######################
function Get-Table
{
    param($table)

    $table

    if ($table.HasNext)
    { Get-Table $table.Next }
    
}

$sqlparser =[MacroScope.Factory]::CreateParser($commandText)
$expression = $sqlparser.queryExpression()
Get-Table $expression.From.Item | Select @{n='Name';e={$_.Source.Identifier}}, @{n='Alias';e={$_.Alias}}