New test.
[mono.git] / mcs / class / System.Data / System.Data.Common / DbConnectionStringBuilder.cs
1 //
2 // System.Data.Common.DbConnectionStringBuilder.cs
3 //
4 // Author:
5 //   Sureshkumar T (tsureshkumar@novell.com)
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 #if NET_2_0
30 using System;
31 using System.Text;
32 using System.Reflection;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Collections.Generic;
36
37 using System.Data;
38 using System.Data.Common;
39
40 namespace System.Data.Common
41 {
42
43         public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, ICustomTypeDescriptor
44         {
45                 #region Fields
46                 Dictionary<string, object> _dictionary = null;
47                 #endregion Fields
48
49                 #region Constructors
50                 public DbConnectionStringBuilder ()
51                 {
52                         Init ();
53                 }
54
55                 public DbConnectionStringBuilder (bool useFirstKeyValue)
56                 {
57                         throw new NotImplementedException ();
58                 }
59
60                 private void Init ()
61                 {
62                         _dictionary = new Dictionary<string, object> ();
63                 }
64
65                 #endregion // Constructors
66
67                 #region Properties
68                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
69                 [EditorBrowsable (EditorBrowsableState.Never)]
70                 [Browsable (false)]
71                 [DesignOnly (true)]
72                 public bool BrowsableConnectionString
73                 {
74                         get { throw new NotImplementedException (); }
75                         set { throw new NotImplementedException (); }
76                 }
77
78                 [RefreshProperties (RefreshProperties.All)]
79                 public string ConnectionString
80                 {
81                         get
82                         {
83                                 IDictionary<string, object> dictionary = (IDictionary <string, object>) _dictionary;
84                                 string conn = "";
85                                 foreach (string key in dictionary.Keys) {
86                                         conn += key + "=" + dictionary [key].ToString () + ";";
87                                 }
88                                 conn = conn.TrimEnd (';');
89                                 return conn;
90                         }
91                         set { 
92                                 if (value == null)
93                                         throw new ArgumentNullException ("ConnectionString cannot be null");
94                                 
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 ();
101                                                 this [key] = val;
102                                         }
103                                 }
104                         }
105                 }
106
107                 [Browsable (false)]
108                 public virtual int Count
109                 {
110                         get { return _dictionary.Count; }
111                 }
112
113                 [Browsable (false)]
114                 public virtual bool IsFixedSize
115                 {
116                         get { return false; }
117                 }
118
119                 [Browsable (false)]
120                 public bool IsReadOnly
121                 {
122                         get { throw new NotImplementedException (); }
123                 }
124
125                 [Browsable (false)]
126                 public virtual object this [string keyword]
127                 {
128                         get
129                         {
130                                 if (ContainsKey (keyword))
131                                         return _dictionary [keyword];
132                                 else
133                                         throw new ArgumentException ();
134                         }
135                         set { Add (keyword, value); }
136                 }
137
138                 [Browsable (false)]
139                 public virtual ICollection Keys
140                 {
141                         get { return _dictionary.Keys; }
142                 }
143
144                 bool ICollection.IsSynchronized
145                 {
146                         get { throw new NotImplementedException (); }
147                 }
148
149                 object ICollection.SyncRoot
150                 {
151                         get { throw new NotImplementedException (); }
152                 }
153
154                 object IDictionary.this [object keyword]
155                 {
156                         get { return this [(string) keyword]; }
157                         set { this [(string) keyword] = value; }
158                 }
159
160                 [Browsable (false)]
161                 public virtual ICollection Values
162                 {
163                         get { return _dictionary.Values; }
164                 }
165
166                 #endregion // Properties
167
168
169                 #region Methods
170
171
172                 public void Add (string keyword, object value)
173                 {
174                         if (ContainsKey (keyword)) {
175                                 _dictionary [keyword] = value;
176                         } else {
177                                 _dictionary.Add (keyword, value);
178                         }
179
180                 }
181
182                 [MonoTODO]
183                 public static void AppendKeyValuePair(StringBuilder builder, string keyword, string value,
184                                                       bool useOdbcRules)
185                 {
186                         throw new NotImplementedException ();
187                 }
188
189                 public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
190                 {
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 (';');
197                         }
198                         builder.AppendFormat ("{0}={1}", keyword, value);
199                 }
200
201                 public virtual void Clear ()
202                 {
203                         _dictionary.Clear ();
204                 }
205
206                 public virtual bool ContainsKey (string keyword)
207                 {
208                         return _dictionary.ContainsKey (keyword);
209                 }
210
211                 public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
212                 {
213                         bool ret = true;
214                         try {
215                                 if (Count != connectionStringBuilder.Count)
216                                         ret = false;
217                                 else {
218                                         foreach (string key in Keys) {
219                                                 if (!this [key].Equals (connectionStringBuilder [key])) {
220                                                         ret = false;
221                                                         break;
222                                                 }
223                                         }
224                                 }
225                         } catch (ArgumentException e) {
226                                 ret = false;
227                         }
228                         return ret;
229                 }
230
231                 [MonoTODO]
232                 protected virtual void GetProperties(Hashtable propertyDescriptors)
233                 {
234                         throw new NotImplementedException ();
235                 }
236
237                 protected internal void ClearPropertyDescriptors ()
238                 {
239                         throw new NotImplementedException ();
240                 }
241
242                 public virtual bool Remove (string keyword)
243                 {
244                         return _dictionary.Remove (keyword);
245                 }
246
247                 public virtual bool ShouldSerialize (string keyword)
248                 {
249                         throw new NotImplementedException ();
250                 }
251
252                 void ICollection.CopyTo (Array array, int index)
253                 {
254                         if (array == null)
255                                 throw new ArgumentNullException ("array");
256                         KeyValuePair<string, object> [] arr = array as KeyValuePair<string, object> [];
257                         if (arr == null)
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);
260                 }
261
262                 void IDictionary.Add (object keyword, object value)
263                 {
264                         this.Add ((string) keyword, value);
265                 }
266
267                 bool IDictionary.Contains (object keyword)
268                 {
269                         return ContainsKey ((string) keyword);
270                 }
271
272                 IDictionaryEnumerator IDictionary.GetEnumerator ()
273                 {
274                         return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
275                 }
276
277                 void IDictionary.Remove (object keyword)
278                 {
279                         Remove ((string) keyword);
280                 }
281
282                 IEnumerator IEnumerable.GetEnumerator ()
283                 {
284                         return (IEnumerator) _dictionary.GetEnumerator ();
285                 }
286
287                 private static object _staticAttributeCollection = null;
288                 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
289                 {
290                         object value = _staticAttributeCollection;
291                         if (value == null) {
292                                 CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
293                                 DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
294                                 Attribute [] attrs = {clsAttr, defMemAttr};
295                                 value = new AttributeCollection (attrs);
296                         }
297                         System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
298                         return _staticAttributeCollection as AttributeCollection;
299                 }
300
301                 string ICustomTypeDescriptor.GetClassName ()
302                 {
303                         return this.GetType ().ToString ();
304                 }
305
306                 string ICustomTypeDescriptor.GetComponentName ()
307                 {
308                         return null;
309                 }
310
311                 TypeConverter ICustomTypeDescriptor.GetConverter ()
312                 {
313                         return new CollectionConverter ();
314                 }
315
316                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
317                 {
318                         return null;
319                 }
320
321                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
322                 {
323                         return null;
324                 }
325
326                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
327                 {
328                         return null;
329                 }
330
331                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
332                 {
333                         return EventDescriptorCollection.Empty;
334                 }
335
336                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
337                 {
338                         return EventDescriptorCollection.Empty;
339                 }
340
341                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
342                 {
343                         return PropertyDescriptorCollection.Empty;
344                 }
345
346                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
347                 {
348                         return PropertyDescriptorCollection.Empty;
349                 }
350
351                 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
352                 {
353                         throw new NotImplementedException ();
354                 }
355
356                 public override string ToString ()
357                 {
358                         return ConnectionString;
359                 }
360
361                 public virtual bool TryGetValue (string keyword, out object value)
362                 {
363                         // FIXME : not sure, difference between this [keyword] and this method
364                         bool found = ContainsKey (keyword);
365                         if (found)
366                                 value = this [keyword];
367                         else
368                                 value = null;
369                         return found;
370                 }
371
372                 #endregion // Public Methods
373         }
374 }
375 #endif // NET_2_0 using