OdbcParameterCollection.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. //
  2. // System.Data.Odbc.OdbcParameterCollection
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. // Umadevi S ([email protected])
  7. // Amit Biswas ([email protected])
  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.Data;
  36. using System.ComponentModel;
  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. ArrayList list = new ArrayList ();
  51. int nullParamCount = 1;
  52. #endregion // Fields
  53. #region Constructors
  54. internal OdbcParameterCollection () {
  55. }
  56. #endregion // Constructors
  57. #region Properties
  58. #if ONLY_1_1
  59. [Browsable (false)]
  60. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  61. #endif
  62. public
  63. #if NET_2_0
  64. override
  65. #endif
  66. int Count {
  67. get { return list.Count; }
  68. }
  69. [Browsable (false)]
  70. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  71. public new OdbcParameter this [int index] {
  72. get { return (OdbcParameter) list[index]; }
  73. set { list[index] = value; }
  74. }
  75. [Browsable (false)]
  76. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  77. public new OdbcParameter this [string parameterName] {
  78. get {
  79. foreach (OdbcParameter p in list)
  80. if (p.ParameterName.Equals (parameterName))
  81. return p;
  82. throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
  83. }
  84. set {
  85. if (!Contains (parameterName))
  86. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  87. this [IndexOf (parameterName)] = value;
  88. }
  89. }
  90. #if ONLY_1_1
  91. bool IList.IsFixedSize {
  92. #else
  93. public override bool IsFixedSize {
  94. #endif
  95. get { return false; }
  96. }
  97. #if ONLY_1_1
  98. bool IList.IsReadOnly {
  99. #else
  100. public override bool IsReadOnly {
  101. #endif
  102. get { return false; }
  103. }
  104. #if ONLY_1_1
  105. bool ICollection.IsSynchronized {
  106. #else
  107. public override bool IsSynchronized {
  108. #endif
  109. get { return list.IsSynchronized; }
  110. }
  111. #if ONLY_1_1
  112. object ICollection.SyncRoot {
  113. #else
  114. public override object SyncRoot {
  115. #endif
  116. get { return list.SyncRoot; }
  117. }
  118. #if ONLY_1_1
  119. object IList.this [int index] {
  120. get { return list [index]; }
  121. set { list [index] = value; }
  122. }
  123. object IDataParameterCollection.this [string name]
  124. {
  125. get { return this [name]; }
  126. set {
  127. if (!(value is OdbcParameter))
  128. throw new InvalidCastException ("Only OdbcParameter objects can be used.");
  129. this [name] = (OdbcParameter) value;
  130. }
  131. }
  132. #endif // ONLY_1_1
  133. #endregion // Properties
  134. #region Methods
  135. #if NET_2_0
  136. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  137. #endif
  138. public
  139. #if NET_2_0
  140. override
  141. #endif
  142. int Add (object value)
  143. {
  144. if (!(value is OdbcParameter))
  145. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  146. Add ((OdbcParameter) value);
  147. return IndexOf (value);
  148. }
  149. public OdbcParameter Add (OdbcParameter parameter)
  150. {
  151. if (parameter.Container != null)
  152. throw new ArgumentException ("The OdbcParameter specified in " +
  153. "the value parameter is already " +
  154. "added to this or another OdbcParameterCollection.");
  155. if (parameter.ParameterName == null || parameter.ParameterName == "") {
  156. parameter.ParameterName = "Parameter" + nullParamCount;
  157. nullParamCount ++;
  158. }
  159. parameter.Container = this;
  160. list.Add (parameter);
  161. return parameter;
  162. }
  163. #if NET_2_0
  164. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  165. [Obsolete ("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value).")]
  166. #endif
  167. public OdbcParameter Add (string name, object value)
  168. {
  169. return Add (new OdbcParameter (name, value));
  170. }
  171. public OdbcParameter Add (string name, OdbcType type)
  172. {
  173. return Add (new OdbcParameter (name, type));
  174. }
  175. public OdbcParameter Add (string name, OdbcType type, int width)
  176. {
  177. return Add (new OdbcParameter (name, type, width));
  178. }
  179. public OdbcParameter Add (string name, OdbcType type,
  180. int width, string src_col)
  181. {
  182. return Add (new OdbcParameter (name, type, width, src_col));
  183. }
  184. internal void Bind (IntPtr hstmt)
  185. {
  186. for (int i = 0; i < Count; i++)
  187. this [i].Bind (hstmt, i + 1);
  188. }
  189. public
  190. #if NET_2_0
  191. override
  192. #endif
  193. void Clear()
  194. {
  195. foreach (OdbcParameter p in list)
  196. p.Container = null;
  197. list.Clear ();
  198. }
  199. public
  200. #if NET_2_0
  201. override
  202. #endif // NET_2_0
  203. bool Contains (object value)
  204. {
  205. if (value == null)
  206. //should not throw ArgumentNullException
  207. return false;
  208. if (!(value is OdbcParameter))
  209. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  210. return Contains (((OdbcParameter) value).ParameterName);
  211. }
  212. public
  213. #if NET_2_0
  214. override
  215. #endif // NET_2_0
  216. bool Contains (string value)
  217. {
  218. if (value == null || value == "")
  219. //should not throw ArgumentNullException
  220. return false;
  221. string value_upper = value.ToUpper ();
  222. foreach (OdbcParameter p in this)
  223. if (p.ParameterName.ToUpper ().Equals (value_upper))
  224. return true;
  225. return false;
  226. }
  227. public
  228. #if NET_2_0
  229. override
  230. #endif // NET_2_0
  231. void CopyTo (Array array, int index)
  232. {
  233. list.CopyTo (array, index);
  234. }
  235. public
  236. #if NET_2_0
  237. override
  238. #endif // NET_2_0
  239. IEnumerator GetEnumerator()
  240. {
  241. return list.GetEnumerator ();
  242. }
  243. public
  244. #if NET_2_0
  245. override
  246. #endif // NET_2_0
  247. int IndexOf (object value)
  248. {
  249. if (value == null)
  250. return -1;
  251. if (!(value is OdbcParameter))
  252. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  253. return list.IndexOf (value);
  254. }
  255. public
  256. #if NET_2_0
  257. override
  258. #endif // NET_2_0
  259. int IndexOf (string parameterName)
  260. {
  261. if (parameterName == null || parameterName == "")
  262. return -1;
  263. string parameterName_upper = parameterName.ToUpper ();
  264. for (int i = 0; i < Count; i += 1)
  265. if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
  266. return i;
  267. return -1;
  268. }
  269. public
  270. #if NET_2_0
  271. override
  272. #endif // NET_2_0
  273. void Insert (int index, object value)
  274. {
  275. if (value == null)
  276. throw new ArgumentNullException ("value");
  277. if (!(value is OdbcParameter))
  278. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  279. Insert (index, (OdbcParameter) value);
  280. }
  281. public
  282. #if NET_2_0
  283. override
  284. #endif // NET_2_0
  285. void Remove (object value)
  286. {
  287. if (value == null)
  288. throw new ArgumentNullException ("value");
  289. if (!(value is OdbcParameter))
  290. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  291. Remove ((OdbcParameter) value);
  292. }
  293. public
  294. #if NET_2_0
  295. override
  296. #endif // NET_2_0
  297. void RemoveAt (int index)
  298. {
  299. if (index >= list.Count || index < 0)
  300. throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
  301. this [index].Container = null;
  302. list.RemoveAt (index);
  303. }
  304. public
  305. #if NET_2_0
  306. override
  307. #endif // NET_2_0
  308. void RemoveAt (string parameterName)
  309. {
  310. RemoveAt (IndexOf (parameterName));
  311. }
  312. #if NET_2_0
  313. protected override DbParameter GetParameter (string name)
  314. {
  315. return this [name];
  316. }
  317. protected override DbParameter GetParameter (int index)
  318. {
  319. return this [index];
  320. }
  321. protected override void SetParameter (string name, DbParameter value)
  322. {
  323. this [name] = (OdbcParameter) value;
  324. }
  325. protected override void SetParameter (int index, DbParameter value)
  326. {
  327. this [index] = (OdbcParameter) value;
  328. }
  329. public override void AddRange (Array values)
  330. {
  331. if (values == null)
  332. throw new ArgumentNullException ("values");
  333. foreach (OdbcParameter p in values) {
  334. if (p == null)
  335. throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
  336. }
  337. // no need to check if parameter is already contained
  338. foreach (OdbcParameter p in values)
  339. Add (p);
  340. }
  341. public void AddRange (OdbcParameter [] values)
  342. {
  343. AddRange ((Array)values);
  344. }
  345. public void Insert (int index, OdbcParameter value)
  346. {
  347. if (index > list.Count || index < 0)
  348. throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
  349. if (value == null)
  350. throw new ArgumentNullException ("value");
  351. if (value.Container != null)
  352. throw new ArgumentException ("The OdbcParameter is already contained by another collection");
  353. if (String.IsNullOrEmpty (value.ParameterName)) {
  354. value.ParameterName = "Parameter" + nullParamCount;
  355. nullParamCount ++;
  356. }
  357. value.Container = this;
  358. list.Insert (index, value);
  359. }
  360. public OdbcParameter AddWithValue (string parameterName, Object value)
  361. {
  362. if (value == null)
  363. return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
  364. return Add (new OdbcParameter (parameterName, value));
  365. }
  366. public void Remove (OdbcParameter value)
  367. {
  368. if (value == null)
  369. throw new ArgumentNullException ("value");
  370. if (value.Container != this)
  371. throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
  372. value.Container = null;
  373. list.Remove (value);
  374. }
  375. public bool Contains (OdbcParameter value)
  376. {
  377. if (value == null)
  378. //should not throw ArgumentNullException
  379. return false;
  380. if (value.Container != this)
  381. return false;
  382. return Contains (value.ParameterName);
  383. }
  384. public int IndexOf (OdbcParameter value)
  385. {
  386. if (value == null)
  387. //should not throw ArgumentNullException
  388. return -1;
  389. return IndexOf ((Object) value);
  390. }
  391. public void CopyTo (OdbcParameter [] array, int index)
  392. {
  393. list.CopyTo (array, index);
  394. }
  395. #endif
  396. #endregion // Methods
  397. }
  398. }