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