OdbcParameterCollection.cs 13 KB

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