DbParameterCollectionHelper.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DbParameterCollectionBase.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. //------------------------------------------------------------------------------
  7. namespace NAMESPACE
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Data.Common;
  14. using System.Data.ProviderBase;
  15. using System.Diagnostics;
  16. using System.Globalization;
  17. using System.Runtime.InteropServices;
  18. public sealed partial class PARAMETERCOLLECTIONOBJECTNAME : DbParameterCollection {
  19. private List<PARAMETEROBJECTNAME> _items; // the collection of parameters
  20. override public int Count {
  21. get {
  22. // NOTE: we don't construct the list just to get the count.
  23. return ((null != _items) ? _items.Count : 0);
  24. }
  25. }
  26. private List<PARAMETEROBJECTNAME> InnerList {
  27. get {
  28. List<PARAMETEROBJECTNAME> items = _items;
  29. if (null == items) {
  30. items = new List<PARAMETEROBJECTNAME>();
  31. _items = items;
  32. }
  33. return items;
  34. }
  35. }
  36. override public bool IsFixedSize {
  37. get {
  38. return ((System.Collections.IList)InnerList).IsFixedSize;
  39. }
  40. }
  41. override public bool IsReadOnly {
  42. get {
  43. return ((System.Collections.IList)InnerList).IsReadOnly;
  44. }
  45. }
  46. override public bool IsSynchronized {
  47. get {
  48. return ((System.Collections.ICollection)InnerList).IsSynchronized;
  49. }
  50. }
  51. override public object SyncRoot {
  52. get {
  53. return ((System.Collections.ICollection)InnerList).SyncRoot;
  54. }
  55. }
  56. [
  57. EditorBrowsableAttribute(EditorBrowsableState.Never)
  58. ]
  59. override public int Add(object value) {
  60. OnChange(); // fire event before value is validated
  61. ValidateType(value);
  62. Validate(-1, value);
  63. InnerList.Add((PARAMETEROBJECTNAME)value);
  64. return Count-1;
  65. }
  66. override public void AddRange(System.Array values) {
  67. OnChange(); // fire event before value is validated
  68. if (null == values) {
  69. throw ADP.ArgumentNull("values");
  70. }
  71. foreach(object value in values) {
  72. ValidateType(value);
  73. }
  74. foreach(PARAMETEROBJECTNAME value in values) {
  75. Validate(-1, value);
  76. InnerList.Add((PARAMETEROBJECTNAME)value);
  77. }
  78. }
  79. private int CheckName(string parameterName) {
  80. int index = IndexOf(parameterName);
  81. if (index < 0) {
  82. throw ADP.ParametersSourceIndex(parameterName, this, ItemType);
  83. }
  84. return index;
  85. }
  86. override public void Clear() {
  87. OnChange(); // fire event before value is validated
  88. List<PARAMETEROBJECTNAME> items = InnerList;
  89. if (null != items) {
  90. foreach(PARAMETEROBJECTNAME item in items) {
  91. item.ResetParent();
  92. }
  93. items.Clear();
  94. }
  95. }
  96. override public bool Contains(object value) {
  97. return (-1 != IndexOf(value));
  98. }
  99. override public void CopyTo(Array array, int index) {
  100. ((System.Collections.ICollection)InnerList).CopyTo(array, index);
  101. }
  102. override public System.Collections.IEnumerator GetEnumerator() {
  103. return ((System.Collections.ICollection)InnerList).GetEnumerator();
  104. }
  105. override protected DbParameter GetParameter(int index) {
  106. RangeCheck(index);
  107. return InnerList[index];
  108. }
  109. override protected DbParameter GetParameter(string parameterName) {
  110. int index = IndexOf(parameterName);
  111. if (index < 0) {
  112. throw ADP.ParametersSourceIndex(parameterName, this, ItemType);
  113. }
  114. return InnerList[index];
  115. }
  116. private static int IndexOf(System.Collections.IEnumerable items, string parameterName) {
  117. if (null != items) {
  118. int i = 0;
  119. // first case, kana, width sensitive search
  120. foreach(PARAMETEROBJECTNAME parameter in items) {
  121. if (0 == ADP.SrcCompare(parameterName, parameter.ParameterName)) {
  122. return i;
  123. }
  124. ++i;
  125. }
  126. i = 0;
  127. // then insensitive search
  128. foreach(PARAMETEROBJECTNAME parameter in items) {
  129. if (0 == ADP.DstCompare(parameterName, parameter.ParameterName)) {
  130. return i;
  131. }
  132. ++i;
  133. }
  134. }
  135. return -1;
  136. }
  137. override public int IndexOf(string parameterName) {
  138. return IndexOf(InnerList, parameterName);
  139. }
  140. override public int IndexOf(object value) {
  141. if (null != value) {
  142. ValidateType(value);
  143. List<PARAMETEROBJECTNAME> items = InnerList;
  144. if (null != items) {
  145. int count = items.Count;
  146. for (int i = 0; i < count; i++) {
  147. if (value == items[i]) {
  148. return i;
  149. }
  150. }
  151. }
  152. }
  153. return -1;
  154. }
  155. override public void Insert(int index, object value) {
  156. OnChange(); // fire event before value is validated
  157. ValidateType(value);
  158. Validate(-1, (PARAMETEROBJECTNAME)value);
  159. InnerList.Insert(index, (PARAMETEROBJECTNAME)value);
  160. }
  161. private void RangeCheck(int index) {
  162. if ((index < 0) || (Count <= index)) {
  163. throw ADP.ParametersMappingIndex(index, this);
  164. }
  165. }
  166. override public void Remove(object value) {
  167. OnChange(); // fire event before value is validated
  168. ValidateType(value);
  169. int index = IndexOf(value);
  170. if (-1 != index) {
  171. RemoveIndex(index);
  172. }
  173. else if (this != ((PARAMETEROBJECTNAME)value).CompareExchangeParent(null, this)) {
  174. throw ADP.CollectionRemoveInvalidObject(ItemType, this);
  175. }
  176. }
  177. override public void RemoveAt(int index) {
  178. OnChange(); // fire event before value is validated
  179. RangeCheck(index);
  180. RemoveIndex(index);
  181. }
  182. override public void RemoveAt(string parameterName) {
  183. OnChange(); // fire event before value is validated
  184. int index = CheckName(parameterName);
  185. RemoveIndex(index);
  186. }
  187. private void RemoveIndex(int index) {
  188. List<PARAMETEROBJECTNAME> items = InnerList;
  189. Debug.Assert((null != items) && (0 <= index) && (index < Count), "RemoveIndex, invalid");
  190. PARAMETEROBJECTNAME item = items[index];
  191. items.RemoveAt(index);
  192. item.ResetParent();
  193. }
  194. private void Replace(int index, object newValue) {
  195. List<PARAMETEROBJECTNAME> items = InnerList;
  196. Debug.Assert((null != items) && (0 <= index) && (index < Count), "Replace Index invalid");
  197. ValidateType(newValue);
  198. Validate(index, newValue);
  199. PARAMETEROBJECTNAME item = items[index];
  200. items[index] = (PARAMETEROBJECTNAME)newValue;
  201. item.ResetParent();
  202. }
  203. override protected void SetParameter(int index, DbParameter value) {
  204. OnChange(); // fire event before value is validated
  205. RangeCheck(index);
  206. Replace(index, value);
  207. }
  208. override protected void SetParameter(string parameterName, DbParameter value) {
  209. OnChange(); // fire event before value is validated
  210. int index = IndexOf(parameterName);
  211. if (index < 0) {
  212. throw ADP.ParametersSourceIndex(parameterName, this, ItemType);
  213. }
  214. Replace(index, value);
  215. }
  216. private void Validate(int index, object value) {
  217. if (null == value) {
  218. throw ADP.ParameterNull("value", this, ItemType);
  219. }
  220. // Validate assigns the parent - remove clears the parent
  221. object parent = ((PARAMETEROBJECTNAME)value).CompareExchangeParent(this, null);
  222. if (null != parent) {
  223. if (this != parent) {
  224. throw ADP.ParametersIsNotParent(ItemType, this);
  225. }
  226. if (index != IndexOf(value)) {
  227. throw ADP.ParametersIsParent(ItemType, this);
  228. }
  229. }
  230. // generate a ParameterName
  231. String name = ((PARAMETEROBJECTNAME)value).ParameterName;
  232. if (0 == name.Length) {
  233. index = 1;
  234. do {
  235. name = ADP.Parameter + index.ToString(CultureInfo.CurrentCulture);
  236. index++;
  237. } while (-1 != IndexOf(name));
  238. ((PARAMETEROBJECTNAME)value).ParameterName = name;
  239. }
  240. }
  241. private void ValidateType(object value) {
  242. if (null == value) {
  243. throw ADP.ParameterNull("value", this, ItemType);
  244. }
  245. else if (!ItemType.IsInstanceOfType(value)) {
  246. throw ADP.InvalidParameterType(this, ItemType, value);
  247. }
  248. }
  249. };
  250. }