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