DataTableMapping.cs 8.8 KB

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