2 // System.Data.Common.DbConnectionStringBuilder.cs
5 // Sureshkumar T (tsureshkumar@novell.com)
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Data.Common
36 using System.Reflection;
37 using System.Collections;
38 using System.ComponentModel;
39 using System.Collections.Generic;
42 using System.Data.Common;
46 public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable,
47 IDictionary<string, object>,
48 ICollection<KeyValuePair<string, object>>,
49 IEnumerable<KeyValuePair<string, object>>,
53 Dictionary<string, object> _dictionary = null;
57 public DbConnectionStringBuilder ()
62 public DbConnectionStringBuilder (bool useFirstKeyValue)
64 throw new NotImplementedException ();
69 _dictionary = new Dictionary<string, object> ();
72 #endregion // Constructors
75 public bool BrowsableConnectionString
77 get { throw new NotImplementedException (); }
78 set { throw new NotImplementedException (); }
80 public string ConnectionString
84 IDictionary<string, object> dictionary = (IDictionary <string, object>) _dictionary;
86 foreach (string key in dictionary.Keys) {
87 conn += key + "=" + dictionary [key].ToString () + ";";
89 conn = conn.TrimEnd (';');
92 set { throw new NotImplementedException (); }
94 public virtual int Count
96 get { return _dictionary.Count; }
99 public virtual bool IsFixedSize
101 get { return false; }
104 public bool IsReadOnly
106 get { throw new NotImplementedException (); }
109 public virtual object this [string keyword]
113 if (ContainsKey (keyword))
114 return _dictionary [keyword];
116 throw new ArgumentException ();
118 set { Add (keyword, value); }
120 public virtual ICollection Keys
122 get { return (ICollection) ( (IDictionary <string, object>)_dictionary).Keys; }
125 ICollection<string> IDictionary<string, object>.Keys
127 get { return (ICollection<string>) ( (IDictionary<string, object>) _dictionary).Keys; }
130 ICollection<object> IDictionary<string, object>.Values
132 get { return (ICollection<object>) ( (IDictionary<string, object>)_dictionary).Values; }
135 bool ICollection.IsSynchronized
137 get { throw new NotImplementedException (); }
140 object ICollection.SyncRoot
142 get { throw new NotImplementedException (); }
145 object IDictionary.this [object keyword]
147 get { return this [(string) keyword]; }
148 set { this [(string) keyword] = value; }
151 public virtual ICollection Values
153 get { return (ICollection) ( (IDictionary<string, object>)_dictionary).Values; }
156 #endregion // Properties
162 public void Add (string keyword, object value)
164 if (ContainsKey (keyword)) {
165 _dictionary [keyword] = value;
167 _dictionary.Add (keyword, value);
172 public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
174 if (builder.Length > 0) {
175 char lastChar = builder [builder.Length];
176 if (lastChar != ';' && lastChar != ' ')
177 builder.Append (';');
178 else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
179 builder.Append (';');
181 builder.AppendFormat ("{0}={1}", keyword, value);
184 public virtual void Clear ()
186 _dictionary.Clear ();
189 public virtual bool ContainsKey (string keyword)
191 return _dictionary.ContainsKey (keyword);
194 public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
198 if (Count != connectionStringBuilder.Count)
201 foreach (string key in Keys) {
202 if (!this [key].Equals (connectionStringBuilder [key])) {
208 } catch (ArgumentException e) {
214 public virtual bool Remove (string keyword)
216 return _dictionary.Remove (keyword);
219 [Obsolete ("Do not use. Please use the Remove method.")]
220 public virtual void Reset (string keyword)
222 throw new NotImplementedException ();
225 public virtual bool ShouldSerialize (string keyword)
227 throw new NotImplementedException ();
230 void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> keyValuePair)
232 Add (keyValuePair.Key, keyValuePair.Value);
235 bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> keyValuePair)
237 return ContainsKey (keyValuePair.Key);
240 void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int index)
242 if (index + Count > array.Length)
243 throw new ArgumentException ("The destination does not have enough length!");
244 foreach (KeyValuePair<string, object> keyValue in this) {
245 array [index++] = keyValue;
249 bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> keyValuePair)
251 return Remove (keyValuePair.Key);
254 IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator ()
256 return _dictionary.GetEnumerator ();
259 void ICollection.CopyTo (Array array, int index)
262 KeyValuePair <string, object> [] arr = null;
264 arr = (KeyValuePair<string, object> []) array;
265 } catch (InvalidCastException e) {
266 throw new ArgumentException (
267 "Target array type is not compatible with the type of items in the collection."
270 ICollection<KeyValuePair<string, object>> ptr = (ICollection<KeyValuePair<string, object>>) this;
271 ptr.CopyTo (arr, index);
275 void IDictionary.Add (object keyword, object value)
277 this.Add ((string) keyword, value);
280 bool IDictionary.Contains (object keyword)
282 return ContainsKey ((string) keyword);
285 IDictionaryEnumerator IDictionary.GetEnumerator ()
287 return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
290 void IDictionary.Remove (object keyword)
292 Remove ((string) keyword);
295 IEnumerator IEnumerable.GetEnumerator ()
297 return (IEnumerator) _dictionary.GetEnumerator ();
300 private static object _staticAttributeCollection = null;
301 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
303 object value = _staticAttributeCollection;
305 CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
306 DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
307 Attribute [] attrs = {clsAttr, defMemAttr};
308 value = new AttributeCollection (attrs);
310 System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
311 return _staticAttributeCollection as AttributeCollection;
314 string ICustomTypeDescriptor.GetClassName ()
316 return this.GetType ().ToString ();
319 string ICustomTypeDescriptor.GetComponentName ()
324 TypeConverter ICustomTypeDescriptor.GetConverter ()
326 return new CollectionConverter ();
329 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
334 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
339 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
344 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
346 return EventDescriptorCollection.Empty;
349 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
351 return EventDescriptorCollection.Empty;
354 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
356 return PropertyDescriptorCollection.Empty;
359 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
361 return PropertyDescriptorCollection.Empty;
364 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
366 throw new NotImplementedException ();
369 public override string ToString ()
371 return ConnectionString;
374 public virtual bool TryGetValue (string keyword, out object value)
376 // FIXME : not sure, difference between this [keyword] and this method
377 bool found = ContainsKey (keyword);
379 value = this [keyword];
385 #endregion // Public Methods
388 #endif // NET_2_0 using