Skip to content

Commit 7074f17

Browse files
committed
Adding SQL parameters to Invoke-DBOQuery
1 parent 1b1f404 commit 7074f17

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

functions/Invoke-DBOQuery.ps1

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ function Invoke-DBOQuery {
7474
.PARAMETER As
7575
Specifies output type. Valid options for this parameter are 'DataSet', 'DataTable', 'DataRow', 'PSObject', and 'SingleValue'
7676
77+
.PARAMETER Parameter
78+
Uses values in specified hashtable as parameters inside the SQL query.
79+
For example, <Invoke-DBOQuery -Query 'SELECT @p1' -Parameter @{ p1 = 1 }> would return "1" on SQL Server.
80+
7781
.PARAMETER Confirm
7882
Prompts to confirm certain actions
7983
@@ -142,7 +146,9 @@ function Invoke-DBOQuery {
142146
[string]$Type = (Get-DBODefaultSetting -Name rdbms.type -Value),
143147
[ValidateSet("DataSet", "DataTable", "DataRow", "PSObject", "SingleValue")]
144148
[string]
145-
$As = "DataRow"
149+
$As = "DataRow",
150+
[Alias('SqlParameters', 'SqlParameter', 'Parameters')]
151+
[System.Collections.IDictionary]$Parameter
146152
)
147153

148154
begin {
@@ -178,7 +184,13 @@ function Invoke-DBOQuery {
178184
if (-Not $config.Silent) {
179185
$dbUpConnection.IsScriptOutputLogged = $true
180186
}
181-
$managedConnection = $dbUpConnection.OperationStarting($dbUpLog, $null)
187+
try {
188+
$managedConnection = $dbUpConnection.OperationStarting($dbUpLog, $null)
189+
}
190+
catch {
191+
Stop-PSFFunction -EnableException $true -Message "Failed to connect to the server" -ErrorRecord $_
192+
return
193+
}
182194
if ($Query) {
183195
$queryText = $Query
184196
}
@@ -211,12 +223,23 @@ function Invoke-DBOQuery {
211223
if ($PSCmdlet.ShouldProcess("Executing query $qCount", $config.SqlInstance)) {
212224
foreach ($splitQuery in $dbUpConnection.SplitScriptIntoCommands($queryItem)) {
213225
$dt = [System.Data.DataTable]::new()
214-
$rows = $dbUpConnection.ExecuteCommandsWithManagedConnection( [Func[Func[Data.IDbCommand], [pscustomobject]]] {
226+
$rows = $dbUpConnection.ExecuteCommandsWithManagedConnection( [Func[Func[Data.IDbCommand], pscustomobject]] {
215227
Param (
216228
$dbCommandFactory
217229
)
230+
# Compile the parameters as Expression functions
231+
$exp = [System.Linq.Expressions.Expression]
232+
$parameterList = @()
233+
foreach ($key in $Parameter.Keys) {
234+
$param = $exp::Parameter([string], $key)
235+
$value = $exp::Constant($Parameter.$key)
236+
$body = $exp::Convert($value, [System.Object])
237+
$lambda = $exp::Lambda([Func[string, object]], $body, $param)
238+
$parameterList += $lambda
239+
}
240+
# Initiating the AdHocSqlRunner
218241
$sqlRunner = [DbUp.Helpers.AdHocSqlRunner]::new($dbCommandFactory, $dbUpSqlParser, $config.Schema)
219-
return $sqlRunner.ExecuteReader($splitQuery)
242+
return $sqlRunner.ExecuteReader($splitQuery, $parameterList)
220243
})
221244
$rowCount = ($rows | Measure-Object).Count
222245
if ($rowCount -gt 0) {

internal/classes/DBOpsLog.class.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class DBOpsLog : DbUp.Engine.Output.IUpgradeLog {
44
hidden [bool]$Silent
55
hidden [object]$CallStack
66
hidden [DBOpsDeploymentStatus]$Status
7-
7+
88
#Constructors
99
DBOpsLog ([bool]$silent, [string]$outFile, [bool]$append) {
1010
$this.Init($silent, $outFile, $append)
@@ -27,10 +27,10 @@ class DBOpsLog : DbUp.Engine.Output.IUpgradeLog {
2727
$txt | Out-File $this.logToFile -Force
2828
}
2929
}
30-
$this.CallStack = (Get-PSCallStack)[1]
30+
$this.CallStack = (Get-PSCallStack)[0]
3131
}
32-
33-
32+
33+
3434
#Methods
3535
[void] WriteInformation([string]$format, [object[]]$params) {
3636
$level = switch ($this.silent) {
@@ -45,7 +45,7 @@ class DBOpsLog : DbUp.Engine.Output.IUpgradeLog {
4545
Line = $this.callStack.Position.StartLineNumber
4646
Level = $level
4747
Message = $format -f $params
48-
48+
4949
}
5050
Write-PSFMessage @splatParam
5151
if ($this.logToFile) {
@@ -66,7 +66,7 @@ class DBOpsLog : DbUp.Engine.Output.IUpgradeLog {
6666
Line = $this.callStack.Position.StartLineNumber
6767
Level = $level
6868
Message = $format -f $params
69-
69+
7070
}
7171
Write-PSFMessage @splatParam
7272
if ($this.logToFile) {
@@ -87,7 +87,7 @@ class DBOpsLog : DbUp.Engine.Output.IUpgradeLog {
8787
Line = $this.callStack.Position.StartLineNumber
8888
Level = $level
8989
Message = $format -f $params
90-
90+
9191
}
9292
Write-PSFMessage @splatParam
9393
if ($this.logToFile) {

tests/Invoke-DBOQuery.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,11 @@ Describe "Invoke-DBOQuery tests" -Tag $commandName, IntegrationTests {
104104
$result.A | Should -Be '1', '3'
105105
$result.B | Should -Be '2', '4'
106106
}
107+
It "should run the query with custom parameters" {
108+
$query = "SELECT @p1 AS A, @p2 AS B"
109+
$result = Invoke-DBOQuery -Query $query -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Parameter @{ p1 = '1'; p2 = 'string'}
110+
$result.A | Should -Be 1
111+
$result.B | Should -Be string
112+
}
107113
}
108114
}

0 commit comments

Comments
 (0)