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