2 // System.Data.Common.DataTableMappingCollection.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
9 // Copyright (C) Tim Coleman, 2002-2003
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.ComponentModel;
39 namespace System.Data.Common {
40 [ListBindable (false)]
41 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DataTableMappingCollectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
42 public sealed class DataTableMappingCollection : MarshalByRefObject, ITableMappingCollection, IList, ICollection, IEnumerable
47 Hashtable sourceTables;
48 Hashtable dataSetTables;
54 public DataTableMappingCollection()
56 mappings = new ArrayList ();
57 sourceTables = new Hashtable ();
58 dataSetTables = new Hashtable ();
61 #endregion // Constructors
67 [DataSysDescription ("The number of items in the collection")]
69 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
71 get { return mappings.Count; }
76 [DataSysDescription ("The specified DataTableMapping object")]
78 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
79 public DataTableMapping this [int index] {
80 get { return (DataTableMapping)(mappings[index]); }
82 DataTableMapping mapping = (DataTableMapping) mappings[index];
83 sourceTables [mapping.SourceTable] = value;
84 dataSetTables [mapping.DataSetTable] = value;
85 mappings [index] = value;
91 [DataSysDescription ("The specified DataTableMapping object")]
93 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94 public DataTableMapping this [string sourceTable] {
95 get { return (DataTableMapping) sourceTables[sourceTable]; }
96 set { this [mappings.IndexOf (sourceTables[sourceTable])] = value; }
99 object IList.this [int index] {
100 get { return (object)(this[index]); }
102 if (!(value is DataTableMapping))
103 throw new ArgumentException ();
104 this[index] = (DataTableMapping)value;
108 bool ICollection.IsSynchronized {
109 get { return mappings.IsSynchronized; }
112 object ICollection.SyncRoot {
113 get { return mappings.SyncRoot; }
116 bool IList.IsFixedSize {
117 get { return false; }
120 bool IList.IsReadOnly {
121 get { return false; }
124 object ITableMappingCollection.this [string sourceTable] {
125 get { return this [sourceTable]; }
127 if (!(value is DataTableMapping))
128 throw new ArgumentException ();
129 this [sourceTable] = (DataTableMapping) value;
133 #endregion // Properties
137 public int Add (object value)
139 if (!(value is System.Data.Common.DataTableMapping))
140 throw new InvalidCastException ("The object passed in was not a DataTableMapping object.");
142 sourceTables[((DataTableMapping)value).SourceTable] = value;
143 dataSetTables[((DataTableMapping)value).DataSetTable] = value;
144 return mappings.Add (value);
147 public DataTableMapping Add (string sourceTable, string dataSetTable)
149 DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
156 public void AddRange (Array values)
158 throw new NotImplementedException ();
162 public void AddRange (DataTableMapping[] values)
164 foreach (DataTableMapping dataTableMapping in values)
165 this.Add (dataTableMapping);
170 sourceTables.Clear ();
171 dataSetTables.Clear ();
175 public bool Contains (object value)
177 return mappings.Contains (value);
180 public bool Contains (string value)
182 return sourceTables.Contains (value);
185 public void CopyTo (Array array, int index)
187 mappings.CopyTo (array, index);
191 public void CopyTo (DataTableMapping[] array, int index)
193 mappings.CopyTo (array, index);
197 public DataTableMapping GetByDataSetTable (string dataSetTable)
200 // this should work case-insenstive.
201 if (!(dataSetTables[dataSetTable] == null))
202 return (DataTableMapping)(dataSetTables[dataSetTable]);
204 string lowcasevalue = dataSetTable.ToLower();
205 object [] keyarray = new object[dataSetTables.Count];
206 dataSetTables.Keys.CopyTo(keyarray,0);
207 for (int i=0; i<keyarray.Length; i++) {
208 string temp = (string) keyarray[i];
209 if (lowcasevalue.Equals(temp.ToLower())) return (DataTableMapping)(dataSetTables[keyarray[i]]);
217 [EditorBrowsable (EditorBrowsableState.Advanced)]
218 public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
220 if (tableMappings.Contains (sourceTable))
221 return tableMappings[sourceTable];
222 if (mappingAction == MissingMappingAction.Error)
223 throw new InvalidOperationException (String.Format ("Missing source table mapping: '{0}'",
225 if (mappingAction == MissingMappingAction.Ignore)
227 return new DataTableMapping (sourceTable, dataSetTable);
230 public IEnumerator GetEnumerator ()
232 return mappings.GetEnumerator ();
235 public int IndexOf (object value)
237 return mappings.IndexOf (value);
240 public int IndexOf (string sourceTable)
242 return IndexOf (sourceTables[sourceTable]);
245 public int IndexOfDataSetTable (string dataSetTable)
247 // this should work case-insensitive
249 if (!(dataSetTables[dataSetTable] == null))
250 return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
252 string lowcasevalue = dataSetTable.ToLower();
253 object [] keyarray = new object[dataSetTables.Count];
254 dataSetTables.Keys.CopyTo(keyarray,0);
255 for (int i=0; i<keyarray.Length; i++) {
256 string temp = (string) keyarray[i];
257 if (lowcasevalue.Equals(temp.ToLower()))
258 return IndexOf ((DataTableMapping)(dataSetTables[keyarray[i]]));
267 public void Insert (int index, object value)
269 mappings.Insert (index, value);
270 sourceTables[((DataTableMapping)value).SourceTable] = value;
271 dataSetTables[((DataTableMapping)value).DataSetTable] = value;
275 public void Insert (int index, DataTableMapping value)
277 mappings.Insert (index, value);
278 sourceTables[value.SourceTable] = value;
279 dataSetTables[value.DataSetTable] = value;
283 ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
285 ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
290 ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
292 return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
295 public void Remove (object value)
297 if (!(value is DataTableMapping))
298 throw new InvalidCastException ();
299 int index = mappings.IndexOf (value);
300 if (( index < 0 ) || (index >=mappings.Count))
301 throw new ArgumentException("There is no such element in collection.");
302 mappings.Remove ((DataTableMapping) value);
306 public void Remove (DataTableMapping value)
308 int index = mappings.IndexOf (value);
309 if (( index < 0 ) || (index >=mappings.Count))
310 throw new ArgumentException("There is no such element in collection.");
311 mappings.Remove ((DataTableMapping) value);
315 public void RemoveAt (int index)
317 if (( index < 0 ) || (index >=mappings.Count))
318 throw new IndexOutOfRangeException("There is no element in collection.");
320 mappings.RemoveAt (index);
323 public void RemoveAt (string sourceTable)
325 RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
328 #endregion // Methods