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.
32 using System.Reflection;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Collections.Generic;
38 using System.Data.Common;
40 namespace System.Data.Common
43 public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, ICustomTypeDescriptor
46 Dictionary<string, object> _dictionary = null;
50 public DbConnectionStringBuilder ()
55 public DbConnectionStringBuilder (bool useFirstKeyValue)
57 throw new NotImplementedException ();
62 _dictionary = new Dictionary<string, object> ();
65 #endregion // Constructors
68 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
69 [EditorBrowsable (EditorBrowsableState.Never)]
72 public bool BrowsableConnectionString
74 get { throw new NotImplementedException (); }
75 set { throw new NotImplementedException (); }
78 [RefreshProperties (RefreshProperties.All)]
79 public string ConnectionString
83 IDictionary<string, object> dictionary = (IDictionary <string, object>) _dictionary;
85 foreach (string key in dictionary.Keys) {
86 conn += key + "=" + dictionary [key].ToString () + ";";
88 conn = conn.TrimEnd (';');
93 throw new ArgumentNullException ("ConnectionString cannot be null");
95 string [] parameters = value.Split (new char [] {';'});
96 foreach (string args in parameters) {
97 string [] arg = args.Split (new char [] {'='}, 2);
98 if (arg.Length == 2) {
99 string key = arg [0].Trim ().ToUpper ();
100 string val = arg [1].Trim ();
108 public virtual int Count
110 get { return _dictionary.Count; }
114 public virtual bool IsFixedSize
116 get { return false; }
120 public bool IsReadOnly
122 get { throw new NotImplementedException (); }
126 public virtual object this [string keyword]
130 if (ContainsKey (keyword))
131 return _dictionary [keyword];
133 throw new ArgumentException ();
135 set { Add (keyword, value); }
139 public virtual ICollection Keys
141 get { return _dictionary.Keys; }
144 bool ICollection.IsSynchronized
146 get { throw new NotImplementedException (); }
149 object ICollection.SyncRoot
151 get { throw new NotImplementedException (); }
154 object IDictionary.this [object keyword]
156 get { return this [(string) keyword]; }
157 set { this [(string) keyword] = value; }
161 public virtual ICollection Values
163 get { return _dictionary.Values; }
166 #endregion // Properties
172 public void Add (string keyword, object value)
174 if (ContainsKey (keyword)) {
175 _dictionary [keyword] = value;
177 _dictionary.Add (keyword, value);
183 public static void AppendKeyValuePair(StringBuilder builder, string keyword, string value,
186 throw new NotImplementedException ();
189 public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
191 if (builder.Length > 0) {
192 char lastChar = builder [builder.Length];
193 if (lastChar != ';' && lastChar != ' ')
194 builder.Append (';');
195 else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
196 builder.Append (';');
198 builder.AppendFormat ("{0}={1}", keyword, value);
201 public virtual void Clear ()
203 _dictionary.Clear ();
206 public virtual bool ContainsKey (string keyword)
208 return _dictionary.ContainsKey (keyword);
211 public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
215 if (Count != connectionStringBuilder.Count)
218 foreach (string key in Keys) {
219 if (!this [key].Equals (connectionStringBuilder [key])) {
225 } catch (ArgumentException e) {
232 protected virtual void GetProperties(Hashtable propertyDescriptors)
234 throw new NotImplementedException ();
237 protected internal void ClearPropertyDescriptors ()
239 throw new NotImplementedException ();
242 public virtual bool Remove (string keyword)
244 return _dictionary.Remove (keyword);
247 public virtual bool ShouldSerialize (string keyword)
249 throw new NotImplementedException ();
252 void ICollection.CopyTo (Array array, int index)
255 throw new ArgumentNullException ("array");
256 KeyValuePair<string, object> [] arr = array as KeyValuePair<string, object> [];
258 throw new ArgumentException ("Target array type is not compatible with the type of items in the collection");
259 ((ICollection<KeyValuePair<string, object>>) _dictionary).CopyTo (arr, index);
262 void IDictionary.Add (object keyword, object value)
264 this.Add ((string) keyword, value);
267 bool IDictionary.Contains (object keyword)
269 return ContainsKey ((string) keyword);
272 IDictionaryEnumerator IDictionary.GetEnumerator ()
274 return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
277 void IDictionary.Remove (object keyword)
279 Remove ((string) keyword);
282 IEnumerator IEnumerable.GetEnumerator ()
284 return (IEnumerator) _dictionary.GetEnumerator ();
287 private static object _staticAttributeCollection = null;
288 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
290 object value = _staticAttributeCollection;
292 CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
293 DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
294 Attribute [] attrs = {clsAttr, defMemAttr};
295 value = new AttributeCollection (attrs);
297 System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
298 return _staticAttributeCollection as AttributeCollection;
301 string ICustomTypeDescriptor.GetClassName ()
303 return this.GetType ().ToString ();
306 string ICustomTypeDescriptor.GetComponentName ()
311 TypeConverter ICustomTypeDescriptor.GetConverter ()
313 return new CollectionConverter ();
316 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
321 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
326 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
331 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
333 return EventDescriptorCollection.Empty;
336 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
338 return EventDescriptorCollection.Empty;
341 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
343 return PropertyDescriptorCollection.Empty;
346 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
348 return PropertyDescriptorCollection.Empty;
351 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
353 throw new NotImplementedException ();
356 public override string ToString ()
358 return ConnectionString;
361 public virtual bool TryGetValue (string keyword, out object value)
363 // FIXME : not sure, difference between this [keyword] and this method
364 bool found = ContainsKey (keyword);
366 value = this [keyword];
372 #endregion // Public Methods
375 #endif // NET_2_0 using