2 // System.Data.Common.DataColumnMappingCollection
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;
40 namespace System.Data.Common {
41 public sealed class DataColumnMappingCollection : MarshalByRefObject, IColumnMappingCollection , IList, ICollection, IEnumerable
46 Hashtable sourceColumns;
47 Hashtable dataSetColumns;
53 public DataColumnMappingCollection ()
55 list = new ArrayList ();
56 sourceColumns = new Hashtable ();
57 dataSetColumns = new Hashtable ();
60 #endregion // Constructors
65 [DataSysDescription ("The number of items in the collection")]
66 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
68 get { return list.Count; }
72 [DataSysDescription ("The specified DataColumnMapping object.")]
73 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
74 public DataColumnMapping this [int index] {
75 get { return (DataColumnMapping)(list[index]); }
77 DataColumnMapping mapping = (DataColumnMapping)(list[index]);
78 sourceColumns[mapping] = value;
79 dataSetColumns[mapping] = value;
85 [DataSysDescription ("DataTableMappings_Item")]
86 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87 public DataColumnMapping this [string sourceColumn] {
89 if (!Contains(sourceColumn)) {
90 throw new IndexOutOfRangeException("DataColumnMappingCollection doesn't contain DataColumnMapping with SourceColumn '" + sourceColumn + "'.");
92 return (DataColumnMapping) sourceColumns[sourceColumn]; }
93 set { this [list.IndexOf (sourceColumns[sourceColumn])] = value; }
96 object ICollection.SyncRoot {
97 get { return list.SyncRoot; }
100 bool ICollection.IsSynchronized {
101 get { return list.IsSynchronized; }
104 object IColumnMappingCollection.this [string sourceColumn] {
105 get { return this [sourceColumn]; }
107 if (!(value is DataColumnMapping))
108 throw new ArgumentException ();
109 this [sourceColumn] = (DataColumnMapping) value;
113 object IList.this [int index] {
114 get { return this[index]; }
116 if (!(value is DataColumnMapping))
117 throw new ArgumentException ();
118 this [index] = (DataColumnMapping) value;
122 bool IList.IsReadOnly {
123 get { return false; }
126 bool IList.IsFixedSize {
127 get { return false; }
130 #endregion // Properties
134 public int Add (object value)
136 if (!(value is DataColumnMapping))
137 throw new InvalidCastException ();
140 sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
141 dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
142 return list.IndexOf (value);
145 public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
147 DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
154 public void AddRange (Array values)
156 throw new NotImplementedException ();
160 public void AddRange (DataColumnMapping[] values)
162 foreach (DataColumnMapping mapping in values)
171 public bool Contains (object value)
173 if (!(value is DataColumnMapping))
174 throw new InvalidCastException("Object is not of type DataColumnMapping");
175 return (list.Contains (value));
178 public bool Contains (string value)
180 return (sourceColumns.Contains (value));
183 public void CopyTo (Array array, int index)
185 (list.ToArray()).CopyTo(array,index);
188 public DataColumnMapping GetByDataSetColumn (string value)
190 // this should work case-insenstive.
191 if (!(dataSetColumns[value] == null))
192 return (DataColumnMapping)(dataSetColumns[value]);
194 string lowcasevalue = value.ToLower();
195 object [] keyarray = new object[dataSetColumns.Count];
196 dataSetColumns.Keys.CopyTo(keyarray,0);
197 for (int i=0; i<keyarray.Length; i++) {
198 string temp = (string) keyarray[i];
199 if (lowcasevalue.Equals(temp.ToLower()))
200 return (DataColumnMapping)(dataSetColumns[keyarray[i]]);
207 [EditorBrowsable (EditorBrowsableState.Advanced)]
208 public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
210 if (columnMappings.Contains (sourceColumn))
211 return columnMappings[sourceColumn];
212 if (mappingAction == MissingMappingAction.Ignore)
214 if (mappingAction == MissingMappingAction.Error)
215 throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
216 return new DataColumnMapping (sourceColumn, sourceColumn);
221 public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
223 throw new NotImplementedException ();
227 public IEnumerator GetEnumerator ()
229 return list.GetEnumerator ();
232 IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
234 return Add (sourceColumnName, dataSetColumnName);
237 IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
239 return GetByDataSetColumn (dataSetColumnName);
242 public int IndexOf (object value)
244 return list.IndexOf (value);
247 public int IndexOf (string sourceColumn)
249 return list.IndexOf (sourceColumns[sourceColumn]);
252 public int IndexOfDataSetColumn (string value)
254 // this should work case-insensitive
256 if (!(dataSetColumns[value] == null))
257 return list.IndexOf (dataSetColumns[value]);
259 string lowcasevalue = value.ToLower();
260 object [] keyarray = new object[dataSetColumns.Count];
261 dataSetColumns.Keys.CopyTo(keyarray,0);
262 for (int i=0; i<keyarray.Length; i++) {
263 string temp = (string) keyarray[i];
264 if (lowcasevalue.Equals(temp.ToLower()))
265 return list.IndexOf (dataSetColumns[keyarray[i]]);
274 public void Insert (int index, object value)
276 list.Insert (index, value);
277 sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
278 dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
281 public void Remove (object value)
283 int index = list.IndexOf (value);
284 sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
285 dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
286 if (( index < 0 ) || (index >=list.Count))
287 throw new ArgumentException("There is no such element in collection.");
292 public void RemoveAt (int index)
294 if (( index < 0 ) || (index >=list.Count))
295 throw new IndexOutOfRangeException("There is no element in collection.");
297 Remove (list[index]);
300 public void RemoveAt (string sourceColumn)
302 RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
305 #endregion // Methods