DataTableMappingCollection.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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) 2002 Tim Coleman
  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. public void AddRange (DataTableMapping[] values)
  101. {
  102. foreach (DataTableMapping dataTableMapping in values)
  103. this.Add (dataTableMapping);
  104. }
  105. public void Clear ()
  106. {
  107. sourceTables.Clear ();
  108. dataSetTables.Clear ();
  109. mappings.Clear ();
  110. }
  111. public bool Contains (object value)
  112. {
  113. return mappings.Contains (value);
  114. }
  115. public bool Contains (string value)
  116. {
  117. return sourceTables.Contains (value);
  118. }
  119. public void CopyTo (Array array, int index)
  120. {
  121. mappings.CopyTo (array, index);
  122. }
  123. public DataTableMapping GetByDataSetTable (string dataSetTable)
  124. {
  125. return (DataTableMapping)(dataSetTables[dataSetTable]);
  126. }
  127. [EditorBrowsable (EditorBrowsableState.Advanced)]
  128. public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
  129. {
  130. if (tableMappings.Contains (sourceTable))
  131. return tableMappings[sourceTable];
  132. if (mappingAction == MissingMappingAction.Error)
  133. throw new InvalidOperationException ();
  134. if (mappingAction == MissingMappingAction.Ignore)
  135. return null;
  136. return new DataTableMapping (sourceTable, dataSetTable);
  137. }
  138. public IEnumerator GetEnumerator ()
  139. {
  140. return mappings.GetEnumerator ();
  141. }
  142. public int IndexOf (object value)
  143. {
  144. return mappings.IndexOf (value);
  145. }
  146. public int IndexOf (string sourceTable)
  147. {
  148. return IndexOf (sourceTables[sourceTable]);
  149. }
  150. public int IndexOfDataSetTable (string dataSetTable)
  151. {
  152. return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
  153. }
  154. public void Insert (int index, object value)
  155. {
  156. mappings.Insert (index, value);
  157. }
  158. ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
  159. {
  160. ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
  161. Add (tableMapping);
  162. return tableMapping;
  163. }
  164. ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
  165. {
  166. return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
  167. }
  168. public void Remove (object value)
  169. {
  170. mappings.Remove ((DataTableMapping) value);
  171. }
  172. public void RemoveAt (int index)
  173. {
  174. mappings.RemoveAt (index);
  175. }
  176. public void RemoveAt (string sourceTable)
  177. {
  178. RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
  179. }
  180. #endregion // Methods
  181. }
  182. }