DataColumnMappingCollection.cs 9.4 KB

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