OdbcParameterCollection.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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.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. public sealed class OdbcParameterCollection : DbParameterCollection
  43. {
  44. #region Fields
  45. readonly ArrayList list = new ArrayList ();
  46. int nullParamCount = 1;
  47. #endregion // Fields
  48. #region Constructors
  49. internal OdbcParameterCollection ()
  50. {
  51. }
  52. #endregion // Constructors
  53. #region Properties
  54. public
  55. override
  56. int Count {
  57. get { return list.Count; }
  58. }
  59. [Browsable (false)]
  60. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  61. public new OdbcParameter this [int index] {
  62. get { return (OdbcParameter) list [index]; }
  63. set { list [index] = value; }
  64. }
  65. [Browsable (false)]
  66. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  67. public new OdbcParameter this [string parameterName] {
  68. get {
  69. foreach (OdbcParameter p in list)
  70. if (p.ParameterName.Equals (parameterName))
  71. return p;
  72. throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
  73. }
  74. set {
  75. if (!Contains (parameterName))
  76. throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
  77. this [IndexOf (parameterName)] = value;
  78. }
  79. }
  80. public override bool IsFixedSize {
  81. get { return false; }
  82. }
  83. public override bool IsReadOnly {
  84. get { return false; }
  85. }
  86. public override bool IsSynchronized {
  87. get { return list.IsSynchronized; }
  88. }
  89. public override object SyncRoot {
  90. get { return list.SyncRoot; }
  91. }
  92. #endregion // Properties
  93. #region Methods
  94. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  95. public
  96. override
  97. int Add (object value)
  98. {
  99. if (!(value is OdbcParameter))
  100. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  101. Add ((OdbcParameter) value);
  102. return IndexOf (value);
  103. }
  104. public OdbcParameter Add (OdbcParameter value)
  105. {
  106. if (value.Container != null)
  107. throw new ArgumentException ("The OdbcParameter specified in " +
  108. "the value parameter is already " +
  109. "added to this or another OdbcParameterCollection.");
  110. if (value.ParameterName == null || value.ParameterName.Length == 0) {
  111. value.ParameterName = "Parameter" + nullParamCount;
  112. nullParamCount++;
  113. }
  114. value.Container = this;
  115. list.Add (value);
  116. return value;
  117. }
  118. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  119. [Obsolete ("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value).")]
  120. public OdbcParameter Add (string parameterName, object value)
  121. {
  122. return Add (new OdbcParameter (parameterName, value));
  123. }
  124. public OdbcParameter Add (string parameterName, OdbcType odbcType)
  125. {
  126. return Add (new OdbcParameter (parameterName, odbcType));
  127. }
  128. public OdbcParameter Add (string parameterName, OdbcType odbcType, int size)
  129. {
  130. return Add (new OdbcParameter (parameterName, odbcType, size));
  131. }
  132. public OdbcParameter Add (string parameterName, OdbcType odbcType,
  133. int size, string sourceColumn)
  134. {
  135. return Add (new OdbcParameter (parameterName, odbcType,
  136. size, sourceColumn));
  137. }
  138. public
  139. override
  140. void Clear()
  141. {
  142. foreach (OdbcParameter p in list)
  143. p.Container = null;
  144. list.Clear ();
  145. }
  146. public
  147. override
  148. bool Contains (object value)
  149. {
  150. if (value == null)
  151. //should not throw ArgumentNullException
  152. return false;
  153. if (!(value is OdbcParameter))
  154. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  155. return Contains (((OdbcParameter) value).ParameterName);
  156. }
  157. public
  158. override
  159. bool Contains (string value)
  160. {
  161. if (value == null || value.Length == 0)
  162. //should not throw ArgumentNullException
  163. return false;
  164. string value_upper = value.ToUpper ();
  165. foreach (OdbcParameter p in this)
  166. if (p.ParameterName.ToUpper ().Equals (value_upper))
  167. return true;
  168. return false;
  169. }
  170. public
  171. override
  172. void CopyTo (Array array, int index)
  173. {
  174. list.CopyTo (array, index);
  175. }
  176. public
  177. override
  178. IEnumerator GetEnumerator()
  179. {
  180. return list.GetEnumerator ();
  181. }
  182. public
  183. override
  184. int IndexOf (object value)
  185. {
  186. if (value == null)
  187. return -1;
  188. if (!(value is OdbcParameter))
  189. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  190. return list.IndexOf (value);
  191. }
  192. public
  193. override
  194. int IndexOf (string parameterName)
  195. {
  196. if (parameterName == null || parameterName.Length == 0)
  197. return -1;
  198. string parameterName_upper = parameterName.ToUpper ();
  199. for (int i = 0; i < Count; i += 1)
  200. if (this [i].ParameterName.ToUpper ().Equals (parameterName_upper))
  201. return i;
  202. return -1;
  203. }
  204. public
  205. override
  206. void Insert (int index, object value)
  207. {
  208. if (value == null)
  209. throw new ArgumentNullException ("value");
  210. if (!(value is OdbcParameter))
  211. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  212. Insert (index, (OdbcParameter) value);
  213. }
  214. public
  215. override
  216. void Remove (object value)
  217. {
  218. if (value == null)
  219. throw new ArgumentNullException ("value");
  220. if (!(value is OdbcParameter))
  221. throw new InvalidCastException ("The parameter was not an OdbcParameter.");
  222. Remove ((OdbcParameter) value);
  223. }
  224. public
  225. override
  226. void RemoveAt (int index)
  227. {
  228. if (index >= list.Count || index < 0)
  229. throw new IndexOutOfRangeException (String.Format ("Invalid index {0} for this OdbcParameterCollection with count = {1}", index, list.Count));
  230. this [index].Container = null;
  231. list.RemoveAt (index);
  232. }
  233. public
  234. override
  235. void RemoveAt (string parameterName)
  236. {
  237. RemoveAt (IndexOf (parameterName));
  238. }
  239. protected override DbParameter GetParameter (string parameterName)
  240. {
  241. return this [parameterName];
  242. }
  243. protected override DbParameter GetParameter (int index)
  244. {
  245. return this [index];
  246. }
  247. protected override void SetParameter (string parameterName, DbParameter value)
  248. {
  249. this [parameterName] = (OdbcParameter) value;
  250. }
  251. protected override void SetParameter (int index, DbParameter value)
  252. {
  253. this [index] = (OdbcParameter) value;
  254. }
  255. public override void AddRange (Array values)
  256. {
  257. if (values == null)
  258. throw new ArgumentNullException ("values");
  259. foreach (OdbcParameter p in values)
  260. if (p == null)
  261. throw new ArgumentNullException ("values", "The OdbcParameterCollection only accepts non-null OdbcParameter type objects");
  262. // no need to check if parameter is already contained
  263. foreach (OdbcParameter p in values)
  264. Add (p);
  265. }
  266. public void AddRange (OdbcParameter [] values)
  267. {
  268. AddRange ((Array) values);
  269. }
  270. public void Insert (int index, OdbcParameter value)
  271. {
  272. if (index > list.Count || index < 0)
  273. throw new ArgumentOutOfRangeException ("index", "The index must be non-negative and less than or equal to size of the collection");
  274. if (value == null)
  275. throw new ArgumentNullException ("value");
  276. if (value.Container != null)
  277. throw new ArgumentException ("The OdbcParameter is already contained by another collection");
  278. if (String.IsNullOrEmpty (value.ParameterName)) {
  279. value.ParameterName = "Parameter" + nullParamCount;
  280. nullParamCount++;
  281. }
  282. value.Container = this;
  283. list.Insert (index, value);
  284. }
  285. public OdbcParameter AddWithValue (string parameterName, Object value)
  286. {
  287. if (value == null)
  288. return Add (new OdbcParameter (parameterName, OdbcType.NVarChar));
  289. return Add (new OdbcParameter (parameterName, value));
  290. }
  291. public void Remove (OdbcParameter value)
  292. {
  293. if (value == null)
  294. throw new ArgumentNullException ("value");
  295. if (value.Container != this)
  296. throw new ArgumentException ("values", "Attempted to remove an OdbcParameter that is not contained in this OdbcParameterCollection");
  297. value.Container = null;
  298. list.Remove (value);
  299. }
  300. public bool Contains (OdbcParameter value)
  301. {
  302. if (value == null)
  303. //should not throw ArgumentNullException
  304. return false;
  305. if (value.Container != this)
  306. return false;
  307. return Contains (value.ParameterName);
  308. }
  309. public int IndexOf (OdbcParameter value)
  310. {
  311. if (value == null)
  312. //should not throw ArgumentNullException
  313. return -1;
  314. return IndexOf ((Object) value);
  315. }
  316. public void CopyTo (OdbcParameter [] array, int index)
  317. {
  318. list.CopyTo (array, index);
  319. }
  320. #endregion // Methods
  321. }
  322. }