1 //------------------------------------------------------------------------------
2 // <copyright file="DataTableMapping.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
9 namespace System.Data.Common {
12 using System.ComponentModel;
13 using System.ComponentModel.Design.Serialization;
15 using System.Diagnostics;
16 using System.Globalization;
17 using System.Reflection;
20 System.ComponentModel.TypeConverterAttribute(typeof(System.Data.Common.DataTableMapping.DataTableMappingConverter))
22 public sealed class DataTableMapping : MarshalByRefObject, ITableMapping, ICloneable {
23 private DataTableMappingCollection parent;
24 private DataColumnMappingCollection _columnMappings;
25 private string _dataSetTableName;
26 private string _sourceTableName;
28 public DataTableMapping() {
31 public DataTableMapping(string sourceTable, string dataSetTable) {
32 SourceTable = sourceTable;
33 DataSetTable = dataSetTable;
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);
44 // explict ITableMapping implementation
45 IColumnMappingCollection ITableMapping.ColumnMappings {
46 get { return ColumnMappings; }
50 DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
51 ResCategoryAttribute(Res.DataCategory_Mapping),
52 ResDescriptionAttribute(Res.DataTableMapping_ColumnMappings),
54 public DataColumnMappingCollection ColumnMappings {
56 DataColumnMappingCollection columnMappings = _columnMappings;
57 if (null == columnMappings) {
58 columnMappings = new DataColumnMappingCollection();
59 _columnMappings = columnMappings;
61 return columnMappings;
67 ResCategoryAttribute(Res.DataCategory_Mapping),
68 ResDescriptionAttribute(Res.DataTableMapping_DataSetTable),
70 public string DataSetTable {
72 string dataSetTableName = _dataSetTableName;
73 return ((null != dataSetTableName) ? dataSetTableName : ADP.StrEmpty);
76 _dataSetTableName = value;
80 internal DataTableMappingCollection Parent {
91 ResCategoryAttribute(Res.DataCategory_Mapping),
92 ResDescriptionAttribute(Res.DataTableMapping_SourceTable),
94 public string SourceTable {
96 string sourceTableName = _sourceTableName;
97 return ((null != sourceTableName) ? sourceTableName : ADP.StrEmpty);
100 if ((null != Parent) && (0 != ADP.SrcCompare(_sourceTableName, value))) {
101 Parent.ValidateSourceTable(-1, value);
103 _sourceTableName = value;
107 object ICloneable.Clone() {
108 DataTableMapping clone = new DataTableMapping(); // MDAC 81448
109 clone._dataSetTableName = _dataSetTableName;
110 clone._sourceTableName = _sourceTableName;
112 if ((null != _columnMappings) && (0 < ColumnMappings.Count)) {
113 DataColumnMappingCollection parameters = clone.ColumnMappings;
114 foreach(ICloneable parameter in ColumnMappings) {
115 parameters.Add(parameter.Clone());
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);
126 [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
127 public DataColumnMapping GetColumnMappingBySchemaAction(string sourceColumn, MissingMappingAction mappingAction) {
128 return DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMappings, sourceColumn, mappingAction);
131 [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
132 public DataTable GetDataTableBySchemaAction(DataSet dataSet, MissingSchemaAction schemaAction) {
133 if (null == dataSet) {
134 throw ADP.ArgumentNull("dataSet");
136 string dataSetTable = DataSetTable;
138 if (ADP.IsEmpty(dataSetTable)) {
140 if (AdapterSwitches.DataSchema.TraceWarning) {
141 Debug.WriteLine("explicit filtering of SourceTable \"" + SourceTable + "\"");
146 DataTableCollection tables = dataSet.Tables;
147 int index = tables.IndexOf(dataSetTable);
148 if ((0 <= index) && (index < tables.Count)) {
150 if (AdapterSwitches.DataSchema.TraceInfo) {
151 Debug.WriteLine("schema match on DataTable \"" + dataSetTable);
154 return tables[index];
156 switch (schemaAction) {
157 case MissingSchemaAction.Add:
158 case MissingSchemaAction.AddWithKey:
160 if (AdapterSwitches.DataSchema.TraceInfo) {
161 Debug.WriteLine("schema add of DataTable \"" + dataSetTable + "\"");
164 return new DataTable(dataSetTable);
166 case MissingSchemaAction.Ignore:
168 if (AdapterSwitches.DataSchema.TraceWarning) {
169 Debug.WriteLine("schema filter of DataTable \"" + dataSetTable + "\"");
174 case MissingSchemaAction.Error:
176 if (AdapterSwitches.DataSchema.TraceError) {
177 Debug.WriteLine("schema error on DataTable \"" + dataSetTable + "\"");
180 throw ADP.MissingTableSchema(dataSetTable, SourceTable);
182 throw ADP.InvalidMissingSchemaAction(schemaAction);
185 public override String ToString() {
189 sealed internal class DataTableMappingConverter : System.ComponentModel.ExpandableObjectConverter {
191 // converter classes should have public ctor
192 public DataTableMappingConverter() {
195 override public bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
196 if (typeof(InstanceDescriptor) == destinationType) {
199 return base.CanConvertTo(context, destinationType);
202 override public object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
203 if (null == destinationType) {
204 throw ADP.ArgumentNull("destinationType");
207 if ((typeof(InstanceDescriptor) == destinationType) && (value is DataTableMapping)) {
208 DataTableMapping mapping = (DataTableMapping)value;
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[]) };
215 ConstructorInfo ctor = typeof(DataTableMapping).GetConstructor(types);
216 return new InstanceDescriptor(ctor, values);
218 return base.ConvertTo(context, culture, value, destinationType);