|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
5 | 5 | using System;
|
6 |
| -using System.Collections.Generic; |
7 |
| -using System.Globalization; |
8 | 6 | using System.IO;
|
9 |
| -using System.Text; |
10 | 7 |
|
11 | 8 | namespace Microsoft.Data.Common
|
12 | 9 | {
|
13 | 10 | internal partial class DbConnectionOptions
|
14 | 11 | {
|
15 |
| - // instances of this class are intended to be immutable, i.e readonly |
16 |
| - // used by pooling classes so it is much easier to verify correctness |
17 |
| - // when not worried about the class being modified during execution |
18 |
| - |
19 |
| - public DbConnectionOptions(string connectionString, Dictionary<string, string> synonyms) |
20 |
| - { |
21 |
| - _parsetable = new Dictionary<string, string>(); |
22 |
| - _usersConnectionString = ((null != connectionString) ? connectionString : ""); |
23 |
| - |
24 |
| - // first pass on parsing, initial syntax check |
25 |
| - if (0 < _usersConnectionString.Length) |
26 |
| - { |
27 |
| - _keyChain = ParseInternal(_parsetable, _usersConnectionString, true, synonyms, false); |
28 |
| - HasPasswordKeyword = (_parsetable.ContainsKey(KEY.Password) || _parsetable.ContainsKey(SYNONYM.Pwd)); |
29 |
| - HasUserIdKeyword = (_parsetable.ContainsKey(KEY.User_ID) || _parsetable.ContainsKey(SYNONYM.UID)); |
30 |
| - } |
31 |
| - } |
32 |
| - |
33 |
| - protected DbConnectionOptions(DbConnectionOptions connectionOptions) |
34 |
| - { // Clone used by SqlConnectionString |
35 |
| - _usersConnectionString = connectionOptions._usersConnectionString; |
36 |
| - _parsetable = connectionOptions._parsetable; |
37 |
| - _keyChain = connectionOptions._keyChain; |
38 |
| - HasPasswordKeyword = connectionOptions.HasPasswordKeyword; |
39 |
| - HasUserIdKeyword = connectionOptions.HasUserIdKeyword; |
40 |
| - } |
41 |
| - |
42 |
| - public bool IsEmpty => _keyChain == null; |
43 |
| - |
44 |
| - internal bool TryGetParsetableValue(string key, out string value) => _parsetable.TryGetValue(key, out value); |
45 |
| - |
46 |
| - // same as Boolean, but with SSPI thrown in as valid yes |
47 |
| - public bool ConvertValueToIntegratedSecurity() |
| 12 | + internal string ExpandAttachDbFileName(string replacementValue) |
48 | 13 | {
|
49 |
| - string value; |
50 |
| - return _parsetable.TryGetValue(KEY.Integrated_Security, out value) && value != null ? |
51 |
| - ConvertValueToIntegratedSecurityInternal(value) : |
52 |
| - false; |
53 |
| - } |
| 14 | + int copyPosition = 0; |
54 | 15 |
|
55 |
| - internal bool ConvertValueToIntegratedSecurityInternal(string stringValue) |
56 |
| - { |
57 |
| - if (CompareInsensitiveInvariant(stringValue, "sspi") || CompareInsensitiveInvariant(stringValue, "true") || CompareInsensitiveInvariant(stringValue, "yes")) |
58 |
| - return true; |
59 |
| - else if (CompareInsensitiveInvariant(stringValue, "false") || CompareInsensitiveInvariant(stringValue, "no")) |
60 |
| - return false; |
61 |
| - else |
| 16 | + System.Text.StringBuilder builder = new(_usersConnectionString.Length); |
| 17 | + for (NameValuePair current = KeyChain; null != current; current = current.Next) |
62 | 18 | {
|
63 |
| - string tmp = stringValue.Trim(); // Remove leading & trailing whitespace. |
64 |
| - if (CompareInsensitiveInvariant(tmp, "sspi") || CompareInsensitiveInvariant(tmp, "true") || CompareInsensitiveInvariant(tmp, "yes")) |
65 |
| - return true; |
66 |
| - else if (CompareInsensitiveInvariant(tmp, "false") || CompareInsensitiveInvariant(tmp, "no")) |
67 |
| - return false; |
| 19 | + if (string.Equals(current.Name, DbConnectionStringKeywords.AttachDBFilename, StringComparison.InvariantCultureIgnoreCase)) |
| 20 | + { |
| 21 | + builder.Append($"{current.Name}={replacementValue};"); |
| 22 | + } |
68 | 23 | else
|
69 | 24 | {
|
70 |
| - throw ADP.InvalidConnectionOptionValue(KEY.Integrated_Security); |
| 25 | + builder.Append(_usersConnectionString, copyPosition, current.Length); |
71 | 26 | }
|
| 27 | + copyPosition += current.Length; |
72 | 28 | }
|
73 |
| - } |
74 |
| - |
75 |
| - public int ConvertValueToInt32(string keyName, int defaultValue) |
76 |
| - { |
77 |
| - string value; |
78 |
| - return _parsetable.TryGetValue(keyName, out value) && value != null ? |
79 |
| - ConvertToInt32Internal(keyName, value) : |
80 |
| - defaultValue; |
81 |
| - } |
82 |
| - |
83 |
| - internal static int ConvertToInt32Internal(string keyname, string stringValue) |
84 |
| - { |
85 |
| - try |
86 |
| - { |
87 |
| - return int.Parse(stringValue, System.Globalization.NumberStyles.Integer, CultureInfo.InvariantCulture); |
88 |
| - } |
89 |
| - catch (FormatException e) |
90 |
| - { |
91 |
| - throw ADP.InvalidConnectionOptionValue(keyname, e); |
92 |
| - } |
93 |
| - catch (OverflowException e) |
94 |
| - { |
95 |
| - throw ADP.InvalidConnectionOptionValue(keyname, e); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - public string ConvertValueToString(string keyName, string defaultValue) |
100 |
| - { |
101 |
| - string value; |
102 |
| - return _parsetable.TryGetValue(keyName, out value) && value != null ? value : defaultValue; |
103 |
| - } |
104 | 29 |
|
105 |
| - public bool ContainsKey(string keyword) |
106 |
| - { |
107 |
| - return _parsetable.ContainsKey(keyword); |
108 |
| - } |
109 |
| - |
110 |
| - protected internal virtual string Expand() |
111 |
| - { |
112 |
| - return _usersConnectionString; |
| 30 | + return builder.ToString(); |
113 | 31 | }
|
114 | 32 |
|
115 | 33 | // SxS notes:
|
@@ -151,25 +69,5 @@ internal static string ExpandDataDirectory(string keyword, string value)
|
151 | 69 | return fullPath;
|
152 | 70 | }
|
153 | 71 |
|
154 |
| - internal string ExpandAttachDbFileName(string replacementValue) |
155 |
| - { |
156 |
| - int copyPosition = 0; |
157 |
| - |
158 |
| - StringBuilder builder = new StringBuilder(_usersConnectionString.Length); |
159 |
| - for (NameValuePair current = _keyChain; null != current; current = current.Next) |
160 |
| - { |
161 |
| - if (current.Name == KEY.AttachDBFileName) |
162 |
| - { |
163 |
| - builder.Append($"{KEY.AttachDBFileName}={replacementValue};"); |
164 |
| - } |
165 |
| - else |
166 |
| - { |
167 |
| - builder.Append(_usersConnectionString, copyPosition, current.Length); |
168 |
| - } |
169 |
| - copyPosition += current.Length; |
170 |
| - } |
171 |
| - |
172 |
| - return builder.ToString(); |
173 |
| - } |
174 | 72 | }
|
175 | 73 | }
|
0 commit comments