public/Get-GzDbOption.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
139
140
141
142
143
144
145
146
$GzDbOptions = @{
    ConnectionStrings = @{
        Default = $null
    }
    DbProviderFactoryNames= @{
        SqlServer = "System.Data.SqlClient.SqlClientFactory"
        MsSql = "System.Data.SqlClient.SqlClientFactory"
    }
    DbProviderFactories = @{
        SqlServer = [System.Data.SqlClient.SqlClientFactory]::Instance
        MsSql = [System.Data.SqlClient.SqlClientFactory]::Instance
        Default = [System.Data.SqlClient.SqlClientFactory]::Instance
    }
}


function Get-GzDbOption() {
    <#
    .SYNOPSIS
        Gets one or all options/configuraiton values for SqlDb Module.
     
    .DESCRIPTION
        AdoOption holds connection strings and provider factory strings which are accessible
        via path strings. e.g. "DbProviderFactories/SqlServer", "ConnectionStrings/Default"
 
    .PARAMETER Path
        Optional. If the path is specified, the value is returned. Otherwise all the values are returned
        as a hashable. Aliases: Name
     
    .EXAMPLE
        PS C:\> Get-GzDbOption "ConnectionStrings/Default"
        Returns the default connection string value.
 
    .NOTES
        Additional values can be added using Set-SqDbOption -Path "MyValue" -Value "Value"
    #>

    [CmdletBinding()]
    Param(
        [Alias("Name")]
        [String] $Path
    )

    if(!$Path) {
        return $GzDbOptions;
    }
    $segments = @($Path);

    if($Path.Contains(".")) { $segments = $Path.Split("."); }
    if($Path.Contains("/")) { $segments = $Path.Split("/"); }

    $root = $GzDbOptions
    foreach($segment in $segments) {
        $root = $root[$segment];
    }

    return $root;
}


function Set-GzDbOption() {
    <#
    .SYNOPSIS
        Sets a value for a configuration key.
 
    .DESCRIPTION
        Sets a config value using a name/path value that is stored in memory
        for this module.
 
    .PARAMETER Path
        Reqiured. The path to the configuration option. Paths can use forward slashes or dots
        as a delimiter. Aliases: Name
 
    .PARAMETER Value
        Required. The value for the configuration option. 'ConfigurationStrings` and 'DbProviderFactories'
        must be a hashtable object.
 
    .EXAMPLE
        PS C:\> Set-GzDbOption -Name "ConnectionStrings/Default" -Value "DataStore=:memory:"
        Sets a configuration value.
    #>

    [CmdletBinding()]
    Param(
        [Alias("Name")]
        [String] $Path,

        [Object] $Value
    )


    while([string]::IsNullOrWhiteSpace($Path)) {
        $Path = Read-Host "DbOption Path e.g. ConnectionStrings/MyApp ?"
    }

    if($Path -eq "ConnectionStrings") {
        if(! ($Value -is [Hashtable])) {
            Write-Warning "ConnectionStrings must be a hastable"
            return;
        }
    }

    if($Path -eq "DbProviderFactories") {
        if(! ($Value -is [Hashtable])) {
            Write-Warning "DbPRoviderFactories must be a hastable"
            return;
        }
    }

    if($Path -eq "DbProviderFactoryNames") {
        if(! ($Value -is [Hashtable])) {
            Write-Warning "DbPRoviderFactoryNames must be a hastable"
            return;
        }
    }

    $segments = @($Path);

    if($Path.Contains(".")) { $segments = $Path.Split("."); }
    if($Path.Contains("/")) { $segments = $Path.Split("/"); }

    $root = $GzDbOptions
    if(!$root) {
        Write-Warning "missing gzAdpOptions"
    }
    for($i = 0; $i -lt $segments.Length; $i++) {
        $segment = $segments[$i]
        if($i -eq ($segments.Length - 1)) {
            if($root.ContainsKey($segment)) {
                $root[$segment] = $Value
            } else {
                $root.Add($segment, $Value);
            }
            break;
        }

        if($root.ContainsKey($segment)) {
            $root = $root[$segment];
            if(! ($root -is [Hashtable])) {
                Write-Warning "$segment is not a hashable"
                return;
            }
        } else {
            $root.Add($segment, @{})
            $root = $root[$segment];
        }
    }
}