internal/functions/Get-DatabaseConnection.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function Get-DatabaseConnection {
    # Returns a connection manager object
    Param (
        [Parameter(ParameterSetName = 'ConnString')]
        [string]$ConnectionString,
        [Parameter(ParameterSetName = 'Config', Mandatory)]
        [DBOpsConfig]$Configuration,
        [DBOps.ConnectionType]$Type
    )
    if ($Configuration) {
        $ConnectionString = Get-ConnectionString -Configuration $Configuration -Type $Type
    }
    $connection = switch ($Type) {
        SqlServer { [System.Data.SqlClient.SqlConnection]::new($ConnectionString) }
        Oracle { [Oracle.ManagedDataAccess.Client.OracleConnection]::new($ConnectionString) }
        MySQL { [MySql.Data.MySqlClient.MySqlConnection]::new($ConnectionString) }
        PostgreSQL { [Npgsql.NpgsqlConnection]::new($ConnectionString) }
        default { Stop-PSFFunction -Message "Unknown type $Type" -EnableException $true }
    }
    return $connection
}