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.

355 lines
13 KiB

4 years ago
  1. #if MYSQL_6_9
  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. internal sealed partial class MySqlParameterCollection
  34. {
  35. List<MySqlParameter> items = new List<MySqlParameter>();
  36. private Dictionary<string,int> indexHashCS;
  37. private Dictionary<string,int> indexHashCI;
  38. //turns to true if any parameter is unnamed
  39. internal bool containsUnnamedParameters;
  40. internal MySqlParameterCollection(MySqlCommand cmd)
  41. {
  42. indexHashCS = new Dictionary<string, int>();
  43. indexHashCI = new Dictionary<string,int>(StringComparer.CurrentCultureIgnoreCase);
  44. containsUnnamedParameters = false;
  45. Clear();
  46. }
  47. /// <summary>
  48. /// Gets the number of MySqlParameter objects in the collection.
  49. /// </summary>
  50. public override int Count
  51. {
  52. get { return items.Count; }
  53. }
  54. #region Public Methods
  55. /// <summary>
  56. /// Gets the <see cref="MySqlParameter"/> at the specified index.
  57. /// </summary>
  58. /// <overloads>Gets the <see cref="MySqlParameter"/> with a specified attribute.
  59. /// [C#] In C#, this property is the indexer for the <see cref="MySqlParameterCollection"/> class.
  60. /// </overloads>
  61. public new MySqlParameter this[int index]
  62. {
  63. get { return InternalGetParameter(index); }
  64. set { InternalSetParameter(index, value); }
  65. }
  66. /// <summary>
  67. /// Gets the <see cref="MySqlParameter"/> with the specified name.
  68. /// </summary>
  69. public new MySqlParameter this[string name]
  70. {
  71. get { return InternalGetParameter(name); }
  72. set { InternalSetParameter(name, value); }
  73. }
  74. /// <summary>
  75. /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
  76. /// </summary>
  77. /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
  78. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  79. public MySqlParameter Add(MySqlParameter value)
  80. {
  81. return InternalAdd(value, -1);
  82. }
  83. /// <summary>
  84. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
  85. /// </summary>
  86. /// <param name="parameterName">The name of the parameter.</param>
  87. /// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
  88. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  89. [Obsolete("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value)")]
  90. public MySqlParameter Add(string parameterName, object value)
  91. {
  92. return Add(new MySqlParameter(parameterName, value));
  93. }
  94. public MySqlParameter AddWithValue(string parameterName, object value)
  95. {
  96. return Add(new MySqlParameter(parameterName, value));
  97. }
  98. /// <summary>
  99. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the parameter name and the data type.
  100. /// </summary>
  101. /// <param name="parameterName">The name of the parameter.</param>
  102. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  103. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  104. public MySqlParameter Add(string parameterName, MySqlDbType dbType)
  105. {
  106. return Add(new MySqlParameter(parameterName, dbType));
  107. }
  108. /// <summary>
  109. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, and the column length.
  110. /// </summary>
  111. /// <param name="parameterName">The name of the parameter.</param>
  112. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  113. /// <param name="size">The length of the column.</param>
  114. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  115. public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size)
  116. {
  117. return Add(new MySqlParameter(parameterName, dbType, size));
  118. }
  119. #endregion
  120. /// <summary>
  121. /// Removes all items from the collection.
  122. /// </summary>
  123. public override void Clear()
  124. {
  125. foreach (MySqlParameter p in items)
  126. p.Collection = null;
  127. items.Clear();
  128. indexHashCS.Clear();
  129. indexHashCI.Clear();
  130. }
  131. void CheckIndex(int index)
  132. {
  133. if (index < 0 || index >= Count)
  134. throw new IndexOutOfRangeException("Parameter index is out of range.");
  135. }
  136. private MySqlParameter InternalGetParameter(int index)
  137. {
  138. CheckIndex(index);
  139. return items[index];
  140. }
  141. private MySqlParameter InternalGetParameter(string parameterName)
  142. {
  143. int index = IndexOf(parameterName);
  144. if (index < 0)
  145. {
  146. // check to see if the user has added the parameter without a
  147. // parameter marker. If so, kindly tell them what they did.
  148. if (parameterName.StartsWith("@", StringComparison.Ordinal) ||
  149. parameterName.StartsWith("?", StringComparison.Ordinal))
  150. {
  151. string newParameterName = parameterName.Substring(1);
  152. index = IndexOf(newParameterName);
  153. if (index != -1)
  154. return items[index];
  155. }
  156. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  157. }
  158. return items[index];
  159. }
  160. private void InternalSetParameter(string parameterName, MySqlParameter value)
  161. {
  162. int index = IndexOf(parameterName);
  163. if (index < 0)
  164. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  165. InternalSetParameter(index, value);
  166. }
  167. private void InternalSetParameter(int index, MySqlParameter value)
  168. {
  169. MySqlParameter newParameter = value as MySqlParameter;
  170. if (newParameter == null)
  171. throw new ArgumentException(Resources.NewValueShouldBeMySqlParameter);
  172. CheckIndex(index);
  173. MySqlParameter p = (MySqlParameter)items[index];
  174. // first we remove the old parameter from our hashes
  175. indexHashCS.Remove(p.ParameterName);
  176. indexHashCI.Remove(p.ParameterName);
  177. // then we add in the new parameter
  178. items[index] = newParameter;
  179. indexHashCS.Add(value.ParameterName, index);
  180. indexHashCI.Add(value.ParameterName, index);
  181. }
  182. /// <summary>
  183. /// Gets the location of the <see cref="MySqlParameter"/> in the collection with a specific parameter name.
  184. /// </summary>
  185. /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
  186. /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
  187. public override int IndexOf(string parameterName)
  188. {
  189. int i = -1;
  190. if (!indexHashCS.TryGetValue(parameterName, out i) &&
  191. !indexHashCI.TryGetValue(parameterName, out i))
  192. return -1;
  193. return i;
  194. }
  195. /// <summary>
  196. /// Gets the location of a <see cref="MySqlParameter"/> in the collection.
  197. /// </summary>
  198. /// <param name="value">The <see cref="MySqlParameter"/> object to locate. </param>
  199. /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
  200. /// <overloads>Gets the location of a <see cref="MySqlParameter"/> in the collection.</overloads>
  201. public override int IndexOf(object value)
  202. {
  203. MySqlParameter parameter = value as MySqlParameter;
  204. if (null == parameter)
  205. throw new ArgumentException("Argument must be of type DbParameter", "value");
  206. return items.IndexOf(parameter);
  207. }
  208. internal void ParameterNameChanged(MySqlParameter p, string oldName, string newName)
  209. {
  210. int index = IndexOf(oldName);
  211. indexHashCS.Remove(oldName);
  212. indexHashCI.Remove(oldName);
  213. indexHashCS.Add(newName, index);
  214. indexHashCI.Add(newName, index);
  215. }
  216. private MySqlParameter InternalAdd(MySqlParameter value, int index)
  217. {
  218. if (value == null)
  219. throw new ArgumentException("The MySqlParameterCollection only accepts non-null MySqlParameter type objects.", "value");
  220. // if the parameter is unnamed, then assign a default name
  221. if (String.IsNullOrEmpty(value.ParameterName))
  222. value.ParameterName = String.Format("Parameter{0}", GetNextIndex());
  223. // make sure we don't already have a parameter with this name
  224. if (IndexOf(value.ParameterName) >= 0)
  225. {
  226. throw new MySqlException(
  227. String.Format(Resources.ParameterAlreadyDefined, value.ParameterName));
  228. }
  229. else
  230. {
  231. string inComingName = value.ParameterName;
  232. if (inComingName[0] == '@' || inComingName[0] == '?')
  233. inComingName = inComingName.Substring(1, inComingName.Length - 1);
  234. if (IndexOf(inComingName) >= 0)
  235. throw new MySqlException(
  236. String.Format(Resources.ParameterAlreadyDefined, value.ParameterName));
  237. }
  238. if (index == -1)
  239. {
  240. items.Add(value);
  241. index = items.Count - 1;
  242. }
  243. else
  244. {
  245. items.Insert(index, value);
  246. AdjustHashes(index, true);
  247. }
  248. indexHashCS.Add(value.ParameterName, index);
  249. indexHashCI.Add(value.ParameterName, index);
  250. value.Collection = this;
  251. return value;
  252. }
  253. private int GetNextIndex()
  254. {
  255. int index = Count+1;
  256. while (true)
  257. {
  258. string name = "Parameter" + index.ToString();
  259. if (!indexHashCI.ContainsKey(name)) break;
  260. index++;
  261. }
  262. return index;
  263. }
  264. private static void AdjustHash(Dictionary<string,int> hash, string parameterName, int keyIndex, bool addEntry)
  265. {
  266. if (!hash.ContainsKey(parameterName)) return;
  267. int index = (int)hash[parameterName];
  268. if (index < keyIndex) return;
  269. hash[parameterName] = addEntry ? ++index : --index;
  270. }
  271. /// <summary>
  272. /// This method will update all the items in the index hashes when
  273. /// we insert a parameter somewhere in the middle
  274. /// </summary>
  275. /// <param name="keyIndex"></param>
  276. /// <param name="addEntry"></param>
  277. private void AdjustHashes(int keyIndex, bool addEntry)
  278. {
  279. for (int i = 0; i < Count; i++)
  280. {
  281. string name = (items[i] as MySqlParameter).ParameterName;
  282. AdjustHash(indexHashCI, name, keyIndex, addEntry);
  283. AdjustHash(indexHashCS, name, keyIndex, addEntry);
  284. }
  285. }
  286. private MySqlParameter GetParameterFlexibleInternal(string baseName)
  287. {
  288. int index = IndexOf(baseName);
  289. if (-1 == index)
  290. index = IndexOf("?" + baseName);
  291. if (-1 == index)
  292. index = IndexOf("@" + baseName);
  293. if (-1 != index)
  294. return this[index];
  295. return null;
  296. }
  297. internal MySqlParameter GetParameterFlexible(string parameterName, bool throwOnNotFound)
  298. {
  299. string baseName = parameterName;
  300. MySqlParameter p = GetParameterFlexibleInternal(baseName);
  301. if (p != null) return p;
  302. if (parameterName.StartsWith("@", StringComparison.Ordinal) || parameterName.StartsWith("?", StringComparison.Ordinal))
  303. baseName = parameterName.Substring(1);
  304. p = GetParameterFlexibleInternal(baseName);
  305. if (p != null) return p;
  306. if (throwOnNotFound)
  307. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  308. return null;
  309. }
  310. }
  311. }
  312. #endif