-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathDesignTimeConnectionString.fs
More file actions
77 lines (69 loc) · 3.73 KB
/
DesignTimeConnectionString.fs
File metadata and controls
77 lines (69 loc) · 3.73 KB
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
namespace FSharp.Data.SqlClient
open System.Configuration
open System.IO
open System
open System.Collections.Generic
open System.Diagnostics
[<CompilerMessageAttribute("This API supports the FSharp.Data.SqlClient infrastructure and is not intended to be used directly from your code.", 101, IsHidden = true)>]
type internal DesignTimeConnectionString =
| Literal of string
| NameInConfig of name: string * value: string * provider: string
static member Parse(s: string, resolutionFolder, fileName) =
// Use StringSplitOptions.None so that the empty-string guard actually fires:
// RemoveEmptyEntries turns "" into [||] which skips the [|""|] branch.
match s.Trim().Split([|'='|], 2, StringSplitOptions.None) with
| [| "" |] -> invalidArg "ConnectionStringOrName" "Value is empty!"
| [| prefix; tail |] when prefix.Trim().ToLower() = "name" ->
let name = tail.Trim()
let value, provider = DesignTimeConnectionString.ReadFromConfig( name, resolutionFolder, fileName)
NameInConfig( name, value, provider)
| _ ->
Literal s
static member ReadFromConfig(name, resolutionFolder, fileName) =
let configFilename =
if fileName <> "" then
let path = Path.Combine(resolutionFolder, fileName)
if not <| File.Exists path
then raise <| FileNotFoundException( sprintf "Could not find config file '%s'." path)
else path
else
// note: these filenames are case sensitive on linux
let file =
seq { yield "app.config"
yield "App.config"
yield "web.config"
yield "Web.config" }
|> Seq.map (fun v -> Path.Combine(resolutionFolder,v))
|> Seq.tryFind File.Exists
match file with
| None -> failwithf "Cannot find either App.config or Web.config."
| Some file -> file
let map = ExeConfigurationFileMap()
map.ExeConfigFilename <- configFilename
let configSection = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).ConnectionStrings.ConnectionStrings
match configSection, lazy configSection.[name] with
| null, _ | _, Lazy null -> raise <| KeyNotFoundException(message = sprintf "Cannot find name %s in <connectionStrings> section of %s file." name configFilename)
| _, Lazy x ->
let providerName = if String.IsNullOrEmpty x.ProviderName then "System.Data.SqlClient" else x.ProviderName
x.ConnectionString, providerName
member this.Value =
match this with
| Literal value -> value
| NameInConfig(_, value, _) -> value
member this.RunTimeValueExpr isHostedExecution =
match this with
| Literal value -> <@@ value @@>
| NameInConfig(name, value, _) ->
<@@
let hostProcess = Process.GetCurrentProcess().ProcessName.ToUpper()
if isHostedExecution
|| (Environment.Is64BitProcess && hostProcess = "FSIANYCPU")
|| (not Environment.Is64BitProcess && hostProcess = "FSI")
then
value
else
let section = ConfigurationManager.ConnectionStrings.[name]
if isNull section then raise <| KeyNotFoundException(message = sprintf "Cannot find name %s in <connectionStrings> section of config file." name)
else section.ConnectionString
@@>
member this.IsDefinedByLiteral = match this with | Literal _ -> true | _ -> false