NpgsqlParameterCollection.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // created on 09/07/2003 at 20:20
  2. // Npgsql.NpgsqlParameterCollection.cs
  3. //
  4. // Author:
  5. // Brar Piening ([email protected])
  6. //
  7. // Rewritten from the scratch to derive from MarshalByRefObject instead of ArrayList.
  8. // Recycled some parts of the original NpgsqlParameterCollection.cs
  9. // by Francisco Jr. ([email protected])
  10. //
  11. // Copyright (C) 2002 The Npgsql Development Team
  12. // [email protected]
  13. // http://gborg.postgresql.org/project/npgsql/projdisplay.php
  14. //
  15. // This library is free software; you can redistribute it and/or
  16. // modify it under the terms of the GNU Lesser General Public
  17. // License as published by the Free Software Foundation; either
  18. // version 2.1 of the License, or (at your option) any later version.
  19. //
  20. // This library is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. // Lesser General Public License for more details.
  24. //
  25. // You should have received a copy of the GNU Lesser General Public
  26. // License along with this library; if not, write to the Free Software
  27. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. using System;
  29. using System.Reflection;
  30. using System.Data;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using Npgsql.Design;
  34. using NpgsqlTypes;
  35. namespace Npgsql
  36. {
  37. /// <summary>
  38. /// Represents a collection of parameters relevant to a <see cref="Npgsql.NpgsqlCommand">NpgsqlCommand</see>
  39. /// as well as their respective mappings to columns in a <see cref="System.Data.DataSet">DataSet</see>.
  40. /// This class cannot be inherited.
  41. /// </summary>
  42. [ListBindable(false)]
  43. // [Editor(typeof(NpgsqlParametersEditor), typeof(System.ComponentModel.Design.CollectionEditor))]
  44. [Editor(typeof(NpgsqlParametersEditor), typeof(System.Drawing.Design.UITypeEditor))]
  45. public sealed class NpgsqlParameterCollection : MarshalByRefObject, IDataParameterCollection
  46. {
  47. private ArrayList InternalList = new ArrayList();
  48. // Logging related value
  49. private static readonly String CLASSNAME = "NpgsqlParameterCollection";
  50. // Our resource manager
  51. private System.Resources.ResourceManager resman;
  52. /// <summary>
  53. /// Initializes a new instance of the NpgsqlParameterCollection class.
  54. /// </summary>
  55. internal NpgsqlParameterCollection()
  56. {
  57. this.resman = new System.Resources.ResourceManager(this.GetType());
  58. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
  59. }
  60. #region NpgsqlParameterCollection Member
  61. /// <summary>
  62. /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified name.
  63. /// </summary>
  64. /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
  65. /// <value>The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified name, or a null reference if the parameter is not found.</value>
  66. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  67. public NpgsqlParameter this[string parameterName] {
  68. get
  69. {
  70. NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
  71. return (NpgsqlParameter)this.InternalList[IndexOf(parameterName)];
  72. }
  73. set
  74. {
  75. NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
  76. this.InternalList[IndexOf(parameterName)] = value;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> at the specified index.
  81. /// </summary>
  82. /// <param name="index">The zero-based index of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
  83. /// <value>The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> at the specified index.</value>
  84. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  85. public NpgsqlParameter this[int index] {
  86. get
  87. {
  88. NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
  89. return (NpgsqlParameter)this.InternalList[index];
  90. }
  91. set
  92. {
  93. NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
  94. this.InternalList[index] = value;
  95. }
  96. }
  97. /// <summary>
  98. /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
  99. /// </summary>
  100. /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
  101. /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
  102. public NpgsqlParameter Add(NpgsqlParameter value)
  103. {
  104. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
  105. // Do not allow parameters without name.
  106. if (value.ParameterName == null) {
  107. throw new ArgumentNullException(String.Format(this.resman.GetString("Exception_InvalidParameterName"), value.ParameterName));
  108. }
  109. if (value.ParameterName.Trim() == String.Empty ||
  110. (value.ParameterName.Length == 1 && value.ParameterName[0] == ':'))
  111. throw new ArgumentOutOfRangeException(String.Format(this.resman.GetString("Exception_InvalidParameterName"), value.ParameterName));
  112. this.InternalList.Add(value);
  113. return value;
  114. }
  115. /// <summary>
  116. /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name and value.
  117. /// </summary>
  118. /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param>
  119. /// <param name="value">The Value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
  120. /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
  121. /// <remarks>
  122. /// Use caution when using this overload of the
  123. /// <b>Add</b> method to specify integer parameter values.
  124. /// Because this overload takes a <i>value</i> of type Object,
  125. /// you must convert the integral value to an <b>Object</b>
  126. /// type when the value is zero, as the following C# example demonstrates.
  127. /// <code>parameters.Add(":pname", Convert.ToInt32(0));</code>
  128. /// If you do not perform this conversion, the compiler will assume you
  129. /// are attempting to call the NpgsqlParameterCollection.Add(string, DbType) overload.
  130. /// </remarks>
  131. public NpgsqlParameter Add(string parameterName, object value)
  132. {
  133. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, value);
  134. return this.Add(new NpgsqlParameter(parameterName, value));
  135. }
  136. /// <summary>
  137. /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the parameter name and the data type.
  138. /// </summary>
  139. /// <param name="parameterName">The name of the parameter.</param>
  140. /// <param name="parameterType">One of the DbType values.</param>
  141. /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
  142. public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType)
  143. {
  144. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType);
  145. return this.Add(new NpgsqlParameter(parameterName, parameterType));
  146. }
  147. /// <summary>
  148. /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> with the parameter name, the data type, and the column length.
  149. /// </summary>
  150. /// <param name="parameterName">The name of the parameter.</param>
  151. /// <param name="parameterType">One of the DbType values.</param>
  152. /// <param name="size">The length of the column.</param>
  153. /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
  154. public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size)
  155. {
  156. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size);
  157. return this.Add(new NpgsqlParameter(parameterName, parameterType, size));
  158. }
  159. /// <summary>
  160. /// Adds a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> with the parameter name, the data type, the column length, and the source column name.
  161. /// </summary>
  162. /// <param name="parameterName">The name of the parameter.</param>
  163. /// <param name="parameterType">One of the DbType values.</param>
  164. /// <param name="size">The length of the column.</param>
  165. /// <param name="sourceColumn">The name of the source column.</param>
  166. /// <returns>The index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
  167. public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn)
  168. {
  169. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", parameterName, parameterType, size, sourceColumn);
  170. return this.Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn));
  171. }
  172. #endregion
  173. #region IDataParameterCollection Member
  174. object System.Data.IDataParameterCollection.this[string parameterName] {
  175. get
  176. {
  177. NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, parameterName);
  178. return this.InternalList[IndexOf(parameterName)];
  179. }
  180. set
  181. {
  182. NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, parameterName, value);
  183. CheckType(value);
  184. this.InternalList[IndexOf(parameterName)] = value;
  185. }
  186. }
  187. /// <summary>
  188. /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using the parameter name.
  189. /// </summary>
  190. /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to retrieve.</param>
  191. public void RemoveAt(string parameterName)
  192. {
  193. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", parameterName);
  194. this.InternalList.RemoveAt(IndexOf(parameterName));
  195. }
  196. /// <summary>
  197. /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
  198. /// </summary>
  199. /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
  200. /// <returns><b>true</b> if the collection contains the parameter; otherwise, <b>false</b>.</returns>
  201. public bool Contains(string parameterName)
  202. {
  203. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", parameterName);
  204. return (IndexOf(parameterName) != -1);
  205. }
  206. /// <summary>
  207. /// Gets the location of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection with a specific parameter name.
  208. /// </summary>
  209. /// <param name="parameterName">The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
  210. /// <returns>The zero-based location of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection.</returns>
  211. public int IndexOf(string parameterName)
  212. {
  213. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", parameterName);
  214. // Iterate values to see what is the index of parameter.
  215. Int32 index = 0;
  216. if ( (parameterName[0] != ':') && (parameterName[0] != '@') )
  217. parameterName = ':' + parameterName;
  218. foreach(NpgsqlParameter parameter in this)
  219. {
  220. if (parameter.ParameterName == parameterName)
  221. return index;
  222. index++;
  223. }
  224. return -1;
  225. }
  226. #endregion
  227. #region IList Member
  228. bool IList.IsReadOnly {
  229. get
  230. {
  231. NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsReadOnly");
  232. return this.InternalList.IsReadOnly;
  233. }
  234. }
  235. object System.Collections.IList.this[int index] {
  236. get
  237. {
  238. NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
  239. return (NpgsqlParameter)this.InternalList[index];
  240. }
  241. set
  242. {
  243. NpgsqlEventLog.LogIndexerSet(LogLevel.Debug, CLASSNAME, index, value);
  244. CheckType(value);
  245. this.InternalList[index] = value;
  246. }
  247. }
  248. /// <summary>
  249. /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection using a specific index.
  250. /// </summary>
  251. /// <param name="index">The zero-based index of the parameter.</param>
  252. public void RemoveAt(int index)
  253. {
  254. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "RemoveAt", index);
  255. this.InternalList.RemoveAt(index);
  256. }
  257. /// <summary>
  258. /// Inserts a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> into the collection at the specified index.
  259. /// </summary>
  260. /// <param name="index">The zero-based index where the parameter is to be inserted within the collection.</param>
  261. /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
  262. public void Insert(int index, object value)
  263. {
  264. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Insert", index, value);
  265. CheckType(value);
  266. this.InternalList.Insert(index, value);
  267. }
  268. /// <summary>
  269. /// Removes the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> from the collection.
  270. /// </summary>
  271. /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to remove from the collection.</param>
  272. public void Remove(object value)
  273. {
  274. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Remove", value);
  275. CheckType(value);
  276. this.InternalList.Remove(value);
  277. }
  278. /// <summary>
  279. /// Gets a value indicating whether a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> exists in the collection.
  280. /// </summary>
  281. /// <param name="value">The value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
  282. /// <returns>true if the collection contains the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object; otherwise, false.</returns>
  283. public bool Contains(object value)
  284. {
  285. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Contains", value);
  286. CheckType(value);
  287. return this.InternalList.Contains(value);
  288. }
  289. /// <summary>
  290. /// Removes all items from the collection.
  291. /// </summary>
  292. public void Clear()
  293. {
  294. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Clear");
  295. this.InternalList.Clear();
  296. }
  297. /// <summary>
  298. /// Gets the location of a <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> in the collection.
  299. /// </summary>
  300. /// <param name="value">The value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
  301. /// <returns>The zero-based index of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object in the collection.</returns>
  302. public int IndexOf(object value)
  303. {
  304. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IndexOf", value);
  305. CheckType(value);
  306. return this.InternalList.IndexOf(value);
  307. }
  308. /// <summary>
  309. /// Adds the specified <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
  310. /// </summary>
  311. /// <param name="value">The <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
  312. /// <returns>The zero-based index of the new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> object.</returns>
  313. public int Add(object value)
  314. {
  315. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Add", value);
  316. CheckType(value);
  317. return this.InternalList.Add(value);
  318. }
  319. bool IList.IsFixedSize {
  320. get
  321. {
  322. NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsFixedSize");
  323. return this.InternalList.IsFixedSize;
  324. }
  325. }
  326. #endregion
  327. #region ICollection Member
  328. bool ICollection.IsSynchronized {
  329. get
  330. {
  331. NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsSynchronized");
  332. return this.InternalList.IsSynchronized;
  333. }
  334. }
  335. /// <summary>
  336. /// Gets the number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.
  337. /// </summary>
  338. /// <value>The number of <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</value>
  339. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  340. public int Count {
  341. get
  342. {
  343. NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Count");
  344. return this.InternalList.Count;
  345. }
  346. }
  347. /// <summary>
  348. /// Copies <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects from the <see cref="Npgsql.NpgsqlParameterCollection">NpgsqlParameterCollection</see> to the specified array.
  349. /// </summary>
  350. /// <param name="array">An <see cref="System.Array">Array</see> to which to copy the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</param>
  351. /// <param name="index">The starting index of the array.</param>
  352. public void CopyTo(Array array, int index)
  353. {
  354. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CopyTo", array, index);
  355. this.InternalList.CopyTo(array, index);
  356. }
  357. object ICollection.SyncRoot {
  358. get
  359. {
  360. NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "SyncRoot");
  361. return this.InternalList.SyncRoot;
  362. }
  363. }
  364. #endregion
  365. #region IEnumerable Member
  366. /// <summary>
  367. /// Returns an enumerator that can iterate through the collection.
  368. /// </summary>
  369. /// <returns>An <see cref="System.Collections.IEnumerator">IEnumerator</see> that can be used to iterate through the collection.</returns>
  370. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  371. {
  372. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetEnumerator");
  373. return this.InternalList.GetEnumerator();
  374. }
  375. #endregion
  376. /// <summary>
  377. /// In methods taking an object as argument this method is used to verify
  378. /// that the argument has the type <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
  379. /// </summary>
  380. /// <param name="Object">The object to verify</param>
  381. private void CheckType(object Object)
  382. {
  383. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CheckType", Object);
  384. if(Object.GetType() != typeof(NpgsqlParameter))
  385. throw new InvalidCastException(String.Format(this.resman.GetString("Exception_WrongType"), Object.GetType().ToString()));
  386. }
  387. }
  388. }