public/Get-DbProviderFactory.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
function Get-DbProviderFactory() {
    <#
        .SYNOPSIS
        Gets the default SqlProviderFactory
        .DESCRIPTION
        The default global provider factory is used by the other functions /
        cmdlets in the this module to construct Connection, Commands, Transaction
        and Parameter objects when a provider factory is not specified.
     
        .PARAMETER ProviderName
        An instance of `System.Data.Common.DbProviderFactory`
     
        .EXAMPLE
        $factory = Get-DbProviderFactory
     
        .EXAMPLE
        $factory = Get-DbProviderFactory
     
    #>

    [CmdletBinding()]
    Param(
        [Parameter(Position = 0)]
        [String] $ProviderName = $null
    )

    PROCESS {
        if([string]::IsNullOrWhiteSpace($ProviderName)) {
            $ProviderName = "Default"
        }

        $factory = Get-SqlDbOption -Name "DbProviderFactories/$ProviderName"
        if($null -eq $factory) {
            if($ProviderName -eq "Default") {
                $instance = [System.Data.SqlClient.SqlClientFactory]::Instance
                Add-DbProviderFactory -Name "SqlServer" -Factory $instance -Default
                return $factory;
            }
            
            throw Exception "Could not locate factory for $ProviderName. Call Add-DbProviderFactory"
        }

        return $factory
    }
}