1 // created on 09/07/2003 at 20:20
2 // Npgsql.NpgsqlParameterCollection.cs
5 // Brar Piening (brar@gmx.de)
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
30 using System.Reflection;
32 using System.Collections;
33 using System.ComponentModel;
39 /// Represents a collection of parameters relevant to a <see cref="Npgsql.NpgsqlCommand">NpgsqlCommand</see>
40 /// as well as their respective mappings to columns in a <see cref="System.Data.DataSet">DataSet</see>.
41 /// This class cannot be inherited.
44 // [Editor(typeof(NpgsqlParametersEditor), typeof(System.ComponentModel.Design.CollectionEditor))]
45 [Editor(typeof(NpgsqlParametersEditor), typeof(System.Drawing.Design.UITypeEditor))]
46 public sealed class NpgsqlParameterCollection : MarshalByRefObject, IDataParameterCollection
48 private ArrayList InternalList = new ArrayList();
50 // Logging related value
51 private static readonly String CLASSNAME = "NpgsqlParameterCollection";
53 // Our resource manager
54 private System.Resources.ResourceManager resman;
57 /// Initializes a new instance of the NpgsqlParameterCollection class.
59 internal NpgsqlParameterCollection()
61 this.resman = new System.Resources.ResourceManager(this.GetType());
62 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
65 #region NpgsqlParameterCollection Member
68 /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified name.
70 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
71 /// <value>The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified name, or a null reference if the parameter is not found.</value>
72 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
73 public NpgsqlParameter this[string parameterName] {
76 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
77 return (NpgsqlParameter)this.InternalList[IndexOf(parameterName)];
81 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
82 this.InternalList[IndexOf(parameterName)] = value;
87 /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> at the specified index.
89 /// <param name="index">The zero-based index of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
90 /// <value>The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> at the specified index.</value>
91 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
92 public NpgsqlParameter this[int index] {
95 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
96 return (NpgsqlParameter)this.InternalList[index];
100 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
101 this.InternalList[index] = value;
106 /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
108 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
109 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
110 public NpgsqlParameter Add(NpgsqlParameter value)
112 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
114 // Do not allow parameters without name.
115 if ((value.ParameterName == null) ||
116 (value.ParameterName.Trim() == String.Empty) ||
117 (value.ParameterName.Length == 1 && value.ParameterName[0] == ':'))
118 throw new NpgsqlException(String.Format(this.resman.GetString("Exception_InvalidParameterName"), value.ParameterName));
120 this.InternalList.Add(value);
125 /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name and value.
127 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param>
128 /// <param name="value">The Value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
129 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
131 /// Use caution when using this overload of the
132 /// <b>Add</b> method to specify integer parameter values.
133 /// Because this overload takes a <i>value</i> of type Object,
134 /// you must convert the integral value to an <b>Object</b>
135 /// type when the value is zero, as the following C# example demonstrates.
136 /// <code>parameters.Add(":pname", Convert.ToInt32(0));</code>
137 /// If you do not perform this conversion, the compiler will assume you
138 /// are attempting to call the NpgsqlParameterCollection.Add(string, DbType) overload.
140 public NpgsqlParameter Add(string parameterName, object value)
142 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, value);
143 return this.Add(new NpgsqlParameter(parameterName, value));
147 /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the parameter name and the data type.
149 /// <param name="parameterName">The name of the parameter.</param>
150 /// <param name="parameterType">One of the DbType values.</param>
151 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
152 public NpgsqlParameter Add(string parameterName, DbType parameterType)
154 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType);
155 return this.Add(new NpgsqlParameter(parameterName, parameterType));
159 /// 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.
161 /// <param name="parameterName">The name of the parameter.</param>
162 /// <param name="parameterType">One of the DbType values.</param>
163 /// <param name="size">The length of the column.</param>
164 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
165 public NpgsqlParameter Add(string parameterName, DbType parameterType, int size)
167 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size);
168 return this.Add(new NpgsqlParameter(parameterName, parameterType, size));
172 /// 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.
174 /// <param name="parameterName">The name of the parameter.</param>
175 /// <param name="parameterType">One of the DbType values.</param>
176 /// <param name="size">The length of the column.</param>
177 /// <param name="sourceColumn">The name of the source column.</param>
178 /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
179 public NpgsqlParameter Add(string parameterName, DbType parameterType, int size, string sourceColumn)
181 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size, sourceColumn);
182 return this.Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn));
187 #region IDataParameterCollection Member
189 object System.Data.IDataParameterCollection.this[string parameterName] {
192 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
193 return this.InternalList[IndexOf(parameterName)];
197 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
199 this.InternalList[IndexOf(parameterName)] = value;
204 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using the parameter name.
206 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to retrieve.</param>
207 public void RemoveAt(string parameterName)
209 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", parameterName);
210 this.InternalList.RemoveAt(IndexOf(parameterName));
214 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
216 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
217 /// <returns><b>true</b> if the collection contains the parameter; otherwise, <b>false</b>.</returns>
218 public bool Contains(string parameterName)
220 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", parameterName);
221 return (IndexOf(parameterName) != -1);
225 /// Gets the location of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection with a specific parameter name.
227 /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
228 /// <returns>The zero-based location of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection.</returns>
229 public int IndexOf(string parameterName)
231 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", parameterName);
233 // Iterate values to see what is the index of parameter.
235 if (parameterName[0] != ':')
236 parameterName = ':' + parameterName;
238 foreach(NpgsqlParameter parameter in this)
240 if (parameter.ParameterName == parameterName)
251 bool IList.IsReadOnly {
254 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsReadOnly");
255 return this.InternalList.IsReadOnly;
259 object System.Collections.IList.this[int index] {
262 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
263 return (NpgsqlParameter)this.InternalList[index];
267 NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
269 this.InternalList[index] = value;
274 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using a specific index.
276 /// <param name="index">The zero-based index of the parameter.</param>
277 public void RemoveAt(int index)
279 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", index);
280 this.InternalList.RemoveAt(index);
284 /// Inserts a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> into the collection at the specified index.
286 /// <param name="index">The zero-based index where the parameter is to be inserted within the collection.</param>
287 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
288 public void Insert(int index, object value)
290 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Insert", index, value);
292 this.InternalList.Insert(index, value);
296 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection.
298 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to remove from the collection.</param>
299 public void Remove(object value)
301 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Remove", value);
303 this.InternalList.Remove(value);
307 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> exists in the collection.
309 /// <param name="value">The value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
310 /// <returns>true if the collection contains the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object; otherwise, false.</returns>
311 public bool Contains(object value)
313 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", value);
315 return this.InternalList.Contains(value);
319 /// Removes all items from the collection.
323 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Clear");
324 this.InternalList.Clear();
328 /// Gets the location of a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection.
330 /// <param name="value">The value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
331 /// <returns>The zero-based index of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object in the collection.</returns>
332 public int IndexOf(object value)
334 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", value);
336 return this.InternalList.IndexOf(value);
340 /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
342 /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
343 /// <returns>The zero-based index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
344 public int Add(object value)
346 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
348 return this.InternalList.Add(value);
351 bool IList.IsFixedSize {
354 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsFixedSize");
355 return this.InternalList.IsFixedSize;
361 #region ICollection Member
363 bool ICollection.IsSynchronized {
366 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsSynchronized");
367 return this.InternalList.IsSynchronized;
372 /// Gets the number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.
374 /// <value>The number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</value>
375 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
379 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Count");
380 return this.InternalList.Count;
385 /// Copies <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects from the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> to the specified array.
387 /// <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>
388 /// <param name="index">The starting index of the array.</param>
389 public void CopyTo(Array array, int index)
391 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CopyTo", array, index);
392 this.InternalList.CopyTo(array, index);
395 object ICollection.SyncRoot {
398 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "SyncRoot");
399 return this.InternalList.SyncRoot;
405 #region IEnumerable Member
408 /// Returns an enumerator that can iterate through the collection.
410 /// <returns>An <see cref="System.Collections.IEnumerator">IEnumerator</see> that can be used to iterate through the collection.</returns>
411 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
413 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetEnumerator");
414 return this.InternalList.GetEnumerator();
420 /// In methods taking an object as argument this method is used to verify
421 /// that the argument has the type <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
423 /// <param name="Object">The object to verify</param>
424 private void CheckType(object Object)
426 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CheckType", Object);
427 if(Object.GetType() != typeof(NpgsqlParameter))
428 throw new InvalidCastException(String.Format(this.resman.GetString("Exception_WrongType"), Object.GetType().ToString()));