// Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as published by // the Free Software Foundation // // There are special exceptions to the terms and conditions of the GPL // as it is applied to this software. View the full text of the // exception in file EXCEPTIONS in the directory of this software // distribution. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; using System.Data.Common; using System.Collections; using System.ComponentModel; using System.Collections.Generic; using MySql.Data.MySqlClient.Properties; namespace MySql.Data.MySqlClient { /// /// Represents a collection of parameters relevant to a as well as their respective mappings to columns in a . This class cannot be inherited. /// /// #if !CF [Editor("MySql.Data.MySqlClient.Design.DBParametersEditor,MySql.Design", typeof(System.Drawing.Design.UITypeEditor))] [ListBindable(true)] #endif public sealed class MySqlParameterCollection : DbParameterCollection { List items = new List(); //private ArrayList items = new ArrayList(); private Hashtable indexHashCS; private Hashtable indexHashCI; internal MySqlParameterCollection(MySqlCommand cmd) { indexHashCS = new Hashtable(); indexHashCI = new Hashtable(StringComparer.CurrentCultureIgnoreCase); Clear(); } #region Public Methods /// /// Gets the at the specified index. /// /// Gets the with a specified attribute. /// [C#] In C#, this property is the indexer for the class. /// public new MySqlParameter this[int index] { get { return (MySqlParameter)GetParameter(index); } set { SetParameter(index, value); } } /// /// Gets the with the specified name. /// public new MySqlParameter this[string name] { get { return (MySqlParameter)GetParameter(name); } set { SetParameter(name, value); } } /// /// Adds the specified object to the . /// /// The to add to the collection. /// The newly added object. public MySqlParameter Add(MySqlParameter value) { return InternalAdd(value, -1); } /// /// Adds a to the given the specified parameter name and value. /// /// The name of the parameter. /// The of the to add to the collection. /// The newly added object. [Obsolete("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value)")] public MySqlParameter Add(string parameterName, object value) { return Add(new MySqlParameter(parameterName, value)); } public MySqlParameter AddWithValue(string parameterName, object value) { return Add(new MySqlParameter(parameterName, value)); } /// /// Adds a to the given the parameter name and the data type. /// /// The name of the parameter. /// One of the values. /// The newly added object. public MySqlParameter Add(string parameterName, MySqlDbType dbType) { return Add(new MySqlParameter(parameterName, dbType)); } /// /// Adds a to the with the parameter name, the data type, and the column length. /// /// The name of the parameter. /// One of the values. /// The length of the column. /// The newly added object. public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size) { return Add(new MySqlParameter(parameterName, dbType, size)); } /// /// Adds a to the with the parameter name, the data type, the column length, and the source column name. /// /// The name of the parameter. /// One of the values. /// The length of the column. /// The name of the source column. /// The newly added object. public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size, string sourceColumn) { return Add(new MySqlParameter(parameterName, dbType, size, sourceColumn)); } #endregion #region DbParameterCollection Implementation /// /// Adds an array of values to the end of the . /// /// public override void AddRange(Array values) { foreach (DbParameter p in values) Add(p); } void CheckIndex(int index) { if (index < 0 || index >= Count) throw new IndexOutOfRangeException("Parameter index is out of range."); } /// /// Retrieve the parameter with the given name. /// /// /// protected override DbParameter GetParameter(string parameterName) { int index = IndexOf(parameterName); if (index < 0) { // check to see if the user has added the parameter without a // parameter marker. If so, kindly tell them what they did. if (parameterName.StartsWith("@") || parameterName.StartsWith("?")) { string newParameterName = parameterName.Substring(1); index = IndexOf(newParameterName); if (index != -1) return (DbParameter)items[index]; } throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection."); } return (DbParameter)items[index]; } protected override DbParameter GetParameter(int index) { CheckIndex(index); return (DbParameter)items[index]; } protected override void SetParameter(string parameterName, DbParameter value) { int index = IndexOf(parameterName); if (index < 0) throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection."); SetParameter(index, value); } protected override void SetParameter(int index, DbParameter value) { CheckIndex(index); MySqlParameter p = (MySqlParameter)items[index]; // first we remove the old parameter from our hashes indexHashCS.Remove(p.ParameterName); indexHashCI.Remove(p.ParameterName); // then we add in the new parameter items[index] = value; indexHashCS.Add(value.ParameterName, index); indexHashCI.Add(value.ParameterName, index); } /// /// Adds the specified object to the . /// /// The to add to the collection. /// The index of the new object. public override int Add(object value) { MySqlParameter parameter = value as MySqlParameter; if (parameter == null) throw new MySqlException("Only MySqlParameter objects may be stored"); if (parameter.ParameterName == null || parameter.ParameterName == String.Empty) throw new MySqlException("Parameters must be named"); parameter = Add(parameter); return IndexOf(parameter); } /// /// Removes all items from the collection. /// public override void Clear() { foreach (MySqlParameter p in items) p.Collection = null; items.Clear(); indexHashCS.Clear(); indexHashCI.Clear(); } /// /// Gets a value indicating whether a with the specified parameter name exists in the collection. /// /// The name of the object to find. /// true if the collection contains the parameter; otherwise, false. public override bool Contains(string parameterName) { return IndexOf(parameterName) != -1; } /// /// Gets a value indicating whether a MySqlParameter exists in the collection. /// /// The value of the object to find. /// true if the collection contains the object; otherwise, false. /// Gets a value indicating whether a exists in the collection. public override bool Contains(object value) { DbParameter parameter = value as DbParameter; if (null == parameter) throw new ArgumentException("Argument must be of type DbParameter", "value"); return items.Contains(parameter); } /// /// Copies MySqlParameter objects from the MySqlParameterCollection to the specified array. /// /// /// public override void CopyTo(Array array, int index) { items.ToArray().CopyTo(array, index); } /// /// Gets the number of MySqlParameter objects in the collection. /// public override int Count { get { return items.Count; } } /// /// Returns an enumerator that iterates through the . /// /// public override IEnumerator GetEnumerator() { return items.GetEnumerator(); } /// /// Gets the location of the in the collection with a specific parameter name. /// /// The name of the object to retrieve. /// The zero-based location of the in the collection. public override int IndexOf(string parameterName) { object o = indexHashCS[parameterName]; if (o == null) o = indexHashCI[parameterName]; if (o == null) return -1; return (int)o; } /// /// Gets the location of a in the collection. /// /// The object to locate. /// The zero-based location of the in the collection. /// Gets the location of a in the collection. public override int IndexOf(object value) { DbParameter parameter = value as DbParameter; if (null == parameter) throw new ArgumentException("Argument must be of type DbParameter", "value"); return items.IndexOf(parameter); } /// /// Inserts a MySqlParameter into the collection at the specified index. /// /// /// public override void Insert(int index, object value) { MySqlParameter parameter = value as MySqlParameter; if (parameter == null) throw new MySqlException("Only MySqlParameter objects may be stored"); InternalAdd(parameter, index); } /// /// Gets a value that indicates whether the /// has a fixed size. /// public override bool IsFixedSize { get { return (items as IList).IsFixedSize; } } /// /// Gets a value that indicates whether the /// is read-only. /// public override bool IsReadOnly { get { return (items as IList).IsReadOnly; } } /// /// Gets a value that indicates whether the /// is synchronized. /// public override bool IsSynchronized { get { return (items as IList).IsSynchronized; } } /// /// Removes the specified MySqlParameter from the collection. /// /// public override void Remove(object value) { MySqlParameter p = (value as MySqlParameter); p.Collection = null; int index = IndexOf(p); items.Remove(p); indexHashCS.Remove(p.ParameterName); indexHashCI.Remove(p.ParameterName); AdjustHashes(index, false); } /// /// Removes the specified from the collection using the parameter name. /// /// The name of the object to retrieve. public override void RemoveAt(string parameterName) { DbParameter p = GetParameter(parameterName); Remove(p); } /// /// Removes the specified from the collection using a specific index. /// /// The zero-based index of the parameter. /// Removes the specified from the collection. public override void RemoveAt(int index) { object o = items[index]; Remove(o); } /// /// Gets an object that can be used to synchronize access to the /// . /// public override object SyncRoot { get { return (items as IList).SyncRoot; } } #endregion internal void ParameterNameChanged(MySqlParameter p, string oldName, string newName) { int index = IndexOf(oldName); indexHashCS.Remove(oldName); indexHashCI.Remove(oldName); indexHashCS.Add(newName, index); indexHashCI.Add(newName, index); } private MySqlParameter InternalAdd(MySqlParameter value, int index) { if (value == null) throw new ArgumentException("The MySqlParameterCollection only accepts non-null MySqlParameter type objects.", "value"); // make sure we don't already have a parameter with this name if (IndexOf(value.ParameterName) >= 0) { throw new MySqlException( String.Format(Resources.ParameterAlreadyDefined, value.ParameterName)); } else { string inComingName = value.ParameterName; if (inComingName[0] == '@' || inComingName[0] == '?') inComingName = inComingName.Substring(1, inComingName.Length - 1); if (IndexOf(inComingName) >= 0) throw new MySqlException( String.Format(Resources.ParameterAlreadyDefined, value.ParameterName)); } if (index == -1) { items.Add(value); index = items.IndexOf(value); } else { items.Insert(index, value); AdjustHashes(index, true); } indexHashCS.Add(value.ParameterName, index); indexHashCI.Add(value.ParameterName, index); value.Collection = this; return value; } private static void AdjustHash(Hashtable hash, string parameterName, int keyIndex, bool addEntry) { if (!hash.ContainsKey(parameterName)) return; int index = (int)hash[parameterName]; if (index < keyIndex) return; hash[parameterName] = addEntry ? ++index : --index; } /// /// This method will update all the items in the index hashes when /// we insert a parameter somewhere in the middle /// /// /// private void AdjustHashes(int keyIndex, bool addEntry) { for (int i = 0; i < Count; i++) { string name = (items[i] as MySqlParameter).ParameterName; AdjustHash(indexHashCI, name, keyIndex, addEntry); AdjustHash(indexHashCS, name, keyIndex, addEntry); } } internal MySqlParameter GetParameterFlexible(string parameterName, bool throwOnNotFound) { int index = IndexOf(parameterName); if (-1 == index) index = IndexOf("?" + parameterName); if (-1 == index) index = IndexOf("@" + parameterName); if (-1 == index) { if (parameterName.StartsWith("@") || parameterName.StartsWith("?")) index = IndexOf(parameterName.Substring(1)); } if (-1 != index) return this[index]; if (throwOnNotFound) throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection."); return null; } } }