You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
13 KiB

  1. #if NET40 || NET461
  2. // Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Collections;
  25. using System.ComponentModel;
  26. using System.Collections.Generic;
  27. using Externals.MySql.Data.MySqlClient.Properties;
  28. namespace Externals.MySql.Data.MySqlClient
  29. {
  30. /// <summary>
  31. /// Represents a collection of parameters relevant to a <see cref="MySqlCommand"/> as well as their respective mappings to columns in a <see cref="System.Data.DataSet"/>. This class cannot be inherited.
  32. /// </summary>
  33. /// <include file='Externals/MySql.Data-6.9.11/docs/mysqlParameterCollection.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>
  34. internal sealed partial class MySqlParameterCollection
  35. {
  36. List<MySqlParameter> items = new List<MySqlParameter>();
  37. private Dictionary<string,int> indexHashCS;
  38. private Dictionary<string,int> indexHashCI;
  39. //turns to true if any parameter is unnamed
  40. internal bool containsUnnamedParameters;
  41. internal MySqlParameterCollection(MySqlCommand cmd)
  42. {
  43. indexHashCS = new Dictionary<string, int>();
  44. indexHashCI = new Dictionary<string,int>(StringComparer.CurrentCultureIgnoreCase);
  45. containsUnnamedParameters = false;
  46. Clear();
  47. }
  48. /// <summary>
  49. /// Gets the number of MySqlParameter objects in the collection.
  50. /// </summary>
  51. public override int Count
  52. {
  53. get { return items.Count; }
  54. }
  55. #region Public Methods
  56. /// <summary>
  57. /// Gets the <see cref="MySqlParameter"/> at the specified index.
  58. /// </summary>
  59. /// <overloads>Gets the <see cref="MySqlParameter"/> with a specified attribute.
  60. /// [C#] In C#, this property is the indexer for the <see cref="MySqlParameterCollection"/> class.
  61. /// </overloads>
  62. public new MySqlParameter this[int index]
  63. {
  64. get { return InternalGetParameter(index); }
  65. set { InternalSetParameter(index, value); }
  66. }
  67. /// <summary>
  68. /// Gets the <see cref="MySqlParameter"/> with the specified name.
  69. /// </summary>
  70. public new MySqlParameter this[string name]
  71. {
  72. get { return InternalGetParameter(name); }
  73. set { InternalSetParameter(name, value); }
  74. }
  75. /// <summary>
  76. /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
  77. /// </summary>
  78. /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
  79. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  80. public MySqlParameter Add(MySqlParameter value)
  81. {
  82. return InternalAdd(value, -1);
  83. }
  84. /// <summary>
  85. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
  86. /// </summary>
  87. /// <param name="parameterName">The name of the parameter.</param>
  88. /// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
  89. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  90. [Obsolete("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value)")]
  91. public MySqlParameter Add(string parameterName, object value)
  92. {
  93. return Add(new MySqlParameter(parameterName, value));
  94. }
  95. public MySqlParameter AddWithValue(string parameterName, object value)
  96. {
  97. return Add(new MySqlParameter(parameterName, value));
  98. }
  99. /// <summary>
  100. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the parameter name and the data type.
  101. /// </summary>
  102. /// <param name="parameterName">The name of the parameter.</param>
  103. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  104. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  105. public MySqlParameter Add(string parameterName, MySqlDbType dbType)
  106. {
  107. return Add(new MySqlParameter(parameterName, dbType));
  108. }
  109. /// <summary>
  110. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, and the column length.
  111. /// </summary>
  112. /// <param name="parameterName">The name of the parameter.</param>
  113. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  114. /// <param name="size">The length of the column.</param>
  115. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  116. public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size)
  117. {
  118. return Add(new MySqlParameter(parameterName, dbType, size));
  119. }
  120. #endregion
  121. /// <summary>
  122. /// Removes all items from the collection.
  123. /// </summary>
  124. public override void Clear()
  125. {
  126. foreach (MySqlParameter p in items)
  127. p.Collection = null;
  128. items.Clear();
  129. indexHashCS.Clear();
  130. indexHashCI.Clear();
  131. }
  132. void CheckIndex(int index)
  133. {
  134. if (index < 0 || index >= Count)
  135. throw new IndexOutOfRangeException("Parameter index is out of range.");
  136. }
  137. private MySqlParameter InternalGetParameter(int index)
  138. {
  139. CheckIndex(index);
  140. return items[index];
  141. }
  142. private MySqlParameter InternalGetParameter(string parameterName)
  143. {
  144. int index = IndexOf(parameterName);
  145. if (index < 0)
  146. {
  147. // check to see if the user has added the parameter without a
  148. // parameter marker. If so, kindly tell them what they did.
  149. if (parameterName.StartsWith("@", StringComparison.Ordinal) ||
  150. parameterName.StartsWith("?", StringComparison.Ordinal))
  151. {
  152. string newParameterName = parameterName.Substring(1);
  153. index = IndexOf(newParameterName);
  154. if (index != -1)
  155. return items[index];
  156. }
  157. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  158. }
  159. return items[index];
  160. }
  161. private void InternalSetParameter(string parameterName, MySqlParameter value)
  162. {
  163. int index = IndexOf(parameterName);
  164. if (index < 0)
  165. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  166. InternalSetParameter(index, value);
  167. }
  168. private void InternalSetParameter(int index, MySqlParameter value)
  169. {
  170. MySqlParameter newParameter = value as MySqlParameter;
  171. if (newParameter == null)
  172. throw new ArgumentException(Resources.NewValueShouldBeMySqlParameter);
  173. CheckIndex(index);
  174. MySqlParameter p = (MySqlParameter)items[index];
  175. // first we remove the old parameter from our hashes
  176. indexHashCS.Remove(p.ParameterName);
  177. indexHashCI.Remove(p.ParameterName);
  178. // then we add in the new parameter
  179. items[index] = newParameter;
  180. indexHashCS.Add(value.ParameterName, index);
  181. indexHashCI.Add(value.ParameterName, index);
  182. }
  183. /// <summary>
  184. /// Gets the location of the <see cref="MySqlParameter"/> in the collection with a specific parameter name.
  185. /// </summary>
  186. /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
  187. /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
  188. public override int IndexOf(string parameterName)
  189. {
  190. int i = -1;
  191. if (!indexHashCS.TryGetValue(parameterName, out i) &&
  192. !indexHashCI.TryGetValue(parameterName, out i))
  193. return -1;
  194. return i;
  195. }
  196. /// <summary>
  197. /// Gets the location of a <see cref="MySqlParameter"/> in the collection.
  198. /// </summary>
  199. /// <param name="value">The <see cref="MySqlParameter"/> object to locate. </param>
  200. /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
  201. /// <overloads>Gets the location of a <see cref="MySqlParameter"/> in the collection.</overloads>
  202. public override int IndexOf(object value)
  203. {
  204. MySqlParameter parameter = value as MySqlParameter;
  205. if (null == parameter)
  206. throw new ArgumentException("Argument must be of type DbParameter", "value");
  207. return items.IndexOf(parameter);
  208. }
  209. internal void ParameterNameChanged(MySqlParameter p, string oldName, string newName)
  210. {
  211. int index = IndexOf(oldName);
  212. indexHashCS.Remove(oldName);
  213. indexHashCI.Remove(oldName);
  214. indexHashCS.Add(newName, index);
  215. indexHashCI.Add(newName, index);
  216. }
  217. private MySqlParameter InternalAdd(MySqlParameter value, int index)
  218. {
  219. if (value == null)
  220. throw new ArgumentException("The MySqlParameterCollection only accepts non-null MySqlParameter type objects.", "value");
  221. // if the parameter is unnamed, then assign a default name
  222. if (String.IsNullOrEmpty(value.ParameterName))
  223. value.ParameterName = String.Format("Parameter{0}", GetNextIndex());
  224. // make sure we don't already have a parameter with this name
  225. if (IndexOf(value.ParameterName) >= 0)
  226. {
  227. throw new MySqlException(
  228. String.Format(Resources.ParameterAlreadyDefined, value.ParameterName));
  229. }
  230. else
  231. {
  232. string inComingName = value.ParameterName;
  233. if (inComingName[0] == '@' || inComingName[0] == '?')
  234. inComingName = inComingName.Substring(1, inComingName.Length - 1);
  235. if (IndexOf(inComingName) >= 0)
  236. throw new MySqlException(
  237. String.Format(Resources.ParameterAlreadyDefined, value.ParameterName));
  238. }
  239. if (index == -1)
  240. {
  241. items.Add(value);
  242. index = items.Count - 1;
  243. }
  244. else
  245. {
  246. items.Insert(index, value);
  247. AdjustHashes(index, true);
  248. }
  249. indexHashCS.Add(value.ParameterName, index);
  250. indexHashCI.Add(value.ParameterName, index);
  251. value.Collection = this;
  252. return value;
  253. }
  254. private int GetNextIndex()
  255. {
  256. int index = Count+1;
  257. while (true)
  258. {
  259. string name = "Parameter" + index.ToString();
  260. if (!indexHashCI.ContainsKey(name)) break;
  261. index++;
  262. }
  263. return index;
  264. }
  265. private static void AdjustHash(Dictionary<string,int> hash, string parameterName, int keyIndex, bool addEntry)
  266. {
  267. if (!hash.ContainsKey(parameterName)) return;
  268. int index = (int)hash[parameterName];
  269. if (index < keyIndex) return;
  270. hash[parameterName] = addEntry ? ++index : --index;
  271. }
  272. /// <summary>
  273. /// This method will update all the items in the index hashes when
  274. /// we insert a parameter somewhere in the middle
  275. /// </summary>
  276. /// <param name="keyIndex"></param>
  277. /// <param name="addEntry"></param>
  278. private void AdjustHashes(int keyIndex, bool addEntry)
  279. {
  280. for (int i = 0; i < Count; i++)
  281. {
  282. string name = (items[i] as MySqlParameter).ParameterName;
  283. AdjustHash(indexHashCI, name, keyIndex, addEntry);
  284. AdjustHash(indexHashCS, name, keyIndex, addEntry);
  285. }
  286. }
  287. private MySqlParameter GetParameterFlexibleInternal(string baseName)
  288. {
  289. int index = IndexOf(baseName);
  290. if (-1 == index)
  291. index = IndexOf("?" + baseName);
  292. if (-1 == index)
  293. index = IndexOf("@" + baseName);
  294. if (-1 != index)
  295. return this[index];
  296. return null;
  297. }
  298. internal MySqlParameter GetParameterFlexible(string parameterName, bool throwOnNotFound)
  299. {
  300. string baseName = parameterName;
  301. MySqlParameter p = GetParameterFlexibleInternal(baseName);
  302. if (p != null) return p;
  303. if (parameterName.StartsWith("@", StringComparison.Ordinal) || parameterName.StartsWith("?", StringComparison.Ordinal))
  304. baseName = parameterName.Substring(1);
  305. p = GetParameterFlexibleInternal(baseName);
  306. if (p != null) return p;
  307. if (throwOnNotFound)
  308. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  309. return null;
  310. }
  311. }
  312. }
  313. #endif