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