PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Data/System.Data.Odbc/OdbcParameterCollection.cs

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