OdbcParameterCollection.cs 13 KB

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