* OdbcErrorCollection.cs: Corcompare fixes and code formatting.
[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                 #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 #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                 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                         list.Clear ();
228                 }
229
230                 public
231 #if NET_2_0
232                 override
233 #endif // NET_2_0
234                 bool Contains (object value)
235                 {
236                         if (value == null)
237                                 //should not throw ArgumentNullException
238                                 return false;
239                         if (!(value is OdbcParameter))
240                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
241                         return Contains (((OdbcParameter) value).ParameterName);
242                 }
243
244                 public
245 #if NET_2_0
246                 override
247 #endif // NET_2_0
248                 bool Contains (string value)
249                 {
250                         if (value == null || value.Length == 0)
251                                 //should not throw ArgumentNullException
252                                 return false;
253                         string value_upper = value.ToUpper ();
254                         foreach (OdbcParameter p in this)
255                                 if (p.ParameterName.ToUpper ().Equals (value_upper))
256                                         return true;
257                         return false;
258                 }
259
260                 public
261 #if NET_2_0
262                 override
263 #endif // NET_2_0
264                 void CopyTo (Array array, int index)
265                 {
266                         list.CopyTo (array, index);
267                 }
268
269                 public
270 #if NET_2_0
271                 override
272 #endif // NET_2_0
273                 IEnumerator GetEnumerator()
274                 {
275                         return list.GetEnumerator ();
276                 }
277
278                 public
279 #if NET_2_0
280                 override
281 #endif // NET_2_0
282                 int IndexOf (object value)
283                 {
284                         if (value == null)
285                                 return -1;
286                         if (!(value is OdbcParameter))
287                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
288                         return list.IndexOf (value);
289                 }
290
291                 public
292 #if NET_2_0
293                 override
294 #endif // NET_2_0
295                 int IndexOf (string parameterName)
296                 {
297                         if (parameterName == null || parameterName.Length == 0)
298                                 return -1;
299                         string parameterName_upper = parameterName.ToUpper ();
300                         for (int i = 0; i < Count; i += 1)
301                                 if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
302                                         return i;
303                         return -1;
304                 }
305
306                 public
307 #if NET_2_0
308                 override
309 #endif // NET_2_0
310                 void Insert (int index, object value)
311                 {
312                         if (value == null)
313                                 throw new ArgumentNullException ("value");
314                         if (!(value is OdbcParameter))
315                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
316                         Insert (index, (OdbcParameter) value);
317                 }
318
319                 public
320 #if NET_2_0
321                 override
322 #endif // NET_2_0
323                 void Remove (object value)
324                 {
325                         if (value == null)
326                                 throw new ArgumentNullException ("value");
327                         if (!(value is OdbcParameter))
328                                 throw new InvalidCastException ("The parameter was not an OdbcParameter.");
329                         Remove ((OdbcParameter) value);
330                 }
331
332                 public
333 #if NET_2_0
334                 override
335 #endif // NET_2_0
336                 void RemoveAt (int index)
337                 {
338                         if (index >= list.Count || index < 0)
339                                 throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
340                         this [index].Container = null;
341                         list.RemoveAt (index);
342                 }
343
344                 public
345 #if NET_2_0
346                 override
347 #endif // NET_2_0
348                 void RemoveAt (string parameterName)
349                 {
350                         RemoveAt (IndexOf (parameterName));
351                 }
352
353 #if NET_2_0
354                 protected override DbParameter GetParameter (string name)
355                 {
356                         return this [name];
357                 }
358
359                 protected override DbParameter GetParameter (int index)
360                 {
361                         return this [index];
362                 }
363
364                 protected override void SetParameter (string name, DbParameter value)
365                 {
366                         this [name] = (OdbcParameter) value;
367                 }
368
369                 protected override void SetParameter (int index, DbParameter value)
370                 {
371                         this [index] = (OdbcParameter) value;
372                 }
373
374
375                 public override void AddRange (Array values)
376                 {
377                         if (values == null)
378                                 throw new ArgumentNullException ("values");
379                         foreach (OdbcParameter p in values)
380                                 if (p == null)
381                                         throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
382                         // no need to check if parameter is already contained
383                         foreach (OdbcParameter p in values)
384                                 Add (p);
385                 }
386
387                 public void AddRange (OdbcParameter [] values)
388                 {
389                         AddRange ((Array)values);
390                 }
391
392                 public void Insert (int index, OdbcParameter value)
393                 {
394                         if (index > list.Count || index < 0)
395                                 throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
396                         if (value == null)
397                                 throw new ArgumentNullException ("value");
398                         if (value.Container != null)
399                                 throw new ArgumentException ("The OdbcParameter is already contained by another collection");
400                         if (String.IsNullOrEmpty (value.ParameterName)) {
401                                 value.ParameterName = "Parameter" + nullParamCount;
402                                 nullParamCount ++;
403                         }
404                         value.Container = this;
405                         list.Insert (index, value);
406                 }
407
408                 public OdbcParameter AddWithValue (string parameterName, Object value)
409                 {
410                         if (value == null)
411                                 return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
412                         return Add (new OdbcParameter (parameterName, value));
413                 }
414
415                 public void Remove (OdbcParameter value)
416                 {
417                         if (value == null)
418                                 throw new ArgumentNullException ("value");
419                         if (value.Container != this)
420                                 throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
421                         value.Container = null;
422                         list.Remove (value);
423                 }
424
425                 public bool Contains (OdbcParameter value)
426                 {
427                         if (value == null)
428                                 //should not throw ArgumentNullException
429                                 return false;
430                         if (value.Container != this)
431                                 return false;
432                         return Contains (value.ParameterName);
433                 }
434
435                 public int IndexOf (OdbcParameter value)
436                 {
437                         if (value == null)
438                                 //should not throw ArgumentNullException
439                                 return -1;
440                         return IndexOf ((Object) value);
441                 }
442
443                 public void CopyTo (OdbcParameter [] array, int index)
444                 {
445                         list.CopyTo (array, index);
446                 }
447 #endif
448
449                 #endregion // Methods
450         }
451 }