AbstractDbParameterCollection.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // System.Data.Common.AbstractDbParameterCollection
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Data.Common;
  32. using System.Collections;
  33. namespace System.Data.ProviderBase
  34. {
  35. public abstract class AbstractDbParameterCollection : DbParameterCollection
  36. {
  37. #region Fields
  38. readonly AbstractDbCommand _parent;
  39. readonly ArrayList _list = new ArrayList();
  40. #endregion // Fields
  41. #region Constructors
  42. public AbstractDbParameterCollection(DbCommand parent)
  43. {
  44. _parent = (AbstractDbCommand)parent;
  45. }
  46. #endregion // Constructors
  47. #region Properties
  48. public override int Count {
  49. get { return _list.Count; }
  50. }
  51. public override bool IsFixedSize {
  52. get { return _list.IsFixedSize; }
  53. }
  54. public override bool IsReadOnly {
  55. get { return _list.IsReadOnly; }
  56. }
  57. public override bool IsSynchronized {
  58. get { return _list.IsSynchronized; }
  59. }
  60. protected abstract Type ItemType { get; }
  61. public override object SyncRoot {
  62. get { return _list.SyncRoot; }
  63. }
  64. #endregion // Properties
  65. #region Methods
  66. public override int Add (object value) {
  67. Validate (-1, value);
  68. OnSchemaChanging();
  69. ((AbstractDbParameter)value).Parent = this;
  70. return _list.Add (value);
  71. }
  72. #if NET_2_0
  73. public override void AddRange (Array values)
  74. {
  75. foreach (object value in values)
  76. Add (value);
  77. }
  78. #endif
  79. public override void Clear () {
  80. OnSchemaChanging();
  81. if (_list != null && Count != 0) {
  82. for (int i = 0; i < _list.Count; i++) {
  83. ((AbstractDbParameter)_list [i]).Parent = null;
  84. }
  85. _list.Clear ();
  86. }
  87. }
  88. public override bool Contains (object value) {
  89. if (IndexOf (value) != -1)
  90. return true;
  91. else
  92. return false;
  93. }
  94. public override bool Contains (string value) {
  95. if (IndexOf (value) != -1)
  96. return true;
  97. else
  98. return false;
  99. }
  100. public override void CopyTo (Array array, int index) {
  101. _list.CopyTo (array, index);
  102. }
  103. public override IEnumerator GetEnumerator () {
  104. return _list.GetEnumerator ();
  105. }
  106. protected override DbParameter GetParameter (int index) {
  107. return (DbParameter) _list [index];
  108. }
  109. #if NET_2_0
  110. protected override DbParameter GetParameter (string parameterName) {
  111. return GetParameter (IndexOf (parameterName));
  112. }
  113. protected override void SetParameter (string parameterName, DbParameter value) {
  114. SetParameter (IndexOf (parameterName), value);
  115. }
  116. #endif
  117. public override int IndexOf (object value) {
  118. ValidateType (value);
  119. return _list.IndexOf (value);
  120. }
  121. public override int IndexOf (string parameterName) {
  122. if (_list == null)
  123. return -1;
  124. for (int i = 0; i < _list.Count; i++) {
  125. string name = ((DbParameter)_list [i]).ParameterName;
  126. if (name == parameterName) {
  127. return i;
  128. }
  129. }
  130. return -1;
  131. }
  132. public override void Insert (int index, object value) {
  133. Validate(-1, (DbParameter)value);
  134. OnSchemaChanging();
  135. ((AbstractDbParameter)value).Parent = this;
  136. _list.Insert (index, value);
  137. }
  138. public override void Remove (object value) {
  139. ValidateType (value);
  140. int index = IndexOf (value);
  141. RemoveIndex (index);
  142. }
  143. public override void RemoveAt (int index) {
  144. RemoveIndex (index);
  145. }
  146. public override void RemoveAt (string parameterName) {
  147. int index = IndexOf (parameterName);
  148. RemoveIndex (index);
  149. }
  150. protected override void SetParameter (int index, DbParameter value) {
  151. Replace (index, value);
  152. }
  153. void Validate (int index, object value) {
  154. ValidateType (value);
  155. AbstractDbParameter parameter = (AbstractDbParameter) value;
  156. if (parameter.Parent != null) {
  157. if (parameter.Parent.Equals (this)) {
  158. if (IndexOf (parameter) != index)
  159. throw ExceptionHelper.CollectionAlreadyContains (ItemType,"ParameterName",parameter.ParameterName,this);
  160. }
  161. else {
  162. // FIXME : The OleDbParameter with ParameterName 'MyParam2' is already contained by another OleDbParameterCollection.
  163. throw new ArgumentException ("");
  164. }
  165. }
  166. if (parameter.ParameterName == null || parameter.ParameterName == String.Empty) {
  167. int newIndex = 1;
  168. string parameterName;
  169. do {
  170. parameterName = "Parameter" + newIndex;
  171. newIndex++;
  172. }
  173. while(IndexOf (parameterName) != -1);
  174. parameter.ParameterName = parameterName;
  175. }
  176. }
  177. void ValidateType (object value) {
  178. if (value == null)
  179. throw ExceptionHelper.CollectionNoNullsAllowed (this,ItemType);
  180. Type objectType = value.GetType ();
  181. Type itemType = ItemType;
  182. if (itemType.IsInstanceOfType(objectType)) {
  183. Type thisType = this.GetType ();
  184. string err = String.Format ("The {0} only accepts non-null {1} type objects, not {2} objects.", thisType.Name, itemType.Name, objectType.Name);
  185. throw new InvalidCastException (err);
  186. }
  187. }
  188. private void RemoveIndex (int index) {
  189. OnSchemaChanging();
  190. AbstractDbParameter oldItem = (AbstractDbParameter)_list [index];
  191. oldItem.Parent = null;
  192. _list.RemoveAt (index);
  193. }
  194. private void Replace (int index, DbParameter value) {
  195. Validate (index, value);
  196. AbstractDbParameter oldItem = (AbstractDbParameter)this [index];
  197. oldItem.Parent = null;
  198. ((AbstractDbParameter)value).Parent = this;
  199. _list [index] = value;
  200. }
  201. protected internal void OnSchemaChanging()
  202. {
  203. if (_parent != null) {
  204. _parent.OnSchemaChanging();
  205. }
  206. }
  207. #endregion // Methods
  208. }
  209. }