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;
38 namespace Mainsoft.Data.Configuration
40 public class ConnectionStringDictionary: IConnectionStringDictionary
44 private readonly IDictionary _dictionary;
45 private readonly NameValueCollection _mapping;
46 private readonly NameValueCollection _actualKeys;
52 public ConnectionStringDictionary(string connectionString, NameValueCollection defaultMapping)
54 _actualKeys = new NameValueCollection();
55 _dictionary = Parse (connectionString);
56 _mapping = defaultMapping;
59 #endregion // Constructors
63 public string GetConnectionStringKey (string key)
65 string cached = _actualKeys [key];
70 for(int i = 0, c = _mapping.Keys.Count; i < c; i++) {
71 if (string.Compare(key, _mapping.Keys[i], true,
72 CultureInfo.InvariantCulture) == 0) {
73 string[] values = _mapping.GetValues(i);
74 for(int j = 0; j < values.Length; j++) {
75 string actualKey = values[j];
76 if (_dictionary.Contains (actualKey)) {
77 _actualKeys.Add (key, actualKey);
84 if (_dictionary.Contains(key))
90 public static IDictionary Parse (string connectionString)
92 IDictionary userParameters = CollectionsUtil.CreateCaseInsensitiveHashtable();
94 if (connectionString == null || connectionString.Length == 0) {
95 return userParameters;
97 //connectionString += ";";
100 bool inDQuote = false;
103 string name = String.Empty;
104 string value = String.Empty;
105 StringBuilder sb = new StringBuilder (connectionString.Length);
107 for (int i = 0; i < connectionString.Length; i ++) {
108 char c = connectionString [i];
110 if (i == connectionString.Length - 1)
113 peek = connectionString [i + 1];
119 else if (peek == c) {
129 else if (peek == c) {
134 inDQuote = !inDQuote;
137 if (inDQuote || inQuote)
140 if (name != null && name.Length > 0) {
141 value = sb.ToString();
142 userParameters [name.Trim()] = value.Trim();
146 value = String.Empty;
151 if (inDQuote || inQuote || !inName)
153 else if (peek == c) {
158 name = sb.ToString();
164 if (inQuote || inDQuote)
166 else if (sb.Length > 0 && peek != ';')
175 if (inDQuote || inQuote)
176 throw new ArgumentException("connectionString");
178 if (name != null && name.Length > 0) {
179 value = sb.ToString();
180 userParameters [name.Trim()] = value.Trim();
183 return userParameters;
187 #endregion // Methods
189 #region IDictionary Members
191 public virtual bool IsFixedSize
193 get { return _dictionary.IsFixedSize; }
196 public virtual bool IsReadOnly
198 get { return _dictionary.IsReadOnly; }
201 public virtual ICollection Keys
204 return _dictionary.Keys;
208 public virtual object this [object key] {
211 if (!(key is String))
212 throw new ArgumentException("key is not string");
214 string skey = (string)key;
216 skey = GetConnectionStringKey(skey);
220 return _dictionary[skey];
224 if (!(key is String))
225 throw new ArgumentException("key is not string");
227 string skey = (string)key;
229 skey = GetConnectionStringKey(skey);
233 _dictionary[skey] = value;
237 public virtual ICollection Values
239 get { return _dictionary.Values; }
242 public virtual void Add (object key, object value)
244 _dictionary.Add ((string)key, (string)value);
247 public virtual void Clear ()
249 _dictionary.Clear ();
252 public virtual bool Contains (object key)
254 return _dictionary.Contains (key);
257 public virtual IDictionaryEnumerator GetEnumerator ()
259 return _dictionary.GetEnumerator ();
262 public virtual void Remove (object key)
264 _dictionary.Remove ((string)key);
267 #endregion // IDictionary Members
269 #region IEnumerable Members
271 IEnumerator IEnumerable.GetEnumerator ()
273 return this.GetEnumerator();
276 #endregion // IEnumerable Members
278 #region ICollection Members
280 public virtual bool IsSynchronized
282 get { return ((ICollection)_dictionary).IsSynchronized; }
285 public virtual int Count
287 get { return _dictionary.Count; }
290 public virtual void CopyTo (Array array, int index)
292 _dictionary.CopyTo (array, index);
295 public virtual object SyncRoot
297 get {return ((ICollection)_dictionary).SyncRoot; }
300 #endregion // ICollection Members