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
66 [DataSysDescription ("The number of items in the collection")]
67 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
69 get { return mappings.Count; }
73 [DataSysDescription ("The specified DataTableMapping object")]
74 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
75 public DataTableMapping this [int index] {
76 get { return (DataTableMapping)(mappings[index]); }
78 DataTableMapping mapping = (DataTableMapping) mappings[index];
79 sourceTables [mapping.SourceTable] = value;
80 dataSetTables [mapping.DataSetTable] = value;
81 mappings [index] = value;
86 [DataSysDescription ("The specified DataTableMapping object")]
87 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88 public DataTableMapping this [string sourceTable] {
89 get { return (DataTableMapping) sourceTables[sourceTable]; }
90 set { this [mappings.IndexOf (sourceTables[sourceTable])] = value; }
93 object IList.this [int index] {
94 get { return (object)(this[index]); }
96 if (!(value is DataTableMapping))
97 throw new ArgumentException ();
98 this[index] = (DataTableMapping)value;
102 bool ICollection.IsSynchronized {
103 get { return mappings.IsSynchronized; }
106 object ICollection.SyncRoot {
107 get { return mappings.SyncRoot; }
110 bool IList.IsFixedSize {
111 get { return false; }
114 bool IList.IsReadOnly {
115 get { return false; }
118 object ITableMappingCollection.this [string sourceTable] {
119 get { return this [sourceTable]; }
121 if (!(value is DataTableMapping))
122 throw new ArgumentException ();
123 this [sourceTable] = (DataTableMapping) value;
127 #endregion // Properties
131 public int Add (object value)
133 if (!(value is System.Data.Common.DataTableMapping))
134 throw new InvalidCastException ("The object passed in was not a DataTableMapping object.");
136 sourceTables[((DataTableMapping)value).SourceTable] = value;
137 dataSetTables[((DataTableMapping)value).DataSetTable] = value;
138 return mappings.Add (value);
141 public DataTableMapping Add (string sourceTable, string dataSetTable)
143 DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
150 public void AddRange (Array values)
152 throw new NotImplementedException ();
156 public void AddRange (DataTableMapping[] values)
158 foreach (DataTableMapping dataTableMapping in values)
159 this.Add (dataTableMapping);
164 sourceTables.Clear ();
165 dataSetTables.Clear ();
169 public bool Contains (object value)
171 return mappings.Contains (value);
174 public bool Contains (string value)
176 return sourceTables.Contains (value);
179 public void CopyTo (Array array, int index)
181 mappings.CopyTo (array, index);
184 public DataTableMapping GetByDataSetTable (string dataSetTable)
187 // this should work case-insenstive.
188 if (!(dataSetTables[dataSetTable] == null))
189 return (DataTableMapping)(dataSetTables[dataSetTable]);
191 string lowcasevalue = dataSetTable.ToLower();
192 object [] keyarray = new object[dataSetTables.Count];
193 dataSetTables.Keys.CopyTo(keyarray,0);
194 for (int i=0; i<keyarray.Length; i++) {
195 string temp = (string) keyarray[i];
196 if (lowcasevalue.Equals(temp.ToLower())) return (DataTableMapping)(dataSetTables[keyarray[i]]);
204 [EditorBrowsable (EditorBrowsableState.Advanced)]
205 public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
207 if (tableMappings.Contains (sourceTable))
208 return tableMappings[sourceTable];
209 if (mappingAction == MissingMappingAction.Error)
210 throw new InvalidOperationException ();
211 if (mappingAction == MissingMappingAction.Ignore)
213 return new DataTableMapping (sourceTable, dataSetTable);
216 public IEnumerator GetEnumerator ()
218 return mappings.GetEnumerator ();
221 public int IndexOf (object value)
223 return mappings.IndexOf (value);
226 public int IndexOf (string sourceTable)
228 return IndexOf (sourceTables[sourceTable]);
231 public int IndexOfDataSetTable (string dataSetTable)
233 // this should work case-insensitive
235 if (!(dataSetTables[dataSetTable] == null))
236 return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
238 string lowcasevalue = dataSetTable.ToLower();
239 object [] keyarray = new object[dataSetTables.Count];
240 dataSetTables.Keys.CopyTo(keyarray,0);
241 for (int i=0; i<keyarray.Length; i++) {
242 string temp = (string) keyarray[i];
243 if (lowcasevalue.Equals(temp.ToLower()))
244 return IndexOf ((DataTableMapping)(dataSetTables[keyarray[i]]));
253 public void Insert (int index, object value)
255 mappings.Insert (index, value);
256 sourceTables[((DataTableMapping)value).SourceTable] = value;
257 dataSetTables[((DataTableMapping)value).DataSetTable] = value;
260 ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
262 ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
267 ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
269 return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
272 public void Remove (object value)
274 if (!(value is DataTableMapping))
275 throw new InvalidCastException ();
276 int index = mappings.IndexOf (value);
277 if (( index < 0 ) || (index >=mappings.Count))
278 throw new ArgumentException("There is no such element in collection.");
280 mappings.Remove ((DataTableMapping) value);
283 public void RemoveAt (int index)
285 if (( index < 0 ) || (index >=mappings.Count))
286 throw new IndexOutOfRangeException("There is no element in collection.");
288 mappings.RemoveAt (index);
291 public void RemoveAt (string sourceTable)
293 RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
296 #endregion // Methods