1 //------------------------------------------------------------------------------
2 // <copyright file="DataTableMappingCollection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
9 namespace System.Data.Common {
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.ComponentModel;
16 using System.Diagnostics;
19 Editor("Microsoft.VSDesigner.Data.Design.DataTableMappingCollectionEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
22 public sealed class DataTableMappingCollection : MarshalByRefObject, ITableMappingCollection {
23 private List<DataTableMapping> items; // delay creation until AddWithoutEvents, Insert, CopyTo, GetEnumerator
25 public DataTableMappingCollection() {
28 // explicit ICollection implementation
29 bool System.Collections.ICollection.IsSynchronized {
32 object System.Collections.ICollection.SyncRoot {
36 // explicit IList implementation
37 bool System.Collections.IList.IsReadOnly {
40 bool System.Collections.IList.IsFixedSize {
43 object System.Collections.IList.this[int index] {
49 this[index] = (DataTableMapping) value;
53 object ITableMappingCollection.this[string index] {
59 this[index] = (DataTableMapping) value;
62 ITableMapping ITableMappingCollection.Add(string sourceTableName, string dataSetTableName) {
63 return Add(sourceTableName, dataSetTableName);
65 ITableMapping ITableMappingCollection.GetByDataSetTable(string dataSetTableName) {
66 return GetByDataSetTable(dataSetTableName);
71 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
72 ResDescriptionAttribute(Res.DataTableMappings_Count),
76 return ((null != items) ? items.Count : 0);
80 private Type ItemType {
81 get { return typeof(DataTableMapping); }
86 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
87 ResDescriptionAttribute(Res.DataTableMappings_Item),
89 public DataTableMapping this[int index] {
96 Replace(index, value);
102 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
103 ResDescriptionAttribute(Res.DataTableMappings_Item),
105 public DataTableMapping this[string sourceTable] {
107 int index = RangeCheck(sourceTable);
111 int index = RangeCheck(sourceTable);
112 Replace(index, value);
116 public int Add(object value) {
118 Add((DataTableMapping) value);
122 private DataTableMapping Add(DataTableMapping value) {
123 AddWithoutEvents(value);
127 public void AddRange(DataTableMapping[] values) { // V1.0.3300
128 AddEnumerableRange(values, false);
131 public void AddRange(System.Array values) { // V1.2.3300
132 AddEnumerableRange(values, false);
135 private void AddEnumerableRange(IEnumerable values, bool doClone) {
136 if (null == values) {
137 throw ADP.ArgumentNull("values");
139 foreach(object value in values) {
143 foreach(ICloneable value in values) {
144 AddWithoutEvents(value.Clone() as DataTableMapping);
148 foreach(DataTableMapping value in values) {
149 AddWithoutEvents(value);
154 /*/// <include file='doc\DataTableMappingCollection.uex' path='docs/doc[@for="DataTableMappingCollection.AddCloneOfRange"]/*' />
155 public void AddCloneOfRange(IEnumerable values) {
156 AddEnumerableRange(values, true);
159 public DataTableMapping Add(string sourceTable, string dataSetTable) {
160 return Add(new DataTableMapping(sourceTable, dataSetTable));
163 private void AddWithoutEvents(DataTableMapping value) {
166 ArrayList().Add(value);
169 // implemented as a method, not as a property because the VS7 debugger
170 // object browser calls properties to display their value, and we want this delayed
171 private List<DataTableMapping> ArrayList() {
172 if (null == this.items) {
173 this.items = new List<DataTableMapping>();
178 public void Clear() {
180 ClearWithoutEvents();
184 private void ClearWithoutEvents() {
186 foreach(DataTableMapping item in items) {
193 public bool Contains(string value) {
194 return (-1 != IndexOf(value));
197 public bool Contains(object value) {
198 return (-1 != IndexOf(value));
201 public void CopyTo(Array array, int index) {
202 ((ICollection)ArrayList()).CopyTo(array, index);
205 public void CopyTo(DataTableMapping[] array, int index) {
206 ArrayList().CopyTo(array, index);
209 public DataTableMapping GetByDataSetTable(string dataSetTable) {
210 int index = IndexOfDataSetTable(dataSetTable);
212 throw ADP.TablesDataSetTable(dataSetTable);
217 public IEnumerator GetEnumerator() {
218 return ArrayList().GetEnumerator();
221 public int IndexOf(object value) {
224 for (int i = 0; i < Count; ++i) {
225 if (items[i] == value) {
233 public int IndexOf(string sourceTable) {
234 if (!ADP.IsEmpty(sourceTable)) {
235 for (int i = 0; i < Count; ++i) {
236 string value = items[i].SourceTable;
237 if ((null != value) && (0 == ADP.SrcCompare(sourceTable, value))) {
245 public int IndexOfDataSetTable(string dataSetTable) {
246 if (!ADP.IsEmpty(dataSetTable)) {
247 for (int i = 0; i < Count; ++i) {
248 string value = items[i].DataSetTable;
249 if ((null != value) && (0 == ADP.DstCompare(dataSetTable, value))) {
257 public void Insert(int index, Object value) {
259 Insert(index, (DataTableMapping) value);
262 public void Insert(int index, DataTableMapping value) {
264 throw ADP.TablesAddNullAttempt("value");
268 ArrayList().Insert(index, value);
271 private void RangeCheck(int index) {
272 if ((index < 0) || (Count <= index)) {
273 throw ADP.TablesIndexInt32(index, this);
277 private int RangeCheck(string sourceTable) {
278 int index = IndexOf(sourceTable);
280 throw ADP.TablesSourceIndex(sourceTable);
285 public void RemoveAt(int index) {
290 public void RemoveAt(string sourceTable) {
291 int index = RangeCheck(sourceTable);
295 private void RemoveIndex(int index) {
296 Debug.Assert((null != items) && (0 <= index) && (index < Count), "RemoveIndex, invalid");
297 items[index].Parent = null;
298 items.RemoveAt(index);
301 public void Remove(object value) {
303 Remove((DataTableMapping) value);
306 public void Remove (DataTableMapping value) {
308 throw ADP.TablesAddNullAttempt ("value");
310 int index = IndexOf (value);
316 throw ADP.CollectionRemoveInvalidObject (ItemType, this);
320 private void Replace (int index, DataTableMapping newValue) {
321 Validate(index, newValue);
322 items[index].Parent = null;
323 newValue.Parent = this;
324 items[index] = newValue;
327 private void ValidateType(object value) {
329 throw ADP.TablesAddNullAttempt("value");
331 else if (!ItemType.IsInstanceOfType(value)) {
332 throw ADP.NotADataTableMapping(value);
336 private void Validate(int index, DataTableMapping value) {
338 throw ADP.TablesAddNullAttempt("value");
340 if (null != value.Parent) {
341 if (this != value.Parent) {
342 throw ADP.TablesIsNotParent(this);
344 else if (index != IndexOf(value)) {
345 throw ADP.TablesIsParent(this);
348 String name = value.SourceTable;
349 if (ADP.IsEmpty(name)) {
352 name = ADP.SourceTable + index.ToString(System.Globalization.CultureInfo.InvariantCulture);
354 } while (-1 != IndexOf(name));
355 value.SourceTable = name;
358 ValidateSourceTable(index, name);
362 internal void ValidateSourceTable(int index, string value) {
363 int pindex = IndexOf(value);
364 if ((-1 != pindex) && (index != pindex)) { // must be non-null and unique
365 throw ADP.TablesUniqueSourceTable(value);
369 [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
370 static public DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) {
371 if (null != tableMappings) {
372 int index = tableMappings.IndexOf(sourceTable);
375 if (AdapterSwitches.DataSchema.TraceWarning) {
376 Debug.WriteLine("mapping match on SourceTable \"" + sourceTable + "\"");
379 return tableMappings.items[index];
382 if (ADP.IsEmpty(sourceTable)) {
383 throw ADP.InvalidSourceTable("sourceTable");
385 switch (mappingAction) {
386 case MissingMappingAction.Passthrough:
388 if (AdapterSwitches.DataSchema.TraceInfo) {
389 Debug.WriteLine("mapping passthrough of SourceTable \"" + sourceTable + "\" -> \"" + dataSetTable + "\"");
392 return new DataTableMapping(sourceTable, dataSetTable);
394 case MissingMappingAction.Ignore:
396 if (AdapterSwitches.DataSchema.TraceWarning) {
397 Debug.WriteLine("mapping filter of SourceTable \"" + sourceTable + "\"");
402 case MissingMappingAction.Error:
404 if (AdapterSwitches.DataSchema.TraceError) {
405 Debug.WriteLine("mapping error on SourceTable \"" + sourceTable + "\"");
408 throw ADP.MissingTableMapping(sourceTable);
411 throw ADP.InvalidMissingMappingAction(mappingAction);