cosmetic diff
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Firebird / FbParameterCollection.cs
1 /*
2  *      Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *         The contents of this file are subject to the Initial 
5  *         Developer's Public License Version 1.0 (the "License"); 
6  *         you may not use this file except in compliance with the 
7  *         License. You may obtain a copy of the License at 
8  *         http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *         Software distributed under the License is distributed on 
11  *         an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *         express or implied. See the License for the specific 
13  *         language governing rights and limitations under the License.
14  * 
15  *      Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16  *      All Rights Reserved.
17  */
18
19 using System;
20 using System.Data;
21 using System.ComponentModel;
22 using System.Collections;
23 using System.Globalization;
24
25 using FirebirdSql.Data.Common;
26
27 namespace FirebirdSql.Data.Firebird
28 {
29         /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/overview/*'/>
30 #if     (NET)
31         [ListBindable(false)]
32         [Editor(typeof(Design.FbParameterCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
33 #endif
34         public sealed class FbParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
35         {
36                 #region Fields
37
38                 private ArrayList parameters;
39
40                 #endregion
41
42                 #region Indexers
43
44                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/indexer[@name="Item(System.String)"]/*'/>
45 #if     (!NETCF)
46                 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
47 #endif
48                 public FbParameter this[string parameterName]
49                 {
50                         get { return (FbParameter)this[this.IndexOf(parameterName)]; }
51                         set { this[this.IndexOf(parameterName)] = (FbParameter)value; }
52                 }
53
54                 object IDataParameterCollection.this[string parameterName]
55                 {
56                         get { return this[parameterName]; }
57                         set { this[parameterName] = (FbParameter)value; }
58                 }
59
60                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/indexer[@name="Item(System.Int32)"]/*'/>
61 #if     (!NETCF)
62                 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
63 #endif
64                 public FbParameter this[int index]
65                 {
66                         get { return (FbParameter)this.parameters[index]; }
67                         set { this.parameters[index] = (FbParameter)value; }
68                 }
69
70                 object IList.this[int index]
71                 {
72                         get { return (FbParameter)this[index]; }
73                         set { this[index] = (FbParameter)value; }
74                 }
75
76                 #endregion
77
78                 #region Constructors
79
80                 internal FbParameterCollection()
81                 {
82                         this.parameters = ArrayList.Synchronized(new ArrayList());
83                 }
84
85                 #endregion
86
87                 #region IList Properties
88
89                 bool IList.IsFixedSize
90                 {
91                         get { return this.parameters.IsFixedSize; }
92                 }
93
94                 bool IList.IsReadOnly
95                 {
96                         get { return this.parameters.IsReadOnly; }
97                 }
98
99                 #endregion
100
101                 #region ICollection     Properties
102
103                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/property[@name="Count"]/*'/>
104 #if     (!NETCF)
105                 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
106 #endif
107                 public int Count
108                 {
109                         get { return this.parameters.Count; }
110                 }
111
112                 bool ICollection.IsSynchronized
113                 {
114                         get { return this.parameters.IsSynchronized; }
115                 }
116
117                 object ICollection.SyncRoot
118                 {
119                         get { return this.parameters.SyncRoot; }
120                 }
121
122                 #endregion
123
124                 #region ICollection     Methods
125
126                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="CopyTo(System.Array,System.Int32)"]/*'/>
127                 public void CopyTo(Array array, int index)
128                 {
129                         this.parameters.CopyTo(array, index);
130                 }
131
132                 #endregion
133
134                 #region IList Methods
135
136                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Clear"]/*'/>
137                 public void Clear()
138                 {
139                         this.parameters.Clear();
140                 }
141
142                 #endregion
143
144                 #region IEnumerable     Methods
145
146                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="GetEnumerator"]/*'/>
147                 public IEnumerator GetEnumerator()
148                 {
149                         return this.parameters.GetEnumerator();
150                 }
151
152                 #endregion
153
154                 #region Methods
155
156                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Contains(System.Object)"]/*'/>
157                 public bool Contains(object value)
158                 {
159                         return this.parameters.Contains(value);
160                 }
161
162                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Contains(System.String)"]/*'/>
163                 public bool Contains(string parameterName)
164                 {
165                         return (-1 != this.IndexOf(parameterName));
166                 }
167
168                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="IndexOf(System.Object)"]/*'/>
169                 public int IndexOf(object value)
170                 {
171                         return this.parameters.IndexOf(value);
172                 }
173
174                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="IndexOf(System.String)"]/*'/>
175                 public int IndexOf(string parameterName)
176                 {
177                         int index = 0;
178                         foreach (FbParameter item in this.parameters)
179                         {
180                                 if (GlobalizationHelper.CultureAwareCompare(item.ParameterName, parameterName))
181                                 {
182                                         return index;
183                                 }
184                                 index++;
185                         }
186                         return -1;
187                 }
188
189                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Insert(System.Int32,System.Object)"]/*'/>
190                 public void Insert(int index, object value)
191                 {
192                         this.parameters.Insert(index, value);
193                 }
194
195                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Remove(System.Object)"]/*'/>
196                 public void Remove(object value)
197                 {
198                         if (!(value is FbParameter))
199                         {
200                                 throw new InvalidCastException("The parameter passed was not a FbParameter.");
201                         }
202                         if (!this.Contains(value))
203                         {
204                                 throw new SystemException("The parameter does not exist in the collection.");
205                         }
206
207                         this.parameters.Remove(value);
208
209                         ((FbParameter)value).Parent = null;
210                 }
211
212                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="RemoveAt(System.Int32)"]/*'/>
213                 public void RemoveAt(int index)
214                 {
215                         if (index < 0 || index > this.Count)
216                         {
217                                 throw new IndexOutOfRangeException("The specified index does not exist.");
218                         }
219
220                         FbParameter parameter = this[index];
221                         this.parameters.RemoveAt(index);
222                         parameter.Parent = null;
223                 }
224
225                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="RemoveAt(System.String)"]/*'/>
226                 public void RemoveAt(string parameterName)
227                 {
228                         this.RemoveAt(this.IndexOf(parameterName));
229                 }
230
231                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Add(System.String,System.Object)"]/*'/>
232                 public FbParameter Add(string parameterName, object value)
233                 {
234                         FbParameter param = new FbParameter(parameterName, value);
235
236                         return this.Add(param);
237                 }
238
239                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Add(System.String,FbDbType)"]/*'/>
240                 public FbParameter Add(string parameterName, FbDbType type)
241                 {
242                         FbParameter param = new FbParameter(parameterName, type);
243
244                         return this.Add(param);
245                 }
246
247                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Add(System.String,FbDbType,System.Int32)"]/*'/>
248                 public FbParameter Add(string parameterName, FbDbType fbType, int size)
249                 {
250                         FbParameter param = new FbParameter(parameterName, fbType, size);
251
252                         return this.Add(param);
253                 }
254
255                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Add(System.String,FbDbType,System.Int32,System.String)"]/*'/>
256                 public FbParameter Add(
257                         string parameterName, FbDbType fbType, int size, string sourceColumn)
258                 {
259                         FbParameter param = new FbParameter(parameterName, fbType, size, sourceColumn);
260
261                         return this.Add(param);
262                 }
263
264                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Add(System.Object)"]/*'/>
265                 public int Add(object value)
266                 {
267                         if (!(value is FbParameter))
268                         {
269                                 throw new InvalidCastException("The parameter passed was not a FbParameter.");
270                         }
271
272                         return this.IndexOf(this.Add(value as FbParameter));
273                 }
274
275                 /// <include file='Doc/en_EN/FbParameterCollection.xml' path='doc/class[@name="FbParameterCollection"]/method[@name="Add(FbParameter)"]/*'/>
276                 public FbParameter Add(FbParameter value)
277                 {
278                         lock (this.parameters.SyncRoot)
279                         {
280                                 if (value == null)
281                                 {
282                                         throw new ArgumentException("The value parameter is null.");
283                                 }
284                                 if (value.Parent != null)
285                                 {
286                                         throw new ArgumentException("The FbParameter specified in the value parameter is already added to this or another FbParameterCollection.");
287                                 }
288                                 if (value.ParameterName == null ||
289                                         value.ParameterName.Length == 0)
290                                 {
291                                         value.ParameterName = this.GenerateParameterName();
292                                 }
293                                 else
294                                 {
295                                         if (this.IndexOf(value) != -1)
296                                         {
297                                                 throw new ArgumentException("FbParameterCollection already contains FbParameter with ParameterName '" + value.ParameterName + "'.");
298                                         }
299                                 }
300
301                                 this.parameters.Add(value);
302
303                                 return value;
304                         }
305                 }
306
307                 #endregion
308
309                 #region Private Methods
310
311                 private string GenerateParameterName()
312                 {
313                         int index = this.Count + 1;
314                         string name = String.Empty;
315
316                         while (index > 0)
317                         {
318                                 name = "Parameter" + index.ToString(CultureInfo.InvariantCulture);
319
320                                 if (this.IndexOf(name) == -1)
321                                 {
322                                         index = -1;
323                                 }
324                                 else
325                                 {
326                                         index++;
327                                 }
328                         }
329
330                         return name;
331                 }
332
333                 #endregion
334         }
335 }