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