DataTableMappingCollection.cs 9.6 KB

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