DataTableMappingCollection.cs 9.1 KB

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