disable broken tests on net_4_0
[mcs.git] / class / Npgsql / Npgsql / NpgsqlParameterCollection.cs
blob03140387ebf4d08ac43114e0ba16944386510cf6
1 // created on 09/07/2003 at 20:20
2 // Npgsql.NpgsqlParameterCollection.cs
3 //
4 // Author:
5 // Brar Piening (brar@gmx.de)
6 //
7 // Rewritten from the scratch to derive from MarshalByRefObject instead of ArrayList.
8 // Recycled some parts of the original NpgsqlParameterCollection.cs
9 // by Francisco Jr. (fxjrlists@yahoo.com.br)
11 // Copyright (C) 2002 The Npgsql Development Team
12 // npgsql-general@gborg.postgresql.org
13 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
15 // This library is free software; you can redistribute it and/or
16 // modify it under the terms of the GNU Lesser General Public
17 // License as published by the Free Software Foundation; either
18 // version 2.1 of the License, or (at your option) any later version.
20 // This library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 // Lesser General Public License for more details.
25 // You should have received a copy of the GNU Lesser General Public
26 // License along with this library; if not, write to the Free Software
27 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 using System;
30 using System.Reflection;
31 using System.Data;
32 using System.Collections;
33 using System.ComponentModel;
34 using NpgsqlTypes;
36 #if WITHDESIGN
37 using Npgsql.Design;
38 #endif
40 namespace Npgsql
42 /// <summary>
43 /// Represents a collection of parameters relevant to a <see cref="Npgsql.NpgsqlCommand">NpgsqlCommand</see>
44 /// as well as their respective mappings to columns in a <see cref="System.Data.DataSet">DataSet</see>.
45 /// This class cannot be inherited.
46 /// </summary>
48 #if WITHDESIGN
49 [ListBindable(false)]
50 [Editor(typeof(NpgsqlParametersEditor), typeof(System.Drawing.Design.UITypeEditor))]
51 #endif
53 public sealed class NpgsqlParameterCollection : MarshalByRefObject, IDataParameterCollection
55 private ArrayList InternalList = new ArrayList();
57 // Logging related value
58 private static readonly String CLASSNAME = "NpgsqlParameterCollection";
60 // Our resource manager
61 private System.Resources.ResourceManager resman;
63 /// <summary>
64 /// Initializes a new instance of the NpgsqlParameterCollection class.
65 /// </summary>
66 internal NpgsqlParameterCollection()
68 this.resman = new System.Resources.ResourceManager(this.GetType());
69 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
72 #region NpgsqlParameterCollection Member
74 /// <summary>
75 /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified name.
76 /// </summary>
77 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
78 /// <value>The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified name, or a null reference if the parameter is not found.</value>
80 #if WITHDESIGN
81 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
82 #endif
84 public NpgsqlParameter this[string parameterName] {
85 get
87 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
88 return (NpgsqlParameter)this.InternalList[IndexOf(parameterName)];
90 set
92 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
93 this.InternalList[IndexOf(parameterName)] = value;
97 /// <summary>
98 /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> at the specified index.
99 /// </summary>
100 /// <param name="index">The zero-based index of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
101 /// <value>The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> at the specified index.</value>
103 #if WITHDESIGN
104 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
105 #endif
107 public NpgsqlParameter this[int index] {
110 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
111 return (NpgsqlParameter)this.InternalList[index];
115 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
116 this.InternalList[index] = value;
120 /// <summary>
121 /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
122 /// </summary>
123 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
124 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
125 public NpgsqlParameter Add(NpgsqlParameter value)
127 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
129 // Do not allow parameters without name.
131 this.InternalList.Add(value);
133 // Check if there is a name. If not, add a name based in the index of parameter.
134 if (value.ParameterName.Trim() == String.Empty ||
135 (value.ParameterName.Length == 1 && value.ParameterName[0] == ':'))
136 value.ParameterName = ":" + "Parameter" + (IndexOf(value) + 1);
139 return value;
142 /// <summary>
143 /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name and value.
144 /// </summary>
145 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param>
146 /// <param name="value">The Value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
147 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
148 /// <remarks>
149 /// Use caution when using this overload of the
150 /// <b>Add</b> method to specify integer parameter values.
151 /// Because this overload takes a <i>value</i> of type Object,
152 /// you must convert the integral value to an <b>Object</b>
153 /// type when the value is zero, as the following C# example demonstrates.
154 /// <code>parameters.Add(":pname", Convert.ToInt32(0));</code>
155 /// If you do not perform this conversion, the compiler will assume you
156 /// are attempting to call the NpgsqlParameterCollection.Add(string, DbType) overload.
157 /// </remarks>
158 public NpgsqlParameter Add(string parameterName, object value)
160 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, value);
161 return this.Add(new NpgsqlParameter(parameterName, value));
164 /// <summary>
165 /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the parameter name and the data type.
166 /// </summary>
167 /// <param name="parameterName">The name of the parameter.</param>
168 /// <param name="parameterType">One of the DbType values.</param>
169 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
170 public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType)
172 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType);
173 return this.Add(new NpgsqlParameter(parameterName, parameterType));
176 /// <summary>
177 /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> with the parameter name, the data type, and the column length.
178 /// </summary>
179 /// <param name="parameterName">The name of the parameter.</param>
180 /// <param name="parameterType">One of the DbType values.</param>
181 /// <param name="size">The length of the column.</param>
182 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
183 public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size)
185 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size);
186 return this.Add(new NpgsqlParameter(parameterName, parameterType, size));
189 /// <summary>
190 /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> with the parameter name, the data type, the column length, and the source column name.
191 /// </summary>
192 /// <param name="parameterName">The name of the parameter.</param>
193 /// <param name="parameterType">One of the DbType values.</param>
194 /// <param name="size">The length of the column.</param>
195 /// <param name="sourceColumn">The name of the source column.</param>
196 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
197 public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn)
199 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size, sourceColumn);
200 return this.Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn));
203 #endregion
205 #region IDataParameterCollection Member
207 object System.Data.IDataParameterCollection.this[string parameterName] {
210 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
211 return this.InternalList[IndexOf(parameterName)];
215 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
216 CheckType(value);
217 this.InternalList[IndexOf(parameterName)] = value;
221 /// <summary>
222 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using the parameter name.
223 /// </summary>
224 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to retrieve.</param>
225 public void RemoveAt(string parameterName)
227 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", parameterName);
228 this.InternalList.RemoveAt(IndexOf(parameterName));
231 /// <summary>
232 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
233 /// </summary>
234 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
235 /// <returns><b>true</b> if the collection contains the parameter; otherwise, <b>false</b>.</returns>
236 public bool Contains(string parameterName)
238 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", parameterName);
239 return (IndexOf(parameterName) != -1);
242 /// <summary>
243 /// Gets the location of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection with a specific parameter name.
244 /// </summary>
245 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
246 /// <returns>The zero-based location of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection.</returns>
247 public int IndexOf(string parameterName)
249 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", parameterName);
251 // Iterate values to see what is the index of parameter.
252 Int32 index = 0;
253 if ((parameterName[0] == ':') || (parameterName[0] == '@'))
254 parameterName = parameterName.Remove(0, 1);
256 foreach (NpgsqlParameter parameter in this)
258 if (parameter.ParameterName.Remove(0, 1) == parameterName)
259 return index;
260 index++;
262 return -1;
265 #endregion
267 #region IList Member
269 bool IList.IsReadOnly {
272 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsReadOnly");
273 return this.InternalList.IsReadOnly;
277 object System.Collections.IList.this[int index] {
280 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
281 return (NpgsqlParameter)this.InternalList[index];
285 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
286 CheckType(value);
287 this.InternalList[index] = value;
291 /// <summary>
292 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using a specific index.
293 /// </summary>
294 /// <param name="index">The zero-based index of the parameter.</param>
295 public void RemoveAt(int index)
297 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", index);
298 this.InternalList.RemoveAt(index);
301 /// <summary>
302 /// Inserts a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> into the collection at the specified index.
303 /// </summary>
304 /// <param name="index">The zero-based index where the parameter is to be inserted within the collection.</param>
305 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
306 public void Insert(int index, object value)
308 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Insert", index, value);
309 CheckType(value);
310 this.InternalList.Insert(index, value);
313 /// <summary>
314 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection.
315 /// </summary>
316 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to remove from the collection.</param>
317 public void Remove(object value)
319 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Remove", value);
320 CheckType(value);
321 this.InternalList.Remove(value);
324 /// <summary>
325 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> exists in the collection.
326 /// </summary>
327 /// <param name="value">The value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
328 /// <returns>true if the collection contains the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object; otherwise, false.</returns>
329 public bool Contains(object value)
331 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", value);
332 CheckType(value);
333 return this.InternalList.Contains(value);
336 /// <summary>
337 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
338 /// </summary>
339 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
340 /// <param name="parameter">A reference to the requested parameter is returned in this out param if it is found in the list. This value is null if the parameter is not found.</param>
341 /// <returns><b>true</b> if the collection contains the parameter and param will contain the parameter; otherwise, <b>false</b>.</returns>
342 public bool TryGetValue(string parameterName, out NpgsqlParameter parameter)
344 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "TryGetValue", parameterName);
345 int index = IndexOf(parameterName);
346 if (index != -1)
348 parameter = this[index];
349 return true;
351 else
353 parameter = null;
354 return false;
358 /// <summary>
359 /// Removes all items from the collection.
360 /// </summary>
361 public void Clear()
363 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Clear");
364 this.InternalList.Clear();
367 /// <summary>
368 /// Gets the location of a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection.
369 /// </summary>
370 /// <param name="value">The value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
371 /// <returns>The zero-based index of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object in the collection.</returns>
372 public int IndexOf(object value)
374 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", value);
375 CheckType(value);
376 return this.InternalList.IndexOf(value);
379 /// <summary>
380 /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
381 /// </summary>
382 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
383 /// <returns>The zero-based index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
384 public int Add(object value)
386 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
387 CheckType(value);
388 this.Add((NpgsqlParameter)value);
389 return IndexOf(value);
392 bool IList.IsFixedSize {
395 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsFixedSize");
396 return this.InternalList.IsFixedSize;
400 #endregion
402 #region ICollection Member
404 bool ICollection.IsSynchronized {
407 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsSynchronized");
408 return this.InternalList.IsSynchronized;
412 /// <summary>
413 /// Gets the number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.
414 /// </summary>
415 /// <value>The number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</value>
417 #if WITHDESIGN
418 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
419 #endif
421 public int Count {
424 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Count");
425 return this.InternalList.Count;
429 /// <summary>
430 /// Copies <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects from the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> to the specified array.
431 /// </summary>
432 /// <param name="array">An <see cref="System.Array">Array</see> to which to copy the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</param>
433 /// <param name="index">The starting index of the array.</param>
434 public void CopyTo(Array array, int index)
436 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CopyTo", array, index);
437 this.InternalList.CopyTo(array, index);
440 object ICollection.SyncRoot {
443 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "SyncRoot");
444 return this.InternalList.SyncRoot;
448 #endregion
450 #region IEnumerable Member
452 /// <summary>
453 /// Returns an enumerator that can iterate through the collection.
454 /// </summary>
455 /// <returns>An <see cref="System.Collections.IEnumerator">IEnumerator</see> that can be used to iterate through the collection.</returns>
456 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
458 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetEnumerator");
459 return this.InternalList.GetEnumerator();
462 #endregion
464 /// <summary>
465 /// In methods taking an object as argument this method is used to verify
466 /// that the argument has the type <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
467 /// </summary>
468 /// <param name="Object">The object to verify</param>
469 private void CheckType(object Object)
471 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CheckType", Object);
472 if(Object.GetType() != typeof(NpgsqlParameter))
473 throw new InvalidCastException(String.Format(this.resman.GetString("Exception_WrongType"), Object.GetType().ToString()));