// created on 09/07/2003 at 20:20
// Npgsql.NpgsqlParameterCollection.cs
//
// Author:
// Brar Piening (brar@gmx.de)
//
// Rewritten from the scratch to derive from MarshalByRefObject instead of ArrayList.
// Recycled some parts of the original NpgsqlParameterCollection.cs
// by Francisco Jr. (fxjrlists@yahoo.com.br)
//
// Copyright (C) 2002 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Reflection;
using System.Data;
using System.Collections;
using System.ComponentModel;
using NpgsqlTypes;
#if WITHDESIGN
using Npgsql.Design;
#endif
namespace Npgsql
{
///
/// Represents a collection of parameters relevant to a NpgsqlCommand
/// as well as their respective mappings to columns in a DataSet.
/// This class cannot be inherited.
///
#if WITHDESIGN
[ListBindable(false)]
[Editor(typeof(NpgsqlParametersEditor), typeof(System.Drawing.Design.UITypeEditor))]
#endif
public sealed class NpgsqlParameterCollection : MarshalByRefObject, IDataParameterCollection
{
private ArrayList InternalList = new ArrayList();
// Logging related value
private static readonly String CLASSNAME = "NpgsqlParameterCollection";
// Our resource manager
private System.Resources.ResourceManager resman;
///
/// Initializes a new instance of the NpgsqlParameterCollection class.
///
internal NpgsqlParameterCollection()
{
this.resman = new System.Resources.ResourceManager(this.GetType());
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
}
#region NpgsqlParameterCollection Member
///
/// Gets the NpgsqlParameter with the specified name.
///
/// The name of the NpgsqlParameter to retrieve.
/// The NpgsqlParameter with the specified name, or a null reference if the parameter is not found.
#if WITHDESIGN
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public NpgsqlParameter this[string parameterName] {
get
{
NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
return (NpgsqlParameter)this.InternalList[IndexOf(parameterName)];
}
set
{
NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
this.InternalList[IndexOf(parameterName)] = value;
}
}
///
/// Gets the NpgsqlParameter at the specified index.
///
/// The zero-based index of the NpgsqlParameter to retrieve.
/// The NpgsqlParameter at the specified index.
#if WITHDESIGN
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public NpgsqlParameter this[int index] {
get
{
NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
return (NpgsqlParameter)this.InternalList[index];
}
set
{
NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
this.InternalList[index] = value;
}
}
///
/// Adds the specified NpgsqlParameter object to the NpgsqlParameterCollection.
///
/// The NpgsqlParameter to add to the collection.
/// The index of the new NpgsqlParameter object.
public NpgsqlParameter Add(NpgsqlParameter value)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
// Do not allow parameters without name.
if (value.ParameterName == null) {
throw new ArgumentNullException(String.Format(this.resman.GetString("Exception_InvalidParameterName"), value.ParameterName));
}
if (value.ParameterName.Trim() == String.Empty ||
(value.ParameterName.Length == 1 && value.ParameterName[0] == ':'))
throw new ArgumentOutOfRangeException(String.Format(this.resman.GetString("Exception_InvalidParameterName"), value.ParameterName));
this.InternalList.Add(value);
return value;
}
///
/// Adds a NpgsqlParameter to the NpgsqlParameterCollection given the specified parameter name and value.
///
/// The name of the NpgsqlParameter.
/// The Value of the NpgsqlParameter to add to the collection.
/// The index of the new NpgsqlParameter object.
///
/// Use caution when using this overload of the
/// Add method to specify integer parameter values.
/// Because this overload takes a value of type Object,
/// you must convert the integral value to an Object
/// type when the value is zero, as the following C# example demonstrates.
/// parameters.Add(":pname", Convert.ToInt32(0));
/// If you do not perform this conversion, the compiler will assume you
/// are attempting to call the NpgsqlParameterCollection.Add(string, DbType) overload.
///
public NpgsqlParameter Add(string parameterName, object value)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, value);
return this.Add(new NpgsqlParameter(parameterName, value));
}
///
/// Adds a NpgsqlParameter to the NpgsqlParameterCollection given the parameter name and the data type.
///
/// The name of the parameter.
/// One of the DbType values.
/// The index of the new NpgsqlParameter object.
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType);
return this.Add(new NpgsqlParameter(parameterName, parameterType));
}
///
/// Adds a NpgsqlParameter to the NpgsqlParameterCollection with the parameter name, the data type, and the column length.
///
/// The name of the parameter.
/// One of the DbType values.
/// The length of the column.
/// The index of the new NpgsqlParameter object.
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size);
return this.Add(new NpgsqlParameter(parameterName, parameterType, size));
}
///
/// Adds a NpgsqlParameter to the NpgsqlParameterCollection with the parameter name, the data type, the column length, and the source column name.
///
/// The name of the parameter.
/// One of the DbType values.
/// The length of the column.
/// The name of the source column.
/// The index of the new NpgsqlParameter object.
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size, sourceColumn);
return this.Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn));
}
#endregion
#region IDataParameterCollection Member
object System.Data.IDataParameterCollection.this[string parameterName] {
get
{
NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
return this.InternalList[IndexOf(parameterName)];
}
set
{
NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
CheckType(value);
this.InternalList[IndexOf(parameterName)] = value;
}
}
///
/// Removes the specified NpgsqlParameter from the collection using the parameter name.
///
/// The name of the NpgsqlParameter object to retrieve.
public void RemoveAt(string parameterName)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", parameterName);
this.InternalList.RemoveAt(IndexOf(parameterName));
}
///
/// Gets a value indicating whether a NpgsqlParameter with the specified parameter name exists in the collection.
///
/// The name of the NpgsqlParameter object to find.
/// true if the collection contains the parameter; otherwise, false.
public bool Contains(string parameterName)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", parameterName);
return (IndexOf(parameterName) != -1);
}
///
/// Gets the location of the NpgsqlParameter in the collection with a specific parameter name.
///
/// The name of the NpgsqlParameter object to find.
/// The zero-based location of the NpgsqlParameter in the collection.
public int IndexOf(string parameterName)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", parameterName);
// Iterate values to see what is the index of parameter.
Int32 index = 0;
if ( (parameterName[0] != ':') && (parameterName[0] != '@') )
parameterName = ':' + parameterName;
foreach(NpgsqlParameter parameter in this)
{
if (parameter.ParameterName == parameterName)
return index;
index++;
}
return -1;
}
#endregion
#region IList Member
bool IList.IsReadOnly {
get
{
NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsReadOnly");
return this.InternalList.IsReadOnly;
}
}
object System.Collections.IList.this[int index] {
get
{
NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
return (NpgsqlParameter)this.InternalList[index];
}
set
{
NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
CheckType(value);
this.InternalList[index] = value;
}
}
///
/// Removes the specified NpgsqlParameter from the collection using a specific index.
///
/// The zero-based index of the parameter.
public void RemoveAt(int index)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", index);
this.InternalList.RemoveAt(index);
}
///
/// Inserts a NpgsqlParameter into the collection at the specified index.
///
/// The zero-based index where the parameter is to be inserted within the collection.
/// The NpgsqlParameter to add to the collection.
public void Insert(int index, object value)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Insert", index, value);
CheckType(value);
this.InternalList.Insert(index, value);
}
///
/// Removes the specified NpgsqlParameter from the collection.
///
/// The NpgsqlParameter to remove from the collection.
public void Remove(object value)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Remove", value);
CheckType(value);
this.InternalList.Remove(value);
}
///
/// Gets a value indicating whether a NpgsqlParameter exists in the collection.
///
/// The value of the NpgsqlParameter object to find.
/// true if the collection contains the NpgsqlParameter object; otherwise, false.
public bool Contains(object value)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", value);
CheckType(value);
return this.InternalList.Contains(value);
}
///
/// Removes all items from the collection.
///
public void Clear()
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Clear");
this.InternalList.Clear();
}
///
/// Gets the location of a NpgsqlParameter in the collection.
///
/// The value of the NpgsqlParameter object to find.
/// The zero-based index of the NpgsqlParameter object in the collection.
public int IndexOf(object value)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", value);
CheckType(value);
return this.InternalList.IndexOf(value);
}
///
/// Adds the specified NpgsqlParameter object to the NpgsqlParameterCollection.
///
/// The NpgsqlParameter to add to the collection.
/// The zero-based index of the new NpgsqlParameter object.
public int Add(object value)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
CheckType(value);
return this.InternalList.Add(value);
}
bool IList.IsFixedSize {
get
{
NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsFixedSize");
return this.InternalList.IsFixedSize;
}
}
#endregion
#region ICollection Member
bool ICollection.IsSynchronized {
get
{
NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsSynchronized");
return this.InternalList.IsSynchronized;
}
}
///
/// Gets the number of NpgsqlParameter objects in the collection.
///
/// The number of NpgsqlParameter objects in the collection.
#if WITHDESIGN
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public int Count {
get
{
NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Count");
return this.InternalList.Count;
}
}
///
/// Copies NpgsqlParameter objects from the NpgsqlParameterCollection to the specified array.
///
/// An Array to which to copy the NpgsqlParameter objects in the collection.
/// The starting index of the array.
public void CopyTo(Array array, int index)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CopyTo", array, index);
this.InternalList.CopyTo(array, index);
}
object ICollection.SyncRoot {
get
{
NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "SyncRoot");
return this.InternalList.SyncRoot;
}
}
#endregion
#region IEnumerable Member
///
/// Returns an enumerator that can iterate through the collection.
///
/// An IEnumerator that can be used to iterate through the collection.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetEnumerator");
return this.InternalList.GetEnumerator();
}
#endregion
///
/// In methods taking an object as argument this method is used to verify
/// that the argument has the type NpgsqlParameter
///
/// The object to verify
private void CheckType(object Object)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CheckType", Object);
if(Object.GetType() != typeof(NpgsqlParameter))
throw new InvalidCastException(String.Format(this.resman.GetString("Exception_WrongType"), Object.GetType().ToString()));
}
}
}