DbParameterCollectionBase.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // System.Data.ProviderBase.DbParameterCollectionBase
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Boris Kirzner ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2003
  9. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  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. #if NET_2_0 || TARGET_JVM
  34. using System.Collections;
  35. using System.Data.Common;
  36. namespace System.Data.ProviderBase {
  37. public abstract class DbParameterBaseCollection : DbParameterCollection
  38. {
  39. #region Fields
  40. ArrayList _list;
  41. #endregion // Fields
  42. #region Constructors
  43. protected DbParameterBaseCollection ()
  44. {
  45. _list = new ArrayList ();
  46. }
  47. #endregion // Constructors
  48. #region Properties
  49. public override int Count {
  50. get { return _list.Count; }
  51. }
  52. public override bool IsFixedSize {
  53. get { return _list.IsFixedSize; }
  54. }
  55. public override bool IsReadOnly {
  56. get { return _list.IsReadOnly; }
  57. }
  58. public override bool IsSynchronized {
  59. get { return _list.IsSynchronized; }
  60. }
  61. protected abstract Type ItemType { get; }
  62. #if NET_2_0
  63. [MonoTODO]
  64. protected virtual string ParameterNamePrefix {
  65. get { throw new NotImplementedException (); }
  66. }
  67. #endif
  68. public override object SyncRoot {
  69. get { return _list.SyncRoot; }
  70. }
  71. #endregion // Properties
  72. #region Methods
  73. public override int Add (object value)
  74. {
  75. Validate (-1, value);
  76. ((DbParameterBase)value).Parent = this;
  77. return _list.Add (value);
  78. }
  79. #if NET_2_0
  80. public override void AddRange (Array values)
  81. {
  82. foreach (object value in values)
  83. Add (value);
  84. }
  85. [MonoTODO]
  86. protected override int CheckName (string parameterName)
  87. {
  88. throw new NotImplementedException ();
  89. }
  90. #endif
  91. public override void Clear ()
  92. {
  93. if (_list != null && Count != 0) {
  94. for (int i = 0; i < _list.Count; i++) {
  95. ((DbParameterBase)_list [i]).Parent = null;
  96. }
  97. _list.Clear ();
  98. }
  99. }
  100. public override bool Contains (object value)
  101. {
  102. if (IndexOf (value) != -1)
  103. return true;
  104. else
  105. return false;
  106. }
  107. public override bool Contains (string value)
  108. {
  109. if (IndexOf (value) != -1)
  110. return true;
  111. else
  112. return false;
  113. }
  114. public override void CopyTo (Array array, int index)
  115. {
  116. _list.CopyTo (array, index);
  117. }
  118. public override IEnumerator GetEnumerator ()
  119. {
  120. return _list.GetEnumerator ();
  121. }
  122. protected override DbParameter GetParameter (int index)
  123. {
  124. return (DbParameter) _list [index];
  125. }
  126. public override int IndexOf (object value)
  127. {
  128. ValidateType (value);
  129. return _list.IndexOf (value);
  130. }
  131. public override int IndexOf (string parameterName)
  132. {
  133. if (_list == null)
  134. return -1;
  135. for (int i = 0; i < _list.Count; i++) {
  136. string name = ((DbParameterBase)_list [i]).ParameterName;
  137. if (name == parameterName) {
  138. return i;
  139. }
  140. }
  141. return -1;
  142. }
  143. #if NET_2_0
  144. [MonoTODO]
  145. protected internal static int IndexOf (IEnumerable items, string parameterName)
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. #endif
  150. public override void Insert (int index, object value)
  151. {
  152. Validate(-1, (DbParameterBase)value);
  153. ((DbParameterBase)value).Parent = this;
  154. _list.Insert (index, value);
  155. }
  156. #if NET_2_0
  157. [MonoTODO]
  158. protected virtual void OnChange ()
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. #endif
  163. public override void Remove (object value)
  164. {
  165. ValidateType (value);
  166. int index = IndexOf (value);
  167. RemoveIndex (index);
  168. }
  169. public override void RemoveAt (int index)
  170. {
  171. RemoveIndex (index);
  172. }
  173. public override void RemoveAt (string parameterName)
  174. {
  175. int index = IndexOf (parameterName);
  176. RemoveIndex (index);
  177. }
  178. protected override void SetParameter (int index, DbParameter value)
  179. {
  180. Replace (index, value);
  181. }
  182. protected virtual void Validate (int index, object value)
  183. {
  184. ValidateType (value);
  185. DbParameterBase parameter = (DbParameterBase) value;
  186. if (parameter.Parent != null) {
  187. if (parameter.Parent.Equals (this)) {
  188. if (IndexOf (parameter) != index)
  189. throw ExceptionHelper.CollectionAlreadyContains (ItemType,"ParameterName",parameter.ParameterName,this);
  190. }
  191. else {
  192. // FIXME : The OleDbParameter with ParameterName 'MyParam2' is already contained by another OleDbParameterCollection.
  193. throw new ArgumentException ("");
  194. }
  195. }
  196. if (parameter.ParameterName == null || parameter.ParameterName == String.Empty) {
  197. int newIndex = 1;
  198. string parameterName;
  199. do {
  200. parameterName = "Parameter" + newIndex;
  201. newIndex++;
  202. }
  203. while(IndexOf (parameterName) != -1);
  204. parameter.ParameterName = parameterName;
  205. }
  206. }
  207. protected virtual void ValidateType (object value)
  208. {
  209. if (value == null)
  210. throw ExceptionHelper.CollectionNoNullsAllowed (this,ItemType);
  211. Type objectType = value.GetType ();
  212. Type itemType = ItemType;
  213. if (itemType.IsInstanceOfType(objectType)) {
  214. Type thisType = this.GetType ();
  215. string err = String.Format ("The {0} only accepts non-null {1} type objects, not {2} objects.", thisType.Name, itemType.Name, objectType.Name);
  216. throw new InvalidCastException (err);
  217. }
  218. }
  219. private void RemoveIndex (int index)
  220. {
  221. DbParameterBase oldItem = (DbParameterBase)_list [index];
  222. oldItem.Parent = null;
  223. _list.RemoveAt (index);
  224. }
  225. private void Replace (int index, DbParameter value)
  226. {
  227. Validate (index, value);
  228. DbParameterBase oldItem = (DbParameterBase)this [index];
  229. oldItem.Parent = null;
  230. ((DbParameterBase)value).Parent = this;
  231. _list [index] = value;
  232. }
  233. #endregion // Methods
  234. }
  235. }
  236. #endif