Missing 2.0 APIs
[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.Data;
38 using System.ComponentModel;
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                 ArrayList list = new ArrayList ();
55                 int nullParamCount = 1;
56
57                 #endregion // Fields
58         
59                 #region Constructors
60
61                 internal OdbcParameterCollection () {
62                 }
63
64                 #endregion // Constructors
65         
66                 #region Properties
67
68 #if ONLY_1_1
69                 [Browsable (false)]
70                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
71 #endif
72                 public 
73 #if NET_2_0
74                 override
75 #endif
76                 int Count {
77                         get { return list.Count; }
78                 }
79
80                 [Browsable (false)]
81                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
82                 public new OdbcParameter this [int index] {
83                         get { return (OdbcParameter) list[index]; }
84                         set { list[index] = value; }
85                 }
86
87                 [Browsable (false)]
88                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89                 public new OdbcParameter this [string parameterName] {
90                         get {
91                                 foreach (OdbcParameter p in list)
92                                         if (p.ParameterName.Equals (parameterName))
93                                                 return p;
94                                 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
95                         }
96                         set {
97                                 if (!Contains (parameterName))
98                                         throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
99                                 this [IndexOf (parameterName)] = value;
100                         }
101                 }
102
103 #if ONLY_1_1
104                 bool IList.IsFixedSize {
105 #else
106                 public override bool IsFixedSize {
107 #endif
108                         get { return false; }
109                 }
110                 
111 #if ONLY_1_1
112                 bool IList.IsReadOnly {
113 #else
114                 public override bool IsReadOnly {
115 #endif
116                         get { return false; }
117                 }
118                 
119 #if ONLY_1_1    
120                 bool ICollection.IsSynchronized {
121 #else
122                 public override bool IsSynchronized {
123 #endif
124                         get { return list.IsSynchronized; }
125                 }
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 name]
143                 {
144                         get { return this [name]; }
145             set {
146                                 if (!(value is OdbcParameter))
147                                         throw new InvalidCastException ("Only OdbcParameter objects can be used.");
148                                 this [name] = (OdbcParameter) value;
149                         }
150
151                 }
152 #endif // ONLY_1_1
153
154                 #endregion // Properties
155
156                 #region Methods
157
158 #if NET_2_0
159                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
160 #endif
161                 public 
162 #if NET_2_0
163                 override
164 #endif
165                 int Add (object value)
166                 {
167                         if (!(value is OdbcParameter))
168                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
169                         Add ((OdbcParameter) value);
170                         return IndexOf (value);
171                 }
172
173                 public OdbcParameter Add (OdbcParameter parameter)
174                 {
175                         if (parameter.Container != null)
176                                 throw new ArgumentException ("The OdbcParameter specified in " +
177                                                              "the value parameter is already " +
178                                                              "added to this or another OdbcParameterCollection.");
179                         if (parameter.ParameterName == null || parameter.ParameterName == "") {
180                                 parameter.ParameterName = "Parameter" + nullParamCount;
181                                 nullParamCount ++;
182                         }
183                         parameter.Container = this;
184                         list.Add (parameter);
185                         return parameter;
186                 }
187
188 #if NET_2_0
189                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
190                 [Obsolete ("Add(String parameterName, Object value) has been deprecated.  Use AddWithValue(String parameterName, Object value).")]
191 #endif
192                 public OdbcParameter Add (string name, object value)
193                 {
194                         return Add (new OdbcParameter (name, value));
195                 }
196
197                 public OdbcParameter Add (string name, OdbcType type)
198                 {
199                         return Add (new OdbcParameter (name, type));
200                 }
201
202                 public OdbcParameter Add (string name, OdbcType type, int width)
203                 {
204                         return Add (new OdbcParameter (name, type, width));
205                 }
206
207                 public OdbcParameter Add (string name, OdbcType type,
208                                            int width, string src_col)
209                 {
210                         return Add (new OdbcParameter (name, type, width, src_col));
211                 }
212
213                 internal void Bind (IntPtr hstmt)
214                 {
215                         for (int i = 0; i < Count; i++)
216                                 this [i].Bind (hstmt, i + 1);
217                 }
218
219                 public 
220 #if NET_2_0
221                 override
222 #endif
223                 void Clear()
224                 {
225                         foreach (OdbcParameter p in list)
226                                 p.Container = null;
227                                                                                                     
228                         list.Clear ();
229                 }
230
231                 public
232 #if NET_2_0
233                 override
234 #endif // NET_2_0
235                 bool Contains (object value)
236                 {
237                         if (value == null)
238                                 //should not throw ArgumentNullException
239                                 return false;
240                         if (!(value is OdbcParameter))
241                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
242                         return Contains (((OdbcParameter) value).ParameterName);
243                 }
244                                                                             
245                 public
246 #if NET_2_0
247                 override
248 #endif // NET_2_0
249                 bool Contains (string value)
250                 {
251                         if (value == null || value == "")
252                                 //should not throw ArgumentNullException
253                                 return false;
254                         string value_upper = value.ToUpper ();
255                         foreach (OdbcParameter p in this)
256                                 if (p.ParameterName.ToUpper ().Equals (value_upper))
257                                         return true;
258                         return false;
259                 }
260
261                 public
262 #if NET_2_0
263                 override
264 #endif // NET_2_0
265                 void CopyTo (Array array, int index)
266                 {
267                         list.CopyTo (array, index);
268                 }
269
270                 public
271 #if NET_2_0
272                 override
273 #endif // NET_2_0
274                 IEnumerator GetEnumerator()
275                 {
276                         return list.GetEnumerator ();
277                 }
278
279                 public
280 #if NET_2_0
281                 override
282 #endif // NET_2_0
283                 int IndexOf (object value)
284                 {
285                         if (value == null)
286                                 return -1;
287                         if (!(value is OdbcParameter))
288                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
289                         return list.IndexOf (value);
290                 }
291                                                                                                     
292                 public
293 #if NET_2_0
294                 override
295 #endif // NET_2_0
296                 int IndexOf (string parameterName)
297                 {
298                         if (parameterName == null || parameterName == "")
299                                 return -1;
300                         string parameterName_upper = parameterName.ToUpper ();
301                         for (int i = 0; i < Count; i += 1)
302                                 if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
303                                         return i;
304                         return -1;             
305                 }
306
307                 public
308 #if NET_2_0
309                 override
310 #endif // NET_2_0
311                 void Insert (int index, object value)
312                 {
313                         if (value == null)
314                                 throw new ArgumentNullException ("value");
315                         if (!(value is OdbcParameter))
316                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
317                         Insert (index, (OdbcParameter) value);
318                 }
319                                                                                                     
320                 public
321 #if NET_2_0
322                 override
323 #endif // NET_2_0
324                 void Remove (object value)
325                 {
326                         if (value == null)
327                                 throw new ArgumentNullException ("value");
328                         if (!(value is OdbcParameter))
329                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
330                         Remove ((OdbcParameter) value);
331                 }
332                                                                                                     
333                 public
334 #if NET_2_0
335                 override
336 #endif // NET_2_0
337                 void RemoveAt (int index)
338                 {
339                         if (index >= list.Count || index < 0)
340                                 throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
341                         this [index].Container = null;
342                         list.RemoveAt (index);
343                 }
344                                                                                                     
345                 public
346 #if NET_2_0
347                 override
348 #endif // NET_2_0
349                 void RemoveAt (string parameterName)
350                 {
351                         RemoveAt (IndexOf (parameterName));
352                 }
353
354
355 #if NET_2_0
356                 protected override DbParameter GetParameter (string name)
357                 {
358                         return this [name];
359                 }
360
361                 protected override DbParameter GetParameter (int index)
362                 {
363                         return this [index];
364                 }
365
366                 protected override void SetParameter (string name, DbParameter value)
367                 {
368                         this [name] = (OdbcParameter) value;
369                 }
370
371                 protected override void SetParameter (int index, DbParameter value)
372                 {
373                         this [index] = (OdbcParameter) value;
374                 }
375
376
377                 public override void AddRange (Array values)
378                 {
379                         if (values == null)
380                                 throw new ArgumentNullException ("values");
381                         foreach (OdbcParameter p in values) {
382                                 if (p == null)
383                                         throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
384                         }       
385                         // no need to check if parameter is already contained
386                         foreach (OdbcParameter p in values)
387                                 Add (p);
388                 }
389
390                 public void AddRange (OdbcParameter [] values)
391                 {
392                         AddRange ((Array)values);
393                 }
394
395                 public void Insert (int index, OdbcParameter value)
396                 {
397                         if (index > list.Count || index < 0)
398                                 throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
399                         if (value == null)
400                                 throw new ArgumentNullException ("value");
401                         if (value.Container != null)
402                                 throw new ArgumentException ("The OdbcParameter is already contained by another collection");
403                         if (String.IsNullOrEmpty (value.ParameterName)) {
404                                 value.ParameterName = "Parameter" + nullParamCount;
405                                 nullParamCount ++;
406                         }
407                         value.Container = this;
408                         list.Insert (index, value);
409                 }
410
411                 public OdbcParameter AddWithValue (string parameterName, Object value)
412                 {
413                         if (value == null)
414                                 return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
415                         return Add (new OdbcParameter (parameterName, value));
416                 }
417
418                 public void Remove (OdbcParameter value)
419                 {
420                         if (value == null)
421                                 throw new ArgumentNullException ("value");
422                         if (value.Container != this)
423                                 throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
424                         value.Container = null;
425                         list.Remove (value);
426                 }
427
428                 public bool Contains (OdbcParameter value)
429                 {
430                         if (value == null)
431                                 //should not throw ArgumentNullException
432                                 return false;
433                         if (value.Container != this)
434                                 return false;
435                         return Contains (value.ParameterName);
436                 }
437
438                 public int IndexOf (OdbcParameter value)
439                 {
440                         if (value == null)
441                                 //should not throw ArgumentNullException
442                                 return -1;
443                         return IndexOf ((Object) value);
444                 }
445
446                 public void CopyTo (OdbcParameter [] array, int index)
447                 {
448                         list.CopyTo (array, index);
449                 }
450  #endif
451                 #endregion // Methods
452
453         }
454 }