New test.
[mono.git] / mcs / class / System.Data / System.Data.Common / DataColumnMappingCollection.cs
1 //
2 // System.Data.Common.DataColumnMappingCollection
3 //
4 // Authors:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // (C) Ximian, Inc
9 // Copyright (C) Tim Coleman, 2002-2003
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34
35 using System;
36 using System.Collections;
37 using System.ComponentModel;
38 using System.Data;
39
40 namespace System.Data.Common {
41         public sealed class DataColumnMappingCollection : MarshalByRefObject, IColumnMappingCollection , IList, ICollection, IEnumerable
42         {
43                 #region Fields
44
45                 ArrayList list;
46                 Hashtable sourceColumns;
47                 Hashtable dataSetColumns;
48
49                 #endregion // Fields
50
51                 #region Constructors 
52
53                 public DataColumnMappingCollection () 
54                 {
55                         list = new ArrayList ();
56                         sourceColumns = new Hashtable ();
57                         dataSetColumns = new Hashtable ();
58                 }
59
60                 #endregion // Constructors
61
62                 #region Properties
63
64                 [Browsable (false)]
65 #if !NET_2_0
66                 [DataSysDescription ("The number of items in the collection")]
67 #endif
68                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
69                 public int Count {
70                         get { return list.Count; }
71                 }
72
73                 [Browsable (false)]
74 #if !NET_2_0
75                 [DataSysDescription ("The specified DataColumnMapping object.")]
76 #endif
77                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
78                 public DataColumnMapping this [int index] {
79                         get { return (DataColumnMapping)(list[index]); }
80                         set { 
81                                 DataColumnMapping mapping = (DataColumnMapping)(list[index]);
82                                 sourceColumns[mapping] = value;
83                                 dataSetColumns[mapping] = value;
84                                 list[index] = value;
85                         }
86                 }
87
88                 [Browsable (false)]
89 #if !NET_2_0
90                 [DataSysDescription ("The specified DataColumnMapping object.")]
91 #endif
92                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93                 public DataColumnMapping this [string sourceColumn] {
94                         get {
95                                 if (!Contains(sourceColumn)) {
96                                         throw new IndexOutOfRangeException("DataColumnMappingCollection doesn't contain DataColumnMapping with SourceColumn '" + sourceColumn + "'.");
97                                 }
98                                 return (DataColumnMapping) sourceColumns[sourceColumn]; }
99                         set { this [list.IndexOf (sourceColumns[sourceColumn])] = value; }
100                 }
101
102                 object ICollection.SyncRoot {
103                         get { return list.SyncRoot; }
104                 }
105
106                 bool ICollection.IsSynchronized {
107                         get { return list.IsSynchronized; }
108                 }
109
110                 object IColumnMappingCollection.this [string sourceColumn] {
111                         get { return this [sourceColumn]; }
112                         set {
113                                 if (!(value is DataColumnMapping))
114                                         throw new ArgumentException ();
115                                 this [sourceColumn] = (DataColumnMapping) value;
116                         }
117                 }
118
119                 object IList.this [int index] {
120                         get { return this[index]; }
121                         set {
122                                 if (!(value is DataColumnMapping))
123                                         throw new ArgumentException ();
124                                 this [index] = (DataColumnMapping) value;
125                          }
126                 }
127
128                 bool IList.IsReadOnly {
129                         get { return false; }
130                 }
131
132                 bool IList.IsFixedSize {
133                         get { return false; }
134                 }
135                 
136                 #endregion // Properties
137
138                 #region Methods
139
140                 public int Add (object value)
141                 {
142                         if (!(value is DataColumnMapping))
143                                 throw new InvalidCastException ();
144
145                         list.Add (value);
146                         sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
147                         dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
148                         return list.IndexOf (value);
149                 }
150
151                 public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
152                 {
153                         DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
154                         Add (mapping);
155                         return mapping;
156                 }
157
158 #if NET_2_0
159                 [MonoTODO]
160                 public void AddRange (Array values)
161                 {
162                         for (int i=0; i < values.Length; ++i)
163                                 Add (values.GetValue (i));
164                 }
165 #endif
166
167                 public void AddRange (DataColumnMapping[] values) 
168                 {
169                         foreach (DataColumnMapping mapping in values)
170                                 Add (mapping);
171                 }
172
173                 public void Clear () 
174                 {
175                         list.Clear ();
176                 }
177
178                 public bool Contains (object value) 
179                 {
180                         if  (!(value is DataColumnMapping))
181                                 throw new InvalidCastException("Object is not of type DataColumnMapping");
182                         return (list.Contains (value));
183                 }
184
185                 public bool Contains (string value)
186                 {
187                         return (sourceColumns.Contains (value));
188                 }
189
190                 public void CopyTo (Array array, int index) 
191                 {
192                         list.CopyTo(array,index);
193                 }
194 #if NET_2_0
195                 public void CopyTo (DataColumnMapping[] arr, int index) 
196                 {
197                         list.CopyTo (arr, index);
198                 }
199 #endif
200
201                 public DataColumnMapping GetByDataSetColumn (string value) 
202                 {
203                         // this should work case-insenstive.
204                         if (!(dataSetColumns[value] == null))
205                                 return (DataColumnMapping)(dataSetColumns[value]);
206                         else {
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]]);
214                                 }
215                                 return null;
216                         
217                         }       
218                 }
219
220                 [EditorBrowsable (EditorBrowsableState.Advanced)]
221                 public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) 
222                 {
223                         if (columnMappings.Contains (sourceColumn))
224                                 return columnMappings[sourceColumn];
225                         if (mappingAction == MissingMappingAction.Ignore)
226                                 return null;
227                         if (mappingAction == MissingMappingAction.Error)
228                                 throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
229                         return new DataColumnMapping (sourceColumn, sourceColumn);
230                 }
231
232 #if NET_2_0
233                 [MonoTODO]
234                 [EditorBrowsable (EditorBrowsableState.Advanced)]
235                 public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
236                 {
237                         throw new NotImplementedException ();
238                 }
239 #endif
240
241                 public IEnumerator GetEnumerator ()
242                 {
243                         return list.GetEnumerator ();
244                 }
245
246                 IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
247                 {
248                         return Add (sourceColumnName, dataSetColumnName);
249                 }
250
251                 IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
252                 {
253                         return GetByDataSetColumn (dataSetColumnName);
254                 }
255
256                 public int IndexOf (object value) 
257                 {
258                         return list.IndexOf (value);
259                 }
260
261                 public int IndexOf (string sourceColumn)
262                 {
263                         return list.IndexOf (sourceColumns[sourceColumn]);
264                 }
265
266                 public int IndexOfDataSetColumn (string value) 
267                 {
268                         // this should work case-insensitive
269                                         
270                          if (!(dataSetColumns[value] == null))
271                                 return list.IndexOf (dataSetColumns[value]);
272                         else {
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]]);
280
281                                 }
282                                 return -1;
283                                                                                                     
284                         }
285         
286                 }
287
288                 public void Insert (int index, object value) 
289                 {
290                         list.Insert (index, value);
291                         sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
292                         dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
293                 }
294
295 #if NET_2_0
296                 public void Insert (int index, DataColumnMapping mapping) 
297                 {
298                         list.Insert (index, mapping);
299                         sourceColumns[mapping.SourceColumn] = mapping;
300                         dataSetColumns[mapping.DataSetColumn] = mapping;
301                 }
302 #endif
303
304                 public void Remove (object value) 
305                 {
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.");
311
312                         list.Remove (value);
313                 }
314 #if NET_2_0
315                 public void Remove (DataColumnMapping value) 
316                 {
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.");
322
323                         list.Remove (value);
324                 }
325 #endif
326
327                 public void RemoveAt (int index) 
328                 {
329                         if (( index < 0 ) || (index >=list.Count))
330                                     throw new IndexOutOfRangeException("There is no element in collection.");
331
332                         Remove (list[index]);
333                 }
334
335                 public void RemoveAt (string sourceColumn)
336                 {
337                         RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
338                 }
339
340                 #endregion // Methods
341         }
342 }