DataTableMappingCollection.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DataTableMappingCollectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  17. public sealed class DataTableMappingCollection : MarshalByRefObject, ITableMappingCollection, IList, ICollection, IEnumerable
  18. {
  19. #region Fields
  20. ArrayList mappings;
  21. Hashtable sourceTables;
  22. Hashtable dataSetTables;
  23. #endregion
  24. #region Constructors
  25. public DataTableMappingCollection()
  26. {
  27. mappings = new ArrayList ();
  28. sourceTables = new Hashtable ();
  29. dataSetTables = new Hashtable ();
  30. }
  31. #endregion // Constructors
  32. #region Properties
  33. [Browsable (false)]
  34. [DataSysDescription ("The number of items in the collection")]
  35. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  36. public int Count {
  37. get { return mappings.Count; }
  38. }
  39. [Browsable (false)]
  40. [DataSysDescription ("The specified DataTableMapping object")]
  41. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  42. public DataTableMapping this [int index] {
  43. get { return (DataTableMapping)(mappings[index]); }
  44. set {
  45. DataTableMapping mapping = (DataTableMapping) mappings[index];
  46. sourceTables [mapping.SourceTable] = value;
  47. dataSetTables [mapping.DataSetTable] = value;
  48. mappings [index] = value;
  49. }
  50. }
  51. [Browsable (false)]
  52. [DataSysDescription ("The specified DataTableMapping object")]
  53. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  54. public DataTableMapping this [string sourceTable] {
  55. get { return (DataTableMapping) sourceTables[sourceTable]; }
  56. set { this [mappings.IndexOf (sourceTables[sourceTable])] = value; }
  57. }
  58. object IList.this [int index] {
  59. get { return (object)(this[index]); }
  60. set {
  61. if (!(value is DataTableMapping))
  62. throw new ArgumentException ();
  63. this[index] = (DataTableMapping)value;
  64. }
  65. }
  66. bool ICollection.IsSynchronized {
  67. get { return mappings.IsSynchronized; }
  68. }
  69. object ICollection.SyncRoot {
  70. get { return mappings.SyncRoot; }
  71. }
  72. bool IList.IsFixedSize {
  73. get { return false; }
  74. }
  75. bool IList.IsReadOnly {
  76. get { return false; }
  77. }
  78. object ITableMappingCollection.this [string sourceTable] {
  79. get { return this [sourceTable]; }
  80. set {
  81. if (!(value is DataTableMapping))
  82. throw new ArgumentException ();
  83. this [sourceTable] = (DataTableMapping) value;
  84. }
  85. }
  86. #endregion // Properties
  87. #region Methods
  88. public int Add (object value)
  89. {
  90. if (!(value is System.Data.Common.DataTableMapping))
  91. throw new InvalidCastException ("The object passed in was not a DataTableMapping object.");
  92. sourceTables[((DataTableMapping)value).SourceTable] = value;
  93. dataSetTables[((DataTableMapping)value).DataSetTable] = value;
  94. return mappings.Add (value);
  95. }
  96. public DataTableMapping Add (string sourceTable, string dataSetTable)
  97. {
  98. DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
  99. Add (mapping);
  100. return mapping;
  101. }
  102. #if NET_2_0
  103. [MonoTODO]
  104. public void AddRange (Array values)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. #endif
  109. public void AddRange (DataTableMapping[] values)
  110. {
  111. foreach (DataTableMapping dataTableMapping in values)
  112. this.Add (dataTableMapping);
  113. }
  114. public void Clear ()
  115. {
  116. sourceTables.Clear ();
  117. dataSetTables.Clear ();
  118. mappings.Clear ();
  119. }
  120. public bool Contains (object value)
  121. {
  122. return mappings.Contains (value);
  123. }
  124. public bool Contains (string value)
  125. {
  126. return sourceTables.Contains (value);
  127. }
  128. public void CopyTo (Array array, int index)
  129. {
  130. mappings.CopyTo (array, index);
  131. }
  132. public DataTableMapping GetByDataSetTable (string dataSetTable)
  133. {
  134. // this should work case-insenstive.
  135. if (!(dataSetTables[dataSetTable] == null))
  136. return (DataTableMapping)(dataSetTables[dataSetTable]);
  137. else {
  138. string lowcasevalue = dataSetTable.ToLower();
  139. object [] keyarray = new object[dataSetTables.Count];
  140. dataSetTables.Keys.CopyTo(keyarray,0);
  141. for (int i=0; i<keyarray.Length; i++) {
  142. string temp = (string) keyarray[i];
  143. if (lowcasevalue.Equals(temp.ToLower())) return (DataTableMapping)(dataSetTables[keyarray[i]]);
  144. }
  145. return null;
  146. }
  147. }
  148. [EditorBrowsable (EditorBrowsableState.Advanced)]
  149. public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
  150. {
  151. if (tableMappings.Contains (sourceTable))
  152. return tableMappings[sourceTable];
  153. if (mappingAction == MissingMappingAction.Error)
  154. throw new InvalidOperationException ();
  155. if (mappingAction == MissingMappingAction.Ignore)
  156. return null;
  157. return new DataTableMapping (sourceTable, dataSetTable);
  158. }
  159. public IEnumerator GetEnumerator ()
  160. {
  161. return mappings.GetEnumerator ();
  162. }
  163. public int IndexOf (object value)
  164. {
  165. return mappings.IndexOf (value);
  166. }
  167. public int IndexOf (string sourceTable)
  168. {
  169. return IndexOf (sourceTables[sourceTable]);
  170. }
  171. public int IndexOfDataSetTable (string dataSetTable)
  172. {
  173. return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
  174. }
  175. public void Insert (int index, object value)
  176. {
  177. mappings.Insert (index, value);
  178. sourceTables[((DataTableMapping)value).SourceTable] = value;
  179. dataSetTables[((DataTableMapping)value).DataSetTable] = value;
  180. }
  181. ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
  182. {
  183. ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
  184. Add (tableMapping);
  185. return tableMapping;
  186. }
  187. ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
  188. {
  189. return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
  190. }
  191. public void Remove (object value)
  192. {
  193. if (!(value is DataTableMapping))
  194. throw new InvalidCastException ();
  195. int index = mappings.IndexOf (value);
  196. if (( index < 0 ) || (index >=mappings.Count))
  197. throw new ArgumentException("There is no such element in collection.");
  198. mappings.Remove ((DataTableMapping) value);
  199. }
  200. public void RemoveAt (int index)
  201. {
  202. if (( index < 0 ) || (index >=mappings.Count))
  203. throw new IndexOutOfRangeException("There is no element in collection.");
  204. mappings.RemoveAt (index);
  205. }
  206. public void RemoveAt (string sourceTable)
  207. {
  208. RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
  209. }
  210. #endregion // Methods
  211. }
  212. }