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.

523 lines
21 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2004, 2016 Oracle and/or its affiliates. All rights reserved.
  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.Data.Common;
  25. using System.Collections.Generic;
  26. using System.Collections;
  27. namespace Externals.MySql.Data.MySqlClient
  28. {
  29. /// <summary>
  30. /// 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.
  31. /// </summary>
  32. internal sealed partial class MySqlParameterCollection : DbParameterCollection
  33. {
  34. readonly List<MySqlParameter> _items = new List<MySqlParameter>();
  35. private readonly Dictionary<string, int> _indexHashCs;
  36. private readonly Dictionary<string, int> _indexHashCi;
  37. //turns to true if any parameter is unnamed
  38. internal bool containsUnnamedParameters;
  39. internal MySqlParameterCollection(MySqlCommand cmd)
  40. {
  41. _indexHashCs = new Dictionary<string, int>();
  42. _indexHashCi = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
  43. containsUnnamedParameters = false;
  44. Clear();
  45. }
  46. /// <summary>
  47. /// Gets the number of MySqlParameter objects in the collection.
  48. /// </summary>
  49. public override int Count => _items.Count;
  50. #region Public Methods
  51. /// <summary>
  52. /// Gets the <see cref="MySqlParameter"/> at the specified index.
  53. /// </summary>
  54. /// <overloads>Gets the <see cref="MySqlParameter"/> with a specified attribute.
  55. /// [C#] In C#, this property is the indexer for the <see cref="MySqlParameterCollection"/> class.
  56. /// </overloads>
  57. public new MySqlParameter this[int index]
  58. {
  59. get { return InternalGetParameter(index); }
  60. set { InternalSetParameter(index, value); }
  61. }
  62. /// <summary>
  63. /// Gets the <see cref="MySqlParameter"/> with the specified name.
  64. /// </summary>
  65. public new MySqlParameter this[string name]
  66. {
  67. get { return InternalGetParameter(name); }
  68. set { InternalSetParameter(name, value); }
  69. }
  70. /// <summary>
  71. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, the column length, and the source column name.
  72. /// </summary>
  73. /// <param name="parameterName">The name of the parameter.</param>
  74. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  75. /// <param name="size">The length of the column.</param>
  76. /// <param name="sourceColumn">The name of the source column.</param>
  77. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  78. public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size, string sourceColumn)
  79. {
  80. return Add(new MySqlParameter(parameterName, dbType, size, sourceColumn));
  81. }
  82. /// <summary>
  83. /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
  84. /// </summary>
  85. /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
  86. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  87. public MySqlParameter Add(MySqlParameter value)
  88. {
  89. return InternalAdd(value, -1);
  90. }
  91. /// <summary>
  92. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
  93. /// </summary>
  94. /// <param name="parameterName">The name of the parameter.</param>
  95. /// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
  96. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  97. [Obsolete("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value)")]
  98. public MySqlParameter Add(string parameterName, object value)
  99. {
  100. return Add(new MySqlParameter(parameterName, value));
  101. }
  102. /// <summary>
  103. /// Adds a parameter and its value.
  104. /// </summary>
  105. /// <param name="parameterName">The name of the parameter.</param>
  106. /// <param name="value">The value of the parameter.</param>
  107. /// <returns>A <see cref="MySqlParameter"/> object representing the provided values.</returns>
  108. public MySqlParameter AddWithValue(string parameterName, object value)
  109. {
  110. return Add(new MySqlParameter(parameterName, value));
  111. }
  112. /// <summary>
  113. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the parameter name and the data type.
  114. /// </summary>
  115. /// <param name="parameterName">The name of the parameter.</param>
  116. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  117. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  118. public MySqlParameter Add(string parameterName, MySqlDbType dbType)
  119. {
  120. return Add(new MySqlParameter(parameterName, dbType));
  121. }
  122. /// <summary>
  123. /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, and the column length.
  124. /// </summary>
  125. /// <param name="parameterName">The name of the parameter.</param>
  126. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  127. /// <param name="size">The length of the column.</param>
  128. /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
  129. public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size)
  130. {
  131. return Add(new MySqlParameter(parameterName, dbType, size));
  132. }
  133. #endregion
  134. /// <summary>
  135. /// Removes all items from the collection.
  136. /// </summary>
  137. public override void Clear()
  138. {
  139. foreach (MySqlParameter p in _items)
  140. p.Collection = null;
  141. _items.Clear();
  142. _indexHashCs.Clear();
  143. _indexHashCi.Clear();
  144. }
  145. void CheckIndex(int index)
  146. {
  147. if (index < 0 || index >= Count)
  148. throw new IndexOutOfRangeException("Parameter index is out of range.");
  149. }
  150. private MySqlParameter InternalGetParameter(int index)
  151. {
  152. CheckIndex(index);
  153. return _items[index];
  154. }
  155. private MySqlParameter InternalGetParameter(string parameterName)
  156. {
  157. int index = IndexOf(parameterName);
  158. if (index < 0)
  159. {
  160. // check to see if the user has added the parameter without a
  161. // parameter marker. If so, kindly tell them what they did.
  162. if (parameterName.StartsWith("@", StringComparison.Ordinal) ||
  163. parameterName.StartsWith("?", StringComparison.Ordinal))
  164. {
  165. string newParameterName = parameterName.Substring(1);
  166. index = IndexOf(newParameterName);
  167. if (index != -1)
  168. return _items[index];
  169. }
  170. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  171. }
  172. return _items[index];
  173. }
  174. private void InternalSetParameter(string parameterName, MySqlParameter value)
  175. {
  176. int index = IndexOf(parameterName);
  177. if (index < 0)
  178. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  179. InternalSetParameter(index, value);
  180. }
  181. private void InternalSetParameter(int index, MySqlParameter value)
  182. {
  183. MySqlParameter newParameter = value;
  184. if (newParameter == null)
  185. throw new ArgumentException(Resources.NewValueShouldBeMySqlParameter);
  186. CheckIndex(index);
  187. MySqlParameter p = _items[index];
  188. // first we remove the old parameter from our hashes
  189. _indexHashCs.Remove(p.ParameterName);
  190. _indexHashCi.Remove(p.ParameterName);
  191. // then we add in the new parameter
  192. _items[index] = newParameter;
  193. _indexHashCs.Add(value.ParameterName, index);
  194. _indexHashCi.Add(value.ParameterName, index);
  195. }
  196. /// <summary>
  197. /// Gets the location of the <see cref="MySqlParameter"/> in the collection with a specific parameter name.
  198. /// </summary>
  199. /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
  200. /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
  201. public override int IndexOf(string parameterName)
  202. {
  203. int i = -1;
  204. if (!_indexHashCs.TryGetValue(parameterName, out i) &&
  205. !_indexHashCi.TryGetValue(parameterName, out i))
  206. return -1;
  207. return i;
  208. }
  209. /// <summary>
  210. /// Gets the location of a <see cref="MySqlParameter"/> in the collection.
  211. /// </summary>
  212. /// <param name="value">The <see cref="MySqlParameter"/> object to locate. </param>
  213. /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
  214. /// <overloads>Gets the location of a <see cref="MySqlParameter"/> in the collection.</overloads>
  215. public override int IndexOf(object value)
  216. {
  217. MySqlParameter parameter = value as MySqlParameter;
  218. if (null == parameter)
  219. throw new ArgumentException("Argument must be of type DbParameter", "value");
  220. return _items.IndexOf(parameter);
  221. }
  222. internal void ParameterNameChanged(MySqlParameter p, string oldName, string newName)
  223. {
  224. int index = IndexOf(oldName);
  225. _indexHashCs.Remove(oldName);
  226. _indexHashCi.Remove(oldName);
  227. _indexHashCs.Add(newName, index);
  228. _indexHashCi.Add(newName, index);
  229. }
  230. private MySqlParameter InternalAdd(MySqlParameter value, int index)
  231. {
  232. if (value == null)
  233. throw new ArgumentException("The MySqlParameterCollection only accepts non-null MySqlParameter type objects.", "value");
  234. // if the parameter is unnamed, then assign a default name
  235. if (String.IsNullOrEmpty(value.ParameterName))
  236. value.ParameterName = String.Format("Parameter{0}", GetNextIndex());
  237. // make sure we don't already have a parameter with this name
  238. if (IndexOf(value.ParameterName) >= 0)
  239. {
  240. throw new MySqlException(
  241. String.Format(Resources.ParameterAlreadyDefined, value.ParameterName));
  242. }
  243. else
  244. {
  245. string inComingName = value.ParameterName;
  246. if (inComingName[0] == '@' || inComingName[0] == '?')
  247. inComingName = inComingName.Substring(1, inComingName.Length - 1);
  248. if (IndexOf(inComingName) >= 0)
  249. throw new MySqlException(
  250. String.Format(Resources.ParameterAlreadyDefined, value.ParameterName));
  251. }
  252. if (index == -1)
  253. {
  254. _items.Add(value);
  255. index = _items.Count - 1;
  256. }
  257. else
  258. {
  259. _items.Insert(index, value);
  260. AdjustHashes(index, true);
  261. }
  262. _indexHashCs.Add(value.ParameterName, index);
  263. _indexHashCi.Add(value.ParameterName, index);
  264. value.Collection = this;
  265. return value;
  266. }
  267. private int GetNextIndex()
  268. {
  269. int index = Count + 1;
  270. while (true)
  271. {
  272. string name = "Parameter" + index.ToString();
  273. if (!_indexHashCi.ContainsKey(name)) break;
  274. index++;
  275. }
  276. return index;
  277. }
  278. private static void AdjustHash(Dictionary<string, int> hash, string parameterName, int keyIndex, bool addEntry)
  279. {
  280. if (!hash.ContainsKey(parameterName)) return;
  281. int index = hash[parameterName];
  282. if (index < keyIndex) return;
  283. hash[parameterName] = addEntry ? ++index : --index;
  284. }
  285. /// <summary>
  286. /// This method will update all the items in the index hashes when
  287. /// we insert a parameter somewhere in the middle
  288. /// </summary>
  289. /// <param name="keyIndex"></param>
  290. /// <param name="addEntry"></param>
  291. private void AdjustHashes(int keyIndex, bool addEntry)
  292. {
  293. for (int i = 0; i < Count; i++)
  294. {
  295. string name = _items[i].ParameterName;
  296. AdjustHash(_indexHashCi, name, keyIndex, addEntry);
  297. AdjustHash(_indexHashCs, name, keyIndex, addEntry);
  298. }
  299. }
  300. private MySqlParameter GetParameterFlexibleInternal(string baseName)
  301. {
  302. int index = IndexOf(baseName);
  303. if (-1 == index)
  304. index = IndexOf("?" + baseName);
  305. if (-1 == index)
  306. index = IndexOf("@" + baseName);
  307. if (-1 != index)
  308. return this[index];
  309. return null;
  310. }
  311. internal MySqlParameter GetParameterFlexible(string parameterName, bool throwOnNotFound)
  312. {
  313. string baseName = parameterName;
  314. MySqlParameter p = GetParameterFlexibleInternal(baseName);
  315. if (p != null) return p;
  316. if (parameterName.StartsWith("@", StringComparison.Ordinal) || parameterName.StartsWith("?", StringComparison.Ordinal))
  317. baseName = parameterName.Substring(1);
  318. p = GetParameterFlexibleInternal(baseName);
  319. if (p != null) return p;
  320. if (throwOnNotFound)
  321. throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
  322. return null;
  323. }
  324. #region DbParameterCollection Implementation
  325. /// <summary>
  326. /// Adds an array of values to the end of the <see cref="MySqlParameterCollection"/>.
  327. /// </summary>
  328. /// <param name="values"></param>
  329. public override void AddRange(Array values)
  330. {
  331. foreach (DbParameter p in values)
  332. Add(p);
  333. }
  334. /// <summary>
  335. /// Retrieve the parameter with the given name.
  336. /// </summary>
  337. /// <param name="parameterName"></param>
  338. /// <returns></returns>
  339. protected override DbParameter GetParameter(string parameterName)
  340. {
  341. return InternalGetParameter(parameterName);
  342. }
  343. protected override DbParameter GetParameter(int index)
  344. {
  345. return InternalGetParameter(index);
  346. }
  347. protected override void SetParameter(string parameterName, DbParameter value)
  348. {
  349. InternalSetParameter(parameterName, value as MySqlParameter);
  350. }
  351. protected override void SetParameter(int index, DbParameter value)
  352. {
  353. InternalSetParameter(index, value as MySqlParameter);
  354. }
  355. /// <summary>
  356. /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
  357. /// </summary>
  358. /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
  359. /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
  360. public override int Add(object value)
  361. {
  362. MySqlParameter parameter = value as MySqlParameter;
  363. if (parameter == null)
  364. throw new MySqlException("Only MySqlParameter objects may be stored");
  365. parameter = Add(parameter);
  366. return IndexOf(parameter);
  367. }
  368. /// <summary>
  369. /// Gets a value indicating whether a <see cref="MySqlParameter"/> with the specified parameter name exists in the collection.
  370. /// </summary>
  371. /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to find.</param>
  372. /// <returns>true if the collection contains the parameter; otherwise, false.</returns>
  373. public override bool Contains(string parameterName)
  374. {
  375. return IndexOf(parameterName) != -1;
  376. }
  377. /// <summary>
  378. /// Gets a value indicating whether a MySqlParameter exists in the collection.
  379. /// </summary>
  380. /// <param name="value">The value of the <see cref="MySqlParameter"/> object to find. </param>
  381. /// <returns>true if the collection contains the <see cref="MySqlParameter"/> object; otherwise, false.</returns>
  382. /// <overloads>Gets a value indicating whether a <see cref="MySqlParameter"/> exists in the collection.</overloads>
  383. public override bool Contains(object value)
  384. {
  385. MySqlParameter parameter = value as MySqlParameter;
  386. if (null == parameter)
  387. throw new ArgumentException("Argument must be of type DbParameter", nameof(value));
  388. return _items.Contains(parameter);
  389. }
  390. /// <summary>
  391. /// Copies MySqlParameter objects from the MySqlParameterCollection to the specified array.
  392. /// </summary>
  393. /// <param name="array"></param>
  394. /// <param name="index"></param>
  395. public override void CopyTo(Array array, int index)
  396. {
  397. _items.ToArray().CopyTo(array, index);
  398. }
  399. /// <summary>
  400. /// Returns an enumerator that iterates through the <see cref="MySqlParameterCollection"/>.
  401. /// </summary>
  402. /// <returns></returns>
  403. public override IEnumerator GetEnumerator()
  404. {
  405. return _items.GetEnumerator();
  406. }
  407. /// <summary>
  408. /// Inserts a MySqlParameter into the collection at the specified index.
  409. /// </summary>
  410. /// <param name="index"></param>
  411. /// <param name="value"></param>
  412. public override void Insert(int index, object value)
  413. {
  414. MySqlParameter parameter = value as MySqlParameter;
  415. if (parameter == null)
  416. throw new MySqlException("Only MySqlParameter objects may be stored");
  417. InternalAdd(parameter, index);
  418. }
  419. /// <summary>
  420. /// Removes the specified MySqlParameter from the collection.
  421. /// </summary>
  422. /// <param name="value"></param>
  423. public override void Remove(object value)
  424. {
  425. MySqlParameter p = (value as MySqlParameter);
  426. p.Collection = null;
  427. int index = IndexOf(p);
  428. _items.Remove(p);
  429. _indexHashCs.Remove(p.ParameterName);
  430. _indexHashCi.Remove(p.ParameterName);
  431. AdjustHashes(index, false);
  432. }
  433. /// <summary>
  434. /// Removes the specified <see cref="MySqlParameter"/> from the collection using the parameter name.
  435. /// </summary>
  436. /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
  437. public override void RemoveAt(string parameterName)
  438. {
  439. DbParameter p = GetParameter(parameterName);
  440. Remove(p);
  441. }
  442. /// <summary>
  443. /// Removes the specified <see cref="MySqlParameter"/> from the collection using a specific index.
  444. /// </summary>
  445. /// <param name="index">The zero-based index of the parameter. </param>
  446. /// <overloads>Removes the specified <see cref="MySqlParameter"/> from the collection.</overloads>
  447. public override void RemoveAt(int index)
  448. {
  449. object o = _items[index];
  450. Remove(o);
  451. }
  452. /// <summary>
  453. /// Gets an object that can be used to synchronize access to the
  454. /// <see cref="MySqlParameterCollection"/>.
  455. /// </summary>
  456. public override object SyncRoot => (_items as IList).SyncRoot;
  457. #endregion
  458. }
  459. }
  460. #endif