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> (StringComparer.InvariantCultureIgnoreCase);
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 ("Keyword does not exist");
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 (keyword == null || keyword.Trim () == "")
175 throw new ArgumentException ("Keyword should not be emtpy");
177 throw new ArgumentException ("Value should not be null");
178 if (ContainsKey (keyword)) {
179 _dictionary [keyword] = value;
181 _dictionary.Add (keyword, value);
186 [MonoLimitation("useOdbcRules set to true is not supported")]
187 public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value,
190 if (useOdbcRules == false) {
191 AppendKeyValuePair (builder, keyword, value);
193 throw new NotImplementedException ();
197 public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
199 if (builder.Length > 0) {
200 char lastChar = builder [builder.Length];
201 if (lastChar != ';' && lastChar != ' ')
202 builder.Append (';');
203 else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
204 builder.Append (';');
206 builder.AppendFormat ("{0}={1}", keyword, value);
209 public virtual void Clear ()
211 _dictionary.Clear ();
214 public virtual bool ContainsKey (string keyword)
217 throw new ArgumentNullException ("Invalid argument", keyword);
218 return _dictionary.ContainsKey (keyword);
221 public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
225 if (Count != connectionStringBuilder.Count)
228 foreach (string key in Keys) {
229 if (!this [key].Equals (connectionStringBuilder [key])) {
235 } catch (ArgumentException e) {
242 protected virtual void GetProperties (Hashtable propertyDescriptors)
244 throw new NotImplementedException ();
248 protected internal void ClearPropertyDescriptors ()
250 throw new NotImplementedException ();
253 public virtual bool Remove (string keyword)
255 return _dictionary.Remove (keyword);
258 public virtual bool ShouldSerialize (string keyword)
260 throw new NotImplementedException ();
263 void ICollection.CopyTo (Array array, int index)
266 throw new ArgumentNullException ("array");
267 KeyValuePair<string, object> [] arr = array as KeyValuePair<string, object> [];
269 throw new ArgumentException ("Target array type is not compatible with the type of items in the collection");
270 ((ICollection<KeyValuePair<string, object>>) _dictionary).CopyTo (arr, index);
273 void IDictionary.Add (object keyword, object value)
275 this.Add ((string) keyword, value);
278 bool IDictionary.Contains (object keyword)
280 return ContainsKey ((string) keyword);
283 IDictionaryEnumerator IDictionary.GetEnumerator ()
285 return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
288 void IDictionary.Remove (object keyword)
290 Remove ((string) keyword);
293 IEnumerator IEnumerable.GetEnumerator ()
295 return (IEnumerator) _dictionary.GetEnumerator ();
298 private static object _staticAttributeCollection = null;
299 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
301 object value = _staticAttributeCollection;
303 CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
304 DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
305 Attribute [] attrs = {clsAttr, defMemAttr};
306 value = new AttributeCollection (attrs);
308 System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
309 return _staticAttributeCollection as AttributeCollection;
312 string ICustomTypeDescriptor.GetClassName ()
314 return this.GetType ().ToString ();
317 string ICustomTypeDescriptor.GetComponentName ()
322 TypeConverter ICustomTypeDescriptor.GetConverter ()
324 return new CollectionConverter ();
327 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
332 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
337 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
342 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
344 return EventDescriptorCollection.Empty;
347 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
349 return EventDescriptorCollection.Empty;
352 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
354 return PropertyDescriptorCollection.Empty;
357 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
359 return PropertyDescriptorCollection.Empty;
362 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
364 throw new NotImplementedException ();
367 public override string ToString ()
369 return ConnectionString;
372 public virtual bool TryGetValue (string keyword, out object value)
374 // FIXME : not sure, difference between this [keyword] and this method
375 bool found = ContainsKey (keyword);
377 value = this [keyword];
383 #endregion // Public Methods
386 #endif // NET_2_0 using