public/New-GzDbCommand.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

function New-GzDbCommand() {
<#
    .SYNOPSIS
    Creates a new SQL command object
 
    .DESCRIPTION
    Creates a new SQL command, sets the command type to text by default, requires the
    SQL statement, and allows users to specify parameters.
 
    If the `Do` parameter is not specified, the cmd object is returned, otherwise
    the script block expects the cmd to be executed within the script block and is
    disposed
 
    .PARAMETER Connection
    (Optional) The connection object used to create the command and to execute the query.
 
    .PARAMETER Transaction
    (Optional) The transaction object used to be applied to the command.
     
 
    .PARAMETER Query
    The SQL statement that will be excuted by this command object.
 
    .PARAMETER Parameters
    (Optional). The parameters object can be a PsObject, Hashtable, or an Array. The keys for the hashtable and the
    property names for the PsObject are prefixed with the parameter prefix and used as SQL Parameter names.
     
    For an Array, the prefix is prepend to the index for the SQL Parameter name.
 
    .PARAMETER ParameterPrefix
    (Optional) Defaults to `@`. The symbol used to notate a parameter in the SQL statement.
 
    .PARAMETER CommandType
    (Optional) Defaults to 'Text'. Specify the command type for the command.
 
    .PARAMETER Do
    (Optional) If specified, two variables are bound to the
    script block, `$_` and `$Command` which can be used within the script block. The
    command is disposed one the script block is executed.
 
    .EXAMPLE
     PS C:\> $Connection | New-GzDbCommand "SELECT * FROM [People]" -Do {
     PS C:\> $dr = $_.ExecuteReader();
     PS C:\> While($dr.Read()) {
     PS C:\> Write-Host ($dr.GetValue(0))
     PS C:\> }
     PS C:\> }
  
    .EXAMPLE
    $cmd = $Connection | New-GzDbCommand "Select @Value AS Value" -Parameters @{"Value" = 11}
 
#>

    [CmdletBinding()]
    [System.Diagnostics.CodeAnalysis.SuppressMessage("PsUseDeclaredVarsMoreThanAssignments", "")]
    Param(
        [Parameter(ValueFromPipeline =$True)]
        [System.Data.IDbConnection] $Connection,
        
        [Parameter(ValueFromPipeline =$True)]
        [System.Data.IDbTransaction] $Transaction,
        [Parameter(Mandatory = $true, Position = 0)]
        [string] $Query,
        
        [Object] $Parameters,
        
        [string] $ParameterPrefix,

        [Alias("pn")]
        [string] $ProviderName, 
        
        [System.Data.CommandType] $CommandType = "Text",
        
        [ScriptBlock] $Do 
    )

    if($Transaction -eq $null -and $Connection -eq $Null) {
        throw [System.ArgumentNullException] ("Either a Transaction or Connection MUST be specified ")
    }

    if($Transaction -ne $Null) {
        $Connection = $Transaction.Connection;
    }

    $Cmd = $Connection.CreateCommand();

    if($Transaction -ne $Null) {
        $Cmd.Transaction = $Transaction;
    }

    $Cmd.CommandType = $CommandType
    if(![string]::IsNullOrWhiteSpace($Query)) {
        $Cmd.CommandText = $Query;
    }

  

    if($Parameters) {
        $Cmd | Add-GzDbParameter -Parameters $Parameters -ParameterPrefix $ParameterPrefix 
    }

    $Command = $Cmd 
    Set-Variable -Name "_" -Value $cmd
    if($Do) {
        $vars = @(
            (Get-Variable -Name "Command" -Scope 0),
            (Get-Variable -Name "Cmd" -Scope 0) 
            (Get-Variable -Name "_" )
        )
        try {
            $Do.InvokeWithContext(@{}, $vars)
        } finally {
            $cmd.Dispose()
        }
        return;
    }

    return $cmd
}