BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Data / System.Data.Odbc / OdbcParameterCollection.cs
1 //
2 // System.Data.Odbc.OdbcParameterCollection
3 //
4 // Authors:
5 //   Brian Ritchie (brianlritchie@hotmail.com) 
6 //   Umadevi S (sumadevi@novell.com)
7 //   Amit Biswas (amit@amitbiswas.com)
8 //
9 // Copyright (C) Brian Ritchie, 2002
10 // Copyright (C) Novell,Inc 
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System.Collections;
37 using System.ComponentModel;
38 using System.Data;
39 using System.Data.Common;
40
41 namespace System.Data.Odbc
42 {
43         [ListBindable (false)]
44         [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
45 #if NET_2_0
46         public sealed class OdbcParameterCollection : DbParameterCollection
47 #else
48         public sealed class OdbcParameterCollection : MarshalByRefObject,
49                 IDataParameterCollection, IList, ICollection, IEnumerable
50 #endif // NET_2_0
51         {
52                 #region Fields
53
54                 readonly ArrayList list = new ArrayList ();
55                 int nullParamCount = 1;
56
57                 #endregion // Fields
58         
59                 #region Constructors
60
61                 internal OdbcParameterCollection ()
62                 {
63                 }
64
65                 #endregion // Constructors
66         
67                 #region Properties
68
69 #if ONLY_1_1
70                 [Browsable (false)]
71                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
72 #endif
73                 public
74 #if NET_2_0
75                 override
76 #endif
77                 int Count {
78                         get { return list.Count; }
79                 }
80
81                 [Browsable (false)]
82                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83                 public new OdbcParameter this [int index] {
84                         get { return (OdbcParameter) list [index]; }
85                         set { list [index] = value; }
86                 }
87
88                 [Browsable (false)]
89                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90                 public new OdbcParameter this [string parameterName] {
91                         get {
92                                 foreach (OdbcParameter p in list)
93                                         if (p.ParameterName.Equals (parameterName))
94                                                 return p;
95                                 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
96                         }
97                         set {
98                                 if (!Contains (parameterName))
99                                         throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
100                                 this [IndexOf (parameterName)] = value;
101                         }
102                 }
103
104 #if ONLY_1_1
105                 bool IList.IsFixedSize {
106 #else
107                 public override bool IsFixedSize {
108 #endif
109                         get { return false; }
110                 }
111
112 #if ONLY_1_1
113                 bool IList.IsReadOnly {
114 #else
115                 public override bool IsReadOnly {
116 #endif
117                         get { return false; }
118                 }
119
120 #if ONLY_1_1
121                 bool ICollection.IsSynchronized {
122 #else
123                 public override bool IsSynchronized {
124 #endif
125                         get { return list.IsSynchronized; }
126                 }
127
128 #if ONLY_1_1
129                 object ICollection.SyncRoot {
130 #else
131                 public override object SyncRoot {
132 #endif
133                         get { return list.SyncRoot; }
134                 }
135                 
136 #if ONLY_1_1
137                 object IList.this [int index] {
138                         get { return list [index]; }
139                         set { list [index] = value; }
140                 }
141
142                 object IDataParameterCollection.this [string index]
143                 {
144                         get { return this [index]; }
145                         set {
146                                 if (!(value is OdbcParameter))
147                                         throw new InvalidCastException ("Only OdbcParameter objects can be used.");
148                                 this [index] = (OdbcParameter) value;
149                         }
150                 }
151 #endif // ONLY_1_1
152
153                 #endregion // Properties
154
155                 #region Methods
156
157 #if NET_2_0
158                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
159 #endif
160                 public
161 #if NET_2_0
162                 override
163 #endif
164                 int Add (object value)
165                 {
166                         if (!(value is OdbcParameter))
167                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
168                         Add ((OdbcParameter) value);
169                         return IndexOf (value);
170                 }
171
172                 public OdbcParameter Add (OdbcParameter value)
173                 {
174                         if (value.Container != null)
175                                 throw new ArgumentException ("The OdbcParameter specified in " +
176                                                              "the value parameter is already " +
177                                                              "added to this or another OdbcParameterCollection.");
178                         if (value.ParameterName == null || value.ParameterName.Length == 0) {
179                                 value.ParameterName = "Parameter" + nullParamCount;
180                                 nullParamCount++;
181                         }
182                         value.Container = this;
183                         list.Add (value);
184                         return value;
185                 }
186
187 #if NET_2_0
188                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
189                 [Obsolete ("Add(String parameterName, Object value) has been deprecated.  Use AddWithValue(String parameterName, Object value).")]
190 #endif
191                 public OdbcParameter Add (string parameterName, object value)
192                 {
193                         return Add (new OdbcParameter (parameterName, value));
194                 }
195
196                 public OdbcParameter Add (string parameterName, OdbcType odbcType)
197                 {
198                         return Add (new OdbcParameter (parameterName, odbcType));
199                 }
200
201                 public OdbcParameter Add (string parameterName, OdbcType odbcType, int size)
202                 {
203                         return Add (new OdbcParameter (parameterName, odbcType, size));
204                 }
205
206                 public OdbcParameter Add (string parameterName, OdbcType odbcType,
207                                            int size, string sourceColumn)
208                 {
209                         return Add (new OdbcParameter (parameterName, odbcType,
210                                 size, sourceColumn));
211                 }
212
213                 public
214 #if NET_2_0
215                 override
216 #endif
217                 void Clear()
218                 {
219                         foreach (OdbcParameter p in list)
220                                 p.Container = null;
221                         list.Clear ();
222                 }
223
224                 public
225 #if NET_2_0
226                 override
227 #endif // NET_2_0
228                 bool Contains (object value)
229                 {
230                         if (value == null)
231                                 //should not throw ArgumentNullException
232                                 return false;
233                         if (!(value is OdbcParameter))
234                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
235                         return Contains (((OdbcParameter) value).ParameterName);
236                 }
237
238                 public
239 #if NET_2_0
240                 override
241 #endif // NET_2_0
242                 bool Contains (string value)
243                 {
244                         if (value == null || value.Length == 0)
245                                 //should not throw ArgumentNullException
246                                 return false;
247                         string value_upper = value.ToUpper ();
248                         foreach (OdbcParameter p in this)
249                                 if (p.ParameterName.ToUpper ().Equals (value_upper))
250                                         return true;
251                         return false;
252                 }
253
254                 public
255 #if NET_2_0
256                 override
257 #endif // NET_2_0
258                 void CopyTo (Array array, int index)
259                 {
260                         list.CopyTo (array, index);
261                 }
262
263                 public
264 #if NET_2_0
265                 override
266 #endif // NET_2_0
267                 IEnumerator GetEnumerator()
268                 {
269                         return list.GetEnumerator ();
270                 }
271
272                 public
273 #if NET_2_0
274                 override
275 #endif // NET_2_0
276                 int IndexOf (object value)
277                 {
278                         if (value == null)
279                                 return -1;
280                         if (!(value is OdbcParameter))
281                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
282                         return list.IndexOf (value);
283                 }
284
285                 public
286 #if NET_2_0
287                 override
288 #endif // NET_2_0
289                 int IndexOf (string parameterName)
290                 {
291                         if (parameterName == null || parameterName.Length == 0)
292                                 return -1;
293                         string parameterName_upper = parameterName.ToUpper ();
294                         for (int i = 0; i < Count; i += 1)
295                                 if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
296                                         return i;
297                         return -1;
298                 }
299
300                 public
301 #if NET_2_0
302                 override
303 #endif // NET_2_0
304                 void Insert (int index, object value)
305                 {
306                         if (value == null)
307                                 throw new ArgumentNullException ("value");
308                         if (!(value is OdbcParameter))
309                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
310                         Insert (index, (OdbcParameter) value);
311                 }
312
313                 public
314 #if NET_2_0
315                 override
316 #endif // NET_2_0
317                 void Remove (object value)
318                 {
319                         if (value == null)
320                                 throw new ArgumentNullException ("value");
321                         if (!(value is OdbcParameter))
322                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
323                         Remove ((OdbcParameter) value);
324                 }
325
326                 public
327 #if NET_2_0
328                 override
329 #endif // NET_2_0
330                 void RemoveAt (int index)
331                 {
332                         if (index >= list.Count || index < 0)
333                                 throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
334                         this [index].Container = null;
335                         list.RemoveAt (index);
336                 }
337
338                 public
339 #if NET_2_0
340                 override
341 #endif // NET_2_0
342                 void RemoveAt (string parameterName)
343                 {
344                         RemoveAt (IndexOf (parameterName));
345                 }
346
347 #if NET_2_0
348                 protected override DbParameter GetParameter (string name)
349                 {
350                         return this [name];
351                 }
352
353                 protected override DbParameter GetParameter (int index)
354                 {
355                         return this [index];
356                 }
357
358                 protected override void SetParameter (string name, DbParameter value)
359                 {
360                         this [name] = (OdbcParameter) value;
361                 }
362
363                 protected override void SetParameter (int index, DbParameter value)
364                 {
365                         this [index] = (OdbcParameter) value;
366                 }
367
368
369                 public override void AddRange (Array values)
370                 {
371                         if (values == null)
372                                 throw new ArgumentNullException ("values");
373                         foreach (OdbcParameter p in values)
374                                 if (p == null)
375                                         throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
376                         // no need to check if parameter is already contained
377                         foreach (OdbcParameter p in values)
378                                 Add (p);
379                 }
380
381                 public void AddRange (OdbcParameter [] values)
382                 {
383                         AddRange ((Array) values);
384                 }
385
386                 public void Insert (int index, OdbcParameter value)
387                 {
388                         if (index > list.Count || index < 0)
389                                 throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
390                         if (value == null)
391                                 throw new ArgumentNullException ("value");
392                         if (value.Container != null)
393                                 throw new ArgumentException ("The OdbcParameter is already contained by another collection");
394                         if (String.IsNullOrEmpty (value.ParameterName)) {
395                                 value.ParameterName = "Parameter" + nullParamCount;
396                                 nullParamCount++;
397                         }
398                         value.Container = this;
399                         list.Insert (index, value);
400                 }
401
402                 public OdbcParameter AddWithValue (string parameterName, Object value)
403                 {
404                         if (value == null)
405                                 return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
406                         return Add (new OdbcParameter (parameterName, value));
407                 }
408
409                 public void Remove (OdbcParameter value)
410                 {
411                         if (value == null)
412                                 throw new ArgumentNullException ("value");
413                         if (value.Container != this)
414                                 throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
415                         value.Container = null;
416                         list.Remove (value);
417                 }
418
419                 public bool Contains (OdbcParameter value)
420                 {
421                         if (value == null)
422                                 //should not throw ArgumentNullException
423                                 return false;
424                         if (value.Container != this)
425                                 return false;
426                         return Contains (value.ParameterName);
427                 }
428
429                 public int IndexOf (OdbcParameter value)
430                 {
431                         if (value == null)
432                                 //should not throw ArgumentNullException
433                                 return -1;
434                         return IndexOf ((Object) value);
435                 }
436
437                 public void CopyTo (OdbcParameter [] array, int index)
438                 {
439                         list.CopyTo (array, index);
440                 }
441 #endif
442
443                 #endregion // Methods
444         }
445 }