functions/Invoke-DBOQuery.ps1

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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
function Invoke-DBOQuery {
    <#
    .SYNOPSIS
        Runs a query against database
 
    .DESCRIPTION
        Runs a query against a selected database server and returns results
 
    .PARAMETER Query
        One or more queries to execute on the remote server
 
    .PARAMETER InputFile
        Path to one or more SQL sript files.
        Aliases: Name, FileName, ScriptPath, Path
 
    .PARAMETER InputObject
        Pipeline implementation of InputFile. Accepts output from Get-Item and Get-ChildItem, as well as simple strings and arrays.
 
    .PARAMETER SqlInstance
        Database server to connect to. SQL Server only for now.
        Aliases: Server, SQLServer, DBServer, Instance
 
    .PARAMETER Database
        Name of the database to execute the scripts in. Optional - will use default database if not specified.
 
    .PARAMETER ConnectionTimeout
        Database server connection timeout in seconds. Only affects connection attempts. Does not affect execution timeout.
        If 0, will wait for connection until the end of times.
 
        Default: 30
 
    .PARAMETER Encrypt
        Enables connection encryption.
 
    .PARAMETER Credential
        PSCredential object with username and password to login to the database server.
 
    .PARAMETER UserName
        An alternative to -Credential - specify username explicitly
 
    .PARAMETER Password
        An alternative to -Credential - specify password explicitly
 
    .PARAMETER Silent
        Will supress all output from the command.
 
    .PARAMETER Variables
        Hashtable with variables that can be used inside the scripts and deployment parameters.
        Proper format of the variable tokens is #{MyVariableName}
        Can also be provided as a part of Configuration hashtable: -Configuration @{ Variables = @{ Var1 = ...; Var2 = ...}}
        Will augment and/or overwrite Variables defined inside the package.
 
    .PARAMETER OutputFile
        Put the execution log into the specified file.
 
    .PARAMETER Append
        Append output to the -OutputFile instead of overwriting it.
 
    .PARAMETER ConnectionString
        Custom connection string that will override other connection parameters.
        IMPORTANT: Will also ignore user/password/credential parameters, so make sure to include proper authentication credentials into the string.
 
    .PARAMETER Configuration
        A custom configuration that will be used during the execution.
        Can be a Hashtable, a DBOpsConfig object, or a path to a json file.
 
    .PARAMETER Schema
        Execute in a specific schema (if supported by RDBMS)
 
    .PARAMETER Type
        Defines the driver to use when connecting to the database server.
        Available options: SqlServer (default), Oracle
 
    .PARAMETER As
        Specifies output type. Valid options for this parameter are 'DataSet', 'DataTable', 'DataRow', 'PSObject', and 'SingleValue'
 
    .PARAMETER ReturnAsText
        Postgres only: returns all fields of the dataset as text values. This helps with the datatypes that are unknown to npgsql.
 
    .PARAMETER Parameter
        Uses values in specified hashtable as parameters inside the SQL query.
        For example, <Invoke-DBOQuery -Query 'SELECT @p1' -Parameter @{ p1 = 1 }> would return "1" on SQL Server.
 
    .PARAMETER ConnectionAttribute
        Additional connection string attributes that should be added to the existing connection string, provided as a hashtable.
        For example to enable SYSDBA permissions in Oracle, use the following: -ConnectionAttribute @{ 'DBA Privilege' = 'SYSDBA' }
 
    .PARAMETER Interactive
        Opens connection to the server and launches interactive session to query the server directly from the command line
 
    .PARAMETER Confirm
        Prompts to confirm certain actions
 
    .PARAMETER WhatIf
        Shows what would happen if the command would execute, but does not actually perform the command
 
    .EXAMPLE
        # Runs all SQL scripts from the folder .\SqlCode in the target database
        Invoke-DBOQuery .\SqlCode\*.sql -SqlInstance 'myserver\instance1' -Database 'MyDb'
 
    .EXAMPLE
        # Runs script file using specific connection parameters
        Get-Item .\SqlCode\Script1.sql | Invoke-DBOQuery -SqlInstance 'Srv1' -Database 'MyDb' -ExecutionTimeout 3600
 
    .EXAMPLE
        # Runs all the scripts from the .\SqlCode folder using custom logging parameters and schema tracking table
        Get-ChildItem .\SqlCode\* | Invoke-DBOQuery -SqlInstance 'Srv1' -Database 'MyDb' -OutputFile .\out.log -Append
 
    .EXAMPLE
        # Runs two scripts from the current folder using custom configuration file
        Invoke-DBOQuery -InputFile .\Script1.sql,.\Script2.sql -ConfigurationFile .\localconfig.json
 
    .EXAMPLE
        # Runs two scripts from the current folder using variables instead of specifying values directly
        '.\Script1.sql','.\Script2.sql' | Invoke-DBOQuery -SqlInstance '#{server}' -Database '#{db}' -Variables @{server = 'Srv1'; db = 'MyDb'}
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Query')]
    param
    (
        [Parameter(Mandatory = $true,
            Position = 1,
            ParameterSetName = 'Script')]
        [Alias('Name', 'ScriptPath', 'Filename', 'Path')]
        [string[]]$InputFile,
        [Parameter(Mandatory = $true,
            Position = 1,
            ParameterSetName = 'Query')]
        [string[]]$Query,
        [Parameter(Mandatory = $true,
            Position = 1,
            ValueFromPipeline = $true,
            ParameterSetName = 'Pipeline')]
        [object]$InputObject,
        [Parameter(Position = 2)]
        [Alias('Server', 'SqlServer', 'DBServer', 'Instance')]
        [string]$SqlInstance,
        [Parameter(Position = 3)]
        [string]$Database,
        [int]$ConnectionTimeout,
        [switch]$Encrypt,
        [pscredential]$Credential,
        [string]$UserName,
        [securestring]$Password,
        [switch]$Silent,
        [Alias('ArgumentList')]
        [hashtable]$Variables,
        [string]$OutputFile,
        [switch]$Append,
        [Alias('Config')]
        [object]$Configuration,
        [string]$Schema,
        [AllowNull()]
        [string]$ConnectionString,
        [Alias('ConnectionType', 'ServerType')]
        [DBOps.ConnectionType]$Type = (Get-DBODefaultSetting -Name rdbms.type -Value),
        [ValidateSet("DataSet", "DataTable", "DataRow", "PSObject", "SingleValue")]
        [string]
        $As = "DataRow",
        [Alias('SqlParameters', 'SqlParameter', 'Parameters')]
        [System.Collections.IDictionary]$Parameter,
        [hashtable]$ConnectionAttribute,
        [Parameter(Mandatory = $true,
            Position = 1,
            ParameterSetName = 'Interactive')]
        [Alias('i')]
        [switch]$Interactive,
        [switch]$ReturnAsText
    )

    begin {
        # description for different delimiters in different DBMS
        $delimiter = switch ($Type) {
            SqlServer { '''GO'' on a new line' }
            PostgreSQL { 'semicolon (;) on a new line' }
            Oracle { 'semicolon (;)' }
            MySQL { 'semicolon (;)' }
        }
        function Write-HostTable {
            Param (
                $Table
            )
            $format = ""
            $totalLength = 0
            if ($Table) {
                for ($i = 0; $i -lt $Table.Columns.Count; $i++) {
                    $maxLength = 0
                    if ($Table.Rows.Count -gt 0) {
                        $Table.Rows | Foreach-Object {
                            $itemLength = ([string]$_.ItemArray[$i]).Length
                            $maxLength = [Math]::Max($itemLength, $maxLength)
                        }
                        $maxLength += 2
                    }
                    if ($Table.Columns[$i].ColumnName.Length -gt $maxLength) { $maxLength = $Table.Columns[$i].ColumnName.Length }
                    $format += " {$i, $maxLength} |"
                    $totalLength += ($maxLength + 3)
                }
                if ($totalLength -gt 0) {
                    $format = "|$format"
                    $totalLength += 1

                    Write-Host ([string]::new('-', $totalLength))
                    Write-Host ($format -f $Table.Columns.ColumnName)
                    Write-Host ([string]::new('-', $totalLength))
                    foreach ($row in $Table.Rows) {
                        # we don't need no newlines
                        $replacedValue = "$($format -f $row.ItemArray)".Replace("`r", " ").Replace("`n", " ")
                        Write-Host $replacedValue
                    }
                    Write-Host ([string]::new('-', $totalLength))
                    # totals
                    Write-Host ("{0, $totalLength}" -f "($($Table.Rows.Count) rows)")
                }
            }
        }
    }
    process {
        $config = New-DBOConfig -Configuration $Configuration
        #Merge custom parameters into a configuration
        $newConfig = @{ }
        foreach ($key in ($PSBoundParameters.Keys)) {
            if ($key -in [DBOpsConfig]::EnumProperties()) {
                $newConfig.$key = $PSBoundParameters[$key]
            }
        }
        $config.Merge($newConfig)

        # Initialize external libraries if needed
        Initialize-ExternalLibrary -Type $Type

        #Replace tokens if any
        foreach ($property in [DBOpsConfig]::EnumProperties() | Where-Object { $_ -ne 'Variables' }) {
            $config.SetValue($property, (Resolve-VariableToken $config.$property $config.Variables))
        }

        #Build connection string
        Write-PSFMessage -Level Debug -Message "Getting the connection object"
        $dbUpConnection = Get-ConnectionManager -ConnectionString $null -Type $Type # only needed for query parsing for now, connection string is pointless
        $dataConnection = Get-DatabaseConnection -Configuration $config -Type $Type # the 'real' connection
        $dbUpLog = [DBOpsLog]::new($config.Silent, $OutputFile, $Append)
        $dbUpLog.CallStack = (Get-PSCallStack)[0]

        # Define the queries
        if ($Query) {
            $queryText = $Query
        }
        else {
            $fileObjects = @()
            try {
                if ($InputFile) {
                    $fileObjects += $InputFile | Get-Item -ErrorAction Stop
                }
                if ($InputObject) {
                    $fileObjects += $InputObject | Get-Item -ErrorAction Stop
                }
            }
            catch {
                Stop-PSFFunction -Message 'File not found' -ErrorRecord $_ -EnableException $true
            }
            $queryText = $fileObjects | Get-Content -Raw
        }

        #Replace tokens in the sql code if any
        $queryList = @()
        foreach ($qText in $queryText) {
            $queryList += Resolve-VariableToken $qText $config.Variables
        }

        # open the connection
        Write-PSFMessage -Level Verbose -Message "Establishing connection with $Type $($config.SqlInstance)"
        try {
            $dataConnection.Open()
        }
        catch {
            Stop-PSFFunction -EnableException $true -Message "Failed to connect to the server" -ErrorRecord $_
            return
        }
        if ($Interactive) {
            Write-PSFMessage -Level Host -Message "Running in interactive mode. Finish the query with a $delimiter to execute it immediately. \q, exit, or quit to exit."
        }
        else {
            Write-PSFMessage -Level Debug -Message "Preparing to run $($queryList.Count) queries"
        }
        # wrapping everything in a big try {} to support graceful disconnect
        try {
            do {
                if ($Interactive) {
                    # prompt for a query
                    Write-Host ">"
                    $interactiveQuery = @()
                    # read until user finishes with a ;
                    while ($true) {
                        $inputLine = Read-Host
                        # exit on \q
                        if ($inputLine -in '\q', 'exit', 'quit') {
                            $dataConnection.Dispose()
                            return
                        }
                        $interactiveQuery += $inputLine
                        $fullQuery = ($interactiveQuery -join "`n").Trim()
                        $queryList = $dbUpConnection.SplitScriptIntoCommands($fullQuery)
                        if ($queryList[0] -and $queryList[0] -ne $fullQuery) { break }
                    }
                    $queryList = $queryList[0]
                }
                $ds = [System.Data.DataSet]::new()
                $qCount = 0
                foreach ($queryItem in $queryList) {
                    $qCount++
                    if ($PSCmdlet.ShouldProcess($config.SqlInstance, "Executing query $queryItem")) {
                        # split commands using DbUp parser
                        foreach ($splitQuery in $dbUpConnection.SplitScriptIntoCommands($queryItem)) {
                            try {
                                Write-PSFMessage -Level Verbose -Message "Executing sub-query $splitQuery"
                                # only Sql Server supports messaging right now
                                if ($Type -eq 'SqlServer') {
                                    # Generate event id
                                    $eventId = "DBOpsMessaging-$(New-Guid)"
                                    # Add message events
                                    if (Get-Event | Where-Object { $_.SourceIdentifier -eq $eventId }) {
                                        Unregister-Event -SourceIdentifier $eventId -ErrorAction SilentlyContinue
                                    }
                                    $eventOutput = Register-ObjectEvent -InputObject $dataConnection -EventName "InfoMessage" -SourceIdentifier $eventId -Action { $EventArgs.Message }
                                }
                                # create a new command object and define text/parameters
                                $command = $dataConnection.CreateCommand()
                                $command.CommandText = $splitQuery
                                foreach ($key in $Parameter.Keys) {
                                    $null = switch ($Type) {
                                        Oracle {
                                            $command.BindByName = $true
                                            $command.Parameters.Add($key, $Parameter[$key])
                                        }
                                        default { $command.Parameters.AddWithValue($key, $Parameter[$key]) }
                                    }
                                }
                                if ($Type -eq 'Postgresql' -and $ReturnAsText) {
                                    $command.AllResultTypesAreUnknown = $true;
                                }
                                # create an async reader and wait for output receiving logs in the process
                                $readerTask = $command.ExecuteReaderAsync()
                                if ($Type -eq 'SqlServer') {
                                    $logScript = {
                                        # receive events we got so far and put them in the log
                                        $events = $eventOutput | Receive-Job
                                        foreach ($logEntry in $events) {
                                            $dbUpLog.WriteInformation($logEntry, $null)
                                        }
                                    }
                                    while (-not $readerTask.IsCompleted) {
                                        $logScript.Invoke()
                                        Start-Sleep -Milliseconds 50
                                    }
                                    # once completed, receive the final part and unregister the ivent
                                    $logScript.Invoke()
                                    # lastly, unregister the event
                                    Unregister-Event -SourceIdentifier $eventId
                                }
                                $reader = $readerTask.GetAwaiter().GetResult()
                                $setCounter = 0
                                do {
                                    $setCounter++
                                    $table = [System.Data.DataTable]::new()
                                    $definition = $reader.GetSchemaTable()
                                    foreach ($column in $definition) {
                                        $name = $column.ColumnName
                                        $datatype = $column.DataType
                                        for ($j = 1; -not $name; $j++) {
                                            if ($table.Columns.ColumnName -notcontains "Column$j") { $name = "Column$j" }
                                        }
                                        if ($datatype.FullName -eq 'System.DBNull') {
                                            $datatype = [string]
                                        }
                                        elseif (!$datatype.FullName) {
                                            $datatype = [string]
                                        }
                                        $null = $table.Columns.Add($name, $datatype)
                                    }
                                    # read rows async and assign values
                                    $rowCount = 0
                                    while ($reader.ReadAsync().GetAwaiter().GetResult()) {
                                        $rowCount++
                                        $row = $table.NewRow()
                                        for ($i = 0; $i -lt $reader.FieldCount; $i++) {
                                            $row[$table.Columns[$i].ColumnName] = $reader.GetValue($i);
                                        }
                                        $table.Rows.Add($row)
                                    }
                                    Write-PSFMessage -Level Debug -Message "Received $rowCount rows from resultset $setCounter"
                                    $ds.Tables.Add($table)
                                } while ($reader.NextResult())
                                $reader.Close()
                                $reader.Dispose()
                                $command.Dispose()
                            }
                            catch {
                                $dbUpLog.WriteError($_.Exception.InnerException.Message, $null)
                                if ($Type -eq 'SqlServer' -and (Get-Event | Where-Object { $_.SourceIdentifier -eq $eventId })) {
                                    Unregister-Event -SourceIdentifier $eventId -ErrorAction SilentlyContinue
                                }
                                if (-not $Interactive) {
                                    Stop-PSFFunction -EnableException $true -Message "Failed to run the query" -ErrorRecord $_
                                }
                            }
                        }
                    }
                }
                if ($Interactive) {
                    # output right to the screen
                    Write-HostTable -Table $ds.Tables[0]
                }
                else {
                    # output as object
                    switch ($As) {
                        'DataSet' {
                            $ds
                        }
                        'DataTable' {
                            $ds.Tables
                        }
                        'DataRow' {
                            if ($ds.Tables.Count -gt 0) {
                                $ds.Tables[0]
                            }
                        }
                        'PSObject' {
                            if ($ds.Tables.Count -gt 0) {
                                foreach ($row in $ds.Tables[0].Rows) {
                                    [DBOpsHelper]::DataRowToPSObject($row)
                                }
                            }
                        }
                        'SingleValue' {
                            if ($ds.Tables.Count -ne 0) {
                                $ds.Tables[0] | Select-Object -ExpandProperty $ds.Tables[0].Columns[0].ColumnName
                            }
                        }
                    }
                }
            } while ($Interactive)
        }
        catch {
            # don't really need anything else here
            throw $_
        }
        finally {
            # close the connection even when interrupted by Ctrl+C
            $dataConnection.Close()
            $dataConnection.Dispose()
        }
    }
    end {

    }
}