Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Common / DataTableMapping.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="DataTableMapping.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
8
9 namespace System.Data.Common {
10
11     using System;
12     using System.ComponentModel;
13     using System.ComponentModel.Design.Serialization;
14     using System.Data;
15     using System.Diagnostics;
16     using System.Globalization;
17     using System.Reflection;
18
19     [
20     System.ComponentModel.TypeConverterAttribute(typeof(System.Data.Common.DataTableMapping.DataTableMappingConverter))
21     ]
22     public sealed class DataTableMapping : MarshalByRefObject, ITableMapping, ICloneable {
23         private DataTableMappingCollection parent;
24         private DataColumnMappingCollection _columnMappings;
25         private string _dataSetTableName;
26         private string _sourceTableName;
27
28         public DataTableMapping() {
29         }
30
31         public DataTableMapping(string sourceTable, string dataSetTable) {
32             SourceTable = sourceTable;
33             DataSetTable = dataSetTable;
34         }
35
36         public DataTableMapping(string sourceTable, string dataSetTable, DataColumnMapping[] columnMappings) {
37             SourceTable = sourceTable;
38             DataSetTable = dataSetTable;
39             if ((null != columnMappings) && (0 < columnMappings.Length)) {
40                 ColumnMappings.AddRange(columnMappings);
41             }
42         }
43
44         // explict ITableMapping implementation
45         IColumnMappingCollection ITableMapping.ColumnMappings {
46             get { return ColumnMappings; }
47         }
48
49         [
50         DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
51         ResCategoryAttribute(Res.DataCategory_Mapping),
52         ResDescriptionAttribute(Res.DataTableMapping_ColumnMappings),
53         ]
54         public DataColumnMappingCollection ColumnMappings {
55             get {
56                 DataColumnMappingCollection columnMappings = _columnMappings;
57                 if (null == columnMappings) {
58                     columnMappings = new DataColumnMappingCollection();
59                     _columnMappings = columnMappings;
60                 }
61                 return columnMappings;
62             }
63         }
64
65         [
66         DefaultValue(""),
67         ResCategoryAttribute(Res.DataCategory_Mapping),
68         ResDescriptionAttribute(Res.DataTableMapping_DataSetTable),
69         ]
70         public string DataSetTable {
71             get {
72                 string dataSetTableName = _dataSetTableName;
73                 return ((null != dataSetTableName) ? dataSetTableName : ADP.StrEmpty);
74             }
75             set {
76                 _dataSetTableName = value;
77             }
78         }
79         
80         internal DataTableMappingCollection Parent {
81             get {
82                 return parent;
83             }
84             set {
85                 parent = value;
86             }
87         }
88
89         [
90         DefaultValue(""),
91         ResCategoryAttribute(Res.DataCategory_Mapping),
92         ResDescriptionAttribute(Res.DataTableMapping_SourceTable),
93         ]
94         public string SourceTable {
95             get {
96                 string sourceTableName = _sourceTableName;
97                 return ((null != sourceTableName) ? sourceTableName : ADP.StrEmpty);
98             }
99             set {
100                 if ((null != Parent) && (0 != ADP.SrcCompare(_sourceTableName, value))) {
101                     Parent.ValidateSourceTable(-1, value);
102                 }
103                 _sourceTableName = value;
104             }
105         }
106
107         object ICloneable.Clone() {
108             DataTableMapping clone = new DataTableMapping(); // MDAC 81448
109             clone._dataSetTableName = _dataSetTableName;
110             clone._sourceTableName = _sourceTableName;
111
112             if ((null != _columnMappings) && (0 < ColumnMappings.Count)) {
113                 DataColumnMappingCollection parameters = clone.ColumnMappings;
114                 foreach(ICloneable parameter in ColumnMappings) {
115                     parameters.Add(parameter.Clone());
116                 }
117             }
118             return clone;
119         }
120
121         [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
122         public DataColumn GetDataColumn(string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) {
123             return DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction);
124         }
125         
126         [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
127         public DataColumnMapping GetColumnMappingBySchemaAction(string sourceColumn, MissingMappingAction mappingAction) {
128             return DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMappings, sourceColumn, mappingAction);
129         }
130
131         [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
132         public DataTable GetDataTableBySchemaAction(DataSet dataSet, MissingSchemaAction schemaAction) {
133             if (null == dataSet) {
134                 throw ADP.ArgumentNull("dataSet");
135             }
136             string dataSetTable = DataSetTable;
137
138             if (ADP.IsEmpty(dataSetTable)) {
139 #if DEBUG
140                 if (AdapterSwitches.DataSchema.TraceWarning) {
141                     Debug.WriteLine("explicit filtering of SourceTable \"" + SourceTable + "\"");
142                 }
143 #endif
144                 return null;
145             }
146             DataTableCollection tables = dataSet.Tables;
147             int index = tables.IndexOf(dataSetTable);
148             if ((0 <= index) && (index < tables.Count)) {
149 #if DEBUG
150                 if (AdapterSwitches.DataSchema.TraceInfo) {
151                     Debug.WriteLine("schema match on DataTable \"" + dataSetTable);
152                 }
153 #endif
154                 return tables[index];
155             }
156             switch (schemaAction) {
157                 case MissingSchemaAction.Add:
158                 case MissingSchemaAction.AddWithKey:
159 #if DEBUG
160                     if (AdapterSwitches.DataSchema.TraceInfo) {
161                         Debug.WriteLine("schema add of DataTable \"" + dataSetTable + "\"");
162                     }
163 #endif
164                     return new DataTable(dataSetTable);
165
166                 case MissingSchemaAction.Ignore:
167 #if DEBUG
168                     if (AdapterSwitches.DataSchema.TraceWarning) {
169                         Debug.WriteLine("schema filter of DataTable \"" + dataSetTable + "\"");
170                     }
171 #endif
172                     return null;
173
174                 case MissingSchemaAction.Error:
175 #if DEBUG
176                     if (AdapterSwitches.DataSchema.TraceError) {
177                         Debug.WriteLine("schema error on DataTable \"" + dataSetTable + "\"");
178                     }
179 #endif
180                     throw ADP.MissingTableSchema(dataSetTable, SourceTable);
181             }
182             throw ADP.InvalidMissingSchemaAction(schemaAction);
183         }
184
185         public override String ToString() {
186             return SourceTable;
187         }
188
189         sealed internal class DataTableMappingConverter : System.ComponentModel.ExpandableObjectConverter {
190
191             // converter classes should have public ctor
192             public DataTableMappingConverter() {
193             }
194
195             override public bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
196                 if (typeof(InstanceDescriptor) == destinationType) {
197                     return true;
198                 }
199                 return base.CanConvertTo(context, destinationType);
200             }
201
202             override public object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
203                 if (null == destinationType) {
204                     throw ADP.ArgumentNull("destinationType");
205                 }
206
207                 if ((typeof(InstanceDescriptor) == destinationType) && (value is DataTableMapping)) {
208                     DataTableMapping mapping = (DataTableMapping)value;
209
210                     DataColumnMapping[] columnMappings = new DataColumnMapping[mapping.ColumnMappings.Count];
211                     mapping.ColumnMappings.CopyTo(columnMappings, 0);
212                     object[] values = new object[] { mapping.SourceTable, mapping.DataSetTable, columnMappings};
213                     Type[] types = new Type[] { typeof(string), typeof(string), typeof(DataColumnMapping[]) };
214
215                     ConstructorInfo ctor = typeof(DataTableMapping).GetConstructor(types);
216                     return new InstanceDescriptor(ctor, values);
217                 }            
218                 return base.ConvertTo(context, culture, value, destinationType);
219             }
220         }
221     }
222 }