Duplex client has its own listener loop, so special care on reply is needed.
[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 System;
39 using System.Collections;
40 using System.ComponentModel;
41 using System.Data;
42 using System.Data.Common;
43
44 using Mono.Data.Tds;
45
46 namespace System.Data.SqlClient
47 {
48         [ListBindable (false)]
49         [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
50 #if NET_2_0
51         public sealed class SqlParameterCollection : DbParameterCollection, IDataParameterCollection, IList, ICollection, IEnumerable
52 #else
53         public sealed class SqlParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
54 #endif // NET_2_0
55         {
56                 #region Fields
57
58                 ArrayList list = new ArrayList();
59                 TdsMetaParameterCollection metaParameters;
60                 SqlCommand command;
61
62                 #endregion // Fields
63
64                 #region Constructors
65
66                 internal SqlParameterCollection (SqlCommand command)
67                 {
68                         this.command = command;
69                         metaParameters = new TdsMetaParameterCollection ();
70                 }
71
72                 #endregion // Constructors
73
74                 #region Properties
75
76 #if ONLY_1_1 || ONLY_1_0
77                 [Browsable (false)]
78                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
79 #endif
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 #else
113                 object IList.this [int index] {
114                         get { return (SqlParameter) this [index]; }
115                         set { this [index] = (SqlParameter) value; }
116                 }
117
118                 bool IList.IsFixedSize {
119                         get { return list.IsFixedSize; }
120                 }
121
122                 bool IList.IsReadOnly {
123                         get { return list.IsReadOnly; }
124                 }
125
126                 bool ICollection.IsSynchronized {
127                         get { return list.IsSynchronized; }
128                 }
129
130                 object ICollection.SyncRoot {
131                         get { return list.SyncRoot; }
132                 }
133
134                 object IDataParameterCollection.this [string index] {
135                         get { return this [index]; }
136                         set {
137                                 if (!(value is SqlParameter))
138                                         throw new InvalidCastException ("Only SQLParameter objects can be used.");
139                                 this [index] = (SqlParameter) value;
140                         }
141                 }
142 #endif
143
144                 [Browsable (false)]
145                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]      
146                 public
147 #if NET_2_0
148                 new
149 #endif // NET_2_0
150                 SqlParameter this [int index] {
151                         get {
152                                 if (index < 0 || index >= list.Count)
153                                         throw new IndexOutOfRangeException ("The specified index is out of range.");
154                                 return (SqlParameter) list [index];
155                         }
156                         set {
157                                 if (index < 0 || index >= list.Count)
158                                         throw new IndexOutOfRangeException ("The specified index is out of range.");
159                                 list [index] = (SqlParameter) value;
160                         }
161                 }
162
163                 [Browsable (false)]
164                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]      
165                 public
166 #if NET_2_0
167                 new
168 #endif // NET_2_0
169                 SqlParameter this [string parameterName] {
170                         get {
171                                 foreach (SqlParameter p in list)
172                                         if (p.ParameterName.Equals (parameterName))
173                                                 return p;
174                                 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
175                         }
176                         set {
177                                 if (!Contains (parameterName))
178                                         throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
179                                 this [IndexOf (parameterName)] = value;
180                         }
181                 }
182
183 #if NET_2_0
184                 protected  override DbParameter GetParameter (int index)
185                 {
186                         return this [index];
187                 }
188
189                 protected override DbParameter GetParameter (string parameterName)
190                 {
191                         return this [parameterName];
192                 }
193         
194                 protected  override void SetParameter (int index, DbParameter value)
195                 {
196                         this [index] = (SqlParameter) value;
197                 }
198
199                 protected override void SetParameter (string parameterName, DbParameter value)
200                 {
201                         this [parameterName] = (SqlParameter) value;
202                 }
203 #endif
204
205                 internal TdsMetaParameterCollection MetaParameters {
206                         get { return metaParameters; }
207                 }
208
209                 #endregion // Properties
210
211                 #region Methods
212
213 #if NET_2_0
214                 [EditorBrowsable (EditorBrowsableState.Never)]
215 #endif // NET_2_0
216                 public
217 #if NET_2_0
218                 override
219 #endif // NET_2_0
220                 int Add (object value)
221                 {
222                         if (!(value is SqlParameter))
223                                 throw new InvalidCastException ("The parameter was not an SqlParameter.");
224                         Add ((SqlParameter) value);
225                         return IndexOf (value);
226                 }
227                 
228                 public SqlParameter Add (SqlParameter value)
229                 {
230                         if (value.Container != null)
231                                 throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
232                         
233                         value.Container = this;
234                         list.Add (value);
235                         metaParameters.Add (value.MetaParameter);
236                         return value;
237                 }
238                 
239 #if NET_2_0
240                 [EditorBrowsable (EditorBrowsableState.Never)]
241                 [Obsolete ("Do not call this method.")]
242 #endif // NET_2_0
243                 public SqlParameter Add (string parameterName, object value)
244                 {
245                         return Add (new SqlParameter (parameterName, value));
246                 }
247
248 #if NET_2_0
249                 public SqlParameter AddWithValue (string parameterName, object value)
250                 {
251                         return Add (new SqlParameter (parameterName, value));
252                 }
253 #endif // NET_2_0
254
255                 public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
256                 {
257                         return Add (new SqlParameter (parameterName, sqlDbType));
258                 }
259
260                 public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
261                 {
262                         return Add (new SqlParameter (parameterName, sqlDbType, size));
263                 }
264
265                 public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
266                 {
267                         return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
268                 }
269
270                 public
271 #if NET_2_0
272                 override
273 #endif // NET_2_0
274                 void Clear()
275                 {
276                         metaParameters.Clear ();
277
278                         foreach (SqlParameter p in list)
279                                 p.Container = null;
280
281                         list.Clear ();
282                 }
283                 
284                 public
285 #if NET_2_0
286                 override
287 #endif // NET_2_0
288                 bool Contains (object value)
289                 {
290                         if (!(value is SqlParameter))
291                                 throw new InvalidCastException ("The parameter was not an SqlParameter.");
292                         return Contains (((SqlParameter) value).ParameterName);
293                 }
294
295                 public
296 #if NET_2_0
297                 override
298 #endif // NET_2_0
299                 bool Contains (string value)
300                 {
301                         foreach (SqlParameter p in list)
302                                 if (p.ParameterName.Equals (value))
303                                         return true;
304                         return false;
305                 }
306
307 #if NET_2_0
308                 public bool Contains (SqlParameter value)
309                 {
310                         return (this.IndexOf(value) != -1);
311                 }
312 #endif // NET_2_0
313
314                 public
315 #if NET_2_0
316                 override
317 #endif // NET_2_0
318                 void CopyTo (Array array, int index)
319                 {
320                         list.CopyTo (array, index);
321                 }
322
323                 public
324 #if NET_2_0
325                 override
326 #endif // NET_2_0
327                 IEnumerator GetEnumerator()
328                 {
329                         return list.GetEnumerator ();
330                 }
331                 
332                 public
333 #if NET_2_0
334                 override
335 #endif // NET_2_0
336                 int IndexOf (object value)
337                 {
338                         if (!(value is SqlParameter))
339                                 throw new InvalidCastException ("The parameter was not an SqlParameter.");
340                         return IndexOf (((SqlParameter) value).ParameterName);
341                 }
342                 
343                 public
344 #if NET_2_0
345                 override
346 #endif // NET_2_0
347                 int IndexOf (string parameterName)
348                 {
349                         for (int i = 0; i < Count; i += 1)
350                                 if (this [i].ParameterName.Equals (parameterName))
351                                         return i;
352                         return -1;
353                 }
354
355 #if NET_2_0
356                 public int IndexOf (SqlParameter value)
357                 {
358                         return list.IndexOf(value);
359                 }
360 #endif // NET_2_0
361
362                 public
363 #if NET_2_0
364                 override
365 #endif // NET_2_0
366                 void Insert (int index, object value)
367                 {
368                         list.Insert (index, value);
369                 }
370
371 #if NET_2_0
372                 public void Insert (int index, SqlParameter value)
373                 {
374                         list.Insert (index,value);
375                 }
376 #endif //NET_2_0
377
378                 public
379 #if NET_2_0
380                 override
381 #endif // NET_2_0
382                 void Remove (object value)
383                 {
384                         //TODO : this neds validation to check if the object is a 
385                         // sqlparameter.
386
387                         ((SqlParameter) value).Container = null;
388                         
389                         metaParameters.Remove (((SqlParameter) value).MetaParameter);
390                         list.Remove (value);
391                 }
392
393 #if NET_2_0
394                 public void Remove (SqlParameter value)
395                 {
396                         //both this and the above code are the same. but need to work with
397                         // 1.1!
398                         value.Container = null;
399                         metaParameters.Remove (value.MetaParameter);
400                         list.Remove (value);
401                 }
402 #endif //NET_2_0
403
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                                 param.Container = this;
436                                 list.Add (param);
437                                 metaParameters.Add (param.MetaParameter);
438                         }
439                 }
440
441                 public void AddRange (SqlParameter[] values)
442                 {
443                         AddRange((Array) values);
444                 }
445                 
446                 public void CopyTo (SqlParameter[] array, int index)
447                 {
448                         list.CopyTo (array, index);
449                 }
450 #endif
451
452                 #endregion // Methods
453         }
454 }