DataTableMappingCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. public void AddRange (Array values)
  132. {
  133. for (int i = 0; i < values.Length; ++i)
  134. Add (values.GetValue (i));
  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. #if NET_2_0
  161. public void CopyTo (DataTableMapping[] array, int index)
  162. {
  163. mappings.CopyTo (array, index);
  164. }
  165. #endif
  166. public DataTableMapping GetByDataSetTable (string dataSetTable)
  167. {
  168. // this should work case-insenstive.
  169. if (!(dataSetTables[dataSetTable] == null))
  170. return (DataTableMapping)(dataSetTables[dataSetTable]);
  171. else {
  172. string lowcasevalue = dataSetTable.ToLower();
  173. object [] keyarray = new object[dataSetTables.Count];
  174. dataSetTables.Keys.CopyTo(keyarray,0);
  175. for (int i=0; i<keyarray.Length; i++) {
  176. string temp = (string) keyarray[i];
  177. if (lowcasevalue.Equals(temp.ToLower())) return (DataTableMapping)(dataSetTables[keyarray[i]]);
  178. }
  179. return null;
  180. }
  181. }
  182. [EditorBrowsable (EditorBrowsableState.Advanced)]
  183. public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
  184. {
  185. if (tableMappings.Contains (sourceTable))
  186. return tableMappings[sourceTable];
  187. if (mappingAction == MissingMappingAction.Error)
  188. throw new InvalidOperationException (String.Format ("Missing source table mapping: '{0}'",
  189. sourceTable));
  190. if (mappingAction == MissingMappingAction.Ignore)
  191. return null;
  192. return new DataTableMapping (sourceTable, dataSetTable);
  193. }
  194. public IEnumerator GetEnumerator ()
  195. {
  196. return mappings.GetEnumerator ();
  197. }
  198. public int IndexOf (object value)
  199. {
  200. return mappings.IndexOf (value);
  201. }
  202. public int IndexOf (string sourceTable)
  203. {
  204. return IndexOf (sourceTables[sourceTable]);
  205. }
  206. public int IndexOfDataSetTable (string dataSetTable)
  207. {
  208. // this should work case-insensitive
  209. if (!(dataSetTables[dataSetTable] == null))
  210. return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
  211. else {
  212. string lowcasevalue = dataSetTable.ToLower();
  213. object [] keyarray = new object[dataSetTables.Count];
  214. dataSetTables.Keys.CopyTo(keyarray,0);
  215. for (int i=0; i<keyarray.Length; i++) {
  216. string temp = (string) keyarray[i];
  217. if (lowcasevalue.Equals(temp.ToLower()))
  218. return IndexOf ((DataTableMapping)(dataSetTables[keyarray[i]]));
  219. }
  220. return -1;
  221. }
  222. }
  223. public void Insert (int index, object value)
  224. {
  225. mappings.Insert (index, value);
  226. sourceTables[((DataTableMapping)value).SourceTable] = value;
  227. dataSetTables[((DataTableMapping)value).DataSetTable] = value;
  228. }
  229. #if NET_2_0
  230. public void Insert (int index, DataTableMapping value)
  231. {
  232. mappings.Insert (index, value);
  233. sourceTables[value.SourceTable] = value;
  234. dataSetTables[value.DataSetTable] = value;
  235. }
  236. #endif
  237. ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
  238. {
  239. ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
  240. Add (tableMapping);
  241. return tableMapping;
  242. }
  243. ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
  244. {
  245. return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
  246. }
  247. public void Remove (object value)
  248. {
  249. if (!(value is DataTableMapping))
  250. throw new InvalidCastException ();
  251. int index = mappings.IndexOf (value);
  252. if (( index < 0 ) || (index >=mappings.Count))
  253. throw new ArgumentException("There is no such element in collection.");
  254. mappings.Remove ((DataTableMapping) value);
  255. }
  256. #if NET_2_0
  257. public void Remove (DataTableMapping value)
  258. {
  259. int index = mappings.IndexOf (value);
  260. if (( index < 0 ) || (index >=mappings.Count))
  261. throw new ArgumentException("There is no such element in collection.");
  262. mappings.Remove ((DataTableMapping) value);
  263. }
  264. #endif
  265. public void RemoveAt (int index)
  266. {
  267. if (( index < 0 ) || (index >=mappings.Count))
  268. throw new IndexOutOfRangeException("There is no element in collection.");
  269. mappings.RemoveAt (index);
  270. }
  271. public void RemoveAt (string sourceTable)
  272. {
  273. RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
  274. }
  275. #endregion // Methods
  276. }
  277. }