2
0

DataTableMappingCollection.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 (String.Format ("Missing source table mapping: '{0}'",
  177. sourceTable));
  178. if (mappingAction == MissingMappingAction.Ignore)
  179. return null;
  180. return new DataTableMapping (sourceTable, dataSetTable);
  181. }
  182. public IEnumerator GetEnumerator ()
  183. {
  184. return mappings.GetEnumerator ();
  185. }
  186. public int IndexOf (object value)
  187. {
  188. return mappings.IndexOf (value);
  189. }
  190. public int IndexOf (string sourceTable)
  191. {
  192. return IndexOf (sourceTables[sourceTable]);
  193. }
  194. public int IndexOfDataSetTable (string dataSetTable)
  195. {
  196. // this should work case-insensitive
  197. if (!(dataSetTables[dataSetTable] == null))
  198. return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
  199. else {
  200. string lowcasevalue = dataSetTable.ToLower();
  201. object [] keyarray = new object[dataSetTables.Count];
  202. dataSetTables.Keys.CopyTo(keyarray,0);
  203. for (int i=0; i<keyarray.Length; i++) {
  204. string temp = (string) keyarray[i];
  205. if (lowcasevalue.Equals(temp.ToLower()))
  206. return IndexOf ((DataTableMapping)(dataSetTables[keyarray[i]]));
  207. }
  208. return -1;
  209. }
  210. }
  211. public void Insert (int index, object value)
  212. {
  213. mappings.Insert (index, value);
  214. sourceTables[((DataTableMapping)value).SourceTable] = value;
  215. dataSetTables[((DataTableMapping)value).DataSetTable] = value;
  216. }
  217. ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
  218. {
  219. ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
  220. Add (tableMapping);
  221. return tableMapping;
  222. }
  223. ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
  224. {
  225. return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
  226. }
  227. public void Remove (object value)
  228. {
  229. if (!(value is DataTableMapping))
  230. throw new InvalidCastException ();
  231. int index = mappings.IndexOf (value);
  232. if (( index < 0 ) || (index >=mappings.Count))
  233. throw new ArgumentException("There is no such element in collection.");
  234. mappings.Remove ((DataTableMapping) value);
  235. }
  236. public void RemoveAt (int index)
  237. {
  238. if (( index < 0 ) || (index >=mappings.Count))
  239. throw new IndexOutOfRangeException("There is no element in collection.");
  240. mappings.RemoveAt (index);
  241. }
  242. public void RemoveAt (string sourceTable)
  243. {
  244. RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
  245. }
  246. #endregion // Methods
  247. }
  248. }