PageRenderTime 45ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data/System.Data.SqlClient/SqlParameterCollection.cs

https://bitbucket.org/danipen/mono
C# | 454 lines | 355 code | 60 blank | 39 comment | 22 complexity | 624797d39c9e55825c94b95d7618d3c1 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Data.SqlClient.SqlParameterCollection.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya (rodrigo@ximian.com)
  6. // Daniel Morgan (danmorg@sc.rr.com)
  7. // Tim Coleman (tim@timcoleman.com)
  8. // Diego Caravana (diego@toth.it)
  9. // Umadevi S (sumadevi@novell.com)
  10. //
  11. // (C) Ximian, Inc 2002
  12. // Copyright (C) Tim Coleman, 2002
  13. //
  14. //
  15. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. using System;
  37. using System.Collections;
  38. using System.ComponentModel;
  39. using System.Data;
  40. using System.Data.Common;
  41. using Mono.Data.Tds;
  42. namespace System.Data.SqlClient
  43. {
  44. [ListBindable (false)]
  45. [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  46. #if NET_2_0
  47. public sealed class SqlParameterCollection : DbParameterCollection, IDataParameterCollection, IList, ICollection, IEnumerable
  48. #else
  49. public sealed class SqlParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
  50. #endif // NET_2_0
  51. {
  52. #region Fields
  53. ArrayList list = new ArrayList();
  54. TdsMetaParameterCollection metaParameters;
  55. SqlCommand command;
  56. #endregion // Fields
  57. #region Constructors
  58. internal SqlParameterCollection (SqlCommand command)
  59. {
  60. this.command = command;
  61. metaParameters = new TdsMetaParameterCollection ();
  62. }
  63. #endregion // Constructors
  64. #region Properties
  65. #if ONLY_1_1 || ONLY_1_0
  66. [Browsable (false)]
  67. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  68. #endif
  69. public
  70. #if NET_2_0
  71. override
  72. #endif // NET_2_0
  73. int Count {
  74. get { return list.Count; }
  75. }
  76. #if NET_2_0
  77. public override bool IsFixedSize {
  78. get {
  79. return list.IsFixedSize;
  80. }
  81. }
  82. public override bool IsReadOnly {
  83. get {
  84. return list.IsReadOnly;
  85. }
  86. }
  87. public override bool IsSynchronized {
  88. get {
  89. return list.IsSynchronized;
  90. }
  91. }
  92. public override object SyncRoot {
  93. get {
  94. return list.SyncRoot;
  95. }
  96. }
  97. #else
  98. object IList.this [int index] {
  99. get { return (SqlParameter) this [index]; }
  100. set { this [index] = (SqlParameter) value; }
  101. }
  102. bool IList.IsFixedSize {
  103. get { return list.IsFixedSize; }
  104. }
  105. bool IList.IsReadOnly {
  106. get { return list.IsReadOnly; }
  107. }
  108. bool ICollection.IsSynchronized {
  109. get { return list.IsSynchronized; }
  110. }
  111. object ICollection.SyncRoot {
  112. get { return list.SyncRoot; }
  113. }
  114. object IDataParameterCollection.this [string index] {
  115. get { return this [index]; }
  116. set {
  117. if (!(value is SqlParameter))
  118. throw new InvalidCastException ("Only SQLParameter objects can be used.");
  119. this [index] = (SqlParameter) value;
  120. }
  121. }
  122. #endif
  123. [Browsable (false)]
  124. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  125. public
  126. #if NET_2_0
  127. new
  128. #endif // NET_2_0
  129. SqlParameter this [int index] {
  130. get {
  131. if (index < 0 || index >= list.Count)
  132. throw new IndexOutOfRangeException ("The specified index is out of range.");
  133. return (SqlParameter) list [index];
  134. }
  135. set {
  136. if (index < 0 || index >= list.Count)
  137. throw new IndexOutOfRangeException ("The specified index is out of range.");
  138. list [index] = (SqlParameter) value;
  139. }
  140. }
  141. [Browsable (false)]
  142. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  143. public
  144. #if NET_2_0
  145. new
  146. #endif // NET_2_0
  147. SqlParameter this [string parameterName] {
  148. get {
  149. foreach (SqlParameter p in list)
  150. if (p.ParameterName.Equals (parameterName))
  151. return p;
  152. throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
  153. }
  154. set {
  155. if (!Contains (parameterName))
  156. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  157. this [IndexOf (parameterName)] = value;
  158. }
  159. }
  160. #if NET_2_0
  161. protected override DbParameter GetParameter (int index)
  162. {
  163. return this [index];
  164. }
  165. protected override DbParameter GetParameter (string parameterName)
  166. {
  167. return this [parameterName];
  168. }
  169. protected override void SetParameter (int index, DbParameter value)
  170. {
  171. this [index] = (SqlParameter) value;
  172. }
  173. protected override void SetParameter (string parameterName, DbParameter value)
  174. {
  175. this [parameterName] = (SqlParameter) value;
  176. }
  177. #endif
  178. internal TdsMetaParameterCollection MetaParameters {
  179. get { return metaParameters; }
  180. }
  181. #endregion // Properties
  182. #region Methods
  183. #if NET_2_0
  184. [EditorBrowsable (EditorBrowsableState.Never)]
  185. #endif // NET_2_0
  186. public
  187. #if NET_2_0
  188. override
  189. #endif // NET_2_0
  190. int Add (object value)
  191. {
  192. if (!(value is SqlParameter))
  193. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  194. Add ((SqlParameter) value);
  195. return IndexOf (value);
  196. }
  197. public SqlParameter Add (SqlParameter value)
  198. {
  199. if (value.Container != null)
  200. throw new ArgumentException ("The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.");
  201. value.Container = this;
  202. list.Add (value);
  203. metaParameters.Add (value.MetaParameter);
  204. return value;
  205. }
  206. #if NET_2_0
  207. [EditorBrowsable (EditorBrowsableState.Never)]
  208. [Obsolete ("Do not call this method.")]
  209. #endif // NET_2_0
  210. public SqlParameter Add (string parameterName, object value)
  211. {
  212. return Add (new SqlParameter (parameterName, value));
  213. }
  214. #if NET_2_0
  215. public SqlParameter AddWithValue (string parameterName, object value)
  216. {
  217. return Add (new SqlParameter (parameterName, value));
  218. }
  219. #endif // NET_2_0
  220. public SqlParameter Add (string parameterName, SqlDbType sqlDbType)
  221. {
  222. return Add (new SqlParameter (parameterName, sqlDbType));
  223. }
  224. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size)
  225. {
  226. return Add (new SqlParameter (parameterName, sqlDbType, size));
  227. }
  228. public SqlParameter Add (string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  229. {
  230. return Add (new SqlParameter (parameterName, sqlDbType, size, sourceColumn));
  231. }
  232. public
  233. #if NET_2_0
  234. override
  235. #endif // NET_2_0
  236. void Clear()
  237. {
  238. metaParameters.Clear ();
  239. foreach (SqlParameter p in list)
  240. p.Container = null;
  241. list.Clear ();
  242. }
  243. public
  244. #if NET_2_0
  245. override
  246. #endif // NET_2_0
  247. bool Contains (object value)
  248. {
  249. if (!(value is SqlParameter))
  250. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  251. return Contains (((SqlParameter) value).ParameterName);
  252. }
  253. public
  254. #if NET_2_0
  255. override
  256. #endif // NET_2_0
  257. bool Contains (string value)
  258. {
  259. foreach (SqlParameter p in list)
  260. if (p.ParameterName.Equals (value))
  261. return true;
  262. return false;
  263. }
  264. #if NET_2_0
  265. public bool Contains (SqlParameter value)
  266. {
  267. return (this.IndexOf(value) != -1);
  268. }
  269. #endif // NET_2_0
  270. public
  271. #if NET_2_0
  272. override
  273. #endif // NET_2_0
  274. void CopyTo (Array array, int index)
  275. {
  276. list.CopyTo (array, index);
  277. }
  278. public
  279. #if NET_2_0
  280. override
  281. #endif // NET_2_0
  282. IEnumerator GetEnumerator()
  283. {
  284. return list.GetEnumerator ();
  285. }
  286. public
  287. #if NET_2_0
  288. override
  289. #endif // NET_2_0
  290. int IndexOf (object value)
  291. {
  292. if (!(value is SqlParameter))
  293. throw new InvalidCastException ("The parameter was not an SqlParameter.");
  294. return IndexOf (((SqlParameter) value).ParameterName);
  295. }
  296. public
  297. #if NET_2_0
  298. override
  299. #endif // NET_2_0
  300. int IndexOf (string parameterName)
  301. {
  302. for (int i = 0; i < Count; i += 1)
  303. if (this [i].ParameterName.Equals (parameterName))
  304. return i;
  305. return -1;
  306. }
  307. #if NET_2_0
  308. public int IndexOf (SqlParameter value)
  309. {
  310. return list.IndexOf(value);
  311. }
  312. #endif // NET_2_0
  313. public
  314. #if NET_2_0
  315. override
  316. #endif // NET_2_0
  317. void Insert (int index, object value)
  318. {
  319. list.Insert (index, value);
  320. }
  321. #if NET_2_0
  322. public void Insert (int index, SqlParameter value)
  323. {
  324. list.Insert (index,value);
  325. }
  326. #endif //NET_2_0
  327. public
  328. #if NET_2_0
  329. override
  330. #endif // NET_2_0
  331. void Remove (object value)
  332. {
  333. //TODO : this neds validation to check if the object is a
  334. // sqlparameter.
  335. ((SqlParameter) value).Container = null;
  336. metaParameters.Remove (((SqlParameter) value).MetaParameter);
  337. list.Remove (value);
  338. }
  339. #if NET_2_0
  340. public void Remove (SqlParameter value)
  341. {
  342. //both this and the above code are the same. but need to work with
  343. // 1.1!
  344. value.Container = null;
  345. metaParameters.Remove (value.MetaParameter);
  346. list.Remove (value);
  347. }
  348. #endif //NET_2_0
  349. public
  350. #if NET_2_0
  351. override
  352. #endif // NET_2_0
  353. void RemoveAt (int index)
  354. {
  355. this [index].Container = null;
  356. metaParameters.RemoveAt (index);
  357. list.RemoveAt (index);
  358. }
  359. public
  360. #if NET_2_0
  361. override
  362. #endif // NET_2_0
  363. void RemoveAt (string parameterName)
  364. {
  365. RemoveAt (IndexOf (parameterName));
  366. }
  367. #if NET_2_0
  368. public override void AddRange (Array values)
  369. {
  370. if (values == null)
  371. throw new ArgumentNullException("The argument passed was null");
  372. foreach (object value in values) {
  373. if (!(value is SqlParameter))
  374. throw new InvalidCastException ("Element in the array parameter was not an SqlParameter.");
  375. SqlParameter param = (SqlParameter) value;
  376. if (param.Container != null)
  377. throw new ArgumentException ("An SqlParameter specified in the array is already added to this or another SqlParameterCollection.");
  378. param.Container = this;
  379. list.Add (param);
  380. metaParameters.Add (param.MetaParameter);
  381. }
  382. }
  383. public void AddRange (SqlParameter[] values)
  384. {
  385. AddRange((Array) values);
  386. }
  387. public void CopyTo (SqlParameter[] array, int index)
  388. {
  389. list.CopyTo (array, index);
  390. }
  391. #endif
  392. #endregion // Methods
  393. }
  394. }