New test.
[mono.git] / mcs / class / System.Data / System.Data.Common / DataTableMapping.cs
1 //
2 // System.Data.Common.DataTableMapping.cs
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.ComponentModel;
36 using System.Data;
37
38 namespace System.Data.Common {
39         [TypeConverterAttribute (typeof (DataTableMappingConverter))]
40         public sealed class DataTableMapping : MarshalByRefObject, ITableMapping, ICloneable
41         {
42                 #region Fields
43
44                 string sourceTable;
45                 string dataSetTable;
46                 DataColumnMappingCollection columnMappings;
47
48                 #endregion // Fields
49
50                 #region Constructors
51
52                 public DataTableMapping () 
53                 {
54                         dataSetTable = String.Empty;
55                         sourceTable = String.Empty;
56                         columnMappings = new DataColumnMappingCollection ();
57                 }
58
59                 public DataTableMapping (string sourceTable, string dataSetTable) 
60                         : this ()
61                 {
62                         this.sourceTable = sourceTable;
63                         this.dataSetTable = dataSetTable;
64                 }
65                 
66                 public DataTableMapping (string sourceTable, string dataSetTable, DataColumnMapping[] columnMappings) 
67                         : this (sourceTable, dataSetTable)
68                 {
69                         this.columnMappings.AddRange (columnMappings);
70                 }
71
72                 #endregion // Constructors
73
74                 #region Properties
75
76 #if !NET_2_0
77                 [DataSysDescription ("Individual columns mappings when this table mapping is matched.")]
78 #endif
79                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
80                 public DataColumnMappingCollection ColumnMappings {
81                         get { return columnMappings; }
82                 }
83
84 #if !NET_2_0
85                 [DataSysDescription ("DataTable.TableName")]
86 #endif
87                 [DefaultValue ("")]
88                 public string DataSetTable {
89                         get { return dataSetTable; } 
90                         set { dataSetTable = value; }
91                 }
92
93                 IColumnMappingCollection ITableMapping.ColumnMappings {
94                         get { return ColumnMappings; }
95                 }
96         
97 #if !NET_2_0
98                 [DataSysDescription ("The DataTableMapping source table name. This name is case sensitive.")]
99 #endif
100                 [DefaultValue ("")]
101                 public string SourceTable {
102                         get { return sourceTable; }
103                         set { sourceTable = value; }
104                 }
105
106                 #endregion // Properties
107
108                 #region Methods
109
110                 [EditorBrowsable (EditorBrowsableState.Advanced)]
111                 public DataColumnMapping GetColumnMappingBySchemaAction (string sourceColumn, MissingMappingAction mappingAction) 
112                 {
113                         return DataColumnMappingCollection.GetColumnMappingBySchemaAction (columnMappings, sourceColumn, mappingAction);
114                 }
115
116 #if NET_2_0
117                 [MonoTODO]
118                 [EditorBrowsable (EditorBrowsableState.Advanced)]
119                 public DataColumn GetDataColumn (string sourceColumn, 
120                                                  Type dataType, 
121                                                  DataTable dataTable, 
122                                                  MissingMappingAction mappingAction, 
123                                                  MissingSchemaAction schemaAction)
124                 {
125                         throw new NotImplementedException ();
126                 }
127 #endif
128
129                 [EditorBrowsable (EditorBrowsableState.Advanced)]
130                 public DataTable GetDataTableBySchemaAction (DataSet dataSet, MissingSchemaAction schemaAction) 
131                 {
132                         if (dataSet.Tables.Contains (DataSetTable))
133                                 return dataSet.Tables [DataSetTable];
134                         if (schemaAction == MissingSchemaAction.Ignore)
135                                 return null;
136                         if (schemaAction == MissingSchemaAction.Error)
137                                 throw new InvalidOperationException (String.Format ("Missing the '{0} DataTable for the '{1}' SourceTable", DataSetTable, SourceTable));
138                         return new DataTable (DataSetTable);
139                 }
140
141                 object ICloneable.Clone ()
142                 {
143                         DataColumnMapping [] arr = new DataColumnMapping [columnMappings.Count];
144                         columnMappings.CopyTo (arr, 0);
145                         return new DataTableMapping (SourceTable, DataSetTable, arr);
146                 }
147
148                 public override string ToString ()
149                 {
150                         return SourceTable; 
151                 }
152                 
153                 #endregion // Methods
154         }
155 }