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 throw new NotImplementedException ();
166 public void AddRange (DataColumnMapping[] values)
168 foreach (DataColumnMapping mapping in values)
177 public bool Contains (object value)
179 if (!(value is DataColumnMapping))
180 throw new InvalidCastException("Object is not of type DataColumnMapping");
181 return (list.Contains (value));
184 public bool Contains (string value)
186 return (sourceColumns.Contains (value));
189 public void CopyTo (Array array, int index)
191 (list.ToArray()).CopyTo(array,index);
194 public DataColumnMapping GetByDataSetColumn (string value)
196 // this should work case-insenstive.
197 if (!(dataSetColumns[value] == null))
198 return (DataColumnMapping)(dataSetColumns[value]);
200 string lowcasevalue = value.ToLower();
201 object [] keyarray = new object[dataSetColumns.Count];
202 dataSetColumns.Keys.CopyTo(keyarray,0);
203 for (int i=0; i<keyarray.Length; i++) {
204 string temp = (string) keyarray[i];
205 if (lowcasevalue.Equals(temp.ToLower()))
206 return (DataColumnMapping)(dataSetColumns[keyarray[i]]);
213 [EditorBrowsable (EditorBrowsableState.Advanced)]
214 public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
216 if (columnMappings.Contains (sourceColumn))
217 return columnMappings[sourceColumn];
218 if (mappingAction == MissingMappingAction.Ignore)
220 if (mappingAction == MissingMappingAction.Error)
221 throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
222 return new DataColumnMapping (sourceColumn, sourceColumn);
227 [EditorBrowsable (EditorBrowsableState.Advanced)]
228 public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
230 throw new NotImplementedException ();
234 public IEnumerator GetEnumerator ()
236 return list.GetEnumerator ();
239 IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
241 return Add (sourceColumnName, dataSetColumnName);
244 IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
246 return GetByDataSetColumn (dataSetColumnName);
249 public int IndexOf (object value)
251 return list.IndexOf (value);
254 public int IndexOf (string sourceColumn)
256 return list.IndexOf (sourceColumns[sourceColumn]);
259 public int IndexOfDataSetColumn (string value)
261 // this should work case-insensitive
263 if (!(dataSetColumns[value] == null))
264 return list.IndexOf (dataSetColumns[value]);
266 string lowcasevalue = value.ToLower();
267 object [] keyarray = new object[dataSetColumns.Count];
268 dataSetColumns.Keys.CopyTo(keyarray,0);
269 for (int i=0; i<keyarray.Length; i++) {
270 string temp = (string) keyarray[i];
271 if (lowcasevalue.Equals(temp.ToLower()))
272 return list.IndexOf (dataSetColumns[keyarray[i]]);
281 public void Insert (int index, object value)
283 list.Insert (index, value);
284 sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
285 dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
288 public void Remove (object value)
290 int index = list.IndexOf (value);
291 sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
292 dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
293 if (( index < 0 ) || (index >=list.Count))
294 throw new ArgumentException("There is no such element in collection.");
299 public void RemoveAt (int index)
301 if (( index < 0 ) || (index >=list.Count))
302 throw new IndexOutOfRangeException("There is no element in collection.");
304 Remove (list[index]);
307 public void RemoveAt (string sourceColumn)
309 RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
312 #endregion // Methods