DataTableMapping.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. [TypeConverterAttribute (typeof (DataTableMappingConverter))]
  37. public sealed class DataTableMapping : MarshalByRefObject, ITableMapping, ICloneable
  38. {
  39. #region Fields
  40. string sourceTable;
  41. string dataSetTable;
  42. DataColumnMappingCollection columnMappings;
  43. #endregion // Fields
  44. #region Constructors
  45. public DataTableMapping ()
  46. {
  47. dataSetTable = String.Empty;
  48. sourceTable = String.Empty;
  49. columnMappings = new DataColumnMappingCollection ();
  50. }
  51. public DataTableMapping (string sourceTable, string dataSetTable)
  52. : this ()
  53. {
  54. this.sourceTable = sourceTable;
  55. this.dataSetTable = dataSetTable;
  56. }
  57. public DataTableMapping (string sourceTable, string dataSetTable, DataColumnMapping[] columnMappings)
  58. : this (sourceTable, dataSetTable)
  59. {
  60. this.columnMappings.AddRange (columnMappings);
  61. }
  62. #endregion // Constructors
  63. #region Properties
  64. #if !NET_2_0
  65. [DataSysDescription ("Individual columns mappings when this table mapping is matched.")]
  66. #endif
  67. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  68. public DataColumnMappingCollection ColumnMappings {
  69. get { return columnMappings; }
  70. }
  71. #if !NET_2_0
  72. [DataSysDescription ("DataTable.TableName")]
  73. #endif
  74. [DefaultValue ("")]
  75. public string DataSetTable {
  76. get { return dataSetTable; }
  77. set { dataSetTable = value; }
  78. }
  79. IColumnMappingCollection ITableMapping.ColumnMappings {
  80. get { return ColumnMappings; }
  81. }
  82. #if !NET_2_0
  83. [DataSysDescription ("The DataTableMapping source table name. This name is case sensitive.")]
  84. #endif
  85. [DefaultValue ("")]
  86. public string SourceTable {
  87. get { return sourceTable; }
  88. set { sourceTable = value; }
  89. }
  90. #endregion // Properties
  91. #region Methods
  92. [EditorBrowsable (EditorBrowsableState.Advanced)]
  93. public DataColumnMapping GetColumnMappingBySchemaAction (string sourceColumn, MissingMappingAction mappingAction)
  94. {
  95. return DataColumnMappingCollection.GetColumnMappingBySchemaAction (columnMappings, sourceColumn, mappingAction);
  96. }
  97. #if NET_2_0
  98. [MonoTODO]
  99. [EditorBrowsable (EditorBrowsableState.Advanced)]
  100. public DataColumn GetDataColumn (string sourceColumn,
  101. Type dataType,
  102. DataTable dataTable,
  103. MissingMappingAction mappingAction,
  104. MissingSchemaAction schemaAction)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. #endif
  109. [EditorBrowsable (EditorBrowsableState.Advanced)]
  110. public DataTable GetDataTableBySchemaAction (DataSet dataSet, MissingSchemaAction schemaAction)
  111. {
  112. if (dataSet.Tables.Contains (DataSetTable))
  113. return dataSet.Tables [DataSetTable];
  114. if (schemaAction == MissingSchemaAction.Ignore)
  115. return null;
  116. if (schemaAction == MissingSchemaAction.Error)
  117. throw new InvalidOperationException (String.Format ("Missing the '{0} DataTable for the '{1}' SourceTable", DataSetTable, SourceTable));
  118. return new DataTable (DataSetTable);
  119. }
  120. object ICloneable.Clone ()
  121. {
  122. DataColumnMapping [] arr = new DataColumnMapping [columnMappings.Count];
  123. columnMappings.CopyTo (arr, 0);
  124. return new DataTableMapping (SourceTable, DataSetTable, arr);
  125. }
  126. public override string ToString ()
  127. {
  128. return SourceTable;
  129. }
  130. #endregion // Methods
  131. }
  132. }