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