1 //------------------------------------------------------------------------------
2 // <copyright file="DbParameterCollectionBase.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
11 using System.Collections.Generic;
12 using System.ComponentModel;
14 using System.Data.Common;
15 using System.Data.ProviderBase;
16 using System.Diagnostics;
17 using System.Globalization;
18 using System.Runtime.InteropServices;
20 public sealed partial class PARAMETERCOLLECTIONOBJECTNAME : DbParameterCollection {
21 private List<PARAMETEROBJECTNAME> _items; // the collection of parameters
23 override public int Count {
25 // NOTE: we don't construct the list just to get the count.
26 return ((null != _items) ? _items.Count : 0);
30 private List<PARAMETEROBJECTNAME> InnerList {
32 List<PARAMETEROBJECTNAME> items = _items;
35 items = new List<PARAMETEROBJECTNAME>();
42 override public bool IsFixedSize {
44 return ((System.Collections.IList)InnerList).IsFixedSize;
48 override public bool IsReadOnly {
50 return ((System.Collections.IList)InnerList).IsReadOnly;
54 override public bool IsSynchronized {
56 return ((System.Collections.ICollection)InnerList).IsSynchronized;
60 override public object SyncRoot {
62 return ((System.Collections.ICollection)InnerList).SyncRoot;
67 EditorBrowsableAttribute(EditorBrowsableState.Never)
69 override public int Add(object value) {
70 OnChange(); // fire event before value is validated
73 InnerList.Add((PARAMETEROBJECTNAME)value);
77 override public void AddRange(System.Array values) {
78 OnChange(); // fire event before value is validated
80 throw ADP.ArgumentNull("values");
82 foreach(object value in values) {
85 foreach(PARAMETEROBJECTNAME value in values) {
87 InnerList.Add((PARAMETEROBJECTNAME)value);
91 private int CheckName(string parameterName) {
92 int index = IndexOf(parameterName);
94 throw ADP.ParametersSourceIndex(parameterName, this, ItemType);
99 override public void Clear() {
100 OnChange(); // fire event before value is validated
101 List<PARAMETEROBJECTNAME> items = InnerList;
104 foreach(PARAMETEROBJECTNAME item in items) {
111 override public bool Contains(object value) {
112 return (-1 != IndexOf(value));
115 override public void CopyTo(Array array, int index) {
116 ((System.Collections.ICollection)InnerList).CopyTo(array, index);
119 override public System.Collections.IEnumerator GetEnumerator() {
120 return ((System.Collections.ICollection)InnerList).GetEnumerator();
123 override protected DbParameter GetParameter(int index) {
125 return InnerList[index];
128 override protected DbParameter GetParameter(string parameterName) {
129 int index = IndexOf(parameterName);
131 throw ADP.ParametersSourceIndex(parameterName, this, ItemType);
133 return InnerList[index];
136 private static int IndexOf(System.Collections.IEnumerable items, string parameterName) {
139 // first case, kana, width sensitive search
140 foreach(PARAMETEROBJECTNAME parameter in items) {
141 if (0 == ADP.SrcCompare(parameterName, parameter.ParameterName)) {
147 // then insensitive search
148 foreach(PARAMETEROBJECTNAME parameter in items) {
149 if (0 == ADP.DstCompare(parameterName, parameter.ParameterName)) {
158 override public int IndexOf(string parameterName) {
159 return IndexOf(InnerList, parameterName);
162 override public int IndexOf(object value) {
166 List<PARAMETEROBJECTNAME> items = InnerList;
169 int count = items.Count;
171 for (int i = 0; i < count; i++) {
172 if (value == items[i]) {
181 override public void Insert(int index, object value) {
182 OnChange(); // fire event before value is validated
184 Validate(-1, (PARAMETEROBJECTNAME)value);
185 InnerList.Insert(index, (PARAMETEROBJECTNAME)value);
188 private void RangeCheck(int index) {
189 if ((index < 0) || (Count <= index)) {
190 throw ADP.ParametersMappingIndex(index, this);
194 override public void Remove(object value) {
195 OnChange(); // fire event before value is validated
197 int index = IndexOf(value);
201 else if (this != ((PARAMETEROBJECTNAME)value).CompareExchangeParent(null, this)) {
202 throw ADP.CollectionRemoveInvalidObject(ItemType, this);
206 override public void RemoveAt(int index) {
207 OnChange(); // fire event before value is validated
212 override public void RemoveAt(string parameterName) {
213 OnChange(); // fire event before value is validated
214 int index = CheckName(parameterName);
218 private void RemoveIndex(int index) {
219 List<PARAMETEROBJECTNAME> items = InnerList;
220 Debug.Assert((null != items) && (0 <= index) && (index < Count), "RemoveIndex, invalid");
221 PARAMETEROBJECTNAME item = items[index];
222 items.RemoveAt(index);
226 private void Replace(int index, object newValue) {
227 List<PARAMETEROBJECTNAME> items = InnerList;
228 Debug.Assert((null != items) && (0 <= index) && (index < Count), "Replace Index invalid");
229 ValidateType(newValue);
230 Validate(index, newValue);
231 PARAMETEROBJECTNAME item = items[index];
232 items[index] = (PARAMETEROBJECTNAME)newValue;
236 override protected void SetParameter(int index, DbParameter value) {
237 OnChange(); // fire event before value is validated
239 Replace(index, value);
242 override protected void SetParameter(string parameterName, DbParameter value) {
243 OnChange(); // fire event before value is validated
244 int index = IndexOf(parameterName);
246 throw ADP.ParametersSourceIndex(parameterName, this, ItemType);
248 Replace(index, value);
251 private void Validate(int index, object value) {
253 throw ADP.ParameterNull("value", this, ItemType);
255 // Validate assigns the parent - remove clears the parent
256 object parent = ((PARAMETEROBJECTNAME)value).CompareExchangeParent(this, null);
257 if (null != parent) {
258 if (this != parent) {
259 throw ADP.ParametersIsNotParent(ItemType, this);
261 if (index != IndexOf(value)) {
262 throw ADP.ParametersIsParent(ItemType, this);
265 // generate a ParameterName
266 String name = ((PARAMETEROBJECTNAME)value).ParameterName;
267 if (0 == name.Length) {
270 name = ADP.Parameter + index.ToString(CultureInfo.CurrentCulture);
272 } while (-1 != IndexOf(name));
273 ((PARAMETEROBJECTNAME)value).ParameterName = name;
277 private void ValidateType(object value) {
279 throw ADP.ParameterNull("value", this, ItemType);
281 else if (!ItemType.IsInstanceOfType(value)) {
282 throw ADP.InvalidParameterType(this, ItemType, value);