2
0

DataTableMapping.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. [DataSysDescription ("Individual columns mappings when this table mapping is matched.")]
  65. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  66. public DataColumnMappingCollection ColumnMappings {
  67. get { return columnMappings; }
  68. }
  69. [DataSysDescription ("DataTable.TableName")]
  70. [DefaultValue ("")]
  71. public string DataSetTable {
  72. get { return dataSetTable; }
  73. set { dataSetTable = value; }
  74. }
  75. IColumnMappingCollection ITableMapping.ColumnMappings {
  76. get { return ColumnMappings; }
  77. }
  78. [DataSysDescription ("The DataTableMapping source table name. This name is case sensitive.")]
  79. [DefaultValue ("")]
  80. public string SourceTable {
  81. get { return sourceTable; }
  82. set { sourceTable = value; }
  83. }
  84. #endregion // Properties
  85. #region Methods
  86. [EditorBrowsable (EditorBrowsableState.Advanced)]
  87. public DataColumnMapping GetColumnMappingBySchemaAction (string sourceColumn, MissingMappingAction mappingAction)
  88. {
  89. return DataColumnMappingCollection.GetColumnMappingBySchemaAction (columnMappings, sourceColumn, mappingAction);
  90. }
  91. #if NET_2_0
  92. [MonoTODO]
  93. public DataColumn GetDataColumn (string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
  94. {
  95. throw new NotImplementedException ();
  96. }
  97. #endif
  98. [EditorBrowsable (EditorBrowsableState.Advanced)]
  99. public DataTable GetDataTableBySchemaAction (DataSet dataSet, MissingSchemaAction schemaAction)
  100. {
  101. if (dataSet.Tables.Contains (DataSetTable))
  102. return dataSet.Tables [DataSetTable];
  103. if (schemaAction == MissingSchemaAction.Ignore)
  104. return null;
  105. if (schemaAction == MissingSchemaAction.Error)
  106. throw new InvalidOperationException (String.Format ("Missing the '{0} DataTable for the '{1}' SourceTable", DataSetTable, SourceTable));
  107. return new DataTable (DataSetTable);
  108. }
  109. object ICloneable.Clone ()
  110. {
  111. DataColumnMapping [] arr = new DataColumnMapping [columnMappings.Count];
  112. columnMappings.CopyTo (arr, 0);
  113. return new DataTableMapping (SourceTable, DataSetTable, arr);
  114. }
  115. public override string ToString ()
  116. {
  117. return SourceTable;
  118. }
  119. #endregion // Methods
  120. }
  121. }