DataTableMappingCollection.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // System.Data.Common.DataTableMappingCollection.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // (C) Ximian, Inc
  9. // Copyright (C) Tim Coleman, 2002-2003
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. namespace System.Data.Common {
  15. [ListBindable (false)]
  16. public sealed class DataTableMappingCollection : MarshalByRefObject, ITableMappingCollection, IList, ICollection, IEnumerable
  17. {
  18. #region Fields
  19. ArrayList mappings;
  20. Hashtable sourceTables;
  21. Hashtable dataSetTables;
  22. #endregion
  23. #region Constructors
  24. public DataTableMappingCollection()
  25. {
  26. mappings = new ArrayList ();
  27. sourceTables = new Hashtable ();
  28. dataSetTables = new Hashtable ();
  29. }
  30. #endregion // Constructors
  31. #region Properties
  32. [DataSysDescription ("The number of items in the collection")]
  33. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  34. public int Count {
  35. get { return mappings.Count; }
  36. }
  37. [Browsable (false)]
  38. [DataSysDescription ("The specified DataTableMapping object")]
  39. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  40. public DataTableMapping this [int index] {
  41. get { return (DataTableMapping)(mappings[index]); }
  42. set {
  43. DataTableMapping mapping = (DataTableMapping) mappings[index];
  44. sourceTables [mapping.SourceTable] = value;
  45. dataSetTables [mapping.DataSetTable] = value;
  46. mappings [index] = value;
  47. }
  48. }
  49. [Browsable (false)]
  50. [DataSysDescription ("The specified DataTableMapping object")]
  51. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  52. public DataTableMapping this [string sourceTable] {
  53. get { return (DataTableMapping) sourceTables[sourceTable]; }
  54. set { this [mappings.IndexOf (sourceTables[sourceTable])] = value; }
  55. }
  56. object IList.this [int index] {
  57. get { return (object)(this[index]); }
  58. set {
  59. if (!(value is DataTableMapping))
  60. throw new ArgumentException ();
  61. this[index] = (DataTableMapping)value;
  62. }
  63. }
  64. bool ICollection.IsSynchronized {
  65. get { return mappings.IsSynchronized; }
  66. }
  67. object ICollection.SyncRoot {
  68. get { return mappings.SyncRoot; }
  69. }
  70. bool IList.IsFixedSize {
  71. get { return false; }
  72. }
  73. bool IList.IsReadOnly {
  74. get { return false; }
  75. }
  76. object ITableMappingCollection.this [string sourceTable] {
  77. get { return this [sourceTable]; }
  78. set {
  79. if (!(value is DataTableMapping))
  80. throw new ArgumentException ();
  81. this [sourceTable] = (DataTableMapping) value;
  82. }
  83. }
  84. #endregion // Properties
  85. #region Methods
  86. public int Add (object value)
  87. {
  88. if (!(value is System.Data.Common.DataTableMapping))
  89. throw new SystemException ("The object passed in was not a DataTableMapping object.");
  90. sourceTables[((DataTableMapping)value).SourceTable] = value;
  91. dataSetTables[((DataTableMapping)value).DataSetTable] = value;
  92. return mappings.Add (value);
  93. }
  94. public DataTableMapping Add (string sourceTable, string dataSetTable)
  95. {
  96. DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
  97. Add (mapping);
  98. return mapping;
  99. }
  100. #if NET_1_2
  101. [MonoTODO]
  102. public void AddRange (Array values)
  103. {
  104. throw new NotImplementedException ();
  105. }
  106. #endif
  107. public void AddRange (DataTableMapping[] values)
  108. {
  109. foreach (DataTableMapping dataTableMapping in values)
  110. this.Add (dataTableMapping);
  111. }
  112. public void Clear ()
  113. {
  114. sourceTables.Clear ();
  115. dataSetTables.Clear ();
  116. mappings.Clear ();
  117. }
  118. public bool Contains (object value)
  119. {
  120. return mappings.Contains (value);
  121. }
  122. public bool Contains (string value)
  123. {
  124. return sourceTables.Contains (value);
  125. }
  126. public void CopyTo (Array array, int index)
  127. {
  128. mappings.CopyTo (array, index);
  129. }
  130. public DataTableMapping GetByDataSetTable (string dataSetTable)
  131. {
  132. return (DataTableMapping)(dataSetTables[dataSetTable]);
  133. }
  134. [EditorBrowsable (EditorBrowsableState.Advanced)]
  135. public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
  136. {
  137. if (tableMappings.Contains (sourceTable))
  138. return tableMappings[sourceTable];
  139. if (mappingAction == MissingMappingAction.Error)
  140. throw new InvalidOperationException ();
  141. if (mappingAction == MissingMappingAction.Ignore)
  142. return null;
  143. return new DataTableMapping (sourceTable, dataSetTable);
  144. }
  145. public IEnumerator GetEnumerator ()
  146. {
  147. return mappings.GetEnumerator ();
  148. }
  149. public int IndexOf (object value)
  150. {
  151. return mappings.IndexOf (value);
  152. }
  153. public int IndexOf (string sourceTable)
  154. {
  155. return IndexOf (sourceTables[sourceTable]);
  156. }
  157. public int IndexOfDataSetTable (string dataSetTable)
  158. {
  159. return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
  160. }
  161. public void Insert (int index, object value)
  162. {
  163. mappings.Insert (index, value);
  164. }
  165. ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
  166. {
  167. ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
  168. Add (tableMapping);
  169. return tableMapping;
  170. }
  171. ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
  172. {
  173. return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
  174. }
  175. public void Remove (object value)
  176. {
  177. mappings.Remove ((DataTableMapping) value);
  178. }
  179. public void RemoveAt (int index)
  180. {
  181. mappings.RemoveAt (index);
  182. }
  183. public void RemoveAt (string sourceTable)
  184. {
  185. RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
  186. }
  187. #endregion // Methods
  188. }
  189. }