// Copyright (c) 2004, 2018, Oracle and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0, as
// published by the Free Software Foundation.
//
// This program is also distributed with certain software (including
// but not limited to OpenSSL) that is licensed under separate terms,
// as designated in a particular file or component or in included license
// documentation. The authors of MySQL hereby grant you an
// additional permission to link the program and your derivative works
// with the separately licensed software that they have included with
// MySQL.
//
// Without limiting anything contained in the foregoing, this file,
// which is part of MySQL Connector/NET, is also subject to the
// Universal FOSS Exception, version 1.0, a copy of which can be found at
// http://oss.oracle.com/licenses/universal-foss-exception.
//
// 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, version 2.0, 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.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.Data.Common;
using System.Collections.Generic;
using System.Collections;
using Sog.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.
///
///
public sealed partial class MySqlParameterCollection : DbParameterCollection
{
readonly List _items = new List();
private readonly Dictionary _indexHashCs;
private readonly Dictionary _indexHashCi;
// turns to true if any parameter is unnamed
internal bool containsUnnamedParameters;
internal MySqlParameterCollection(MySqlCommand cmd)
{
this._indexHashCs = new Dictionary();
this._indexHashCi = new Dictionary(StringComparer.CurrentCultureIgnoreCase);
this.containsUnnamedParameters = false;
this.Clear();
}
///
/// Gets the number of MySqlParameter objects in the collection.
///
public override int Count => this._items.Count;
#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 this.InternalGetParameter(index); }
set { this.InternalSetParameter(index, value); }
}
///
/// Gets the with the specified name.
///
public new MySqlParameter this[string name]
{
get { return this.InternalGetParameter(name); }
set { this.InternalSetParameter(name, value); }
}
///
/// 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 this.Add(new MySqlParameter(parameterName, dbType, size, sourceColumn));
}
///
/// Adds the specified object to the .
///
/// The to add to the collection.
/// The newly added object.
public MySqlParameter Add(MySqlParameter value)
{
return this.InternalAdd(value, null);
}
///
/// Adds a parameter and its value.
///
/// The name of the parameter.
/// The value of the parameter.
/// A object representing the provided values.
public MySqlParameter AddWithValue(string parameterName, object value)
{
return this.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 this.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 this.Add(new MySqlParameter(parameterName, dbType, size));
}
#endregion
///
/// Removes all items from the collection.
///
public override void Clear()
{
foreach (MySqlParameter p in this._items)
{
p.Collection = null;
}
this._items.Clear();
this._indexHashCs.Clear();
this._indexHashCi.Clear();
}
void CheckIndex(int index)
{
if (index < 0 || index >= this.Count)
{
throw new IndexOutOfRangeException("Parameter index is out of range.");
}
}
private MySqlParameter InternalGetParameter(int index)
{
this.CheckIndex(index);
return this._items[index];
}
private MySqlParameter InternalGetParameter(string parameterName)
{
int index = this.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("@", StringComparison.Ordinal) ||
parameterName.StartsWith("?", StringComparison.Ordinal))
{
string newParameterName = parameterName.Substring(1);
index = this.IndexOf(newParameterName);
if (index != -1)
{
return this._items[index];
}
}
throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
}
return this._items[index];
}
private void InternalSetParameter(string parameterName, MySqlParameter value)
{
int index = this.IndexOf(parameterName);
if (index < 0)
{
throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
}
this.InternalSetParameter(index, value);
}
private void InternalSetParameter(int index, MySqlParameter value)
{
MySqlParameter newParameter = value;
if (newParameter == null)
{
throw new ArgumentException(Resources.NewValueShouldBeMySqlParameter);
}
this.CheckIndex(index);
MySqlParameter p = this._items[index];
// first we remove the old parameter from our hashes
this._indexHashCs.Remove(p.ParameterName);
this._indexHashCi.Remove(p.ParameterName);
// then we add in the new parameter
this._items[index] = newParameter;
this._indexHashCs.Add(value.ParameterName, index);
this._indexHashCi.Add(value.ParameterName, index);
}
///
/// 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)
{
int i = -1;
if (!this._indexHashCs.TryGetValue(parameterName, out i) &&
!this._indexHashCi.TryGetValue(parameterName, out i))
{
return -1;
}
return i;
}
///
/// 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)
{
MySqlParameter parameter = value as MySqlParameter;
if (null == parameter)
{
throw new ArgumentException("Argument must be of type DbParameter", "value");
}
return this._items.IndexOf(parameter);
}
internal void ParameterNameChanged(MySqlParameter p, string oldName, string newName)
{
int index = this.IndexOf(oldName);
this._indexHashCs.Remove(oldName);
this._indexHashCi.Remove(oldName);
this._indexHashCs.Add(newName, index);
this._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");
}
// if the parameter is unnamed, then assign a default name
if (String.IsNullOrEmpty(value.ParameterName))
{
value.ParameterName = String.Format("Parameter{0}", this.GetNextIndex());
}
// make sure we don't already have a parameter with this name
if (this.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 (this.IndexOf(inComingName) >= 0)
{
throw new MySqlException(
String.Format(Resources.ParameterAlreadyDefined, value.ParameterName));
}
}
if (index == null)
{
this._items.Add(value);
index = this._items.Count - 1;
}
else
{
this._items.Insert((int)index, value);
this.AdjustHashes((int)index, true);
}
this._indexHashCs.Add(value.ParameterName, (int)index);
this._indexHashCi.Add(value.ParameterName, (int)index);
value.Collection = this;
return value;
}
private int GetNextIndex()
{
int index = this.Count + 1;
while (true)
{
string name = "Parameter" + index.ToString();
if (!this._indexHashCi.ContainsKey(name))
{
break;
}
index++;
}
return index;
}
private static void AdjustHash(Dictionary hash, string parameterName, int keyIndex, bool addEntry)
{
if (!hash.ContainsKey(parameterName))
{
return;
}
int index = 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 < this.Count; i++)
{
string name = this._items[i].ParameterName;
AdjustHash(this._indexHashCi, name, keyIndex, addEntry);
AdjustHash(this._indexHashCs, name, keyIndex, addEntry);
}
}
private MySqlParameter GetParameterFlexibleInternal(string baseName)
{
int index = this.IndexOf(baseName);
if (-1 == index)
{
index = this.IndexOf("?" + baseName);
}
if (-1 == index)
{
index = this.IndexOf("@" + baseName);
}
if (-1 != index)
{
return this[index];
}
return null;
}
internal MySqlParameter GetParameterFlexible(string parameterName, bool throwOnNotFound)
{
string baseName = parameterName;
MySqlParameter p = this.GetParameterFlexibleInternal(baseName);
if (p != null)
{
return p;
}
if (parameterName.StartsWith("@", StringComparison.Ordinal) || parameterName.StartsWith("?", StringComparison.Ordinal))
{
baseName = parameterName.Substring(1);
}
p = this.GetParameterFlexibleInternal(baseName);
if (p != null)
{
return p;
}
if (throwOnNotFound)
{
throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
}
return null;
}
#region DbParameterCollection Implementation
///
/// Adds an array of values to the end of the .
///
///
public override void AddRange(Array values)
{
foreach (DbParameter p in values)
{
this.Add(p);
}
}
///
/// Retrieve the parameter with the given name.
///
///
///
protected override DbParameter GetParameter(string parameterName)
{
return this.InternalGetParameter(parameterName);
}
protected override DbParameter GetParameter(int index)
{
return this.InternalGetParameter(index);
}
protected override void SetParameter(string parameterName, DbParameter value)
{
this.InternalSetParameter(parameterName, value as MySqlParameter);
}
protected override void SetParameter(int index, DbParameter value)
{
this.InternalSetParameter(index, value as MySqlParameter);
}
///
/// 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");
}
parameter = this.Add(parameter);
return this.IndexOf(parameter);
}
///
/// 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 this.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)
{
MySqlParameter parameter = value as MySqlParameter;
if (null == parameter)
{
throw new ArgumentException("Argument must be of type DbParameter", nameof(value));
}
return this._items.Contains(parameter);
}
///
/// Copies MySqlParameter objects from the MySqlParameterCollection to the specified array.
///
///
///
public override void CopyTo(Array array, int index)
{
this._items.ToArray().CopyTo(array, index);
}
///
/// Returns an enumerator that iterates through the .
///
///
public override IEnumerator GetEnumerator()
{
return this._items.GetEnumerator();
}
///
/// 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");
}
this.InternalAdd(parameter, index);
}
///
/// Removes the specified MySqlParameter from the collection.
///
///
public override void Remove(object value)
{
MySqlParameter p = (value as MySqlParameter);
p.Collection = null;
int index = this.IndexOf(p);
this._items.Remove(p);
this._indexHashCs.Remove(p.ParameterName);
this._indexHashCi.Remove(p.ParameterName);
this.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 = this.GetParameter(parameterName);
this.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 = this._items[index];
this.Remove(o);
}
///
/// Gets an object that can be used to synchronize access to the
/// .
///
public override object SyncRoot => (this._items as IList).SyncRoot;
#endregion
}
}