* SqlMetaData.cs: Implemented an overloaded constructor, missing
[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> (StringComparer.InvariantCultureIgnoreCase);
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 ("Keyword does not exist");
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 (keyword == null || keyword.Trim () == "")
175                                 throw new ArgumentException ("Keyword should not be emtpy");
176                         if (value == null)
177                                 throw new ArgumentException ("Value should not be null");
178                         if (ContainsKey (keyword)) {
179                                 _dictionary [keyword] = value;
180                         } else {
181                                 _dictionary.Add (keyword, value);
182                         }
183
184                 }
185
186                 [MonoLimitation("useOdbcRules set to true is not supported")]
187                 public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value,
188                                                        bool useOdbcRules)
189                 {
190                         if (useOdbcRules == false) {
191                                 AppendKeyValuePair (builder, keyword, value);
192                         } else {
193                                 throw new NotImplementedException ();
194                         }
195                 }
196
197                 public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
198                 {
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 (';');
205                         }
206                         builder.AppendFormat ("{0}={1}", keyword, value);
207                 }
208
209                 public virtual void Clear ()
210                 {
211                         _dictionary.Clear ();
212                 }
213
214                 public virtual bool ContainsKey (string keyword)
215                 {
216                         if (keyword == null)
217                                 throw new ArgumentNullException ("Invalid argument", keyword);
218                         return _dictionary.ContainsKey (keyword);
219                 }
220
221                 public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
222                 {
223                         bool ret = true;
224                         try {
225                                 if (Count != connectionStringBuilder.Count)
226                                         ret = false;
227                                 else {
228                                         foreach (string key in Keys) {
229                                                 if (!this [key].Equals (connectionStringBuilder [key])) {
230                                                         ret = false;
231                                                         break;
232                                                 }
233                                         }
234                                 }
235                         } catch (ArgumentException e) {
236                                 ret = false;
237                         }
238                         return ret;
239                 }
240
241                 [MonoTODO]
242                 protected virtual void GetProperties (Hashtable propertyDescriptors)
243                 {
244                         throw new NotImplementedException ();
245                 }
246
247                 [MonoTODO]
248                 protected internal void ClearPropertyDescriptors ()
249                 {
250                         throw new NotImplementedException ();
251                 }
252
253                 public virtual bool Remove (string keyword)
254                 {
255                         return _dictionary.Remove (keyword);
256                 }
257
258                 public virtual bool ShouldSerialize (string keyword)
259                 {
260                         throw new NotImplementedException ();
261                 }
262
263                 void ICollection.CopyTo (Array array, int index)
264                 {
265                         if (array == null)
266                                 throw new ArgumentNullException ("array");
267                         KeyValuePair<string, object> [] arr = array as KeyValuePair<string, object> [];
268                         if (arr == null)
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);
271                 }
272
273                 void IDictionary.Add (object keyword, object value)
274                 {
275                         this.Add ((string) keyword, value);
276                 }
277
278                 bool IDictionary.Contains (object keyword)
279                 {
280                         return ContainsKey ((string) keyword);
281                 }
282
283                 IDictionaryEnumerator IDictionary.GetEnumerator ()
284                 {
285                         return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
286                 }
287
288                 void IDictionary.Remove (object keyword)
289                 {
290                         Remove ((string) keyword);
291                 }
292
293                 IEnumerator IEnumerable.GetEnumerator ()
294                 {
295                         return (IEnumerator) _dictionary.GetEnumerator ();
296                 }
297
298                 private static object _staticAttributeCollection = null;
299                 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
300                 {
301                         object value = _staticAttributeCollection;
302                         if (value == null) {
303                                 CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
304                                 DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
305                                 Attribute [] attrs = {clsAttr, defMemAttr};
306                                 value = new AttributeCollection (attrs);
307                         }
308                         System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
309                         return _staticAttributeCollection as AttributeCollection;
310                 }
311
312                 string ICustomTypeDescriptor.GetClassName ()
313                 {
314                         return this.GetType ().ToString ();
315                 }
316
317                 string ICustomTypeDescriptor.GetComponentName ()
318                 {
319                         return null;
320                 }
321
322                 TypeConverter ICustomTypeDescriptor.GetConverter ()
323                 {
324                         return new CollectionConverter ();
325                 }
326
327                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
328                 {
329                         return null;
330                 }
331
332                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
333                 {
334                         return null;
335                 }
336
337                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
338                 {
339                         return null;
340                 }
341
342                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
343                 {
344                         return EventDescriptorCollection.Empty;
345                 }
346
347                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
348                 {
349                         return EventDescriptorCollection.Empty;
350                 }
351
352                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
353                 {
354                         return PropertyDescriptorCollection.Empty;
355                 }
356
357                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
358                 {
359                         return PropertyDescriptorCollection.Empty;
360                 }
361
362                 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
363                 {
364                         throw new NotImplementedException ();
365                 }
366
367                 public override string ToString ()
368                 {
369                         return ConnectionString;
370                 }
371
372                 public virtual bool TryGetValue (string keyword, out object value)
373                 {
374                         // FIXME : not sure, difference between this [keyword] and this method
375                         bool found = ContainsKey (keyword);
376                         if (found)
377                                 value = this [keyword];
378                         else
379                                 value = null;
380                         return found;
381                 }
382
383                 #endregion // Public Methods
384         }
385 }
386 #endif // NET_2_0 using