public/Get-GzDbProviderFactory.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
function Get-GzDbProviderFactory() {
    <#
        .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 = "Default"
    )

    PROCESS {
        

        $factory = Get-GzDbOption -Name "DbProviderFactories/$ProviderName"
        if($null -eq $factory) {
            if($ProviderName -eq "Default") {
                $instance = [System.Data.SqlClient.SqlClientFactory]::Instance
                Add-GzDbProviderFactory -Name "SqlServer" -Factory $instance -Default

                $instance = [Microsoft.Data.Sqlite.SqliteFactory]::Instance
                if($instance -eq $null) { throw "sqlite factory is null"}
                Add-GzDbProviderFactory -Name "Sqlite" -Factory $instance
               
                return $factory;
            }
            
            throw "Could not locate factory for $ProviderName. Call Add-DbProviderFactory"
        }

        return $factory
    }
}