DataTableMappingCollection.cs 9.4 KB

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