public/Read-GzDbData.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
function Read-GzDbData() {
<#
    .SYNOPSIS
    Reads data from a SQL query
 
    .DESCRIPTION
    Creates a cmd object, sets the query and parameters, and invokes ExecuteReader on the
    command. This cmdlet only supports single result sets like selecting one or more
    rows from a table.
 
    .PARAMETER ConnectionString
    (Optional) The connection string is used to create and open the connection.
 
    .PARAMETER Connection
    (Optional) The connection object used to create the command and to execute the query.
    If the connection is not supplied, one is created, opened, and closed within in the cmdlet.
 
 
    .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.
 
    .EXAMPLE
     $data = $Connection | Read-GzDbData "SELECT * FROM [table]"
 
    .EXAMPLE
     $results = Read-GzDbData "SELECT FirstName, LastName, Age as [Years] from [People]" -ConnectionString "Data Source=(LocalGzDb)\MSSQLLocalGzDb;Integrated Security=True"
#>

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 1)]
        [String] $Query,
        
        [Object] $Parameters,
        
        [Alias("c")]
        [string] $ConnectionString,

        [Alias("cn")]
        [string] $ConnectionStringName,

        [Alias("pn")]
        [String] $ProviderName = "Default",
        
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [System.Data.IDbConnection] $Connection,
        
        [String] $ParameterPrefix = $null 
    )

    # TODO: handle parameters better in the BEGIN block
    # TODO: wrap execution inside the PROCESS block
    # TODO: wrap up disposal insicd the END block

    
    if(!$Connection -and [string]::IsNullOrWhiteSpace($ConnectionString)) {
        if(![string]::IsNullOrWhiteSpace($ConnectionStringName)) {
            $ConnectionString = Get-GzDbConnectionString -Name $Name 
            if([String]::IsNullOrWhiteSpace($ConnectionString)) {
                throw "Could not find connection string for $Name"
            }
        } else {
            $ConnectionString = Get-GzDbConnectionString
        }

        if([string]::IsNullOrWhiteSpace($ConnectionString)) {
            $msg =  "The ConnectionString parameter or global connection string MUST "
            $msg += "be set before communicating with SQL SERVER." 
            throw [System.ArgumentException] $msg
        }
    } 

    $dispose = $false 
    $open = $false 
    if(!$Connection) {
        $dispose = $true;
        $factory = Get-GzDbProviderFactory $ProviderName
        $obj  = $factory.CreateConnection()
        
        $Connection = [System.Data.IDbConnection]$obj
    }

    if(!$Connection.ConnectionString) {
        $Connection.ConnectionString = $ConnectionString
    }
    

    $cmd = $null;
    $dr = $null;
    try {
        if($Connection.State -ne "Open") {
            $Connection.Open()
            $open = $true;
        }

        $cmd = $Connection | New-GzDbCommand $Query -Parameters $Parameters -ParameterPrefix $ParameterPrefix
        $dr = $cmd.ExecuteReader()
        
        $results = @() 
        # TODO: handle multiple result sets.
        while($dr.Read())
        {
            $item = New-Object PSObject 
            for($i = 0; $i -lt $dr.FieldCount; $i++) {
                $name = ($dr.GetName($i))
                if(!$name) {
                    $name = "column $i"
                }
                $item | Add-Member -MemberType NoteProperty -Name $name -Value ($dr.GetValue($i))
            }
            $results += $item;
        }
        
        
        return $results
    } finally {
        if($dr) {
            $dr.Dispose();
        }
        if($cmd) {
            $cmd.Dispose();
        }
        if($open) {
            $Connection.Close();
        }
        if($dispose) {
            $Connection.Dispose()
        }
    }
}