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
;
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.
50 [Editor(typeof(NpgsqlParametersEditor
), typeof(System
.Drawing
.Design
.UITypeEditor
))]
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
;
64 /// Initializes a new instance of the NpgsqlParameterCollection class.
66 internal NpgsqlParameterCollection()
68 this.resman
= new System
.Resources
.ResourceManager(this.GetType());
69 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, CLASSNAME
);
72 #region NpgsqlParameterCollection Member
75 /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified name.
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>
81 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
84 public NpgsqlParameter
this[string parameterName
] {
87 NpgsqlEventLog
.LogIndexerGet(LogLevel
.Debug
, CLASSNAME
, parameterName
);
88 return (NpgsqlParameter
)this.InternalList
[IndexOf(parameterName
)];
92 NpgsqlEventLog
.LogIndexerSet(LogLevel
.Debug
, CLASSNAME
, parameterName
, value);
93 this.InternalList
[IndexOf(parameterName
)] = value;
98 /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> at the specified index.
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>
104 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
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;
121 /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
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);
143 /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name and value.
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>
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.
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));
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.
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
));
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.
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
));
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.
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
));
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);
217 this.InternalList
[IndexOf(parameterName
)] = value;
222 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using the parameter name.
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
));
232 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
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);
243 /// Gets the location of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection with a specific parameter name.
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.
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
)
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);
287 this.InternalList
[index
] = value;
292 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using a specific index.
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
);
302 /// Inserts a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> into the collection at the specified index.
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);
310 this.InternalList
.Insert(index
, value);
314 /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection.
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);
321 this.InternalList
.Remove(value);
325 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> exists in the collection.
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);
333 return this.InternalList
.Contains(value);
337 /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
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
);
348 parameter
= this[index
];
359 /// Removes all items from the collection.
363 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Clear");
364 this.InternalList
.Clear();
368 /// Gets the location of a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection.
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);
376 return this.InternalList
.IndexOf(value);
380 /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
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);
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
;
402 #region ICollection Member
404 bool ICollection
.IsSynchronized
{
407 NpgsqlEventLog
.LogPropertyGet(LogLevel
.Debug
, CLASSNAME
, "IsSynchronized");
408 return this.InternalList
.IsSynchronized
;
413 /// Gets the number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.
415 /// <value>The number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</value>
418 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
424 NpgsqlEventLog
.LogPropertyGet(LogLevel
.Debug
, CLASSNAME
, "Count");
425 return this.InternalList
.Count
;
430 /// Copies <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects from the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> to the specified array.
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
;
450 #region IEnumerable Member
453 /// Returns an enumerator that can iterate through the collection.
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();
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>
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()));