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
66 [DataSysDescription ("The number of items in the collection")]
68 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
70 get { return list.Count; }
75 [DataSysDescription ("The specified DataColumnMapping object.")]
77 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
78 public DataColumnMapping this [int index] {
79 get { return (DataColumnMapping)(list[index]); }
81 DataColumnMapping mapping = (DataColumnMapping)(list[index]);
82 sourceColumns[mapping] = value;
83 dataSetColumns[mapping] = value;
90 [DataSysDescription ("The specified DataColumnMapping object.")]
92 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93 public DataColumnMapping this [string sourceColumn] {
95 if (!Contains(sourceColumn)) {
96 throw new IndexOutOfRangeException("DataColumnMappingCollection doesn't contain DataColumnMapping with SourceColumn '" + sourceColumn + "'.");
98 return (DataColumnMapping) sourceColumns[sourceColumn]; }
99 set { this [list.IndexOf (sourceColumns[sourceColumn])] = value; }
102 object ICollection.SyncRoot {
103 get { return list.SyncRoot; }
106 bool ICollection.IsSynchronized {
107 get { return list.IsSynchronized; }
110 object IColumnMappingCollection.this [string sourceColumn] {
111 get { return this [sourceColumn]; }
113 if (!(value is DataColumnMapping))
114 throw new ArgumentException ();
115 this [sourceColumn] = (DataColumnMapping) value;
119 object IList.this [int index] {
120 get { return this[index]; }
122 if (!(value is DataColumnMapping))
123 throw new ArgumentException ();
124 this [index] = (DataColumnMapping) value;
128 bool IList.IsReadOnly {
129 get { return false; }
132 bool IList.IsFixedSize {
133 get { return false; }
136 #endregion // Properties
140 public int Add (object value)
142 if (!(value is DataColumnMapping))
143 throw new InvalidCastException ();
146 sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
147 dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
148 return list.IndexOf (value);
151 public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
153 DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
160 public void AddRange (Array values)
162 for (int i=0; i < values.Length; ++i)
163 Add (values.GetValue (i));
167 public void AddRange (DataColumnMapping[] values)
169 foreach (DataColumnMapping mapping in values)
178 public bool Contains (object value)
180 if (!(value is DataColumnMapping))
181 throw new InvalidCastException("Object is not of type DataColumnMapping");
182 return (list.Contains (value));
185 public bool Contains (string value)
187 return (sourceColumns.Contains (value));
190 public void CopyTo (Array array, int index)
192 list.CopyTo(array,index);
195 public void CopyTo (DataColumnMapping[] arr, int index)
197 list.CopyTo (arr, index);
201 public DataColumnMapping GetByDataSetColumn (string value)
203 // this should work case-insenstive.
204 if (!(dataSetColumns[value] == null))
205 return (DataColumnMapping)(dataSetColumns[value]);
207 string lowcasevalue = value.ToLower();
208 object [] keyarray = new object[dataSetColumns.Count];
209 dataSetColumns.Keys.CopyTo(keyarray,0);
210 for (int i=0; i<keyarray.Length; i++) {
211 string temp = (string) keyarray[i];
212 if (lowcasevalue.Equals(temp.ToLower()))
213 return (DataColumnMapping)(dataSetColumns[keyarray[i]]);
220 [EditorBrowsable (EditorBrowsableState.Advanced)]
221 public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
223 if (columnMappings.Contains (sourceColumn))
224 return columnMappings[sourceColumn];
225 if (mappingAction == MissingMappingAction.Ignore)
227 if (mappingAction == MissingMappingAction.Error)
228 throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
229 return new DataColumnMapping (sourceColumn, sourceColumn);
234 [EditorBrowsable (EditorBrowsableState.Advanced)]
235 public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
237 throw new NotImplementedException ();
241 public IEnumerator GetEnumerator ()
243 return list.GetEnumerator ();
246 IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
248 return Add (sourceColumnName, dataSetColumnName);
251 IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
253 return GetByDataSetColumn (dataSetColumnName);
256 public int IndexOf (object value)
258 return list.IndexOf (value);
261 public int IndexOf (string sourceColumn)
263 return list.IndexOf (sourceColumns[sourceColumn]);
266 public int IndexOfDataSetColumn (string value)
268 // this should work case-insensitive
270 if (!(dataSetColumns[value] == null))
271 return list.IndexOf (dataSetColumns[value]);
273 string lowcasevalue = value.ToLower();
274 object [] keyarray = new object[dataSetColumns.Count];
275 dataSetColumns.Keys.CopyTo(keyarray,0);
276 for (int i=0; i<keyarray.Length; i++) {
277 string temp = (string) keyarray[i];
278 if (lowcasevalue.Equals(temp.ToLower()))
279 return list.IndexOf (dataSetColumns[keyarray[i]]);
288 public void Insert (int index, object value)
290 list.Insert (index, value);
291 sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
292 dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
296 public void Insert (int index, DataColumnMapping mapping)
298 list.Insert (index, mapping);
299 sourceColumns[mapping.SourceColumn] = mapping;
300 dataSetColumns[mapping.DataSetColumn] = mapping;
304 public void Remove (object value)
306 int index = list.IndexOf (value);
307 sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
308 dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
309 if (( index < 0 ) || (index >=list.Count))
310 throw new ArgumentException("There is no such element in collection.");
315 public void Remove (DataColumnMapping value)
317 int index = list.IndexOf (value);
318 sourceColumns.Remove (value.SourceColumn);
319 dataSetColumns.Remove (value.DataSetColumn);
320 if (( index < 0 ) || (index >=list.Count))
321 throw new ArgumentException("There is no such element in collection.");
327 public void RemoveAt (int index)
329 if (( index < 0 ) || (index >=list.Count))
330 throw new IndexOutOfRangeException("There is no element in collection.");
332 Remove (list[index]);
335 public void RemoveAt (string sourceColumn)
337 RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
340 #endregion // Methods