2 // System.Data.OleDb.OleDbConnection
5 // Konstantin Triger <kostat@mainsoft.com>
6 // Boris Kirzner <borisk@mainsoft.com>
8 // (C) 2006 Mainsoft Corporation (http://www.mainsoft.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Specialized;
36 using System.Globalization;
39 namespace Mainsoft.Data.Configuration
41 public class ConnectionStringDictionary: IConnectionStringDictionary
45 private readonly IDictionary _dictionary;
46 private readonly NameValueCollection _mapping;
47 private readonly NameValueCollection _actualKeys;
48 const string DataDirectoryPlaceholder = "|DataDirectory|";
54 public ConnectionStringDictionary(string connectionString, NameValueCollection defaultMapping)
56 _actualKeys = new NameValueCollection();
57 _dictionary = Parse (connectionString);
58 _mapping = defaultMapping;
61 #endregion // Constructors
65 public string GetConnectionStringKey (string key)
67 string cached = _actualKeys [key];
72 for(int i = 0, c = _mapping.Keys.Count; i < c; i++) {
73 if (string.Compare(key, _mapping.Keys[i], true,
74 CultureInfo.InvariantCulture) == 0) {
75 string[] values = _mapping.GetValues(i);
76 for(int j = 0; j < values.Length; j++) {
77 string actualKey = values[j];
78 if (_dictionary.Contains (actualKey)) {
79 _actualKeys.Add (key, actualKey);
86 if (_dictionary.Contains(key))
92 public static IDictionary Parse (string connectionString)
94 IDictionary userParameters = CollectionsUtil.CreateCaseInsensitiveHashtable();
96 if (connectionString == null || connectionString.Length == 0) {
97 return userParameters;
99 //connectionString += ";";
101 if (connectionString.IndexOf (DataDirectoryPlaceholder, StringComparison.Ordinal) >= 0) {
102 string dataDirectory = (string) AppDomain.CurrentDomain.GetData ("DataDirectory");
103 if (dataDirectory != null && dataDirectory.Length > 0) {
104 char lastChar = dataDirectory [dataDirectory.Length - 1];
105 if (lastChar != Path.DirectorySeparatorChar &&
106 lastChar != Path.AltDirectorySeparatorChar)
107 dataDirectory += '/';
109 connectionString = connectionString.Replace (DataDirectoryPlaceholder, dataDirectory);
112 bool inQuote = false;
113 bool inDQuote = false;
116 string name = String.Empty;
117 string value = String.Empty;
118 StringBuilder sb = new StringBuilder (connectionString.Length);
120 for (int i = 0; i < connectionString.Length; i ++) {
121 char c = connectionString [i];
123 if (i == connectionString.Length - 1)
126 peek = connectionString [i + 1];
132 else if (peek == c) {
142 else if (peek == c) {
147 inDQuote = !inDQuote;
150 if (inDQuote || inQuote)
153 if (name != null && name.Length > 0) {
154 value = sb.ToString();
155 userParameters [name.Trim()] = value.Trim();
159 value = String.Empty;
164 if (inDQuote || inQuote || !inName)
166 else if (peek == c) {
171 name = sb.ToString();
177 if (inQuote || inDQuote)
179 else if (sb.Length > 0 && peek != ';')
188 if (inDQuote || inQuote)
189 throw new ArgumentException("connectionString");
191 if (name != null && name.Length > 0) {
192 value = sb.ToString();
193 userParameters [name.Trim()] = value.Trim();
196 return userParameters;
200 #endregion // Methods
202 #region IDictionary Members
204 public virtual bool IsFixedSize
206 get { return _dictionary.IsFixedSize; }
209 public virtual bool IsReadOnly
211 get { return _dictionary.IsReadOnly; }
214 public virtual ICollection Keys
217 return _dictionary.Keys;
221 public virtual object this [object key] {
224 if (!(key is String))
225 throw new ArgumentException("key is not string");
227 string skey = (string)key;
229 skey = GetConnectionStringKey(skey);
233 return _dictionary[skey];
237 if (!(key is String))
238 throw new ArgumentException("key is not string");
240 string skey = (string)key;
242 skey = GetConnectionStringKey(skey);
246 _dictionary[skey] = value;
250 public virtual ICollection Values
252 get { return _dictionary.Values; }
255 public virtual void Add (object key, object value)
257 _dictionary.Add ((string)key, (string)value);
260 public virtual void Clear ()
262 _dictionary.Clear ();
265 public virtual bool Contains (object key)
267 return _dictionary.Contains (key);
270 public virtual IDictionaryEnumerator GetEnumerator ()
272 return _dictionary.GetEnumerator ();
275 public virtual void Remove (object key)
277 _dictionary.Remove ((string)key);
280 #endregion // IDictionary Members
282 #region IEnumerable Members
284 IEnumerator IEnumerable.GetEnumerator ()
286 return this.GetEnumerator();
289 #endregion // IEnumerable Members
291 #region ICollection Members
293 public virtual bool IsSynchronized
295 get { return ((ICollection)_dictionary).IsSynchronized; }
298 public virtual int Count
300 get { return _dictionary.Count; }
303 public virtual void CopyTo (Array array, int index)
305 _dictionary.CopyTo (array, index);
308 public virtual object SyncRoot
310 get {return ((ICollection)_dictionary).SyncRoot; }
313 #endregion // ICollection Members