-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-TSqlAbstractSyntaxTree.ps1
More file actions
213 lines (171 loc) · 7.74 KB
/
Get-TSqlAbstractSyntaxTree.ps1
File metadata and controls
213 lines (171 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<#
.SYNOPSIS
Get-TSqlAbstractSyntaxTree is a function that generates an XML representation of the abstract syntax tree for a T-SQL script.
.DESCRIPTION
The function uses the Microsoft T-SQL ScriptDOM library to parse T-SQL scripts and generate an XML representation of the abstract syntax tree
that can be used for further code analysis and checking.
.PARAMETER SqlServerVersion
Specifies which SQL Server version to use the parser from. If you select 2012 and you script contains function only available in a later
version of SQL Server a parse error will be generated.
.PARAMETER ScriptDomLocation
The path to the location of the Microsoft.SqlServer.TransactSql.ScriptDom.dll file.
.PARAMETER Script
The T-SQL script to generate the XML abstract syntax tree for.
.PARAMETER PositionalProperties
Boolean flag to indicate if the position properties are included in the XML output, i.e. line and column numbers. If not needed and to reduce
the size of the XML set to false, otherwise set to true to include. Default is false.
.EXAMPLE
. $PSScriptRoot\Get-TSqlAbstractSyntaxTree.ps1
[xml]$result = Get-TSqlAbstractSyntaxTree -SqlServerVersion 2017 -ScriptDomLocation 'C:\Temp\' -Script $script -PositionalProperties $false
add -Debug at the end to see additional debug information when running.
.NOTES
General notes
#>
function Get-TSqlAbstractSyntaxTree {
[cmdletbinding()]
param (
[Parameter()]
[ValidateSet(2012,2014,2016,2017,2019)]
[int]$SqlServerVersion=2019,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ScriptDomLocation=$(throw 'ScriptDomLocation is mandatory, please provide a value.'),
[Parameter()]
[ValidateNotNullOrEmpty()]
[string[]]$Script=$(throw 'Script is mandatory, please provide a value.'),
[Parameter()]
[bool]$PositionalProperties = $false
)
function Get-TSqlFragmentNode{
[OutputType([System.Xml.XmlNode])]
param (
[System.Xml.XmlDocument]$Doc,
[object]$Fragment,
[string]$PropertyName,
[bool]$PositionalProperties = $true,
[string]$ParserName
)
try {
[System.Xml.XmlNode]$node = $null
$positionalPropertyNames = 'StartOffset','FragmentLength','StartLine','StartColumn','FirstTokenIndex','LastTokenIndex'
$typeName = $Fragment.GetType().Name
$baseTypeName = $Fragment.GetType().BaseType.Name
if ( $baseTypeName -ne 'Enum' ) {
if ( $PropertyName.Length -gt 0 ) {
$node = $Doc.CreateNode("element",$PropertyName,$null)
$node.SetAttribute("Type",$typeName) | Out-Null
}
else {
$node = $Doc.CreateNode("element",$typeName,$null)
}
$node.SetAttribute("BaseType",$baseTypeName) | Out-Null
}
else {
return
}
foreach ( $prop in $Fragment.GetType().GetProperties() ) {
if ( $prop.GetIndexParameters().Length -ne 0 ) {
continue
}
$propTypeName = $prop.PropertyType.Name
$propBaseTypeName = $prop.PropertyType.BaseType.Name
if ( $propBaseTypeName -eq 'ValueType') {
if($PositionalProperties -eq $true -Or $positionalPropertyNames -notcontains $prop.name ) {
$node.SetAttribute($prop.Name,$prop.GetValue($Fragment,$null).ToString()) | Out-Null
}
continue
}
if ( $propTypeName -like 'IList*' ) {
if ( $prop.Name -ne 'ScriptTokenStream' ) {
$list = $prop.GetValue($Fragment,$null)
if ( $list.Count -gt 0 ) {
$listNode = $Doc.CreateNode("element",$prop.Name,$null)
$listNode.SetAttribute('Type',$propTypeName) | Out-Null
$listNode.SetAttribute("Count",$list.Count.ToString()) | Out-Null
foreach ( $listItem in $list) {
$listItemNode = Get-TSqlFragmentNode -Doc $Doc -Fragment $listItem -PositionalProperties $PositionalProperties
$listNode.AppendChild($listItemNode) | Out-Null
}
$node.AppendChild($listNode) | Out-Null
}
}
}
else {
$obj = $prop.GetValue($Fragment, $null)
if ( $null -ne $obj ) {
$objTypeName = $obj.GetType().Name
$objBaseTypeName =$obj.GetType().BaseType.Name
if ( $obj.GetType() -eq [string] ) {
$node.InnerText = $prop.GetValue($Fragment, $null)
}
else {
if ( $objBaseTypeName -eq 'Enum' ) {
$node.SetAttribute($objTypeName,$obj.ToString()) | Out-Null
}
else {
$objNode = Get-TSqlFragmentNode -Doc $Doc -Fragment $obj -PropertyName $prop.Name -PositionalProperties $PositionalProperties
$node.AppendChild($objNode) | Out-Null
}
}
}
}
}
return $node
}
catch {
throw $_
break
}
}
$DebugPreference = 'Continue'
$debugMsg = @"
`r`nSQL Server Version........: $($SqlServerVersion)
ScriptDOM Library Location: $($ScriptDomLocation)
Show Positional Properties: $($PositionalProperties)
PS Version................: $($PSVersionTable.PSVersion.ToString())
"@
Write-Debug -Message $debugMsg
$libraryPath = "$($ScriptDomLocation)Microsoft.SqlServer.TransactSql.ScriptDom.dll"
try {
Add-Type -Path $libraryPath -ErrorAction SilentlyContinue
Write-Debug "Added type from $($libraryPath)"
}
catch {
throw "Couldn't add type $($libraryPath)"
break
}
switch ($SqlServerVersion){
2012 { $parserObjectName = 'TSql110Parser'}
2014 { $parserObjectName = 'TSql120Parser'}
2016 { $parserObjectName = 'TSql130Parser'}
2017 { $parserObjectName = 'TSql140Parser'}
2019 { $parserObjectName = 'TSql150Parser'}
}
$parserType = "Microsoft.SqlServer.TransactSql.ScriptDom.$($parserObjectName)"
try {
$parser = New-Object -TypeName $parserType -ArgumentList ($true)
Write-Debug "Created object $($parserType)"
}
catch {
throw "Couldn't create object $($parserType)"
break
}
try {
$parseError = New-Object -TypeName 'System.Collections.Generic.List[Microsoft.SqlServer.TransactSql.ScriptDom.ParseError]'
$reader = New-Object -TypeName 'System.IO.StringReader' -ArgumentList @($Script)
$fragment = $parser.Parse( $reader, [ref]$parseError )
}
catch {
throw $_
break
}
if ($parseError.Count -eq 0) {
[xml]$doc = New-Object System.Xml.XmlDocument
$node = Get-TSqlFragmentNode -Doc $doc -Fragment $fragment -PositionalProperties $PositionalProperties #-ParserName $parserObjectName
$doc.AppendChild($node) | Out-Null
return $doc
}
else {
return $parseError | ConvertTo-Xml
}
}