DataTableMapping.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // System.Data.Common.DataTableMapping.cs
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // (C) Ximian, Inc
  9. // Copyright (C) Tim Coleman, 2002-2003
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.ComponentModel;
  34. using System.Data;
  35. namespace System.Data.Common {
  36. #if NET_2_0
  37. [TypeConverterAttribute ("System.Data.Common.DataTableMapping+DataTableMappingConverter, " + Consts.AssemblySystem_Data)]
  38. #else
  39. [TypeConverterAttribute (typeof (DataTableMappingConverter))]
  40. #endif
  41. public sealed class DataTableMapping : MarshalByRefObject, ITableMapping, ICloneable
  42. {
  43. #region Fields
  44. string sourceTable;
  45. string dataSetTable;
  46. DataColumnMappingCollection columnMappings;
  47. #endregion // Fields
  48. #region Constructors
  49. public DataTableMapping ()
  50. {
  51. dataSetTable = String.Empty;
  52. sourceTable = String.Empty;
  53. columnMappings = new DataColumnMappingCollection ();
  54. }
  55. public DataTableMapping (string sourceTable, string dataSetTable)
  56. : this ()
  57. {
  58. this.sourceTable = sourceTable;
  59. this.dataSetTable = dataSetTable;
  60. }
  61. public DataTableMapping (string sourceTable, string dataSetTable, DataColumnMapping[] columnMappings)
  62. : this (sourceTable, dataSetTable)
  63. {
  64. this.columnMappings.AddRange (columnMappings);
  65. }
  66. #endregion // Constructors
  67. #region Properties
  68. #if !NET_2_0
  69. [DataSysDescription ("Individual columns mappings when this table mapping is matched.")]
  70. #endif
  71. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  72. public DataColumnMappingCollection ColumnMappings {
  73. get { return columnMappings; }
  74. }
  75. #if !NET_2_0
  76. [DataSysDescription ("DataTable.TableName")]
  77. #endif
  78. [DefaultValue ("")]
  79. public string DataSetTable {
  80. get { return dataSetTable; }
  81. set { dataSetTable = value; }
  82. }
  83. IColumnMappingCollection ITableMapping.ColumnMappings {
  84. get { return ColumnMappings; }
  85. }
  86. #if !NET_2_0
  87. [DataSysDescription ("The DataTableMapping source table name. This name is case sensitive.")]
  88. #endif
  89. [DefaultValue ("")]
  90. public string SourceTable {
  91. get { return sourceTable; }
  92. set { sourceTable = value; }
  93. }
  94. #endregion // Properties
  95. #region Methods
  96. [EditorBrowsable (EditorBrowsableState.Advanced)]
  97. public DataColumnMapping GetColumnMappingBySchemaAction (string sourceColumn, MissingMappingAction mappingAction)
  98. {
  99. return DataColumnMappingCollection.GetColumnMappingBySchemaAction (columnMappings, sourceColumn, mappingAction);
  100. }
  101. #if NET_2_0
  102. [MonoTODO]
  103. [EditorBrowsable (EditorBrowsableState.Advanced)]
  104. public DataColumn GetDataColumn (string sourceColumn,
  105. Type dataType,
  106. DataTable dataTable,
  107. MissingMappingAction mappingAction,
  108. MissingSchemaAction schemaAction)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. #endif
  113. [EditorBrowsable (EditorBrowsableState.Advanced)]
  114. public DataTable GetDataTableBySchemaAction (DataSet dataSet, MissingSchemaAction schemaAction)
  115. {
  116. if (dataSet.Tables.Contains (DataSetTable))
  117. return dataSet.Tables [DataSetTable];
  118. if (schemaAction == MissingSchemaAction.Ignore)
  119. return null;
  120. if (schemaAction == MissingSchemaAction.Error)
  121. throw new InvalidOperationException (String.Format ("Missing the '{0} DataTable for the '{1}' SourceTable", DataSetTable, SourceTable));
  122. return new DataTable (DataSetTable);
  123. }
  124. object ICloneable.Clone ()
  125. {
  126. DataColumnMapping [] arr = new DataColumnMapping [columnMappings.Count];
  127. columnMappings.CopyTo (arr, 0);
  128. return new DataTableMapping (SourceTable, DataSetTable, arr);
  129. }
  130. public override string ToString ()
  131. {
  132. return SourceTable;
  133. }
  134. #endregion // Methods
  135. }
  136. }