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